Tutorials > How to install and configure a LAMP Web Server on Ubuntu 18.04

How to install and configure a LAMP Web Server on Ubuntu 18.04

Published on: 18 January 2020

Apache LAMP MySQL PHP Ubuntu Web Server

LAMP is one of the most widely used software applications for developing web applications worldwide. The acronym derives from the initials of its four software components: Linux, Apache, MySQL, PHP. Each component is designed for excellent operation in conjunction with the others

This guide will explain how to install and configure all the necessary components to install LAMP with Ubuntu 18.04.

To install LAMP, first connect to your server via an SSH connection. If you haven’t done so yet, we recommend following our guide to securely connect with the SSH protocol. In case of a local server, go to the next step and open the terminal of your server.

Apache installation

Apache is the most widespread web server, capable of operating on a wide variety of operating systems, including UNIX / Linux, Microsoft Windows and OpenVMS.

First, update the distribution repositories, to download the latest Apache version, and start the installation.

$ sudo apt update && sudo apt install apache2

Note that by using the sudo command to run the commands as root you will be required to insert the user’s password previously used.

After giving your consent to install Apache, the installation will be performed.

Firewall configuration

In case of a firewall on the system, first set it up to enable HTTP traffic and HTTPS traffic on your machine.

If you use the UFW firewall, pre-installed profiles for Apache are provided. So, let's see how to enable them.

To check the available profiles installed in the UFW firewall, run this command:

$ sudo ufw app list

A list similar to the following will be displayed on the screen:

Available applications:
Apache
Apache Full
Apache Secure
OpenSSH

To allow HTTP (Port 80) and HTTPS (Port 443) traffic, use the "Apache Full" profile. 

Profile information can be checked as follows:

$ sudo ufw app info "Apache Full"

On-screen profile description will be displayed:

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 checking the profile, you are ready to enable it:

$ sudo ufw allow in "Apache Full"

At this point the configuration of Apache and the firewall is completed. To test the web server visit the address http://<SERVER.IP> or in the case of a local server http://localhost .

If the procedure was carried out correctly, the Apache welcome page will be displayed.

Apache Web Page

MySQL installation

MySQL is one of the most widely used relational database management systems in the world.

Update the distribution repositories to download the latest version of MySQL. If you have already launched this command before, go to the next step.

$ sudo apt update

Now, install MySQL:

$ sudo apt install mysql-server

To verify its successful installation , check the MySQL version:

$ sudo mysqld --version

If the procedure was performed correctly, the installed MySQL version will be printed on the screen.

Now secure MySQL by running a script included with MySQL, to increase security and limit access to your databases:

$ sudo mysql_secure_installation

At this point, a guided procedure will start for the configuration of the MySQL security level.

First, you will be asked whether you want to enable the password validation system. If enabled, when setting a user’s password the password is evaluated. If the password does not meet the minimum security requirements, it is rejected with an error message.

Later, you will be asked whether you want to change the root password with one of your choice (if you enable the password validation system, a password that meets the security criteria has to be entered).

Following several best practices for creating a secure password is recommended. This includes:

  • the use of both upper and lower case letters

  • the use of both letters and numbers

  • the use of non-alphanumeric characters like @ # $% ˆ &!

  • the use of passwords that were never previously used

Finally, choose whether to remove anonymous users and test databases, and whether to disable remote login with root user (for an adequate level of security. Confirming all these changes is recommended).

At this point, confirm the updates of the displayed table to apply all the new security criteria.

To verify the functioning of MySQL, try logging in with the following command:

$ sudo mysql

If everything was done correctly, you will be in the MySQL console.

Then exit the MySQL console:

mysql > exit;

The installation and configuration of MySQL are now completed.

PHP installation

PHP is the interpreter of the homonymous scripting language for programming dynamic web pages.

Update the distribution repositories, to download the latest PHP version. If you have already launched this command before, go to the next step.

$ sudo apt update

Install the PHP interpreter and some modules to execute PHP with Apache and make it communicate with MySQL:

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

At this point, the installation will have been completed. All you need to do is verify it by checking the installed PHP version:

$ sudo php -v

e In case of successful installation , the version of PHP just installed will be displayed.

Then, change the priority of the Apache index files to have it give the highest priority to files named index.php.

Then edit the Apache configuration file "dir.conf":

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

Move the index.php file to the top of the list, in this way :

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

Save the changes by pressing CTRL + X, typing Y and then confirm by pressing enter.

Restart Apache to apply the changes

$ sudo systemctl restart apache2

To verify the operation of the PHP interpreter and its configuration, all you need to do is create an index.php file in the folder that contains your site:

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

Insert the following content in the file to print the information of your on-screen configuration:

<?php
phpinfo();
?>

Then save the changes by pressing CTRL + X, typing Y, and then confirm by pressing enter.

At this point, visit the address http://<SERVER.IP> or in the case of a local server http: // localhost .

If the PHP info page is correctly displayed, the procedure was successfully performed.

Phpinfo Page

At this point you have correctly installed and configured your server with a LAMP architecture.