How To Install Linux, stack on Ubuntu 16.04

stack on Ubuntu Introduction

stack on Ubuntu A “LAMP” stack is a collection of open supply software program this is typically established together to allow a server to host dynamic websites and web apps. This time period is actually an acronym which represents the Linux working machine, with the Apache net server. The site statistics is saved in a MySQL database, and dynamic content is processed via PHP.

In this guide, we’ll get a LAMP stack installed on an Ubuntu sixteen.04 Droplet. Ubuntu will fulfill our first requirement: a Linux working device.

stack on Ubuntu Prerequisites

Before you begin with this guide, you need to have a separate, non-root consumer account with sudo privileges installation to your server. You can learn how to try this by means of completing steps 1-four in the preliminary server setup for Ubuntu 16.04.

stack on Ubuntu Step 1: Install Apache and Allow in Firewall

The Apache net server is many of the most popular internet servers inside the world. It’s properly-documented, and has been in extensive use for plenty of the records of the net, which makes it a superb default desire for web hosting a website.

We can set up Apache without problems the usage of Ubuntu’s package manager, apt. A bundle manager permits us to install maximum software ache-loose from a repository maintained via Ubuntu. You can learn more about the way to use apt here.

For our purposes, we can get began by using typing those instructions:

sudo apt-get update
sudo apt-get install apache2

Since we’re the usage of a sudo command, these operations get executed with root privileges. It will ask you for your normal user’s password to affirm your intentions.

Once you’ve entered your password, apt will inform you which ones programs it plans to put in and what kind of more disk space they may take up. Press Y and hit Enter to keep, and the set up will continue.

stack on Ubuntu Set Global ServerName to Suppress Syntax Warnings

Next, we can upload a unmarried line to the /and so on/apache2/apache2.Conf record to suppress a warning message. While harmless, if you do now not set ServerName globally, you’ll receive the subsequent caution when checking your Apache configuration for syntax mistakes:

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

Open up the main configuration file with your text edit:

sudo nano /etc/apache2/apache2.conf

Inside, at the lowest of the record, upload a ServerName directive, pointing in your primary area call. If you do no longer have a site name related to your server, you may use your server’s public IP deal with:

If you don't know your server's IP address, skip down to the section on how to find your server's public IP address to find it.


ServerName server_domain_or_IP

Save and close the file when you are finished.

Next, check for syntax errors by typing:

sudo apache2ctl configtest

Since we added the global ServerName directive, all you should see is:

OutputSyntax OK

Restart Apache to implement your changes:

sudo systemctl restart apache2

You can now begin adjusting the firewall.

Adjust the Firewall to Allow Web Traffic

Next, assuming which you have observed the preliminary server setup commands to permit the UFW firewall, make sure that your firewall permits HTTP and HTTPS site visitors. You can make sure that UFW has an utility profile for Apache like so:

sudo ufw app list
OutputAvailable applications:
  Apache Full
  Apache Secure

If you look at the Apache Full profile, it should show that it enables traffic to ports 80 and 443:

sudo ufw app info "Apache Full"  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

Allow incoming traffic for this profile:

sudo ufw allow in "Apache Full"

