For a web developer having Mac is a great feeling. Most of the important tools you need are already there. OS X comes with Apache and PHP is pre-installed, just need to configure these tools to boot up the development machine. If you are newbie in web development, there are some all in one package applications like MAMP and XAMP, comes with pre-configured Apache, MySQL and PHP. Both are great to start with but you’ll be dependent to have latest version of PHP/Apache/MySQL, and also they have restriction to configure some features.

Personally, I like to configure development environment by myself. It’s fun! Stay with me, you will enjoy it! I’ll keep the process as easy as I can so that it will be easy to understand.

Note: This article is for OS X: Sierra (10.12), El Capitan (10.11) and Yosemite (10.10) only.

The Goal:

The end goal of this article is making your MAMP (Mac Apache MySQL and PHP) stack development machine up and running. We will also configure virtual host so that we can access any project like http://project.test instead of http://localhost/project

Note: To setup local development environment most people use .dev I’m not using it now because .dev TLD is officially own by Google, so its time to move on.

Bootstrapping:

Working in the terminal is real joy. Some people scare of it for no reason! Make yourself comfortable with commaline, it will help you in many ways. I will use it a lot to go through this article.

Tools I’m going to use:

Apache
PHP
MySQL
DNSmasq
Xcode
Homebrew
Vim/Nano
Sequel Pro/phpMyAdmin

First w’re going to install the Xcode. You don’t need to know too much about Xcode, it is a development environment tool by Apple. The package include some important tools that will need sometimes in the work flow. Just download it from app store (its free), install and forget about it. Alternatively, just run this command on the terminal.

xcode-select --install

Note: All command should be on single line and exact.

Homebrew:

Homebrew is a package management application for OS X. If you already don’t have homebrew installed on your Mac then run this command to install homebrew.

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

and export the path into the bash file

echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.bash_profile

In this article, I will use vim to create/modify files. Vim is a fantastic editor with a little high learning curve but if you can adapt, it will help for a long run. If you want to use vim follow their instructions to install and configure vim in your machine.  You can use nano that comes by default with OS X.

Let’s configure the Apache server first.

Apache:

Run this command to start Apache server.

sudo apachectl start

Note: Whenever you run sudo command, you will be asked for the admin password. Enter your password. 

To start Apache automatically on start up , just run the following command once.

sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

Test the server by typing http://localhost/ on the browser. You should get a very simple and clean “It Works!” message. Like this:

mac-os-x-apache-homepage

Very Good! Let’s move forward.

Document root:

Document root is the folder where all your project files will be placed. It’s just a simple directory. I created my document root directory named www on my user’s root. You can create yours anywhere you want. Some people prefer to use Dropbox as a document root so that the files are safe from being lost. It’s completely up to you, though you will need little configuration to connect this directory with Apache server. I’ll tell you how to do that when we configure other settings. For now, just create your document root directory at your preferred location.

Screen_Shot_2016-02-09_at_2_08_26_AM

PHP:

PHP is also there, you just need to configure it for the work. You can also install newer version of PHP or even you can use multiple version of PHP. I will talk about this in my future articles. Now let’s use the existing version.

To enable PHP, open the apache configuration file called /etc/apache2/httpd.conf by run this command:

sudo vim /etc/apache2/httpd.conf

Note: To use nano just replace ‘vim’ with ‘nano’.

TIPS: To get full path of any file, just run the command   locate filename.extention   (ex. locate httpd.conf )

Search for the libphp5.so on line 169 (may differ little). Remove the ‘#’ from the beginning of the line.

169 #LoadModule php5_module local/Cellar/php56/5.6.15/libexec/apache2/libphp5.so

TIPS: To search quickly in vim, in command mode run   /textToFind (ex. /libphp5)

TIPS: For vim press esc and  i to enable insert mode.

To allow dynamically configured mass virtual hosting we need to enable mod_vhost_alias module on line 160 (may differ little).

160 LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so

We know this is the main Apache configuration file so we do not mess with this file rather than we will use httpd-vhosts.conf to configure the root directory and virtual hosts. So we need to include the httpd-vhosts.conf file in the httpd.conf file, so the Apache server will load this file when starts. Search for httpd-vhosts and remove the ‘#’ on line 503 (may differ little).

509 Include /private/etc/apache2/extra/httpd-vhosts.conf

We need to enable the mode_rewrite module.

168 LoadModule rewrite_module libexec/apache2/mod_rewrite.so

Now we need configure the httpd-vhosts.conf file, but we still have some little works to do in this file. Let’s do this first so that we can move out of here completely.

Permissions:

