Set up Apache, PHP and Sites folder on Mac

It is not very often but I do try out PHP and static html sites on my mac. But every time I try running apache server with applications under Sites folder, one or more issues pop up. So I thought I would write down the steps to get it working as it might help me or someone else next time.

To start apache, issue the following command in the Terminal

sudo apachectl start #restart if it is already running

Once done, visit http://localhost. You should see ‘It works’ message.

Next to serve projects, applications under ~/Sites folder, add/update the configuration file at /etc/apache2/users/username.conf with the following content

<Directory "/Users/username/Sites/">
  Options FollowSymLinks Indexes MultiViews
  AllowOverride All
  Order allow,deny
  Allow from all
  Require all granted

Replace username with your user name.

Change the permission if you need to

sudo chmod 644 username.conf

Make a backup of the original httpd.conf file

sudo cp /etc/apache2/httpd.conf /etc/apache2/httpd.conf.orig

Edit the main httpd.conf file to enable some modules

sudo vi /etc/apache2/httpd.conf

LoadModule authz_core_module libexec/apache2/
LoadModule authz_host_module libexec/apache2/
LoadModule userdir_module libexec/apache2/
LoadModule include_module libexec/apache2/
LoadModule rewrite_module libexec/apache2/
#Enable PHP module if not enabled
LoadModule php5_module libexec/apache2/

Now edit the following file to enable user configuration

sudo vi /private/etc/apache2/extra/httpd-userdir.conf

Uncomment this line

Include /private/etc/apache2/users/*.conf

Restart Apache

sudo apachectl restart

The applications/ static html sites under ~/Sites should now be available at
http://localhost/~username (replace username with your user name)

Try this out and let me know if it works (or doesn’t)

(I found had some useful information on the modules that were needed to be enabled to get apache working as expected.)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s