LAMP(Linux, Apache, MySQL, PHP) Installation

  1. Linux (Ubuntu 11.10)
  2. Apache 2.2 or later
  3. PHP 5.3.6 or later
  4. MYSQL 5.1 or later
  5. phpMyAdmin

Installing Apache

Open your Terminal (Applications > Accessories > Terminal) to execute the commands.

  • sudo apt-get install apache2
    [sudo] password for administrator: ENTER YOUR PASSWORD
    After this operation, 10.3 MB of additional disk space will be used.
    Do you want to continue [Y/n]? Y
  • open your web browser and then enter the following into the web address: http://localhost
    You will see a message saying "It works!".
By default, the apache DocumentRoot is  “/var/www” directory. The files such as PHP or HTML will be placed in this directory and will be visible from the URL http://localhost. We want the “localhost” to point to another folder instead, /home/user/Public.

Apache2 has the concept of sites, which are separate configuration files that Apache2 will read. These are available in /etc/apache2/sites-available. You can create many different site configurations available, and activate only those that you need.

We want the default site to be the /home/YOURUSERNAME/Public/ directory.
  • create a new site (for example: websol):
    sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/websol
  • edit the new configuration file in a text editor:
    gksudo gedit /etc/apache2/sites-available/websol
    find the following lines:
    DocumentRoot /var/www change to
    DocumentRoot /home/YOURUSERNAME/Public
    <Directory /var/www/> change to
    <Directory /home/YOURUSERNAME/Public/>
  • change all occurrence of "AllowOverRide None" to "AllowOverRide All" (to allow .htaccess file)
  • save the file
  • deactivate the old site
    sudo a2dissite default
  • activate the new site
    sudo a2ensite websol
  • enable the mod_rewrite module:
    sudo a2enmod rewrite
  • restart your apache
    sudo /etc/init.d/apache2 restart
  • test the new site, type console:
    echo '<b>Hello! It is working!</b>' > /home/YOURUSERNAME/Public/index.html
  • open your web browser and then enter the following into the web address: http://localhost
    You will see a message saying "Hello! It is working!"
      

Installing PHP

  • install php5 and apache configuration
    sudo apt-get install php5 libapache2-mod-php5
  • restart your apache
    sudo /etc/init.d/apache2 restart
  • create a test file (into Public directory):
    sudo gedit testphp.php
    type the following php code:
    <?php phpInfo(); ?>
    save the file
  • open your browser and type:
    localhost/testphp.php
  • to change your php.ini file, open by one of the following text editors:
  • sudo nano /etc/php5/apache2/php.ini  (vi text editor)
    sudo gedit /etc/php5/apache2/php.ini (geditor)

    Find the following line (error_reporting) and uncomment:
    Development Value: E_ALL | E_STRICT
  • install gd PHP5 (to create and manipulate image files in a variety of different image formats)
    sudo apt-get install php5-gd
  • restart your apache
    sudo /etc/init.d/apache2 restart

Installing MySQL

  • install MySQL
    sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql
    remember the root password that you entered at the time of installation
  • restart your apache
    sudo /etc/init.d/apache2 restart
  • test your mysql installation. Open your terminal and type the following lines:
    mysql -u root -p
    Enter password: YOUR MYSQL PASSWORD

    mysql> show databases;
    mysql> create database test;
    mysql>use test;
    mysql> CREATE TABLE myTable (id int primary key auto_increment,myname text);
    mysql> INSERT INTO myTable VALUES(NULL,'fy');
    mysql> SELECT * FROM myTable;
    +----+------------+
    | id | myname |
    +----+-------------+
    | 1  | fy              |
    +----+-------------+
    mysql> quit
  • check whether your MySQL server is running:
    sudo netstat -tap | grep mysql
    tcp        0      0 localhost:mysql         *:*                     LISTEN      939/mysqld
     
  • If the server is not running correctly, you can type the following command to start it:
    sudo /etc/init.d/mysql restart
  • create test page (testmysql.php):

    <?php
         # Define MySQL Settings
         define("HOST", "localhost");
         define("USER", "root");
         define("PASS", "YOUR MYSQL PASSWORD");
         define("DB", "test");

        $conn = mysql_connect("".HOST."", "".USER."", "".PASS."") or die(mysql_error("MYSQL connection ERROR!"));
        mysql_select_db("".DB."",$conn) or die(mysql_error("DB connection ERROR"));

        $sql = "SELECT * FROM myTable";
        $res = mysql_query($sql) or die("DB Table connection ERROR");

        while ($field = mysql_fetch_array($res))
       {
           $id = $field['id'];
           $name = $field['myname'];
           echo 'ID: ' . $field['id'] . '<br />';
          echo 'Name: ' . $field['myname'] . '<br /><br />';
      }
    ?>

  • type your browser:
    localhost/testmysql.php

The following commands allow you to start, restart, stop Mysql.
  • sudo service mysql restart
  • sudo service mysql stop
  • sudo service mysql start

Installing phpMyAdmin

  • install phpMyAdmin from console:
    sudo apt-get install phpmyadmin
    configuring phpmyadmin:
      select web server: apache2
      configure database for phpmyadmin with dbconfig-common: NO
  • sudo cp /etc/phpmyadmin/apache.conf /etc/apache2/conf.d
  • sudo /etc/init.d/apache2 restart
  • open your browser and type:
    localhost/phpmyadmin
    username:root
    password: YOUR MYSQL PASSWORD

How to check your LAMP version?

  • apache
    ~$ apache2 -v
  • php
    ~$ php -v
  • mysql
    ~$ mysql -V
  • ubuntu version
    ~$ lsb_release -a

[ERROR] php5 startup error/warning

php -v
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib/php5/20090626+lfs/sqlite.so' - /usr/lib/php5/20090626+lfs/sqlite.so: cannot open shared object file: No such file or directory in Unknown on line 0
PHP 5.3.6-13ubuntu3.2 with Suhosin-Patch (cli) (built: Oct 13 2011 23:19:13)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
    with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans

[SOLVED]
sudo dpkg -P php5-gd php5-mysql php5-sqlite

Tasksel

The  alternative LAMP to this guide is "tasksel install lamp-serve" which is is a Debian/Ubuntu tool that installs multiple related packages as a co-ordinated "task" onto your system.

Links