Install LAMP Stack on Ubuntu 18.4 & 16.4

Installing Apache

Note: We will assume that a non-root user has been configured for the purposes of this guide. If it has not, you can run all commands without sudo privileges. However, we recommend only using the root user when there is no other option. To configure a new sudo user, please check out the Creating a New Sudo User section of our How to Secure Your SSH Connection in Ubuntu 18.04 article.

Before we install Apache, we first want to ensure that our operating system is fully up to date. We can do this with the following command:

$ sudo apt update

Now that the OS is up to date, we can proceed to install Apache. Do so by entering the following command:

$ sudo apt install apache2

Note: If you have a firewall enabled, you will need to ensure that ports 80 and 443 are listening.

Now type the IP address of your server into a browser to confirm that Apache has installed properly. If it has, you will see the following page:

apache.jpg

Now that we have Apache installed and working, let’s get started installing MySQL.

Installing MySQL

The third portion of our LAMP stack is MySQL. To install MySQL, enter the following command:

$ sudo apt install mysql-server

Now enter the MySQL installation module using the following command:

$ sudo mysql_secure_installation

You will be met with the following prompt:

Securing the MySQL server deployment.

Connecting to MySQL using a blank 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:

It is entirely up to you whether you choose to install this plugin or not. It will check the strength of any passwords you set and make sure they are strong enough. We will proceed as if you did not install the plugin.

Next, you will be prompted to create a new password for your MySQL root user. Choose any password you wish and then confirm the password at the following prompt.

Press y and ENTER for each of the rest of the prompts. When you are finished you will get the following output:

All done! 

Now that we have MySQL installed, let’s take a look at installing PHP.

Installing PHP

In this section, we will be installing and configuring PHP. First, we need to install PHP along with its packages for Apache and MySQL using the following command:

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

Note: If you wish to download other PHP packages, you can append them to the end of this command.

Next, we want to configure Apache to favor PHP files over HTML files (the default file type it chooses). To do so, open the dir.conf file in a text editor of your choice:

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

In this file, we want to move index.php to the beginning of the list of file types. The file should appear as follows.

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

Now we need to restart Apache so that it reads the configuration changes. Do so using the following command:

$ sudo systemctl restart apache2

Finally, we will create a test file to ensure that PHP has been installed and configured correctly. Create a new file called info.php by opening it in your preferred text editor using the following command:

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

Enter the following text into the file:

<?php
phpinfo();
?>

Now navigate to the following website, replacing the x’s with your server’s IP address or domain name: xxx.xxx.xxx.xxx/info.php

You should see the following page.

phpinfo.jpg

If you see the above page, you have correctly deployed PHP and you are done deploying your LAMP stack. Since this page gives information about your server that could be used to exploit your security, delete this file using the following command:

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

Conclusion

A properly deployed LAMP stack is the backbone of many websites. Having read this guide, you should now know how Apache, MySQL, and PHP are used as well as how to perform a basic installation of a LAMP stack.