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

Advertisements

  • 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




Share this with your friends:-

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

  1. Asim says:

    A lot of the programs do not install correctly

    I have run the steps twice and build of mysql5 does not happen correctly
    I have checked in the opt folder and it is not there

  2. If you’re having problem with the mysql_install_db command, try this:

    sudo chown -R mysql:mysql /opt/local/var/db/mysql5
    sudo chmod u+rwx,go= /opt/local/var/db/mysql5
    sudo /opt/local/lib/mysql5/bin/mysql_install_db –user=mysql

    If you’re having problem setting the root password, reset it using the instructions found here: http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html#resetting-permissions-unix

    For starting and stopping the mysql daemon I use the following aliases:
    alias mysql_start=’sudo /opt/local/etc/LaunchDaemons/org.macports.mysql5/mysql5.wrapper start’;
    alias mysql_stop=’sudo /opt/local/etc/LaunchDaemons/org.macports.mysql5/mysql5.wrapper stop’;
    alias mysql_restart=’sudo /opt/local/etc/LaunchDaemons/org.macports.mysql5/mysql5.wrapper restart’;

    Best of luck!

  3. matea says:

    dont forget to do

    sudo port load mysql5-server

    if you are missing the plist

  4. gagarine says:

    Perfect guide. Thanks.

    If you want to have the php 5.2 instead of php 5.3 install like that:
    $sudo port install php5 +apache2 +mysql5-server

    You can setup the password of MySQL before it run… so first
    $sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
    and after
    $/opt/local/lib/mysql5/bin/mysqladmin -u root password root

  5. umut says:

    I solve the problem above now i have another problem i cant change my root pasword is says ; Can’t connect to local MySQL server through socket ‘/opt/local/var/run/mysql5/mysqld.sock’

    so i tried those steps to start server

    mkdir /etc/init.d
    cp /opt/lampp/bin/mysql/mysql.server /etc/init.d/mysql
    chmod 755 /etc/init.d/mysql
    /etc/init.d/mysql start

    it gives this error : Starting MySQL
    . ERROR! Manager of pid-file quit without updating file.

  6. umut says:

    when try to install gawk following lines show up in my terminal and it stuck there however i installed nawk correctly i cant install gawk

    sudo port install gawk

    Password:
    —> Computing dependencies for gawk
    Waiting for lock on /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_ports_textproc_expat/work/.macports.expat.state

    any idea what is this and how can i solve this

  7. chua says:

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

  8. 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

  9. 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

  10. 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!

  11. 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!

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

  14. 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

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

  16. 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.

  17. chua says:

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

  18. 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…

  19. Mark Wilden says:

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

  20. 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

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

  22. AenTan says:

    I use MAMP. This looks more complicated. 😛

Leave a Reply