1.13.2007

Creating VirtualHosts on a Local Server for Testing

After an exhaustive search marathon, for lack of a better word, on google, I have masterminded a way in which it is possible to test a website (built on php, in my case) as if it were actually deployed at its own DocumentRoot. Basic knowledge of Apache configuration is required (assumed since you're actually reading on.)

Subversion control is fantastic and I love running my own Apache server on my Mac. My problem was experienced when I updated between my computer (where the files are a sub-directory of DocumentRoot) and the actual deployed server (where the files are in the DocumentRoot.) For the purpose of including files with their proper paths these files would have to be adjusted each time I updated. Each file would have to be altered, individually, effectively breaking the paths when updated on the other server.

Now, this may not seem like rocket science to any person with experience working on deployed websites, but, there are a few steps that have not yet been grouped together. Specifically, if your own test server is operating simply on a local network or machine and if you don't have a domain pointing to this server (in which case DNS would take care of resolving a name for Apache to pick up on) simply adding an extra VirtualHost does not work.

The Solution: on most *nix systems you can modify your own /etc/hosts (try using sudo, not su) and add an entry in the same pattern as localhost with a name of your choosing. In my case the entry was 127.0.0.1 thanatos. Now, in your apache configuration files httpd.conf or sites-available/sites.conf add the line ServerName name-from-hosts to the VirtualHost section that already exists. Repeat this process from /etc/hosts to .conf file, this time using a "subdomain", still pointing at 127.0.0.1, for each website you wish to have it's own DocumentRoot. From now on, add a new section for each "subdomain" and configure its DocumentRoot to be the directory you wish to use with that site and the ServerName being the entry you added to /etc/hosts.

When finished entering all the new VirtualHosts, make sure that the site configuration is enabled. In apache2 just check sites-enabled/ (same path as sites-available) and see if the .conf file is there. If it is not, try using a2ensite, if it is available, if not link it using ln -s. Now, RESTART APACHE!

The Result: when you navigate to your subdomain.localhost (or the like) your sub-directory will be in it's own root and all includes, links, etc. will be accessible as if it were alone.

No comments: