Official Power Up Hosting Blog

Everything about Linux, Windows, and hosting ;)

Selvakumar
Author

I am an Online Marketer and technology lover. I like to learn new things and share that with people.

Share


Our Newsletter


Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Tags


Twitter


Official Power Up Hosting Blog

How to Install LAMP stack on Ubuntu 16.04 (Easy Guide)

SelvakumarSelvakumar

Introduction

LAMP stands for Linux, Apache, MySQL and PHP.

If you want to host a website, you have to install the LAMP stack on your server.

This is the basic requirement for a server to serve the client request for a website.

Web Server:

If you need a server to serve web content, you have to install a web server software.

Apache is one of the famous web server software used by almost half of the websites.

MySQL

MySQL is used to store and retrieve data from the database. The website will be connected to the Database using the MySQL.

A dynamic website requires MySQL or Maria DB.

PHP

PHP is the Open Source dynamic language used by many websites.

All these three has to be installed and configured properly in a Linux Operating System.

Here in this article we are going to see install lamp stack ubuntu.

Prerequisites

LAMP Installation

Follow the Ubuntu 16.04 install lamp commands as mentioned below.

Installing Apache

To install the Apache, update your repository package index.

We have Apache in the Ubuntu default repository package.

$ sudo apt-get update

After that install the Apache2 using the following command.

$ sudo apt-get install apache2

Now:

The apt will show you some of the details regarding the disk space requirement and the packages which will be installed.

Press Y to accept and continue the installation process.

Apache Configuration

Next:

You have to make some changes in the Apache configuration file located at /etc/apache2/apache2.conf.

If you test the Apache configuration, it will show you a warning message.

To test the Apache configuration, execute the below command.

$ sudo apache2ctl configtest

You will get the following message.

Output:

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK

You have to set the Server Name globally to access it from anywhere around the world through the internet.

To do that, open the Apache configuration file.

$ sudo nano /etc/apache2/apache2.conf

In that, look for a line which contains ServerName.

Add your website domain name or server's Public IP address.

           /etc/apache2/apache2.conf
. . .
ServerName server_domain_or_IP

Then, save and close the file.

After that, check the syntax error using configtest.

$ sudo apache2ctl configtest

Since you have made the changes, you will get the following message.

Syntax OK

Now:

Restart the Apache web server to make the changes work.

$ sudo systemctl restart apache2

After that, you have to adjust the firewall to allow the Apache to listen on the specific port for the incoming traffic.

Adjusting the Firewall

Your firewall has to be configured to work with the web server.

Make sure that, your firewall allows both HTTP and HTTPS traffic.

Also, check whether there is profile for Apache web server in UFW.

Use the below command to check the Apache web server configuration to check the available applications.

$ sudo ufw app list

Output
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH

To view the full profile of Apache Full, execute the below command.

$ sudo ufw app info "Apache Full"

You will get the following output.

Output
Profile: Apache Full
Title: Web Server (HTTP,HTTPS)
Description: Apache v2 is the next generation of the omnipresent Apache web
server.

Ports:
80,443/tcp

After that, use the below command to allow the incoming traffic to the web server.

$ sudo ufw allow in "Apache Full"

After all the steps, you can see the Apache static page when you access your domain name or server's public IP address through your web browser.

http://domain_name_or_server_IP_address

If you don't know how to find server's IP address, here are the two methods for you.

First Method

First, use the below command on the terminal to find out the server's IP address.

$ ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

The above command will return two to three IP addresses.

Not all the IP addresses will work. Only one IP address will work.

You can find the working one by checking them through the browser.

Second Method

The next method is using curl to communicate other website and find the IP address.

Use the below command to do that. First, install the curl.

$sudo apt-get install curl

$ curl http://icanhazip.com

Check the IP through the web server and you will see the Apache web server static page.

Install MySQL

After installing the Apache web server, you have to install the MySQL.

MySQL gives the access to the database. The dynamic website will communicate the database through the MySQL application.

$ sudo apt-get install mysql-server

Now:

The apt will show you some details regarding the disc space and the packages.

Enter Y to accept and install.

After that, MySQL will be installed.

You have to go through few steps. MySQL setup will ask you to set a root password.

Make sure to set the secure password and remember.

You will need that password later to log in to the MySQL from the terminal.

Once you install the MySQL, the next step is to secure the MySQL.

You can secure the MySQL by going through some steps. Execute the below command.

$ mysql_secure_installation

You will be asked series of question which you have to answer to increase the MySQL.

The first thing will be choosing the Validate Password Plugin.

You have to think twice while choosing this Plugin.

Because it will reject the weaker passwords and will show errors.

But:

This option ensures that you setup a strong password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No:

If you choose yes, you will be asked to enter the password strength.

The password which does not have the expected strength will be rejected.

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special 
characters
STRONG Length >= 8, numeric, mixed case, special 
characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1

Also, you will be asked whether you want to change the root password.

$ Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n

The next setup will be going through removing the test users and demo databases.

You have to remove both users and database. Then you have to go for disabling the remote root login for MySQL.

If you are planning to setup your database on another server, then skip this option.

When you allow remote root login for MySQL, your root password should be stronger.

Keeping MySQL Database in another server is best for security as well as load balancing.

Once you are done with the above setup, now your database is ready to use.

The next step is to install the PHP.

Install PHP

PHP is used to process the Dynamic content to display on the website.

Here:

The Web server will hand over the request to the PHP, then PHP will process the web content and return it to the web server.

