Getting LAMP to work in Ubuntu Hardy

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.
1 2 3 | <?php phpinfo(); ?> |
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 userdirRestart 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
127.0.0.1 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 </directory> </virtualHost>
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 mywebRestart 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 rewriteRestart 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…
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.









Hi, Naz
Great post about LAMP setup in ubuntu, and this was a big help for me, and i had a problem in phpmyadmin , coz phpmyadmin, was in /usr/share/phpmyadmin so i had to create a symbolic link to phpmyadmin
sudo ln -s /usr/share/phpmyadmin /var/www/phpmyadmin
this thing worked well.
good work keep it up.
hey bro, did you try sqlbuddy? got to know about it from a tweet, and checked it out..was quite good..think u should check it out, if u haven't already..
Haven't heard of it.. Thanks for the info. Will check it out..
Awesome post! Thank you very much.
very useful.
Thanks a lot.
I love using Firefox with NoScript and Adblock Plus http://wiwapia.com/en/Adblock . Add those two little plug-ins to Firefox and you'll have a much safer browsing experience — provided you use NoScript properly and avoid manually downloading "codecs" from questionable sites.
Thank you very much for this set of instructions!! I tried several other approaches, for different Ubuntu releases, and none worked, but yours did!
Thanks for the great article, I refer back to it all the time. Can you tell me how to access any vhosts from another ubuntu machine on the network, i.e. http://10.0.0.xxx/dir? it wants to default to the /var/www/ and will not bounce to the vhost… it works perfectly locally, but I have a book list I created that my wife and I share and would like to only update one copy. Thanks in advance
Well… never mind, a little more coffee and I got it figured out… well, truth be told I re-read your article and the answer was already there, I was just missing it. Thanks again. Cheers!
hello sir, i am new to ubuntu and have just installed LAMP on my pc..but i dont know how to use it ..as in how to access sql client server through ubuntu..can u please guide me ..