Nazly's BLOG

Getting LAMP to work in Ubuntu Hardy

Posted by Nazly on Monday, 21st July 2008 16:44:05 (GMT +0530)

http://www.nazly.net/imgs/ulamp.jpg

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.

<?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 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
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 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...
Comments (3)

Cheat Sheets for Web Developers

Posted by Nazly on Wednesday, 9th July 2008 14:56:09 (GMT +0530)

I found this excellent list of cheat sheets for Web Developers. It even includes a cheat sheet for SEO. It will be really valuable for Web Developers to keep these cheat sheets in hand.

Cheat Sheets for Front-end Web Developers

Cheat sheets are helpful to have around because they allow you to quickly remember code syntax and see related concepts visually. Additionally, they're nice decorative pieces for your office.

In this article, you'll find 23 excellent, print-ready cheat sheets for HTML/HTML, CSS, and JavaScript (including MooTools and jQuery).

So go ahead - print out your favorites and pepper your workspace with these wonderful references.

Comments (1)

lkgeeks.org - A blog aggregator for Sri Lankan FOSS/geek Blogs

Posted by Nazly on Tuesday, 8th July 2008 13:24:59 (GMT +0530)

http://www.nazly.net/imgs/lkgeeks.jpg

Early this month I put up lkgeeks.org a Blog Aggregator for Sri Lankan FOSS/geek Blogs. This site is not intended to compete with other Sri Lankan Blog Aggregators but its goal is to let everyone read Blog posts only from the Blogs of FOSS/geek Bloggers. I'm a fan of Kottu, but it consists of Blogs from various categories and its too crowded. Its difficult to filter out Blogs that sounds more technical. So that kinda influenced me to start this site.

If you fall into the category of a SriLankan FOSS/geek Blogger send me the URL of your blog to nazly dot ahmed at gmail dot com. There is a review process and once its approved it will be added to lkgeeks.org.
Comments (3)