How to Install Apache, PHP and MySql on Mac OS X

Filed Under: Database

I have been writing blogs for a long time now and playing around with Apache and PHP every once in a while to change my Web Host server settings or to make some changes in my UI layouts. So finally I have decided to learn PHP and I use Mac for my personal usage, so the first step was to install Apache, PHP and MySql on my Mac OS X.

My Mac OS is Mountain Lion (10.8), so for earlier versions you might need to make some changes in the steps provided below.

Install Apache for Mac OS X

Mac OS is ultimately built on Unix so its very easy to install any Unix supporting softwares. Even better part is that Mac OS X comes with apache web server and we just need to start it and make some changes to setup our development environment.

We will start apache server with root user to avoid any permission issues.

pankaj@Pankajs-MacBook-Pro:~/$ sudo su -
Pankajs-MacBook-Pro:~ root# apachectl start
Pankajs-MacBook-Pro:~ root# 

Thats all is needed to start Apache web server on Mac OS X, now you need to verify it by launching browser and visiting page https://localhost/. You should get a page with message “It works!”.

Now we can proceed further for setting up PHP and MySql but before that I want to make some tweaks to setup my development environment.

Creating Apache VirtualHosts

Default location for Apache web server project files is /Library/WebServer/Documents/ which is not a convenient place to keep our project files, also we will have to access it in browser with localhost, so I decided to create my own virtual host pointing to my development directory. To enable VirtualHosts, we need to make some changes in Apache Configuration file (httpd.conf).

Pankajs-MacBook-Pro:~ root# vi /etc/apache2/httpd.conf
Uncomment below lines and save it.
# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf 

Now when Apache will load the configuration file, it will also load the virtual hosts configuration file. We can proceed now to HTTPD virtual hosts file.

Pankajs-MacBook-Pro:~ root# vi /etc/apache2/extra/httpd-vhosts.conf 
Add following configuration for a new VirtualHost and save it.

<VirtualHost *:80>
    DocumentRoot "/Users/pankaj/CODE/"
    ErrorLog "/private/var/log/apache2/"
    CustomLog "/private/var/log/apache2/" common
        <Directory "/Users/pankaj/CODE/">
                AllowOverride All
                Order allow,deny
                Allow from all

Make sure to change the DocumentRoot, ServerName and Directory location according to your needs. The Directory directive is added to avoid any permission issues.

The next thing we need to do is add the server URL to known hosts file, else it will go to DNS server and the request will be redirected to Internet rather than loading it from the local machine.

Pankajs-MacBook-Pro:~ root# vi /etc/hosts
Add below line and save it.

Our virtual hosts setup is done now, just restart the apache with command apachectl restart
Just put a simple HTML file (abc.html) in the server directory and try to access it in the browser If the request is still going to the Internet, try flushing your DNS cache using command dscacheutil -flushcache.

Sometimes Apache can crash because of heavy load, use apache restart script to get it started automatically.

Install PHP on Mac OS X

To enable PHP support for Apache, we need to make changes in its configuration file.

Pankajs-MacBook-Pro:CODE root# vi /etc/apache2/httpd.conf
Uncomment below line and save it.
LoadModule php5_module libexec/apache2/

Now restart the server and it should be able to process PHP files. To test it we can create a simple PHP file as below.


echo phpinfo();

Load the PHP file in browser with URL and you should see a huge page with all the PHP configurations.

Lets move now to install MySql database server on Mac OS X.

Install MySql on Mac OS X

  • Download the MySql DMG file from MySql Download Website.
  • Open the DMG file and install MySql server and Preference Pane for starting and stopping MySql server easily.



  • Start the MySql server if its not running and optionally you can select the checkbox to automatically start MySql server on startup.

There are so many Mac OS softwares that you can use for MySql database management but I liked SequelPro most, its easy to install and use and uses very less memory. You can download it from SequelPro Website.

Before I move forward to test MySql connectivity from PHP program, I will create a database and user. For easier access to MySql scripts, I have added MySql bin directory to my PATH and exported it.

