Coding on the go – Setting up local Apache
Posted by Jesper | Filed under Code, Development, Technology
As one might imagine, I’ve setup Apache with PHP support quite some times over the last years. A thing I’ve already enjoyed, is having a local development environment installed on my laptop, and having my development environment with me, everywhere I go. Over the next couple of weeks, I’ll walk you through setting everything up. We’ll start out by setting up Apache.
Why would I run everything locally ?
Over the last years, a lot of the development agencies in the PHP world, has found out the joys of having local, individual environments for all developers, and managing larger projects through version control like SVN, CVS or, my new favourite, Git. Most people don’t have the luxury of having their own dedicated testserver for development, but you really don’t need one either. If you just have a Mac with OS X, you’ll have everything you need.
The reasons for having a local environment are quite massive: You’re able to have a complete replica of a running website, you’ll be able to develop new features without risking the stability of your public site. You’ll have full control of every setting in Apache, MySQL and PHP, meaning you’ll have the opportunity of testing optimizations and database structures properly, without the risk of destroying everything in a production environment.
Prerequirements
To complete the setup of Apache & friends, you’ll need the assistance of Terminal. If you are not comfortable with Terminal, just feel free to copy’n'paste every command here. Of course, as time passes, and you’re using your new environment, you’ll probably notice the advantages of Terminal anyway.
Setting up Apache
As Apache 2 is bundled with Leopard, this is very easy. Apache is controlled by enabling Web sharing in System Preferences.
Setting up webroots
As you have full control of Apache’s setup, you could actually put your websites whereever you’d like. However, as you already have the Sites folder available in your home directory, I’ll just make use of that. This also ensures, that Time Machine for instance is able to create backups of your sites.
So go ahead and create a directory within Sites for your new site, and try to give it a memorable name (In this example I’ll use jesperrasmussen.com from now on) .
[shell]sudo mkdir -p ~/Sites/jesperrasmussen.com/htdocs[/shell]
While you’re at it, add a general directory for Apache’s logs within Sites, to gather all sites logs within one place, making debugging and log-analyzing a bit easier.
[shell]sudo mkdir ~/Sites/logs[/shell]
Now that that’s done, you should have a couple of nice directories for the virtualhost and its logfiles, so let’s move on.
Setting up a VirtualHost
In the default setting, Apache will see your Sites folder as one site, as it’s setup to allow one personal site per user. To make Apache recognize the new virtual hosts, you need to edit the file /private/etc/apache2/users/.conf in your favourite editor (I recommend emacs or nano for Terminal editing).
The file will look somewhat like this when you open it:
[shell]<Directory "/Users/jesper/Sites/">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>[/shell]
Now, what we’ll do, is that we’ll add the lines for a new virtualhost in the configuration. Furthermore, in my example, there’s an added .dev alias to the domain, allowing me to access the local development version of the site by accessing jesperrasmussen.dev, and access the public version (On another server) by accessing jesperrasmussen.com. Anyway, add the following lines, replacing jesperrasmussen.com with whatever your virtual host is called, and save the file.
[shell]NameVirtualHost *:80
DocumentRoot /Users/jesper/Sites/jesperrasmussen.com/htdocs
ServerName jesperrasmussen.com
ServerAlias jesperrasmussen.dev
ErrorLog /Users/jesper/Sites/logs/jesperrasmussen.com-error_log
CustomLog /Users/jesper/Sites/logs/jesperrasmussen.com-access_log common[/shell]
Restarting Apache
Inorder to test the new site, we need to reload Apache’s configuration files. This is done by testing the new config files first.
[shell]sudo apachectl -t[/shell]
If this says “Syntax OK”, we should gracefully load the changes into Apache. *)
[shell]sudo apachectl graceful[/shell]
Accessing your newly created site
Now, you may think you should be able to access your new site at the domain you’ve entered. But no, you still need one more thing, changing your hosts-file into handling your domain locally (Or at least, the development version). This is done by editing /etc/hosts (Again, using emacs or nano), like so:
[shell]sudo nano /etc/hosts[/shell]
This should load your hosts-file **), which should look like this:
[shell]127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost[/shell]
Now, in the bottom of the file, insert a couple of blank lines to seperate your changes from the default parts, and insert this line:
[shell]127.0.0.1 jesperrasmussen.dev www.jesperrasmussen.dev[/shell]
This will force your machine into believing that your domain is located at localhost, thus making the request at your local Apache.
Now, everything should work, try putting a dummy index.html file in your new htdocs directory, and try loading the site in your browser. Next time I’ll have a look at enabling PHP and MySQL support on your standard machine.
Further reading
It’s come to my attention that Patrick Gibson has a fine shellscript for handling the VirtualHost part, making this a lot easier.
You might want to check that out
* Gracefully restarting Apache lets Apache apply the configuration more smoothly, by allowing it’s child processes to keep running with the old configuration, but loading the new configuration for all new processes. This is usually done in production environments, to avoid hard restarting servers, thereby avoiding possible downtime.
** The hosts-file is OS X’s way of overriding DNS locally. It allows you to force a domain to a specific IP-address rather than the one specified in the domains DNS. This is especially useful for testing a development or staging environment with the correct domainname.
November 4th, 2009 at 10:02 am
[...] just read a post about local development in apache by Jesper Rasmussen and thought one thing was missing. I usually test my sites and applications [...]