You can do a gap check proper away to verify that the whole thing went as deliberate by means of visiting your server’s public IP address in your web browser (see the be aware underneath the subsequent heading to find out what your public IP address is if you do not have this statistics already.


You will see the default Ubuntu sixteen.04 Apache web page, that is there for informational and checking out functions. It must appearance some thing like this:

If you see this page, then your web server is now correctly installed and accessible through your firewall.

stack on Ubuntu How To Find your Server’s Public IP Address

If you do no longer know what your server’s public IP address is, there are a number of methods you can locate it. Usually, this is the cope with you operate to connect with your server thru SSH.

From the command line, you can find this some ways. First, you can use the iproute2 equipment to get your deal with through typing this:

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

This will provide you with two or 3 traces again. They are all accurate addresses, but your laptop can also most effective be able to use one in all them, so experience loose to strive every one.

An opportunity technique is to apply the curl utility to contact an outside birthday party to inform you the way it sees your server. You can do this by using asking a selected server what your IP deal with is:

sudo apt-get install curl

Regardless of the method you use to get your IP address, you can type it into your web browser’s address bar to get to your server.

stack on Ubuntu Step 2: Install MySQL

Now that we have our web server up and jogging, it is time to install MySQL. MySQL is a database control gadget. Basically, it will arrange and offer get right of entry to to databases wherein our site can keep statistics.

Again, we are able to use apt to accumulate and set up our software. This time, we will additionally install some other “helper” applications in order to help us in getting our components to speak with each different:

sudo apt-get install mysql-server

Note: In this example, you do not should run sudo apt-get replace prior to the command. This is because we these days ran it in the commands above to put in Apache. The bundle index on our computer need to already be updated.

Again, you’ll be proven a list of the programs so that it will be hooked up, at the side of the amount of disk space they may soak up. Enter Y to hold.

During the set up, your server will ask you to choose and confirm a password for the MySQL “root” user. This is an administrative account in MySQL that has multiplied privileges. Think of it as being just like the root account for the server itself (the only you’re configuring now could be a MySQL-particular account, but). Make certain that is a robust, specific password, and do not depart it blank.

When the set up is entire, we want to run a easy safety script in an effort to take away some risky defaults and lock down access to our database machine a bit bit. Start the interactive script by using going for walks:


You will be asked to enter the password you set for the MySQL root account. Next, you will be asked if you want to configure the VALIDATE PASSWORD PLUGIN.

Warning: Enabling this feature is some thing of a judgment name. If enabled, passwords which don’t healthy the specified standards might be rejected through MySQL with an error. This will reason troubles if you use a weak password in conjunction with software program which robotically configures MySQL user credentials, which includes the Ubuntu programs for phpMyAdmin. It is secure to go away validation disabled, however you should constantly use strong, particular passwords for database credentials

Answer y for yes, or anything else to continue without enabling.

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:

You’ll be asked to select a degree of password validation. Keep in thoughts that if you input 2, for the strongest degree, you may obtain errors when attempting to set any password which does not include numbers, top and lowercase letters, and unique characters, or that is based on commonplace dictionary words.

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

If you enabled password validation, you’ll be proven a password energy for the prevailing root password, and requested you in case you want to alternate that password. If you are satisfied with your modern password, input n for “no” at the spark off:

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

For the rest of the questions, you should press Y and hit the Enter key at every set off. This will do away with some anonymous users and the take a look at database, disable remote root logins, and load these new regulations in order that MySQL at once respects the changes we have made.

At this point, your database system is now installation and we will pass on.

stack on UbuntuStep 3: Install PHP

PHP is the aspect of our setup so that it will manner code to show dynamic content. It can run scripts, connect to our MySQL databases to get facts, and hand the processed content over to our internet server to display.

We can another time leverage the apt machine to put in our additives. We’re going to include some helper packages as well, in order that PHP code can run under the Apache server and speak to our MySQL database:

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

This ought to set up PHP with none problems. We’ll take a look at this in a moment.

In maximum cases, we will need to regulate the manner that Apache serves files when a directory is requested. Currently, if a user requests a listing from the server, Apache will first search for a record known as index.Html. We want to inform our web server to decide upon PHP files, so we will make Apache search for an index.Personal home page document first.

To try this, type this command to open the dir.Conf report in a text editor with root privileges:

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

It will look like this:/etc/apache2/mods-enabled/dir.conf

<IfModule mod_dir.c>
    DirectoryIndex index.html index.cgi index.php index.xhtml index.htm

We want to move the PHP index file highlighted above to the first position after the DirectoryIndex specification, like this:/etc/apache2/mods-enabled/dir.conf

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.xhtml index.htm

When you are finished, save and close the file by pressing Ctrl-X. You’ll have to confirm the save by typing Y and then hit Enter to confirm the file save location.

After this, we need to restart the Apache web server in order for our changes to be recognized. You can do this by typing this:

sudo systemctl restart apache2

We can also check on the status of the apache2 service using systemctl:

sudo systemctl status apache2
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
   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 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 enhance the functionality of PHP, we can optionally install some additional modules.

To see the available options for PHP modules and libraries, you can pipe the results of apt-cache search into less, a pager which lets you scroll through the output of other commands:

apt-cache search php- | less

Use the arrow keys to scroll up and down, and q to quit.

The results are all optional components that you can install. It will give you a short description for each:

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]

To get more information about what each module does, you can either search the internet, or you can look at the long description of the package by typing:

apt-cache show package_name

There will be a lot of output, with one field called Description-en which will have a longer explanation of the functionality that the module provides.

For example, to find out what the php-cli module does, we could type this:

apt-cache show php-cli

Along with a large amount of other information, you’ll find something that looks like this:

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, after researching, you decide you would like to install a package, you can do so by using the apt-get install command like we have been doing for our other software.

If we decided that php-cli is something that we need, we could type:

sudo apt-get install php-cli

If you want to install more than one module, you can do that by listing each one, separated by a space, following the apt-get install command, like this:

sudo apt-get install package1 package2 ...

At this point, your LAMP stack is installed and configured. We should still test out our PHP though.

Step 4: Test PHP Processing on your Web Server

In order to test that our system is configured properly for PHP, we can create a very basic PHP script.

We will call this script info.php. In order for Apache to find the file and serve it correctly, it must be saved to a very specific directory, which is called the “web root”.

In Ubuntu 16.04, this directory is located at /var/www/html/. We can create the file at that location by typing:

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

This will open a blank file. We want to put the following text, which is valid PHP code, inside the file:info.php


When you are finished, save and close the file.

Now we can test whether our web server can correctly display content generated by a PHP script. To try this out, we just have to visit this page in our web browser. You’ll need your server’s public IP address again.

The address you want to visit will be:


The page that you come to should look something like this:

This page essentially offers you data about your server from the angle of PHP. It is beneficial for debugging and to make certain that your settings are being applied efficaciously.

If this become a hit, then your PHP is running as expected.

You likely want to take away this document after this take a look at due to the fact it may truely supply facts approximately your server to unauthorized users. To do that, you may type this:

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

You can always recreate this page if you need to access the information again later.