pankaj@Pankajs-MacBook-Pro:~$ export PATH=$PATH:/usr/local/mysql/bin
pankaj@Pankajs-MacBook-Pro:~$ mysql -h localhost -u root -p
Enter password: <ENTER>
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5128
Server version: 5.5.25 MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create user 'myuser'@'localhost' identified by 'pwd123';
Query OK, 0 rows affected (0.03 sec)

mysql> GRANT ALL ON *.* TO 'myuser'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> create database Test;
Query OK, 1 row affected (0.00 sec)

mysql> exit;

Connecting PHP with MySql

When we install MySql, its socket file is located at /tmp/mysql.sock but when PHP tries it locate it, it look at /var/mysql directory. The easier solution is to make a symbolic link pointing to the actual MySql socket file.

pankaj@Pankajs-MacBook-Pro:~/$ sudo su -
Pankajs-MacBook-Pro:~ root#cd /var
Pankajs-MacBook-Pro:var root# mkdir mysql
Pankajs-MacBook-Pro:var root# cd mysql
Pankajs-MacBook-Pro:mysql root# ln -s /tmp/mysql.sock mysql.sock

Here is a sample PHP file that connects to MySql server.


// Check connection
if (mysqli_connect_errno($con))
echo “Failed to connect to MySQL: ” . mysqli_connect_error();
}else {
echo “Connected to MySql Test DB”;


Just put this file in directory and try accessing the URL You should see the message.

Connected to MySql Test DB

If you see below error message.

Warning: mysqli_connect() [function.mysqli-connect]: [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /Users/pankaj/CODE/ on line 2

Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2002): No such file or directory in /Users/pankaj/CODE/ on line 2
Failed to connect to MySQL: No such file or directory
Warning: mysqli_close() expects parameter 1 to be mysqli, boolean given in /Users/pankaj/CODE/ on line 10

Please check for the MySql socket file in both the locations and make sure MySql server is started.

If you get error message as:

Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user ‘myuser’@’localhost’ (using password: YES) in /Users/pankaj/CODE/ on line 2
Failed to connect to MySQL: Access denied for user ‘myuser’@’localhost’ (using password: YES)
Warning: mysqli_close() expects parameter 1 to be mysqli, boolean given in /Users/pankaj/CODE/ on line 10

This is some problem with the user creation, check above for the correct syntax for creating the user.

Thats all for setting up local Mac OS X environment with basic setup to learn PHP. I will come up with some PHP articles soon. 馃檪


  1. Kalpesh Popat says:

    Thanks. this saved my day

  2. Ric says:

    Hi Pankaj,
    I’m getting stuck at the point of testing the abc.html … Neither Safari nor Tor browsers display the test html page i created.
    Safari: can’t load the page because the server unexpectedly dropped the connection …
    Tor: We can鈥檛 connect to the server at

    done the usual traces … checked syntax, file names, restarted apache, restarted laptop, created a parallel to of my own. Both produce the same results.

    Any ideas where you might think I could check please?


  3. Altaf says:

    Hi Pankaj,
    Great tutorial. Thanks a lot for this.
    Even I had issues with forbidden error. It would be great if you could update your tutorial & mention the commands for Apache 2.4+, as in, we need to use
    Require all granted


  4. Maverickm says:

    Worked for me…Thanks and keep up the good work :)!

  5. Duc says:

    Followed your steps and it worked perfectly.


  6. Gavin Fonseca says:

    You could just use MAMP software to manage this it’s simpler and faster

    1. Pankaj says:

      There are easy ways and then there is fun to DIY (Do It Yourself).

  7. anonymous says:

    I must thank you for the efforts you’ve put in penning this blog. I am hoping to view the same high-grade content from you in the future as well. In fact, your creative writing abilities has motivated me to get my own, personal website now 馃槈

  8. pyrgus says:

    This is a very good article. However, when I set the virtual host and then open the page I see “You don’t have permission to access / on this server.”.

    1. Pankaj says:

      Try to put some file and access it, it should work.

      For above errors, please see resolution in this SO post

  9. Avinash Kumar says:

    It was very informative and learning article for me. Thanks for the sharing.

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content