LAMP Virtual Hosts

Blog post featured image

In my previous post, I explained how to install LAMP on Ubuntu 11.10. This time, let’s delve into configuring virtual hosts. This setup allows you to host multiple independent sites, potentially across different domains.

Choosing a Storage Location for Site Files

First, decide where to store the files for your sites. I use my large /home partition for this purpose and created a folder at /home/www.

Setting Up the Environment

Execute the following commands to create necessary directories and set permissions:

sudo mkdir /home/www /home/www/log
sudo chmod ugo+rw /home/www /home/www/log

Now, let’s create a new virtual host:

sudo gedit /etc/apache2/sites-available/example

Inside this file, paste the following configuration:

<VirtualHost *:80>
  ServerName example.localhost
  ServerAlias www.example.localhost
  DocumentRoot /home/www/example
  ErrorLog /home/www/log/error.log
  CustomLog /home/www/log/access.log combined

  <Directory /home/www/example>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
  </Directory>
</VirtualHost>

After saving the file execute these commands to set up the site:

mkdir /home/www/example
echo "<?php echo(\"index page\"); ?>" > /home/www/example/index.php
sudo a2ensite example
sudo service apache2 reload

To access your new site you can either configure DNS to resolve http://example.localhost or simply modify /etc/hosts:

sudo gedit /etc/hosts

Then add:

127.0.0.1 example.localhost

Save the file and try accessing http://example.localhost. To view any site errors or warnings, check the `/home/www/log/error.log