Installing and configuring LAMP (Linux, Apache, MySQL, and PHP) is easy in Ubuntu Linux. If you have internet connection in your system, you can able to download the necessary applications using apt-get or synaptic package manager tools.

In this tutorial, I have given step-by-step procedure to setup LAMP web server environment in Ubuntu 9.10 operating system.

Installing Apache:

The first application we will install is the web server Apache. To get started open your console and type:

sudo apt-get install apache2

Now enter the system password to download and install automatically. To test it, point your web browser to http://localhost/. If installation is successful, a text “It Works!” will display.

The location for saving webpages and script files are as follows.

Web Pages: /var/www
Script Files: /usr/lib/cgi-bin
Apache Configuration: /etc/apache2/sites-available

You can access the webpages using default address 127.0.0.1, system name, or localhost. If you wish to setup your own domain name, follow the steps bellow. (In this tutorial, i have chosen www.ashok.com as my domain name.)

sudo gedit /etc/hosts

Add “127.0.0.1         www.ashok.com” in the hosts file and save it. Now the problem is almost solved. To avoid errors in configurations, do the bellow steps too.

The first step is to make a copy of the original configuration file.

sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/ashok.com

The second step is to change the configuration file to your own domain name.

sudo gedit /etc/apache2/sites-available/ashok.com

Add few lines in the configuration file (shown in blue color) as given bellow.

<VirtualHost *:80>

ServerAdmin webmaster@ashok.com
DocumentRoot /var/www
ServerName ashok.com
ServerAlias www.ashok.com

<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
:
:
.

The third step is to active new domain ashok.com and deactivate old domain.

sudo a2ensite ashok.com && a2dissite default

Finally restart the Apache web server.

/etc/init.d/apache2 reload

Your own domain name has been configured successfully.

Installing PHP:

The second application we will install is the PHP. To get started open your console and type:

sudo apt-get install php5 libapache2-mod-php5

Once this finishes we need to restart Apache. This will apply the changes.

sudo /etc/init.d/apache2 restart

Now let’s test it by replacing ‘/var/www/index.html‘ with index.php using command:

sudo cp /var/www/index.html /var/www/index.php

Replace the line with <?php phpinfo(); ?> in index.php file through an editor, using command ‘sudo /var/www/index.php‘. If PHP is installed properly, the page will display the information about installed modules of PHP.

Installing MySQL with PHP5:

Install all the MySQL stuff:

sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

When you get a prompt enter a password you would like to use as the root MySQL user. Once done open, ‘sudo gedit /etc/mysql/my.cnf‘ and change the bind-address to your local IP. (Usually 192.x.x.x number). Note that this can be a security problem, because your database can be accessed by others computers than your own. Skip this step if the applications which require MySQL are running on the same machine.

Change the line bind-address = localhost to your own internal ip address ( e.g. 192.168.1.1 as bind-address = 192.168.1.1). If no need, skip this statement.

If your ip address is dynamic you can also comment out the bind-address line and it will default to your current ip.


Extra Stuff:

Installing phpMyAdmin:

All MySQL tasks including setting the root password and creating databases can be done via a graphical interface using phpMyAdmin or MySQL-Admin.

sudo apt-get install phpmyadmin

After phpMyAdmin is installed you can see it view it, http://localhost/phpmyadmin/.

User Name: phpmyadmin
Password: your_password

Now, you have to grant permission to create or modify database. To do this you must be a super user (su). By default Ubuntu offers only sudo mode. To be safe, create new root password:

sudo passwd root

Keep the su password safely. It is needed to control every operations in Ubuntu. Remember that sudo and su password are different. !!!

Open your console and type:

su

/etc/init.d/mysql start

mysql -u root -p

mysql>GRANT ALL PRIVILEGES ON *.* TO ‘phpmyadmin@localhost’ IDENTIFIED BY ‘your_password’ WITH GRANT OPTION;

You can now able to create or modify tables through graphical interface of phpMyAdmin.

mod_rewrite:

Chances are you want to use mod_rewrite in your applications to make URLs a bit more pretty. It is also required by lots of webserver applications, like WordPress, Drupal, etc.

Do the following:

sudo a2enmod rewrite

/etc/init.d/apache2 restart

Method to Start / Stop Apache:

Use the following command to run Apache:

sudo /usr/sbin/apache2ctl start

To stop it, use:

sudo /usr/sbin/apache2ctl stop

To test configuration changes, use:

sudo /usr/sbin/apache2ctl configtest

Finally, to restart it, run:

sudo /usr/sbin/apache2ctl restart