After that, the web server will send the content to the end user.

Installing PHP is also simple and you can install it from Ubuntu repository using the apt.

Here is the command for that. We will also install some additional required modules. Since php5 has no installation candidate, install php 7 ubuntu.

$ sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql

The Apache will be installed on the web server without any trouble.

When a file is requested by the user, the Apache will look for index.php file.

But:

We have to tell Apache to look for index.php file.

To set that, first look for dir.conf file using nano editor.

$ sudo nano /etc/apache2/mods-enabled/dir.conf

In that file, look for the following lines.

         /etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl index.php 
index.xhtml index.htm
</IfModule>

Interchange the position of index.php as mentioned below.

   /etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

When you finished this, just save and close the file.

In order to make the changes work, we have to restart the apache.

$ sudo systemctl restart apache2

Now:

Check the apache status using systemctl.

$ sudo systemctl status apache2

You will get the below output.

Sample Output
● apache2.service - LSB: Apache2 web server
Loaded: loaded (/etc/init.d/apache2; bad; vendor 
preset: enabled)
Drop-In: /lib/systemd/system/apache2.service.d
       └─apache2-systemd.conf
Active: active (running) since Wed 2016-04-13 14:28:43 
EDT; 45s ago
 Docs: man:systemd-sysv-generator(8)
Process: 13581 ExecStop=/etc/init.d/apache2 stop 
(code=exited, status=0/SUCCESS)
 Process: 13605 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS)
Tasks: 6 (limit: 512)
CGroup: /system.slice/apache2.service
       ├─13623 /usr/sbin/apache2 -k start
       ├─13626 /usr/sbin/apache2 -k start
       ├─13627 /usr/sbin/apache2 -k start
       ├─13628 /usr/sbin/apache2 -k start
       ├─13629 /usr/sbin/apache2 -k start
       └─13630 /usr/sbin/apache2 -k start

Apr 13 14:28:42 ubuntu-16-lamp systemd[1]: Stopped LSB: Apache2 web server.
Apr 13 14:28:42 ubuntu-16-lamp systemd[1]: Starting LSB: Apache2 web server...
Apr 13 14:28:42 ubuntu-16-lamp apache2[13605]:  * Starting Apache httpd web server apache2
Apr 13 14:28:42 ubuntu-16-lamp apache2[13605]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerNam
Apr 13 14:28:43 ubuntu-16-lamp apache2[13605]:  *
Apr 13 14:28:43 ubuntu-16-lamp systemd[1]: Started LSB: Apache2 web server.

Install PHP Modules

To install additional PHP modules, you can check for the available modules.

Sometimes, you will require installing additional PHP modules to work with some websites and plugins.

To view the available modules to install, use the below command.

$ apt-cache search php- | less

To exit the list you can use q.

The output will look like this.

libnet-libidn-perl - Perl bindings for GNU Libidn
php-all-dev - package depending on all supported PHP development packages
php-cgi - server-side, HTML-embedded scripting language (CGI binary) (default)
php-cli - command-line interpreter for the PHP scripting language (default)
php-common - Common files for PHP packages
php-curl - CURL module for PHP [default]
php-dev - Files for PHP module development (default)
php-gd - GD module for PHP [default]
php-gmp - GMP module for PHP [default]
…
:

If you choose a module and want to know more information about it, just use the below command structure.

$ apt-cache show package_name

For example, I want to know the detail about php-cli, use the below command.

$ apt-cache show php-cli

You will get the following output and it will contain the required information about the PHP module.

Output
 …
Description-en: command-line interpreter for the PHP scripting language (default)
This package provides the /usr/bin/php command interpreter, useful for
testing PHP scripts from a shell or performing general 
shell scripting tasks.
.
 PHP (recursive acronym for PHP: Hypertext 
Preprocessor) is a widely-used
open source general-purpose scripting language that is 
especially suited
for web development and can be embedded into HTML.
.
  This package is a dependency package, which depends on Debian's default
 PHP version (currently 7.0).

If you decided to install any module, just follow the below format.

$ apt-cache show php-cli

If you want to install two or more PHP modules at the same time, you can mention them in the command using ','.

Here is the example for that.

$ sudo apt-get install package1 package2 ...

Once you are done with all the process, you have to Test whether the PHP is working properly or not.

For that, we will create a small executable PHP file and then we will see whether it is going to work or not.

First, Open a file called info.php using nano editor.

$ sudo nano /var/www/html/info.php

Then paste the following code in that file.

info.php
<?php
phpinfo();
?>

Once you finished, just save and close the file.

Here:

We are going to test whether the content can be generated by PHP and displayed by the web server.

Go to the browser and enter the domain name or IP address in the following format.

http://your_server_IP_address/info.php

The Page will look like the following one.

The page will contain the information about the server.

If the page is working fine, then it means that PHP also working fine.

Since the page contains information about the server, you have to remove it after testing the PHP.

To remove the file, use the below command.

$ sudo rm /var/www/html/info.php

Now, the file will be removed.

Conclusion

The LAMP stack installation is required for all the dynamic websites.

If you want to install Nginx instead of Apache, you can follow this guide to the installation of LEMP stack.

To secure the server, you can use the Let's encrypt to secure it. Let's encrypt provides free SSL certificate.

Here are some more guides for you to refer.

Selvakumar
Author

Selvakumar

I am an Online Marketer and technology lover. I like to learn new things and share that with people.

Comments