Linux

Install LAMP on Ubuntu

Blog post featured image

In my previous post, I showed you how to install a LAMP (Linux, Apache, MySQL, PHP) stack on Ubuntu 11.10. In this post, I will guide you through the process of configuring virtual hosts. This will allow you to create multiple independent websites, even across different domains.

Setting up the Directory Structure

First, decide where you want to store the folder that will contain your website files. In my case, I have a large ‘/home’ partition, so I chose to create a folder at ‘/home/www’.

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

Creating a Virtual Host Configuration

Now, let’s create a new virtual host configuration file.

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

Edit the ‘example’ file and paste the following code into it:

<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>

Save the file and run the following commands to create the necessary directory structure and enable the virtual host:

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

Configuring Local DNS Resolution

To access your website, you can either configure your DNS to resolve the URL ‘http://example.localhost’, or you can simplify things by adding an entry to your ‘/etc/hosts’ file.

sudo gedit /etc/hosts

Add the following line to the file:

127.0.0.1 example.localhost

Save the file and try opening the URL ‘http://example.localhost’ in your web browser.

You can check for any site errors and warnings by inspecting the log file located at ‘/home/www/log/error.log’.