I’ve read a few posts on how to installed PHP on my mac using macports, but quite a few seem out of date or incomplete. I hope this is a guide on how I think it should be done from start to finish using easier to understand small steps.
Updates:
28/08/09: Now works with Snow Leopard and this post has been updated reflect the latest variant changes to the PHP 5.3 install
03/09/09: Typo fixes and corrections from feedback
22/07/11: Updates for installing on OS-X 10.7 Lion
What? But my mac comes with PHP & Apache!
Well the first question should really be why use MacPorts instead of using the Apache/PHP that comes with OS-X. Basically the answer to that is that by default it’s not really setup for serious developer use, just the average end user who wants to play around with a script of hack together a small website. You could also use MAMP, but personally I don’t think it give me the level of control that I need as I try and develop with an environment as close to my production system that I can (i.e. a base unix environment). MAMP also hasn’t been updated for a while! Once you’re fully setup with MacPorts updating is a breeze and as the whole system has been compiled onto your machine in theory it should be a lot faster than a prebuilt universal binary.
Pre-Configure: Turn off the default web server
So the first thing you should do is to turn off Apache…
This is pretty simple, go to System Preferences, type Apache in it’s spotlight. It will highlight Sharing, but you just need to press return. Just make sure Web Sharing is disabled.
Setting up MacPorts
Install Xcode
Next you must make sure you’ve installed Apple’s Xcode Developer Tools (version 4.1 for Lion, version 3.1 or later for Leopard, or 2.4.1 or later for Tiger), found on the Mac App Store or at the Apple Developer Connection site or on your Mac OS X installation CDs/DVD. This basically installs a more complete unix developer base with compilers are other useful stuff needed for MacPorts.
Install MacPorts
After installing Xcode you can download and install MacPorts. The best way to install it is to use the DMG installer on the MacPorts site. On the other hand if you want to compile it from source then follow these steps:
Open up the Terminal
svn co http://svn.macports.org/repository/macports/trunk/base/ cd base ./configure make sudo make install
I recommend whichever way you install macports you should run this command to make sure everything is up to date:
sudo /opt/local/bin/port -v selfupdate
If nothing as gone wrong so far then you should have MacPorts fully setup. Only one more thing to do before installing PHP/MySQL and Apache
Update your system paths
Just so I don’t have to keep on typing /opt/local/bin all the time i’m going add that directory to my PATH. This information can be stored in your .profile which runs every time you start a new terminal. If you don’t have a .profile file use these steps to create one:
cd ~ echo 'export PATH=/opt/local/bin:/opt/local/sbin:$PATH' >> .profile
and that’s that. If you open a new terminal window (yes do that now) then those directories will be searched for terminal commands before the ones in the default path.
Setting up the LAMP stack
Enable SSL – if you want to!
Before installing PHP, i’m going to make sure that the version of curl installed is compiled with SSL enabled. If you don’t need or care about SSL then you can safely skip this step.
sudo port install curl +ssl
You should note that regardless if you run the command above first or one of the ones below then MacPorts will download a whole bunch or ports that are required to install that port or one of its dependencies. Also these commands may take sometime, so give it a while or go make a cup a tea.
Install MySQL
This one is fairly simple, well kind of…
sudo port install mysql5-server
Note: a lot of blogs recommend installing just the mysql5 port, but if you want the option to have MySQL start automatically at startup you’ll want to install the server. The mysql5-server port installs the mysql5 port anyway, so you can’t lose!
If you want to run MySQL at startup then run this command after the port finishes installing
sudo port load mysql5-server
Finally to make sure that the database if fully setup you must run this command (before starting up any mysql process!)
sudo -u _mysql mysql_install_db5
And make sure you pay attention to its instructions! Personally I didn’t do anything here as this is a development setup for me, so I prefer to keep my settings loose. Although if you want MySQL commands in your terminals path (like port) then run these commands and start a new terminal:
cd ~ echo 'export PATH=/opt/local/lib/mysql5/bin:$PATH' >> .profile
Create /opt/local/etc/mysql5/my.cnf, add the following to it and save
[mysqld_safe] socket = /tmp/mysql.sock
Also to remain compatible with other programs that may have been coded to look for the socket file in its original location then add this symbolic link:
sudo ln -s /tmp/mysql.sock /opt/local/var/run/mysql5/mysqld.sock
Install PHP & Apache
Ok we’re finally installing PHP with MySQL support and Apache (FastCGI is just being installed separately for use with some debugging and profiling tools).
sudo port install php5 +apache2 +pear +fastcgi php5-mysql +mysqlnd
Like MySQL if you want Apache to start automatically when you start your computer then run this command after the port installs (as shown while the ports are installing)
sudo port load apache2
Configure PHP
To customize php, copy /opt/local/etc/php5/php.ini-development (if this is a development server) or /opt/local/etc/php5/php.ini-production (if this is a production server) to /opt/local/etc/php5/php.ini and then make changes (see below).
i.e. for development:
cd /opt/local/etc/php5/ sudo cp php.ini-development php.ini
i.e. for production:
cd /opt/local/etc/php5/ sudo cp php.ini-production php.ini
To customize php edit (using your favourite editor) /opt/local/etc/php5/php.ini to configure it properly, make sure you set the correct timezone (for myself it’s this):
date.timezone = Europe/London
Configure Apache
If this is your first install, you need to activate PHP in your web server.To enable PHP in Apache, run
cd /opt/local/apache2/modules sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so
Open /opt/local/apache2/conf/httpd.conf in your favourite text editor (it’s vi for me, but feel free to use pico, emacs or whatever!).
sudo vi /opt/local/apache2/conf/httpd.conf
If you want to change the default MacPorts Apache document root to match default Apple’s Apache (personally I didn’t, but I found this information on another blog – so it might be useful to some), change:
DocumentRoot "/opt/local/apache2/htdocs"
to:
DocumentRoot "/Library/WebServer/Documents"
If you changed the DocumentRoot, change the Directory directive from:
<Directory "/opt/local/apache2/htdocs">
to
<Directory "/Library/WebServer/Documents">
For PHP to work correctly you must add index.php to the dir_module directive:
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
Add a new mimetype so that Apache will direct files ending in .php to the PHP module for processing. Add the following within the <IfModule mime_module> block. Without this, all you’ll see is the text of your PHP scripts
AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps
And finally, to enable user directories, uncomment:
Include conf/extra/httpd-userdir.conf
But if you are going to use virtual hosts (as described below) then uncomment this line in addition to (or instead of) the line above:
Include conf/extra/httpd-vhosts.conf
Save and close the httpd.conf file.
Advanced Post-Configuration
Configure Apache Virtual Hosts
Now unless you just want to access your new web server using http://localhost you might want to set up some virtual hosts for Apache. I personally only need one virtual host atm i.e. http://testing.example.com. Virtual hosts need to be configured in two places, the hosts file (/etc/hosts) and the Apache virtual hosts file (/opt/local/apache2/conf/extra/httpd-vhosts.conf).
Firstly add this line to /etc/hosts after the localhost has been defined i.e.
127.0.0.1 localhost 127.0.0.1 testing.example.com
Then you must edit the virtual hosts file /opt/local/apache2/conf/extra/httpd-vhosts.conf and add this configuration
<VirtualHost *:80> ServerAdmin webmaster@testing.example.com DocumentRoot "/opt/local/apache2/vhosts/testing.example.com" ServerName testing.example.com ServerAlias www.testing.example.com ErrorLog "logs/testing.example.com-error_log" CustomLog "logs/testing.example.com-access_log" common </VirtualHost>
The document root can be anything you want as long as apache has permissions to read that directory i.e. in this case you can:
sudo mkdir -p /opt/local/apache2/vhosts/testing.example.com cd /opt/local/apache2/vhosts/ sudo chmod -R 755 testing.example.com
You can do this process for as many virtual hosts as you want. Also I personally keep all my websites stored in my user directory – yes bad on a server, but this is my personal development machine. Finally don’t forget to comment out or remove the example vhosts from http-vhost.conf or you’ll get warnings while starting up Apache, they are harmless to leave in, but it’s probably for the best to remove any potential errors
Installing extra PHP modules
There are plenty of extra PHP modules available from MacPorts, you can get a list of them by using this command. Although you won’t need any of the MySQL ones as you’ve already compiled it into PHP.
port search php5-
Here is the list of PHP5 ports I installed
sudo port install php5-openssl php5-curl php5-gd php5-iconv php5-http php5-mcrypt php5-xdebug
Updating MacPorts
MacPorts is simple to update, every few weeks I just run the command below to check for any outdated ports (i’ll also update this post with any changes to this process I discover)
sudo port outdated
to update all installed ports to the latests and greatest
sudo port upgrade outdated
That’s It
Now if you restart your computer and add code into the document root of your website (in this case http://testing.example.com) then you should have a fully working web server with PHP and MySQL. Ok it wasn’t easy, but for me it’s the best solution over the longer term
Great post! Thanks for sharing this. Exactly what I was looking for to switch using MacPorts php5 as I was unable to use GD library for quite a long time which does not come with pre-installed PHP on Mac.
-Pankaj
It Works perfect, thanks!
Thanks for the great instructions! I had one problem – I couldn’t get the mysql socket to be at /tmp/mysql.sock by using the my.cnf file. I had to set mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock in my php.ini
Why do you recommend to move the mysql socket from the default macports location?
Thanks again, Jonathan
Thank you for all the instructions! everything worked like a charm. One thing that happened to me was that I had installed mySQL before.. and by some reason it kept pulling that one’s socket rather than the new one.. but I deleted the past version completely then ran the steps again and all worked great. OS X 10.6 (yes.. there is hope
Hi, Thanks for the feedback. Glad it’s been working for everyone.
Jonathan: I move the socket to /tmp/mysqld.sock as the official mysql gui tools and a few other tools look for the socket at that location by default (at least on my computer). As i’m a lazy person I’d rather not have to enter the path to that socket for any new tool, plus it’s a long path to remember! But yes as you said it can be left where it is and edit the php.ini file to use that socket file.
Daniel: Thanks for letting me know. I didn’t test this over a old install, but it’s something to take note of!
Hi,
I think you have a typo in the following filename:
“Then you must edit the virtual hosts file /opt/local/apache2/conf/extra/http-vhosts.conf and add this configuration”
It should be httpd-vhosts.conf.
How do i change that the CLI version of PHP now uses the same installation, i.e. i can access mysql via PHP CLI?
[propel-sql-exec] Our new url -> mysql://root:1230@localhost/sfdemo
Execution of target “insert-sql” failed for the following reason: /opt/local/lib/php/symfony/vendor/propel-generator/build-propel.xml:296:1: [wrapped: connect failed [Native Error: No such file or directory] [User Info: Array]]
[phing] /opt/local/lib/php/symfony/vendor/propel-generator/build-propel.xml:296:1: [wrapped: connect failed [Native Error: No such file or directory] [User Info: Array]]
when i build the model & sql, i always got the error msg, and db tables create fail.
does anybody meet the same problem and have a solution? help~~ thx for advance
Anguskwan, ever able to find a solution? I am facing the same issue.
Didn’t you mean DocumentRoot “/opt/local/apache2/htdocs/testing.example.com” instead of DocumentRoot “/opt/local/apache2/docs/testing.example.com”? I understand that it would work just fine or even better – being inaccessible under the server IP, but what is your rationale for calling the folder “docs” instead of eg. “vhosts”?
Hi,
Erik Gyepes: Thanks, you’re right. I’ve fixed the typo!
George: I don’t access MySQL directly using the PHP CLI, but I do run phpUnit tests that do access MySQL, so I don’t think there would be a problem
anguskwan, Candace Lapp: I haven’t used Symfony in over a year, so I’m a bit rusty on propel. It could be a database configuration issue in the symfony yaml or it could be that the mysql user you’re using doesn’t have permission do much. If you have a found a solution could you share it?
Cezary: No particular reason, I’ve changed it to vhosts (it does sound better). Like I said personally I keep my development sites in my user folder, so it was just an example. Although I also create a public/private/logs/backups directories in each site I create, where public would be the Document root i.e.
extending the example above:
/opt/local/apache2/vhosts/testing.example.com
/opt/local/apache2/vhosts/testing.example.com/public (Document root)
/opt/local/apache2/vhosts/testing.example.com/private (Temp/Cache/Other files)
/opt/local/apache2/vhosts/testing.example.com/logs (Apache/Site/Debug logs)
/opt/local/apache2/vhosts/testing.example.com/backups (Backups of public)
Great article, I had the same problem, finding old and out of date instructions. Worked a charm!
Hi again
Today I upgraded with Security Update 2010-001 and I think that after that the Apache stopped to start on system startup. Any ideas how to fix this? I tried to run again the launchctl load command, but it said that it is already loaded, so that should be okay. Any hint would be appreciated.
Thanks.
Fantastic instructions! I followed them some time late last year and everything worked wonderfully, but I accidentally deleted my “opt” folder and had to start all over. I’ve got everything working again, except now PHP files are only parsed on the first virtual host, but not on the rest that follow. Any help is greatly appreciated!
Hi Eirk: Not really sure about that one, I applied the same system update myself without any disruption. I have noticed from time to time that Apache doesn’t automatically start up, but most of the time it does. I can only conclude that it unrelated to macports, but more likely launchctl or a bug with the version of apache (you might want to check the error logs to see why it isn’t starting up).
Hi Sammy: I’m assuming that you have all your virtual hosts defined in one file (rather than setup apache to include a directory to load individual config files). I can only assume it’s a configuration issue in one of your virtual hosts, does it pass the apache configuration test i.e. /opt/local/apache2/bin/apachectl configtest? Also if you’re using named virtual hosts (i.e. with server names, do you have those server names defined in /etc/hosts?
Also one small point: I managed to successfully update my version of PHP to 5.3.1 without issues, although it did take quite a while for it to recompile!
Regards
Milan
Thanks for the post. In order for VirtualHost to work for me, I also had to change the default httpd.conf file (/private/etc/apache2/httpd.conf) to reflect the correct DocumentRoot and to include the httpd-vhosts.conf file.
anguskwan, Candace Lapp : Check again your php.ini on line that set mysql.default_socket. And point that to active mysql server.
it works for me
Thx for this awesome tutorial! works great!
I don’t know if anybody has mentioned this typo before, but the syntax of of
sudo ln -s /tmp/mysql.sock /opt/local/var/run/mysql5/mysqld.sock
is not correct. It has to be the other way round:
sudo ln -s /opt/local/var/run/mysql5/mysqld.sock /tmp/mysql.sock
regards,
Markus
Hi
Thanks for the tutorial. I have already installed
php5 @5.3.1_1+apache2+darwin_10+imap+macosx+mysql5+sqlite
but now want to install the pear package. How do I do this?
Thanks
I made everything as written in article and receive It Works!
but i already change DocumentRoot to my server!
Try to /opt/local/apache2/bin/apachectl restart
receive:
httpd not running, trying to start
(13)Permission denied: make_sock: could not bind to address [::]:80
(13)Permission denied: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
Try to /opt/local/apache2/bin/apachectl start
(13)Permission denied: make_sock: could not bind to address [::]:80
(13)Permission denied: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
What can i do?
@Oleg: Sounds like you already have the default version of Apache for OS-X running (or another service running on port 80)
@amc I know if you deactivate your current php5 port i.e. ‘sudo port deactivate php5 @5.3.1_1+apache2+darwin_10+imap+macosx+mysql5+sqlite’ and then run ‘sudo port install php5++apache2+darwin_10+imap+macosx+mysql5+sqlite+pear’ then that should make it work, but I think pear was installed by default anyway (but I could be wrong)
@Markus, @Fahmi, @Babs Thanks for the info – I’ll check it out and update the post
want to make it super easy to start/stop/restart your apache instance? then add the following to the bottom of your .profile
alias apache2ctl='sudo /opt/local/apache2/bin/apachectl'have fun!
By far the most comprehensive, well-written, accurate description of what needs to be done I have found. Thank you!!!
For those interested, I had an existing apache/mysql/php install via macports. I had updated to Snow Leopard in 2009 and (while painful) all was well. Recently, I worked to get updated, in particular, running php 5.3 (was on 5.2 before). At that point, things were not working: no php from apache, and no database from php. This was the first guide I have read that accurately explained the process.
Thanks!
Tom
How about completing the full guide by adding installing PHPMYADMIN. Apart from that its very good thanks
Just another pointless blog? Far from it. After hours of trying and failing to compile PHP 5.3.10 from source for the Mac, I decided to go down the MacPorts route.
This blog is a fantastic quick guide to getting it all set up. Much appreciated.
Thanks now my MAMP works good.
Hello,
seeams like the mysql5-server port has been renamed to mysql5, for details: https://trac.macports.org/ticket/32752
This Was absolutely fantastic thank you so much
Macports is terrible. It’s completely corrupted my installation. I don’t know how you swear by it but I suppose its different strokes for different folks.
First of all i would like to express y gratitude for this wonderful tutorial.
HAve been following it, step by step and more or less got everything working.
There is one thing bothering me:
mysql!
i created my.cnf (copied over from my-huge.cnf) and added line for /tmp/mysq.sock
then i run:
sudo ln -s /tmp/mysql.sock /opt/local/var/run/mysql5/mysqld.sock
i suppose i run that command a few times, so now i am getting message thet file already exists
NOW:
real problem is that i can not get MySQL Workbench working, can not create Server instance, i suppose it can not find mysql.sock.
Another porblem i am getting is that i can nt run phpCake app i am working on with the error message:
ysql_connect() [function.mysql-connect]: [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) [CORE/cake/libs/model/datasources/dbo/dbo_mysql.php, line 561]
Warning (2): mysql_connect() [function.mysql-connect]: No such file or directory [CORE/cake/libs/model/datasources/dbo/dbo_mysql.php, line 561]
Warning (2): mysql_query() expects parameter 2 to be resource, boolean given [CORE/cake/libs/model/datasources/dbo/dbo_mysql.php, line 613]
Warning (2): Cannot modify header information – headers already sent by (output started at /Users/vladimir/Documents/web/eskimo/cake/libs/debugger.php:683) [CORE/cake/libs/controller/controller.php, line 742]
When i go to /tmp, i dont see mysql.sock
mysql is working, and can access it either thru shell or gui tools
Thanks in advanced, its pretty critical to get this working asap
Best regards
Vladimir
Any Clue for MacPorts on Mountain Lion?
This is in answer to George’s question about enabling the configuration on the CLI. I know that question was asked years ago, but this page shows up well in Google.
What I did was simply create a symlink from the configuration file used by Apache in the folder where the CLI looks for it:
sudo ln -s /opt/local/etc/php5/php.ini /etc/php.ini
I hope this helps someone.
It works like charm! Thank you very much, sir!
Hi,
first I want to say THANKS for this wonderful tutorial! But I still got one problem. I did everything like you describe, even the change to . Now I enter localhost in my Chrome and i got a 403 No permission. What I have forgotten, I hope you can help me:)
I will look in the next time at your blog. And thanks again, it’s a really nice work you have done!
Best
LittleOne
Maybe it’s important to know I’m working on a Mac OS X 10.7.5
Best
LittleOne
Hi,
it’s me again. I now know which issue is causing my problem. I have multiple installed apache2, cause i was using xampp bevor. Now my question maybe you can help, will it work when I uninstall xampp or will he leave the config files for apache2 etc.
I hope someone can help.
Best Regards,
LittleOne
to place mysql.sock inside /tmp folder use the following code for your my.cnf:
[mysqld_safe]
socket = /tmp/mysql.sock
note that [mysqld_safe] should be on separate line
I need to use ~/Sites as DocumentRoot I want to store all my apps in that directory . So I changed the DocumentRoot and Directory to point to Sites. But I have Error 403 Forbidden.
Any Ideas
Couple of notes for the amateurs out there like myself.
1. Before you will get the mysqld.sock to work you need to start up the mysql, you do this through sudo port load mysql5 (i’m pretty sure that’s it) The instructions kinda make you think you shouldn’t do that in advance of running the mysql install tool that’s recommended in the instructions. By reading instructions we end up with two choices, one which yields the mysql.sock error.
2. This is probably really dumb, but I’ll say it. When you are setting the date.time in the php.ini, you need to REMOVE the ; in front of the date.time… nowhere does it explicitly say that, but it’s implied in the instruction cut and paste box.
Milan , you are a bad ass and this isn’t another pointless blog
What’s up, its pleasant paragraph concerning media print, we all know media is a impressive source of facts.
avthmcmph, cevihgbjgm
[...] http://blog.magudia.com/2009/08/22/installing-php53-apache2-mysql5-using-macports/ [...]
Thanks for sharing your thoughts about dresses karen millen.
Regards
。女性の中で最も
重要なアクセサリーの一
つは、ハンドバッグです。ほ
とんどの女性は
少し個人的なアイテムを運ぶことなく生きることができない。ハン
ドバッグは本
当に便利になるところです。最高品質のハンドバッグ
は、グッチのようなブラ
ンドによって製
造される。人々は、グッチのオリジナルの片頭痛
を誘発する価格
はあまり気にす
る必要はありませ
ん。レプリカグッチのハンドバッグは、元に安いけど本当
に効果的な代替手段です
hey, im sorry exchange for being noob, i prospect i found the high-mindedness subforum to dispatch to my introduction thread. Im 27yo and im here to understand some enchanting informations! conviction to remain ’round for a desire time and to meet you guys! http://www.filmovita.com/pic/images/50801767783208591711_thumb.png
Thanks for the update on my No.1 favorite celerbrity. I’ve been a huge fan for four years or more. Just fantastic.