How to Install a LAMP Stack on Ubuntu 20.04

Tutorial Difficulty Level    

A LAMP (Linux, Apache, MySQL, PHP) stack is a common, free, and open-source web stack used for hosting web content in a Linux environment. Many consider it the platform of choice on which to develop and deploy high-performance web apps (you can also use NGINX as your web server and MariaDB for the database, depending on speed and load requirements).

In this tutorial we will create an Ubuntu 20.04 (LTS) via the XOA Platform and then install a full LAMP Stack for immediate use.

Building The Server

Login to XOA as usual and (if you have permissions) create a NEW Ubuntu installation using the Ubuntu 20.04 Live Server ISO file.

Choose your language (navigate with with TAB key):

Choose your keyboard layout:

Leave the Network Settings as they are:

Leave the Storage Settings as they are also:

You can review these settings on the next screen, then press DONE to proceed with installation…

..and CONFIRM to begin the actual installation.

After you have created your username and password, be sure to install the OpenSSH Server, we are going to need it later on.

You need not bother installing any other software just yet.

The system will now install. Reboot it when done.

After reboot, login to your new VM and run the command “ip a” to get the IP address attached to interface eth0. It should be in the 10.108.x.x range.

Write this address down along with your username and password you created earlier. You can now exit the XOA Platform, we are done here.

Installing the LAMP Stack

There are 2 methods to installing your LAMP Stack, which we will explain. However, we first need to connect remotely to our VM using the terminal (Linux and MacOS machines) or with a client like PuTTY (Windows machine).

If going via the terminal, the syntax is something like the following, which you adapt for your username and server address:

The output will be something be similar to this:

As you can see, there are updates waiting to be applied. For a nice secure web server, we should do these now.

You may need to reboot. You are now ready to install your web server!

Method #1

Installing Apache

To install Apache on your new Ubuntu server, run the following command.

Installing PHP

Install PHP is simply enough. It can be installed using the Apt package manager from Ubuntu’s repositories.

To install PHP, run the following command:

Install Apache PHP Module

The PHP module for Apache is not bundled with Apache. As such, it must be installed in addition to the Apache package.

Once installed the module will have to be enabled. We accomplish this using the a2enmod command.

Install MySQL Server

and the required extension for Apache


Optionally, install additional cURL, JSON, and CGI support:

Lastly, the Apache web service requires a reboot before the enabled mods & extensions will be fully loaded.

Method #2

Instead of installing Apache, MySQL, and PHP separately, Tasksel offers a convenient way to get a LAMP stack running quickly.

Install Tasksel if not already installed by default:

Use Tasksel to install the LAMP stack:

That’s it, you’re done! Taskel won’t always be available to you, so note BOTH the methods we have explained today.

Configuration and Testing

First, let’s configure MySQL. Log in to MySQL’s shell with this command:

By default, MySQL is configured to use the auth_socket authorization plugin. This authorization scheme allows you to log in to the database’s root user as long as you are connecting from the Linux root user on localhost, or as a user with sudo privileges (i.e. with sudo mysql -u root). In this scheme, no password is assigned to MySQL’s root user:

You can keep using the auth_socket plugin, and this is considered a secure option for production systems. If you’d rather switch to password authentication and assign a password, enter the following commands. Replace password with a new root password in the below command:

After making this change, you should pass the -p option when invoking the MySQL shell in future:

Now create a database and a user with granted permissions for it. In this example, the database is called webdata, the user webuser, and password password. Again, be sure to enter your OWN secure password for password. This should also be different from the root password for MySQL:

Now Exit the SQL shell:

Use the mysql_secure_installation tool to configure additional security options. This tool will ask if you want to set a new password for the MySQL root user, but you can skip that step:

Answer Y at the following prompts:

    • Remove anonymous users?
    • Disallow root login remotely?
    • Remove test database and access to it?
    • Reload privilege tables now?

We’ll now create a test page that shows whether Apache can render PHP and connect to the MySQL database. This can be helpful in locating the source of an error if one of the elements of your LAMP stack is not communicating with the others.

Paste the following code into a new file, phptest.php, in the /var/www/html/ directory (tip: nano is a great text editor). Modify webuser and password to match the information entered in the instructions above:

Navigate to http://serverip/phptest.php (eg. from your local machine’s browser. If the various components of your LAMP stack are working correctly, the browser will display a “Connected successfully” message.

If not, the output will be an error message, which will need further investigation (did you miss a step above?)

If the site does not load at all, check if Apache is running, and restart it if required:

If the site loads, but the page returned is the default “Congratulations” page, check your Virtual Hosts and that the DocumentRoot matches your /var/www/html folder (it probably will).

If the page returned says “Index of /” or has a similar folder tree structure, create a test index.html file or a test file as shown above.

Congratulations! You have now set up and configured a LAMP stack on Ubuntu 20.04 (LTS).