How to install LAMP

I’m using Ubuntu 12.04.03 32bit
What is LAMP?
This tutorial I’m going to install Apache, MySQL, PHP.

Linux

How to install Ubuntu

Apache

Install Apache

$ sudo apt-get update
$ sudo apt-get install apache2

Check with curl Apache is up and running (You need to install curl if you haven’t install it)

$ curl localhost

Apache is working because you get default index with your localhost

<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>

Find out your IP address

$ ifconfig

If your using ethernet your IP is under eth0 or if your using wireless your IP is under wlan0

eth0 / wlan0     inet addr:1.2.3.4

Curl with your IP address. ( 1.2.3.4 is your IP address )

$ curl http://1.2.3.4/

Same IT WORKS should come what came in localhost.

<html><body><h1>It works!</h1>
...

Enable userdirs so computer users can use public_html folders and restart Apache

$ sudo a2enmod userdir
$ sudo service apache2 restart

Create public_html folder under your home directory and in public_html create index.html

$ mkdir ~/public_html
$ cd ~/public_html/
$ nano index.html

Add simple hello world to index.html so we can be sure userdir works

<!DOCTYPE HTML>
<html>
    <body>
         <p>Hello World!</p>
    </body>
</html>

Curl your users public_html. ( user is your users name. DON’T FORGET TO ADD SLASH END OF THE LINE! )

$ curl http://1.2.3.4/~user/

With curl there should come same code what you added in index.html

<!DOCTYPE HTML>
<html>
    <body>
         <p>Hello World!</p>
    </body>
</html>

You can test page with your firefox too.

$ firefox http://1.2.3.4/~user/

Clear default page in /var/www/index.html. ( This is for your own security. )

$ sudoedit /var/www/index.html

Erase everything in there so it’s just blank page. So if you curl your IP there should come anything.

$ curl http://1.2.3.4/

Now you have installed Apache.

PHP

Install libapache2-mod-php5 and restart Apache to get PHP working.

$ sudo apt-get install libapache2-mod-php5
$ sudo service apache2 restart

Change index.html to index.php so it’s PHP file.

$ mv index.html index.php
$ nano index.php

Do simple hello world php file.

<!DOCTYPE html>
<html>
    <body>
        <h1>My first PHP page</h1>
        <?php
            echo "<p>Hello World!</p>\n";
        ?>
    </body>
</html>

To get your php working in userdirs you need to modify php5.conf

$ sudoedit /etc/apache2/mods-available/php5.conf

Comment lines so your php5.conf file looks like this.

<IfModule mod_php5.c>
    <FilesMatch "\.ph(p3?|tml)$">
        SetHandler application/x-httpd-php
    </FilesMatch>
    <FilesMatch "\.phps$">
        SetHandler application/x-httpd-php-source
    </FilesMatch>
    # To re-enable php in user directories comment the following lines
    # (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
    # prevents .htaccess files from disabling it.
    # <IfModule mod_userdir.c>
    #    <Directory /home/*/public_html>
    #        php_admin_value engine Off
    #    </Directory>
    # </IfModule>
</IfModule>

Restart Apache and curl your index.php.

$ sudo service apache2 restart
$ curl 1.2.3.4/~user/

Html code should look like this. So it works!


<!DOCTYPE html>
<html>
    <body>
        <h1>My first PHP page</h1>
        <p>Hello World!</p>
    </body>
</html>

Now PHP is working too.

MySQL

If you want generated passwords you can use pwgen and generate 20 letter password with it. ( This is not necessary but it’s helps you to get secure passwords )

$ sudo apt-get install pwgen
$ pwgen 20

Install mysql-server and php5-mysql. Restart Apache

$ sudo apt-get install mysql-server php5-mysql
$ sudo service apache2 restart

If you forget the password you can change it with the following command:

$ sudo dpkg-reconfigure mysql-server-5.5

Login to mysql as a root.

$ mysql -u root -p

Test root works. Create test database and drop it.

SHOW DATABASES;
CREATE DATABASE testdbsoivi;
SHOW DATABASES;
DROP DATABASE testdbsoivi;
SHOW DATABASES;
EXIT;

MySQL works now.

LAMP

Now let’s test whole LAMP is working together.

Login in as an root and make user person and database person.

$ mysql -u root -p
CREATE DATABASE person;
GRANT ALL ON person.* TO [email protected] IDENTIFIED BY 'SECRETPASSWORD';
EXIT;

Login as person user

$ mysql -u person -p

Use database person and create table person. In person table add two names: John Doe and Jane FooBar.

USE person;
CREATE TABLE person (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName VARCHAR(100), LastName VARCHAR(100));
INSERT INTO person (FirstName, LastName) VALUES ('John', 'Doe');
INSERT INTO person (FirstName, LastName) VALUES ('Jane', 'FooBar');
SELECT * FROM person;
EXIT;

Modify old index.php what we created earlier.

nano ~/public_html/index.php

Create simple php program that get’s John Doe and Jane FooBar from MySQL and shows them in browser.

<!DOCTYPE html>
<html>
    <body>
        <h1>My LAMP test</h1>
        <?php
            $con=mysqli_connect("localhost","person","SECRETPASSWORD","person");
            // Check connection
            if (mysqli_connect_errno())
                {
                    echo "Failed to connect to MySQL: " . mysqli_connect_error();
                }
            $result = mysqli_query($con,"SELECT * FROM person");
            echo "<table border='1'>
                <tr>
                    <th>Firstname</th>
                    <th>Lastname</th>
                </tr>";
            while($row = mysqli_fetch_array($result))
                {
                    echo "<tr>";
                    echo "<td>" . $row['FirstName'] . "</td>";
                    echo "<td>" . $row['LastName'] . "</td>";
                    echo "</tr>";
                }
            echo "</table>\n";
            mysqli_close($con);
        ?>
    </body>
</html>

Test in your Firefox.

$ firefox 1.2.3.4/~user/

Page should look something like this.
LampTest

Now you have installed whole LAMP ( Linux, Apache, MySQL, PHP ). You have also tested all those is working together.

Cleaning test data

Of course we don’t want to leave any test data to computer so we erase them.

$ rm ~/public_html/index.php
$ mysql -u root -p

Drop person database and person user.

SHOW DATABASES;
DROP DATABASE person;
SHOW DATABASES;
SELECT User FROM mysql.user;
DROP USER [email protected];
SELECT User FROM mysql.user;
EXIT;

NEXT TUTORIAL
How to create virtual website

SOURCE:
Samuel Kontiomaa – Installing wordpress under apache 2