Updated on 25.5.2023

How to install LAMP stack web server on Ubuntu 20.04

LAMP stack is a simple yet popular suite of widely supported web server software. It commonly refers to the combination of Linux, Apache, MySQL and PHP. LAMP stack makes a great starting point for users who want to get a web server running quickly and easily on their new cloud server.

In this guide, we’ll walk you through the steps required to install a basic web server using Ubuntu 20.04, Apache2, MariaDB and PHP. These together form a reliable LAMP stack and the groundwork for building your websites! The steps in this guide are written for use with a newly deployed Ubuntu 20.04 UpCloud Cloud Server. The steps and installed software may vary on other platforms.

Step 1: Preparing your Ubuntu server

To begin with, you need a cloud server to run the LAMP stack software. If you are new to UpCloud, have a look at our quick-started guide for deploying your first cloud server and how to connect to it.

Once you have your cloud server up and running and connect to it using SSH, you can get started!

First of all, ensure everything is up to date on your server:

sudo apt update
sudo apt upgrade

Now open ports 22 (for SSH), 80 and 443 and enable Ubuntu Firewall (ufw):

sudo ufw allow ssh
sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable

Step 2: Installing and testing Apache2

Install Apache using apt:

sudo apt install apache2

Confirm that Apache is now running with the following command:

sudo systemctl status apache2

You should then get an output showing that the apache2.service is running and enabled.

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-11-03 10:32:26 UTC; 1min 6s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 52943 (apache2)
      Tasks: 7 (limit: 2282)
     Memory: 11.9M
     CGroup: /system.slice/apache2.service
             ├─52943 /usr/sbin/apache2 -k start
             ├─52944 /usr/sbin/apache2 -k start
             ├─52945 /usr/sbin/apache2 -k start
             ├─52946 /usr/sbin/apache2 -k start
             ├─52947 /usr/sbin/apache2 -k start
             ├─52948 /usr/sbin/apache2 -k start
             └─52953 /usr/sbin/apache2 -k start

Once installed, test by accessing your server’s IP in your browser:

http://YOURSERVERIPADDRESS/

You should see a page with an “Apache2 Ubuntu Default” header showing that Apache2 has been installed successfully. If you do not see this, please ensure that the previous commands in this section have been completed without error

Step 3: Installing and testing PHP 7.4

PHP 7.4 is the latest available right now so let’s install that along with some regularly used modules:

sudo apt install php7.4 php7.4-mysql php-common php7.4-cli php7.4-json php7.4-common php7.4-opcache libapache2-mod-php7.4

Check the installation and version:

php --version

Restart Apache for the changes to take effect:

sudo systemctl restart apache2

Create a phpinfo.php test page:

echo '<?php phpinfo(); ?>' | sudo tee -a /var/www/html/phpinfo.php > /dev/null

Test everything is working by accessing the following in your browser:

http://YOURSERVERIPADDRESS/phpinfo.php

You should see a PHP Version 7.4.3 page listing all of your PHP options. If you don’t or it tries to download a file, double-check that all of the above steps have been completed without error.

Once you’ve confirmed that PHP is working correctly, delete the test file.

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

The information displayed in the PHP info could be used to find an attack vector against your web server so best not to leave it publicly accessible.

Step 4: Installing and securing MariaDB

MariaDB is a fork of MySQL from some of the original MySQL team and is a drop-in replacement. We’ll be using this over MySQL itself in this guide!

Install the required packages:

sudo apt install mariadb-server mariadb-client

Once installed, check it’s running correctly:

sudo systemctl status mariadb

You should see an output similar to the example below.

● mariadb.service - MariaDB 10.3.25 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-11-03 10:33:12 UTC; 4s ago
       Docs: man:mysqld(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 53554 (mysqld)
     Status: "Taking your SQL requests now..."
      Tasks: 31 (limit: 2282)
     Memory: 65.9M
     CGroup: /system.slice/mariadb.service
             └─53554 /usr/sbin/mysqld

Secure your newly installed MariaDB service:

sudo mysql_secure_installation

As you have no root password set for MariaDB you should simply press Enter when prompted, pressing Y on the next question to then set a root password (keep this safe and secure!) With that set, you can press Enter for the remaining questions as the defaults for each of these will help to secure your new installation.

Conclusions

Congratulation! You should then have a functioning LAMP stack web server with Linux, Apache, MariaDB and PHP set up and running! You are now ready to start building your website.

Bret Weber

  1. Nice Tutorial! Clear and focus on the task.
    Thank you!

  2. Excellent tutorial, very clean, lean and to the point. Well done.

  3. Thanks for your supereasy to follow tutorial help me alot

  4. The best Installation procedures I have ever used for LAMP. Thanks

  5. Very good tutorial

  6. Very good tutorial but, why not use tasksel? It is a lot easier and automatic… for example, look here:

    https://vitux.com/install-and-configure-lamp-server-on-ubuntu/

  7. Janne Ruostemaa

    Hi Gonzalo, thanks for the comment. Tasksel is a great option for installing predefined configurations. However, the main point of tutorials for installing a LAMP stack manually is in learning how each component works and interacts.

  8. Very good, nice and clear but I need some help configuring the permissions for PHP file uploads. I determined Apache and PHP both run as ‘me’ and I own the destination Uploads directory but the script fails move_uploaded_file(): Unable to move ‘/tmp/phpSeyVgA’ to ‘uploads/notes.csv’ in /var/www/html/catalogue/index.php on line 428.
    Any thoughts?

  9. Janne Ruostemaa

    Hi Philip, thanks for the question. Commonly, a LAMP stack like in this tutorial is run under the user and group www-data. You may need to change the ownership of your uploads/ directory to allow the webserver to copy the uploaded files over.

  10. Nice tutorial. Maybe should have included phpMyAdmin.

  11. I am not able to install php7.4
    Please suggest me how it works and way to solve this problem.

  12. Janne Ruostemaa

    Hi there, thanks for the question. If your choice of OS doesn’t have PHP available via package manager, you can always just download the version you prefer from PHP’s website.

Leave a Reply to Phill

Your email address will not be published. Required fields are marked *

Back to top