How to install LAMP server in Linux (Ubuntu)

By | July 16, 2017

LAMP_server_banner_fossnaija

 

The LAMP server stack is an acronym for a combination of open source software: Apache (web sever), MySQL (database server) and PHP (programming language) on a Linux machine or computer. The LAMP server stack is a very popular and formidable setup for successful dynamic web development.

Though there are some come common all-in-one installation software (MAMP, XAMP etc). But in this post we would be installing and configuring all the packages individually. Doing it on Linux is very straight forward and easy. Without further ado, lets get going.

 

#1: Install APACHE web server.

Apache can be installed using the default package manager of a Linux distribution. Here we are using Ubuntu’s apt (fedora and fedora based systems use yum). So go to the terminal and enter the following commands:

sudo apt-get update

sudo apt-get install apache2

wait a little …your web server is installed!

After installation your can very by typing: https://server_IP_address on a web browser (the default IP address should be 127.0.0.1 which is commonly known as locallhost). You should see a default welcome page.

LAMP_apache_works_fossnaija

IMAGE: Apache Ubuntu Default Page (FossNaija.com).

 

Instead of typing this IP always (which can be cumbersome and boring), you can change that by resolving the IP to a domain name it is known for “localhost”. To do this simply open the apache configuration (/etc/apache2/apache2.conf) file in your favourite editor (Vim or gedit) like this:

sudo gedit /etc/apache2/apache2.conf

add this “ServerName localhost” (without the double quotes) to the file.

And restart apache:

sudo /etc/init.d/apache2 restart

or

sudo service apache2 reload

Now open your browser and type “localhost” in place of the server IP address:

https://localhost

You should see the server homepage as shown above. Take note of the server’s document root at “/var/www/html” – this where all your PHP scripts should be stored by default to be able to be accessed by the server.

 

#2: Install MySQL.

Now that we have our web server running, lets install the MySQL database software.

sudo apt-get install mysql-server

and let the installation begin. During the installation it would prpompt your for a password for the server root (or admin) user , enter a strong password (twice) , confirm and continue.

After installation confirm by runnig the following command toc heck the server’s status:

sudo /etc/init.d/mysql status

* /usr/bin/mysqladmin Ver 8.42 Distrib 5.5.55, for debian-linux-gnu on i686

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Server version 5.5.55-0ubuntu0.14.04.1

Protocol version 10

Connection Localhost via UNIX socket

UNIX socket /var/run/mysqld/mysqld.sock

Uptime: 15 hours 8 min 34 sec

Threads: 1 Questions: 144 Slow queries: 0 Opens: 60 Flush tables: 1 Open tables: 53 Queries per second avg: 0.002

If you get the above output or one that is similar, you’re good to go. To login into the database:

 

$ mysql -u root -p

Enter password: *******

You should get an output like this:

 

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 54

Server version: 5.5.55-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql>

The “mysql>“prompt means that you can start entering MySQL commands to manage the database.

 

#3: Install PHP.

Simply enter the command:

 

sudo apt-get install php7 libapache2-mod-php7

 

After installation, to test that PHP works create a php file (name it anything you want) with the content:

 

<?php phpinfo(); ?>

 

in the /var/www/html directory (this is where all you php scripts should be stored by default) using a text editor.

For example;

 

sudo gedit /var/www/html/phpinfo.php

 

Then save the file.

Now open the file in a browser like this:

https://localhost/phpinfo.php

you should see a PHP information page like this one:

php_default_page_fossnaija

IMAGE: PHP DEFAULT PAGE (FossNaija.com).

Congrats you have installed LAMP on your system!

 

Lastly, if you want to see if they all (PHP. MySQL, Apache) recognize and would work with each other (which it should that is the whole point). Create another php file with a name you want (mine here is testAllTogether.php) at the document root like this:

 

sudo gedit /var/www/html/testAllTogether.php

 

and add the following content:

<?php

$connection = mysql_connect(“localhost”, “root”, “YOUR_MYSQL_PASSWORD_HERE”);

if (!$connection){

die()’Could not connect to database: ‘.mysql_error());

}else{

echo “Hurray! Connection eastablished. Get to work”;

}

mysql_close($connection);

?>

php_test_code_fossnaija

IMAGE: LAMP PHP test code.

 

Now open the file in your browser (https://localhost/testAllTogether.php) and you should see a page like this if all went well.

 

LAMP_test_script

IMAGE: All Works!

 

Voilà! We are All done.

 

 

UPDATE: This article has been updated to include the latest stable version of PHP (PHP 7).

 

 

Happy Linux’NG!


NEVER MISS AN UPDATE




I agree to have my personal information transfered to MailChimp ( more information )

Join over 10,000 visitors to receive Open Source tips, trick, news, tutorials, programming and more.

We hate spam. Your email address will not be sold or shared with anyone else.

ALEXANDER OMOROKUNWA
MUST READ  You can now download UBUNTU Linux from the Windows Store

Tell us what you think