DEV Community

Alexander Omorokunwa
Alexander Omorokunwa

Posted on • Originally published at fossnaija.com on

How to install LAMP server in Linux (Ubuntu)

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 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 lo __cal_ lhost_). 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 out put or one that is similar, you’re good to go. To login into the database:

$ mysql -u root -p

Enter password:

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 pprompt means that you can start entering mysql command to mannge database.

 

#3: Install PHP.

Simply enter the command:

sudo apt-get install php5 libapache2-mod-php5

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

and save the file.

Now open the file in a browser:

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 to see if the all 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.

The post How to install LAMP server in Linux (Ubuntu) appeared first on Foss Naija.

Top comments (0)