By default, the server restrict the access for security reason. For development machine we need to access the directory for good reason. These are the lines 220-223 (may differ little) that control the permission.

220 <Directory />
221     AllowOverride none
222     Require all denied
223 </Directory>

Just comment out these lines by placing # at the beginning of each line. So it will become:

220 #<Directory />
221 #   AllowOverride none
222 #    Require all denied
223 #</Directory>

We also need to configure the user and group for apache server. By default, apache runs as user _www and Group _www. Change _www to your username and Group staff in line 182 and 183 (may differ little).

182 User _www
183 Group _www

will became

182 User user_name
183 Group staff

We are done here! Save the file and quit. For vim press esc key to enter command mode and type :qw!

Virtual Hosts:

Do you remember that we created our root directory (/Users/MunnaKhan/www in my case) to place all the project files?

The apache server is now need to know about that directory. Apache will load the httpd-vhosts.conf file when start but we need to modify the file to set some rules and permission so that apache can use that directory. Open the file and remove everything to a fresh start.

sudo vim /etc/apache2/extra/httpd-vhosts.conf

TIPS: For vim press esc to enable command mode and type :%d to delete all from the file.

Let’s point out the root directory and set few rules. Copy-paste this code and modify to locate your document root.

<Directory “/path_to/the_directory/you_created">
   Options Indexes MultiViews FollowSymLinks
   AllowOverride All
   Order allow,deny
   Allow from all
</Directory>

In my case its:

<Directory "/Users/MunnaKhan/www">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

Now create a test project called project on the document root and configure a virtual host for the project test. To do that, place this code after the above lines.

<Virtualhost *:80>
    VirtualDocumentRoot "/Users/MunnaKhan/www/project"
    ServerName project.test
    ServerAlias www.project.test
    <Directory "/Users/MunnaKhan/www/project /">
        AllowOverride All
    </Directory>
</Virtualhost>

Save the file and quit.

DNSmasq:

We cant access the www.project.test yet. We need to install the DNSmasq to configure the host.

brew install dnsmasq

To configure dnsmasq, copy the example configuration to /usr/local/etc/dnsmasq.conf
and edit to taste.

Copy the default configure file to local/etc directory to modify and use it
cp /usr/local/opt/dnsmasq/dnsmasq.conf.example /usr/local/etc/dnsmasq.conf

To start DNSmasq on startup
sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
and
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

Now we will modify the dnsmasq.conf file to bind the localhost IP: 127.0.0.1 with the .test TLD:

echo 'address=/test/127.0.0.1' > /usr/local/etc/dnsmasq.conf

and

echo 'listen-address=127.0.0.1' > /usr/local/etc/dnsmasq.conf

Now start dnsmasq:

sudo brew services start dnsmasq

It’s time to restart the server to load all the works we did so far.

sudo apachectl restart

We need to configure the OSX to make the DNS work. Just few steps:

sudo mkdir /etc/resolver

and

sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/test'

We did some great job together! If everything is going fine, we can access the test project by browsing http://project.test

Note: To configure new virtual host you just need to add few more lines to the httpd-vhosts.conf file and you are ready to go.

Lots of time on your development process you may need to view the current PHP settings. The easiest way to do this just create a info.php file on your document root and place this in the file.

<?php echo phpinfo();

Test this by browsing http://localhost/info.php

Screen Shot 2016-02-15 at 10.52.14 PM

MySQL:

We didn’t install any database in our dev machine. We will install the MySQL server; you can ignore this part if like to use any other database. The easiest way to install mysql is use homebrew.

brew install mysql

After successful installation we need to start the mysql server:

mysql.server start

To have launchd start mysql at login :

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
and to load mysql immediately :
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

Finally add the mysql to the PATH environment variable in .bash_profile :
echo 'export PATH="/usr/local/bin/mysql:$PATH"' >> ~/.bash_profile

Now we can access mysql from terminal:

mysql -u root

NOTE: The default username is ‘root’ and password is not set yet.

TIPS: To secure the mysql run this command and flow the instructions mysql_secure_installation

phpMyAdmin:

I use sequel pro and phpMyAdmin to access the MySQL database. Let’s go through to setup phpMyAdmin. Download the latest version of .zip from here and unzip it. Create a new directory ‘phpmyadmin’ on your document root and place all files here. That’s it! Visit this URL:

http://localhost/phpmyadmin/

Screen Shot 2016-02-16 at 12.39.01 AM

Getting tired? Ok then let’s stop here!

Conclusion:

Your local development machine is ready to serve you. On my next article, I’ll discuss how to work with multiple version of PHP on same development machine. I’ll love to hear your comments.