How to install LAMP stack on Ubuntu 20.04
Linux Tutorials

How to install LAMP stack on Ubuntu 20.04

Learn how to install the LAMP stack (Linux, Apache, MySQL, and PHP) on an Ubuntu 20.04 server in this tutorial. Follow the step-by-step guide to set up a powerful and flexible web development environment on your server.

Harrison R
Harrison ROct 18, 2022

In this tutorial, we will provide you with a detailed guide on how to install the LAMP stack on an Ubuntu 20.04 server. The LAMP stack consists of four components: Linux, Apache, MySQL, and PHP. These tools are used together to host web applications written in PHP, making it a powerful and flexible solution for web development.

Installing the LAMP stack on an Ubuntu server is a relatively straightforward process, but it does require some technical knowledge and experience with the command line. We will walk you through each step of the process to ensure that you are able to set up your LAMP stack successfully.


To run a LAMP web server, we're going to need a server with Ubuntu 20.04 installed as its operating system. You can grab yourself $200 free credit from Digital Ocean to get a virtual machine if you haven't got a server already!

Although it isn't required, we highly recommend you set up some basic firewall rules along with a non-root user. Take a look at our basic Ubuntu 20.04 server setup guide on how to do that.

During this guide, we will mention my_domain which you'll need to replace with your actual domain name.

Step 1 - Getting your server ready

Once you have your Ubuntu 20.04 server, you'll need to connect to it via SSH (check out this guide if you don't know how). There are two things we'll need to do first which are opening the required firewall ports and ensuring our server is fully up-to-date.

Before we do anything, ensuring the server is up-to-date and our package manager cache is updated is important to prevent any errors when installing packages later on. This is achieved by running the below commands:

sudo apt update -y
sudo apt upgrade -y

We are going to need two ports open, 80 and 443 which are for the web server. To open these, simply run the following two commands:

sudo ufw allow 80
sudo ufw allow 443

Step 2 - Installing Apache & creating your virtual host

Now we can install our first package of the LAMP stack, Apache, which is going to be used as our web server. To do this, we'll need to run the following command:

sudo apt install apache2 -y

Once this has been installed, we can then create our virtual host which is the web server configuration for our website. We'll need to create a directory where our website files are going to be stored:

sudo mkdir /var/www/my_domain

Then we can change the ownership of the directory we just created:

sudo chown -R $USER:$USER /var/www/my_domain

After that, we can create our domain's configuration file. You can use any text editor for this but we'll be using nano:

sudo nano /etc/apache2/sites-available/my_domain.conf

Once your file is open, we need to add the following code and save it:

<VirtualHost *:80>
    ServerName my_domain
    ServerAlias www.my_domain
    ServerAdmin webmaster@my_domain
    DocumentRoot /var/www/my_domain
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Finally, we need to enable the configuration we've just created and disable the default one that ships with Apache:

sudo a2ensite my_domain
sudo a2dissite 000-default

We can then reload Apache to make our changes live on the web server:

sudo systemctl reload apache2

If you have followed each step carefully, your website will now be live. To test this you can create an index.html file and save the following code to it:

nano /var/www/my_domain/index.html

        <title>My website</title>

        <h1>Hello world</h1>

You should now be able to access your site by visiting http://my_domain in your browser, it should look like this: Your website

Step 3 - Installing MySQL

Now we have our web server running, we need to install MySQL to serve as our database system. MySQL is an excellent database system and is the most used for web applications.

To do this, we'll need to run the following command:

sudo apt install mysql-server -y

We can then run the pre-installed configuration script to start the setup of MySQL. This will allow us to set important security settings. These settings should be set depending on your use case so please read each step carefully before giving an answer. You can enter Y for yes and N for no.

sudo mysql_secure_installation

Step 4 - Installing PHP

We will need to install the final part of our LAMP stack, PHP, which is used to process our code and display it to the end-user.

Along with our php package, we'll also need the php-mysql package which is used to talk to our MySQL databases, and libapache2-mod-php to allow Apache to handle our PHP files. Any required/core packages will be automatically installed when we install PHP.

To install these packages, simply run the following command:

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

We can then verify PHP has been installed using this command:

php -v

By default, a file named index.html will always be used over an index.php file. If you want to change this setting, you’ll need to edit the /etc/apache2/mods-enabled/dir.conf file and adjust the order ensuring index.php comes first.

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

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

If you edit this file, you'll need to reload Apache with the following command:

sudo systemctl reload apache2

Step 5 - Testing PHP can process files

Now we have all of our LAMP stack installed and configured, we need to test PHP can process our PHP files and display these on your website.

The easiest way to do this is to create a file called info.php and use the phpinfo() function to view our PHP configuration. This can be done with the following command and save the below content in the file:

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

<?php phpinfo(); ?>

You should now be able to view this file by visiting http://my_domain/info.php in your browser, it should look like this: info.php

If your website is displaying a page similar to the above screenshot, PHP is successfully processing code to the web server!


Congratulation, you should now have a functioning LAMP stack web server with Linux, Apache, MySQL and PHP set up and running. You can now begin building your website and show it off to the world!

You should now go ahead and protect your website with an SSL certificate using Let's Encrypt, we have a guide outlining exactly how to do this!

Harrison R


Harrison R

Whilst developing and maintaining various products at Letrix Labs, Harrison enjoys writing technical guides and news articles in his spare time on topics such as Linux, web hosting, and cloud computing. These are some of his favorite subjects to write about.

Next article

How to setup an Nginx reverse proxy (Ubuntu)


Get the most talked about articles directly in your inbox

Every week we share the most relevant news in tech, culture and entertainment.

Your privacy is important to us. We promise to not send you spam!