Enable Apache virtual hosts on OS X 10.8 Mountain Lion

I like to place each of my projects in a subfolder of Sites. The Sites folder is no longer present in OS X 10.8, but you can create it manually from the Terminal:

mkdir ~/Sites

For instance, for the project that I will be hosting locally at http://test.site, I created a folder called test.site:

mkdir ~/Sites/test.site

First, let’s add the test.site domain to hosts file:

sudo nano /etc/hosts

At the end of the file, add:

127.0.0.1       test.site

Next step, edit /etc/apache2/httpd.conf, and uncomment:

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

Now, edit Apache’s virtual hosts configuration file httpd-vhosts.conf, and place the following:

NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "/Library/WebServer/Documents"
    <Directory "/Library/WebServer/Documents">
        Allow From All
        AllowOverride All
    </Directory>
    CustomLog "|/usr/sbin/rotatelogs /private/var/log/apache2/default-access_log 86400" combined
    ErrorLog "|/usr/sbin/rotatelogs /private/var/log/apache2/default-error_log 86400"
</VirtualHost>
<VirtualHost *:80>
    ServerName "test.site"
    DocumentRoot "/Users/damiano/Sites/test.site"
    <Directory "/Users/damiano/Sites/test.site/">
        Allow From All
        AllowOverride All
    </Directory>
    CustomLog "|/usr/sbin/rotatelogs /private/var/log/apache2/test.site-access_log 86400" combined
    ErrorLog "|/usr/sbin/rotatelogs /private/var/log/apache2/test.site-error_log 86400"
</VirtualHost>

The first entry points to the default web site, which will also work as a catch-all host for any virtual host that wasn’t defined anywhere else. The second entry points instead to the~/Sites/test.site folder created for http://test.site

Finally, restart Apache to load the new configuration:

sudo apachectl restart
Advertisements

Configure Apache and PHP on OS X 10.8 Mountain Lion

Apache/2.2.22 is pre-installed in Mountain Lion. To check the version installed:

httpd -v

To start | stop | restart Apache:

sudo apachectl start|stop|restart

After starting Apache you should see the “It Works!” text in the browser – http://localhost.

OS X has historically had 2 document roots, one at system level, and one at user level – the user level one allows multiple acounts to have their own web root, whilst the system one is global for all users.

The default system document root is found at /Library/WebServer/Documents/ – http://localhost/.

The user document root level should be found at the ‘~/Sites’ folder in the User account on a clean installation, but is missing in Mountain Lion,  so you need to make a “Sites” yourself:

cd /Users/username
mkdir Sites

Check that you have a “username.conf” filed under: /etc/apache2/users/. If you don’t (very likely), then create one:

cd /etc/apache2/users
sudo nano username.conf

Then add the content below:

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

Permissions on the file should be:

-rw-r--r--   1 root  wheel  298 Jun 28 16:47 username.conf

Then this user level document root will be viewable at: http://localhost/~username/

PHP 5.3.13 is loaded in OSX 10.8 Mountain Lion and needs to be turned on by uncommenting a line in the httpd.conf file.

sudo nano /etc/apache2/httpd.conf

Uncomment the line (remove the #):

LoadModule php5_module libexec/apache2/libphp5.so

To configure php, create php.ini file:

sudo cp /etc/php.ini.default /etc/php.ini
sudo chmod ug+w php.ini
sudo nano /etc/php.ini

Re-load Apache.