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.

[email protected]:~/$ 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>
    ServerAdmin [email protected]
    DocumentRoot "/Users/pankaj/CODE/example.com"
    ServerName example.com
    ServerAlias www.example.com
    ErrorLog "/private/var/log/apache2/example.com-error_log"
    CustomLog "/private/var/log/apache2/example.com-access_log" common
        <Directory "/Users/pankaj/CODE/example.com">
                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.       example.com

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 https://example.com/abc.html. 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/libphp5.so

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 https://example.com/test.php 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. 
  • MySql-DMG-Installer-450x123
  • MySql-DMG-Installer-450x123
  • 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.

[email protected]:~$ export PATH=$PATH:/usr/local/mysql/bin
[email protected]:~$ 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;
[email protected]:~$

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.

[email protected]:~/$ 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 example.com directory and try accessing the URL https://example.com/testdb.php. 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/example.com/testdb.php on line 2Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2002): No such file or directory in /Users/pankaj/CODE/example.com/testdb.php 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/example.com/testdb.php 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/example.com/testdb.php 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/example.com/testdb.php 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. 🙂

By admin

Leave a Reply

%d bloggers like this: