Weblog of Nazly Ahmed

Nazly Ahmed

Nazly Ahmed

Web Developer. PHP Addict. Wordpress Hacker. FOSS Enthusiast. (Micro)Blogger. Photo Hobbyist. Cricket Fanatic. Husband. Dad.

Few of my friends who switched to Ubuntu lately, asked me how to get LAMP (Linux, Apache, MySQL, PHP) working in their desktops. Most of them have installed Ubuntu 8.04 (Hardy Heron) so I planned to write a small guide on how to setup a LAMP development environment. This will require to have an Internet Connection since the installation will download the necessary packages from the repositories. I will also explain how to add Virtual Hosts so that we can setup different environments to each website we create. I will also explain on how to enable Apache modules such as Userdir and Rewrite. I would normally enable Userdir module on development environments because it allows me to quickly test my code in my home directory. Rewrite module will enable features like having Search Engine Friendly URLs if we can write few Rewrite rules in the .htacess file.

Getting LAMP installed is quite easy in Ubuntu 8.04. I will be using the command-line in this small guide.

Open command-line terminal by going to Applications -> Accessories -> Terminal

First Update the repositories

sudo apt-get update

Install Apache2 – PHP5 – MySQL5 in one go

sudo apt-get install apache2 php5 mysql-server-5.0

Installation will take a while depending on your Internet Connection speed to download the necessary packages. You will be prompted to enter the MySQL Server root password. Enter the password and that's it. Apache2, PHP5 and MySQL5 will now be installed in your computer.

Now open a Browser Window, most probably Firefox and in the address bar type http://localhost. You will get a message like It Works! in the browser which means Apache is successfully installed. The default webroot directory will be /var/www/ where you can place a phpinfo page to check whether PHP is installed successfully as well. But this directory is not writable by the user. So we might need to sudo and write to this directory. Later in this guide I will explain how to test files that resides in my home directory.

Create PHP info page to check whether the PHP installation was successful

sudo gedit /var/www/info.php

Copy the blow code in the editor.


Save the file and close the editor.

Now goto http://localhost/info.php. If you see the PHP Info page then PHP is also successfully installed.

Now we need to check whether MySQL is installed successfully as well. In the terminal type

mysql -h localhost -u root -p

Enter the root password you entered during the installation process. If you are able to successfully login and see the welcome massage and the MySQL command prompt, MySQL is also installed successfully.

I would then install PHPMyAdmin, a web based GUI to handle MySQL Databases.

Install PHPMyAdmin

sudo apt-get install phpmyadmin

You will be prompted to select the server type, select apache2.

We will then restart Apache to make the changes in effect

sudo /etc/init.d/apache2 restart

Now goto http://localhost/phpmyadmin/. If you see the PHPMyAdmin login page then it is also installed successfully.

Though we now have everything in place, saving files into /var/www/ can be an issue during the development coz the user doesn't have permissions to write to this directory. We can overcome this by setting permissions to the user to write to this directory. But I always prefer having Userdir module enabled. If I assume my username is spider, I can save my files in a folder called 'public_html' under my home directory (/home/spider/public_html) and call it from the browser using the following address in my browser http://localhost/~spider/. But for this to work UserDir module should be enabled in Apache.

Enable UserDir module in Apache

sudo a2enmod userdir

Restart Apache to make the changes in effect

sudo /etc/init.d/apache2 restart

Userdir module is now installed and working, so now we can save files in the public_html folder which you need to create in your home directory. But this method is good for testing small scripts. Its always advisable to create a Virtual Host for each project so that we can use a hostname for a website and have a directory apart from the default webroot associated with it. We can change certain behaviours while setting up a Virtual Host which allows us a better testing environment.

Setting up a Virtual Host

First we need to create a hostname associated with our local IP address. To do that we need to edit the hosts file

sudo gedit /etc/hosts

Add the following in a new line after the entry for localhost myweb.local

Save the file and close the editor window.

myweb.local is our hostname where it can be any name except localhost

Now we need to setup a Virtual Host in Apache for myweb.local

We need to add a new file for the sites available for Apache

cd /etc/apache2/sites-available
sudo touch myweb
sudo gedit myweb

In the editor add the following lines.

<virtualHost *>
    ServerName myweb.local
    ServerAdmin webmaster@localhost
    DocumentRoot /home/spider/public_html/myweb/
    <directory /home/spider/public_html/myweb/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all

Save the file and close the editor window.

Make sure you have created this directory /home/spider/public_html/myweb/

Now we need to enable this site which is available in Apache. To enable the site simply run this command

sudo a2ensite myweb

Restart Apache to make the changes in effect

sudo /etc/init.d/apache2 restart

Thats it. We have our new website running. In your browser goto http://myweb.local. You will see the new website which is located in the directory we assigned to this Virtual Host.

If you need more websites such as this, you can repeat this process.

If you didn't notice, in the Directory settings I set 'AllowOverride All' because I need mod_rewrite to work in this site. But we need to enable Rewrite module for this to work. If you don't need to override these settings set it to 'AllowOverride None'

Enable Rewrite module in Apache

sudo a2enmod rewrite

Restart Apache to make the changes in effect

sudo /etc/init.d/apache2 restart

We now have mod_rewrite working as well. A .htaccess file with rewrite rules has to be placed in the root directory of this site to have Search Engine Friendly URLs working.

I hope this guide will help you to setup a LAMP development environment in Ubuntu 8.04. If you come across any issues please feel free to write to me using the comment box.

Happy Coding…

Posted on 21st July 2008 11:14:05