One of the things that annoys me about website development is the need to prefix webpages running on my local server with localhost like:
http://localhost/site_name1, http://localhost/site_name2, etc
I don’t usually have the luxury of an external development server, but having an environment that closely mimics my site’s final destination is critical. It was actually pretty easy to set up a local server on my computer. Here is what my local development server addresses look like:
http://www.projectname.localhost, http://www.otherproject.localhost
Set Up
First, I create a public_html folder to store all of my websites. I prefer to keep everything in my home directory, so my folder is /home/jon/public_html. I also recommend that development occurs in the home directory for the simplicity of setting permissions and the ease of performing backups (you backup your home directory, right?)
Your “home” folder can be in any number of locations:
- Linux – /home/<name>
- OSX – /Users/<name>
- Windows:
- XP – C:\Documents and Settings\<name>
- Vista/7 – C:\Users\<name>
- In Windows, your home folder can be located using the %UserProfile% environment variable.
Moving on… let’s assume you’ve created <home directory>public_html
Now install the latest apache server, mysql, and PHP. Use whatever installation packages suit your OS – it actually doesn’t *really* matter that much where everything gets installed. I used synaptic package manager in Ubuntu and installed it all from there – took 10 minutes; 8 minutes of which were download times. There are also some packages out there to make it even easier to set things up on your local computer. I haven’t used any of them – but a quick google search will turn up several candidates. Here are a couple:
Configuration
Here’s where the setup gets fun. (The actual location of the configuration files are different depending on your OS, I will do my best to point you in the right direction)
- Configure Apache for virtual hosts:
- Edit the <path to server>/httpd.conf file (on some systems, this could be called apache2.conf. If you have both, try the apache2.conf file first)
- Find the line (near the end of the file) that looks like this:
# Virtual hosts
# Include path/to/virtual-hosts/file/or/directory - Remove the comment (hash mark) in front of the Include directive
- Save the file
- Find the line (near the end of the file) that looks like this:
- Edit the virtual-hosts.conf file (on my Ubuntu 9.10 system, it is the ‘default’ file under /etc/apache2/sites-available). Additional configuration options can be found here: http://httpd.apache.org/docs/2.2/vhosts/
<virtualhost *:80>
DocumentRoot /path/to/public_html/site_folder
ServerName www.sitename.localhost
ErrorLog logs/sitename.localhost-error_log
</virtualhost> - restart apache
127.0.0.1 www.sitename.localhost
I really like this approach because:
- I can treat each folder in my public_html folder as independent websites
- I can set up each project up to mimic it’s production structure so that the FTP/upload process requires little or no additional configuration.
- I don’t have to worry about absolute/relative paths mapping to the wrong place. The site root is no longer /public_html










