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.

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

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.

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.

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.

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.

test.php

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.

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.

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

testdb.php
<php>
<?php
$con=mysqli_connect(“localhost”,”myuser”,”pwd123″,”Test”);

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

mysqli_close($con);
?>

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: