How to install LAMP server in Linux (Ubuntu)
Tags: opensource, howto, linux
Published: 2018-01-20 20:46:00

---
title: "How to install LAMP server in Linux (Ubuntu)"
published: true 
tags: FOSS,HowTo,Linux,open source
canonical_url: https://fossnaija.com/install-lamp-server-linux-ubuntu/
---

[![LAMP_server_banner_fossnaija](https://i1.wp.com/fossnaija.com/wp-content/uploads/2017/07/LAMP-banner.jpg?resize=665%2C499&ssl=1)](https://i1.wp.com/fossnaija.com/wp-content/uploads/2017/07/LAMP-banner.jpg?ssl=1)

&nbsp;

The LAMP server stack is an acronym for a combination of open source software: _Apache_ (web sever), _My__SQL_ (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.

&nbsp;

## #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 <u>terminal</u> 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](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](https://i1.wp.com/fossnaija.com/wp-content/uploads/2017/07/apache-works.png?resize=665%2C374&ssl=1)](https://i1.wp.com/fossnaija.com/wp-content/uploads/2017/07/apache-works.png?ssl=1)

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

&nbsp;

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](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.

&nbsp;

## #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.

&nbsp;

## #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](https://localhost/phpinfo.php)

you should see a PHP information page like this one:

[![php_default_page_fossnaija](https://i0.wp.com/fossnaija.com/wp-content/uploads/2017/07/php-works.png?resize=665%2C389&ssl=1)](https://i0.wp.com/fossnaija.com/wp-content/uploads/2017/07/php-works.png?ssl=1)

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](https://i0.wp.com/fossnaija.com/wp-content/uploads/2017/07/php-code.png?resize=665%2C199&ssl=1)](https://i0.wp.com/fossnaija.com/wp-content/uploads/2017/07/php-code.png?ssl=1)

IMAGE: LAMP PHP test code.

&nbsp;

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

&nbsp;

[![LAMP_test_script](https://i1.wp.com/fossnaija.com/wp-content/uploads/2017/07/all-works.png?resize=478%2C166&ssl=1)](https://i1.wp.com/fossnaija.com/wp-content/uploads/2017/07/all-works.png?ssl=1)

IMAGE: All Works!

&nbsp;

Voilà! We are All done.



The post [How to install LAMP server in Linux (Ubuntu)](https://fossnaija.com/install-lamp-server-linux-ubuntu/) appeared first on [Foss Naija](https://fossnaija.com).