How to install apache, php, mysql with macport in Mac OS X

The easiest way to install apache, php and mysql in Mac is thru mac port. You can also install apache, php and mysql by compiling each of the service but it’s too time consuming. If you just wan to install apache, php and mysql for development use, Mac Port install is good enough.
Follow the steps below to install apache, php and mysql in Mac OS X:-
- Download and install Mac Port.
They have good documentation, just refer to the documentation if you have problem. - Once Mac Port is running, ensure you have the administrator right because all the steps below need administrator right.
- start your “Terminal” and type:-
sudo port selfupdate
- Once macport selfupdate completed, you enter the command below at the terminal:-
sudo port install gawk sudo port install nawk
- Once gawk and nawk is installed, you can start install apache, php and mysql using the following command:-
sudo port install php5 +apache2 +mysql5 +serversudo port install php5 +apache2 +mysql5-server
** This process take quite some time, go take a walk and come back after 30 min ***
*** you must put the +server at the back, else you wont be able to configure it to auto start at boot up *** - Now you need to stop the local apache process.
- Go to System Preference -> Sharing -> uncheck the Personal Web sharing.
*** You must disable the Personal Web Sharing to avoid any conflict between your apache2 and the default apache. *** - Now use the command below to setup the mysql db.
sudo /opt/local/lib/mysql5/bin/mysql_install_db --user=mysql
Once done, change your root password
/opt/local/lib/mysql5/bin/mysqladmin -u root password 'new-password'
- Next, you can configure apache and mysql to start automatically by enter the command below:-
sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
- Now you need to configure apache to load php file, open /opt/local/apache2/conf/httpd.conf and add this 2 line:-
LoadModule php5_module modules/libphp5.so AddType application/x-httpd-php .php
Once saved, you may restart your apache.
* Thanks to Yolabingo for your comment * - To start your apache2 and mysql5 manually, type the command below:-
sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper start sudo /opt/local/etc/LaunchDaemons/org.macports.mysql5/mysql5.wrapper start
- Done, you just install apache2, php5 and mysql5 with macport in Mac OS X
To test if your apache is running, create a file name index.php with content:-
<?php phpinfo(); ?>
and save it at:-
/opt/local/apache2/htdocs/index.php
Then, Point your browser to http://localhost, and you should see your phpinfo page.
FAQ for How to install Apache, PHP and MySQL with Macport in Mac OS X
Q. Where is port located?
Answer: instead of using “port” you can use “/opt/local/bin/port”. Or you may create an alias for it by using this command:-
alias port='/opt/local/bin/port'
Q. I’ve done everything but when my browser point to http://localhost it show “Page Not Found”.
Answer: Please make sure you have disable your Mac default apache. By default, Mac OS X already installed with apache. You have to make sure you have disable the apache at System Preference -> Sharing -> Personal Web Sharing.
Q. Where is my apache located?
Answer: If you install your apache with MacPort, your all apache files will be located at /opt/local/apache2. Or may be i should said, all MacPort installed program will be store in /opt folder.
Q. Where is my apache configuration file (httpd.conf)?
Answer: Your apache configuration file (httpd.conf) located at /opt/local/apache2/conf/httpd.conf
Q. How to restart my apache?
Answer: You can restart your apache by using this command:-
sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper start
Credit: SamuraiCoder, 2 Tablespoon
Tags: apache, apache php mysql, install php mysql, mysql, PHP, php mysql, setup apache, setup apache php and mysql
Posted at July 14th, 2008 by chua
If you think this article helps you to solve your problem and clear your headache, feel free to buy me a drink :)



July 14th, 2008 at 9:32 pm
I use MAMP. This looks more complicated.
September 18th, 2008 at 12:17 am
Thanks!
November 17th, 2008 at 11:27 am
I have three questions as follows:
1.I have install mySQL before, should I uninstall it first?
2.I have got the following error report:
“Error: The following dependencies failed to build: gd2 XFree86 autoconf help2man p5-locale-gettext perl5.8 perl5 m4 automake fontconfig jpeg libpng libtool libmcrypt libxml2 libxslt mhash tiff
Error: Status 1 encountered during processing.”
How can I figure it out?
3. after I run ”
sudo port /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper start
”
I got the error report:”
Unrecognized action “/opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper”
”
why?
Thank you~~
November 17th, 2008 at 9:23 pm
Qiao:
1. Make a backup of all your db and remove it.
2. make sure you do “sudo port clean”
3. because you didnt specify “+server” during the port install
December 4th, 2008 at 6:32 am
The command for restarting Apache has an extraneous “port” in it. Otherwise, a very useful article – thanks!
December 30th, 2008 at 6:38 am
thanks!
the only thing is that to start demons manually i use “sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper start” and not “sudo port /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper start”
for me it works only withou “port” in command…
December 30th, 2008 at 10:28 am
Thanks mikosh for your correction on “sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper start”
February 16th, 2009 at 10:51 pm
Thanks for the guide – very helpful. When I installed Apache, it built the PHP module but did not install it. If Apache is running, but is simply displaying the contents of your PHP files, this is the likely problem.
To fix this, add the following lines to your Apache config file (/opt/local/apache2/conf/httpd.conf), then restart Apache:
LoadModule php5_module modules/libphp5.so
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
# Don’t add ‘.phps’ Type on production servers , of course.
May 27th, 2009 at 1:48 am
Hi, i just install apche+ mysql+ php and it works!… but no one can see my web server in mi network… what can i do??
October 1st, 2009 at 9:34 pm
After running
sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper start
I get these errors please help?!?
(48)Address already in use: make_sock: could not bind to address [::]:80
(48)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
October 1st, 2009 at 9:37 pm
I just turned off the default Snow Leopard Apache in the system preferences. Then tried to use:
sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper start
Then I got this (48)Address already in use: make_sock: could not bind to address [::]:80
October 2nd, 2009 at 1:37 pm
ur default snow leopard seems not stopped yet.
try to turn off the auto start for default apache.
then start your manual install apache2 manually.
November 19th, 2009 at 1:06 am
I get: Can’t connect to local MySQL server through socket ‘/opt/local/var/run/mysql5/mysqld.sock’ (2)
Any idea?
Thanks!
February 8th, 2010 at 10:41 am
When I typed in the “sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist” command, I got an error:
launchctl: Couldn’t stat(“/Library/LaunchDaemons/org.macports.mysql5.plist”): No such file or directory
Please help? Thanks!
February 8th, 2010 at 4:26 pm
Hi Chong, the +server variant has been obsolete after the post published. to solve this problem you need to install the mysql5-server instead of mysql5
March 12th, 2010 at 5:23 pm
Hi!
i can’t to change a root password in mysql
when i tried it, i get: ‘Can’t connect to local MySQL server through socket ‘/opt/local/var/run/mysql5/mysqld.sock’
and i can’t run mysql to fix it.
If it possible, help me. Thnx
March 12th, 2010 at 8:32 pm
make sure your mysql has started. once mysql started the mysql.sock will be there