I like to run a local version of Apache on my laptop for various projects so I can bring them wherever I go and not have to worry about them being live or screwing anything up. Ubuntu makes it extremely easy to setup a LAMP server. All you have to do is run:
sudo taskselThen follow the onscreen instructions and choose “LAMP Server” when prompted to be up and running with Apache, MySQL, and PHP in mere minutes. This is great for a user that just wants the standard versions of PHP or MySQL. However, I’m working on a project that requires PHP 5.3, and installing a LAMP server using tasksel installs PHP 5.2. I decided that I would just install Apache and PHP manually so that I could use PHP 5.3. This guide starts with my ideal Apache setup. This is kind of a personal preference of how I like Apache setup, others may prefer a different method. If you follow this guide directly you will end up with your web directory in your home folder, with two sites (http://sandbox.local/ and http://production.local/). Lets get started.
Installing Apache
Installing Apache is the easy part, simply use apt:
sudo apt-get install apache2 apache2-prefork-dev apache2-mpm-prefork
Let it do its thing and in a few minutes you should have Apache up and running. Once it is done you can test if it is working by opening a web browser and entering http://localhost/ in the address bar. You should get Apache’s default message that says “It works!”. Now that Apache is installed we can configure it to fit our needs.
Configuring Apache
The first thing we are going to do is edit our hosts file to include our two “domain names” we’ll be using for the site, sandbox.local and production.local. Open up the /etc/hosts file with nano (or your editor of choice):
sudo nano /etc/hosts
And add the following lines below the line that begins with 127.0.1.1:
127.0.1.2 sandbox.local www.sandbox.local 127.0.1.3 production.local www.production.local
For those not familiar with the hosts file, what this is doing is assigning an IP Address to the host names. 127.0.1.2 and 127.0.1.3 are IP addresses that point to the local machine. The values following the IP Address are the host name, and host alias respectively. If you want to additional host names that point to the local machine then add them here. You can call your host names whatever you like instead of sandbox.local and production.local, just be sure to substitute your own names in place of these for the rest of the tutorial.
Now that we have our host names pointing to our local machine we can create directories to hold the content for our two websites. I created my base web directory inside of my home directory:
mkdir ~/www
Now I want a directory for each site inside of the www directory. The following will make a directory for each site with sub directories for the type of content (httpdocs for the web files, logs for the logs). The last line just creates an index.html file in the httpdocs directory so we can test that our websites are working later.
For the sandbox.local website:
mkdir ~/www/sandbox.local mkdir ~/www/sandbox.local/httpdocs mkdir ~/www/sandbox.local/logs echo "<h1>sandbox.local</h1>" > ~/www/sandbox.local/httpdocs/index.html
For the production.local website:
mkdir ~/www/production.local mkdir ~/www/production.local/httpdocs mkdir ~/www/production.local/logs echo "<h1>production.local</h1>" > ~/www/production.local/httpdocs/index.html
Now we have our host names setup, and directories setup that contain our website. The next step is to create configuration files to tell Apache what our host name (domain name) is and where on the computer it is located. The configuration files for each site should be created in "/etc/apache2/sites-available".
First lets create the configuration file for sandbox.local
cd /etc/apache2/sites-available sudo nano sandbox.local
The file sandbox.local should contain the following. You will have to change the "ServerAdmin" value and all the instances of "/home/lane" to your home directory depending on your username.
<VirtualHost *:80> ServerAdmin username@sandbox.local ServerName sandbox.local ServerAlias www.sandbox.local DocumentRoot /home/lane/www/sandbox.local/httpdocs/ <Directory /home/lane/www/sandbox.local/httpdocs/> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> ErrorLog /home/lane/www/sandbox.local/logs/error.log LogLevel warn CustomLog /home/lane/www/sandbox.local/logs/access.log combined </VirtualHost>
Now do the same for production.local.
sudo nano production.local
With contents:
<VirtualHost *:80> ServerAdmin username@production.local ServerName production.local ServerAlias www.production.local DocumentRoot /home/lane/www/production.local/httpdocs/ <Directory /home/lane/www/production.local/httpdocs/> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> ErrorLog /home/lane/www/production.local/logs/error.log LogLevel warn CustomLog /home/lane/www/production.local/logs/access.log combined </VirtualHost>
As you can see the two configuration files are pretty straight forward XML files. The ServerName and ServerAlias values tell Apache what host name the following configuration is meant for. The DocumentRoot value tells Apache where to find the files for this website. The Directory directive allows you to specify specific options for the given directory. Lastly the ErrorLog and CustomLog tells Apache where to store the log files for this domain.
Once the configuration files have been created we want to enable them so that Apache knows they are there. I also chose to disable the default config, this is up to you. The default config will load whatever is in "/var/www" when you point your browser to "localhost" (which is why we recieved the "It works!" message earlier.
sudo a2ensite sandbox.local sudo a2ensite production.local sudo a2dissite default
Finally the last step is to restart Apache. This can be done with the following command:
sudo /etc/init.d/apache2 restart
If all went well we should be able to bring up our two new local websites. Here is where everything we've done comes together. Open up your web browser and type in "http://sandbox.local/". When you type in sandbox.local in your web browser, the host name is looked up (and found in /etc/hosts) which points to the localhost (127.0.1.2). Apache is listening on port 80 (the default http port) and picks up the request for "sandbox.local". It looks in the enabled sites for one with a ServerName of "sandbox.local" then points to the requested file (index.html by default) in "DocumentRoot". The web browser then displays this page and should say "sandbox.local" in big letters.
Now point your web browser to http://production.local/ and you should receive a similar page that says "production.local" in big letters.
You can now put whatever web content you want in ~/www/sandbox.local/httpdocs/ or ~/www/production.local/httpdocs/ and they will load up when you type their host name into your web browser.
This isn't really all that useful yet because all you can load up are html pages, which you could do anyways just by double clicking on an html file. In my next blog post I will show you how to install PHP for use with your Apache web server so you can use it as a testing ground for various projects.
[...] This guide will outline how to compile PHP 5.3 from source for use with Apache. The instructions in this guide have been tested with Ubuntu 9.10 and should work on other debian based distros. This guide is a slight modification of the video guide on setting up Apache, PHP 5.3 with xdebug, MongoDB, and Lithium created by Jon Adams. Before proceeding with the instructions below you will need Apache installed on your computer. If you do not yet have apache you can follow my guide for setting up Apache on Ubuntu 9.10. [...]