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

advertisement

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 +server
    sudo 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: , , , , , , ,


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 :)


17 Responses to “How to install apache, php, mysql with macport in Mac OS X”

  1. AenTan Says:

    I use MAMP. This looks more complicated. :P

  2. Manuel Says:

    Thanks!

  3. Qiao Says:

    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~~

  4. chua Says:

    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

  5. Mark Wilden Says:

    The command for restarting Apache has an extraneous “port” in it. Otherwise, a very useful article – thanks!

  6. mikosh Says:

    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…

  7. chua Says:

    Thanks mikosh for your correction on “sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper start”

  8. Yolabingo Says:

    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.

  9. Josh Says:

    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??

  10. Solidariti Says:

    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

  11. Solidariti Says:

    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

  12. chua Says:

    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.

  13. Francois Arbour Says:

    I get: Can’t connect to local MySQL server through socket ‘/opt/local/var/run/mysql5/mysqld.sock’ (2)

    Any idea?

    Thanks!

  14. Chong Kee Tan Says:

    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!

  15. chua Says:

    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

  16. kenzoe Says:

    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

  17. chua Says:

    make sure your mysql has started. once mysql started the mysql.sock will be there

Leave a Reply