Enabling "Personal Sites" with PHP on Lion

  • strict warning: Non-static method view::load() should not be called statically in /home/sigpboo7/public_html/modules/views/views.module on line 842.
  • strict warning: Declaration of views_handler_field_comment::init() should be compatible with views_handler_field::init(&$view, $options) in /home/sigpboo7/public_html/modules/views/modules/comment/views_handler_field_comment.inc on line 50.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/sigpboo7/public_html/modules/views/plugins/views_plugin_row.inc on line 135.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/sigpboo7/public_html/modules/views/plugins/views_plugin_row.inc on line 135.

After switching over to Lion, I had to re-enable personal sites to continue working on a side project. Fortunately, it’s just as easy in Lion as it was in Snow Leopard. I’ll walk through the setup process in this article. Note that you’ll need administrator access for a few steps in the process, so you’ll need the admin password.

In Snow Leopard, the quickest and easiest way to get sites set up was to use the “Web Sharing” feature of the OS, and it looks like Lion works the same way. OSX comes with Apache 2.2.21 and PHP 5.3.8 installed, which are both pretty current as of this writing. As long as those versions work for you, Lion will let you get to work without installing any extra software.

Since we’re using the Web Sharing feature of OSX, we’ll be setting up our site directories in our $HOME/Sites directory. First, go ahead and make a directory for your project in your local Sites directory; for this dicussion, I’ll use $HOME/Sites/XYZ as the site home directory. Crack open a Terminal and use:

$ mkdir $HOME/Sites/XYZ

Also drop a simple PHP script into that directory for us to load later (note: ^D indicates ctrl-D):

$ cat >$HOME/Sites/XYZ/index.php
<?php phpinfo();

Next, we’ll set up our Apache configuration. As usual in Apache, new sites will be configured as vhosts. Open up /etc/apache2/extra/httpd-vhosts.conf in your favorite text editor. Make sure you’ve got administrator privileges so you can save your changes! On my machine, I used:

$ sudo nano /etc/apache2/extra/httpd-vhosts.conf

In that file, add a VirtualHost section that defines the site for our xyz project. The simple example below will work, or you may want to do some customization, such as by adding a ServerAlias. You’ll need to replace USER with your actual username below:

<VirtualHost *:80>
    ServerAdmin youremail@example.com
    DocumentRoot "/Users/USER/Sites/XYZ"
    ServerName xyz
    ErrorLog "/Users/USER/Sites/XYZ/error.log"
    CustomLog "/Users/USER/Sites/XYZ/access.log" common

Next, we need to tell Apache to read that vhost file, and to load mod_php. Open up /etc/apache2/httpd.conf, again making sure you have administrator priviliges:

$ sudo nano /etc/apache2/httpd.conf

Uncomment the following lines, which are spread throughout the file:

LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
LoadModule php5_module libexec/apache2/libphp5.so
Include /private/etc/apache2/extra/httpd-vhosts.conf

Almost done. Our site needs an alias in /etc/hosts. So, open up /etc/apache2/httpd.conf in your favorite text editor, again making sure you have administrator priviliges:

$ sudo nano /etc/hosts

…and drop in the following line after the last entry:   xyz

Note that this xyz corresponds with the value xyz we put in the ServerName directory in our VirtualHost block.

We’re in business! All we need to do is enable Web Sharing in the Settings application:

Just uncheck and re-check the Web Sharing option. It’s OK if it wasn’t checked to begin with.

Once that’s done, go here in your browser, and you should be rewarded with a page like this:

Just uncheck and re-check the Web Sharing option. It’s OK if it wasn’t checked to begin with.

Congratulations! That page was served from your own machine. Now go make something awesome.

EDIT: You may also want to enable .htaccess files in your vhost, for example if you’re using clean URLs in Drupal. To enable .htaccess files, drop the following into the same /etc/apache2/extra/httpd-vhosts.conf file we edited before:

<Directory /Users/USER/Sites/XYZ>
    AllowOverride All