Saturday 15 December 2012

How to install Apache, MySql and Php on Linux- LAMP

In order to install Apache, Mysql and PHP on Linux you need to run certain commands in the terminal and the rest of the things will be taken care automatically. You also need to have internet connection for this.
Open the terminal(under Applications > Accessories).
Before installing anything you need to make sure you are signed in as a root user, for that execute the following command in the terminal:
        sudo su
It will ask you for the password. Once this is done you can start installing the various components.

Install Apache:

In order to install Apache, execute the following command:
       sudo apt-get install apache2 

If the "sudo apt-get install " command does nothing that means your package management tools are not up to date. In order to fix this, you need to run the following two commands:
      sudo apt-get update
      sudo apt-get dist-update
once, this is done you need to make sure whether the apache server is up and running, for this open the web browser and type "http://localhost", if you see the following screen then your installation is successful
apache-install-success

Configuring Apache: 

Once installation is compete you can start, stop and restart the Apache using the following commands:
      sudo /etc/init.d/apache2 start           #starts apache
      sudo /etc/init.d/apache2 stop           #stops apache
      sudo /etc/init.d/apache2 restart        #restarts apache

To prevent apache from auto start while booting up, use:
    sudo update-rc.d -f apache2 remove
To restore the apache to   auto-start on boot up use,
     sudo update-rc.d apache2 defaults

  
Changing the default website folder for apache:

"/var/www" is the default folder which is used by apache server for hosting the website. In order to change this folder to your own folder. For this type the following command:
     gedit /etc/apache2/sites-enabled/000-default

Now make the following two changes in the file and save it. Note that if you are not able to make the changes then you are not signed in as a root user.
change DocumentRoot /var/www  to  DocumentRoot 'new-folder-path'
change <Directory /var/www/>  to  <Directory /home/user/public_html/>


Installing PHP: 

Once the Apache installation is complete we can install PHP and configure Apache to understand the PHP scripts.
In order to install PHP run the following command:
    sudo apt-get install php5 libapache2-mod-php5
Once the PHP installation is complete, we need to restart the Apache so as the settings could be loaded. Run the following command to restart Apache:
    sudo /etc/init.d/apache2 restart

Now in order to test whether PHP support is enabled or not do the following:
edit the file /var/www/test.php
and copy the following line in it:

   <?php phpinfo(); ?>  


Now open the browser and type http://localhost/test.php. The page should look like this:




Installing MySQL: 

In order to install MySQL, use the following command:
    sudo apt-get install mysql-server mysql-client libmysqlclient-dev

The above command installs three packages:
  1. mysql server
  2. mysql client
  3. mysql client library

Once this is done you can use the following command to log in into mysql and creating databases and tables:
    mysql -u root -p
It asks you for the password.
Once this is done your LAMP server is ready to be used  :-)

No comments:

Post a Comment