How to Install a LAMP Stack on Ubuntu?

The acronym LAMP stands for Linux, Apache, MySQL, and PHP, all of which are open-source software that, when stacked together, create a powerful web server. You should consider the LAMP stack if you’re planning to deploy a website or want to develop one and need a test environment.

You’re in luck! Because in this post, we’ll learn how to install a LAMP stack Ubuntu server step-by-step.


To follow this tutorial, you must have an Ubuntu server and a user account with sudo privileges. This post will be using an Ubuntu 22.04 server.

Step 1: Setup the Firewall

Assuming that your server has the UFW firewall enabled and active, this step will walk you through allowing the web server ports 80 and 443.

  1. Connect to your server via SSH or login to the desktop environment (if available).
  2. Next, update the local package cache to ensure you’re installing the latest packages later:
    sudo apt update
    lamp stack on ubuntu
  3. Run the below commands to add ports 80 and 443 to the allow list:
    sudo ufw allow 80 
    sudo ufw allow 443

    lamp on ubuntu

  4. Lastly, confirm the firewall rules are present and active:
    sudo ufw status numbered

    install lamp on ubuntu 20.04

Step 2: Install Apache2

So the ‘L’ in LAMP stands for Linux. And since you’re running Ubuntu, you’ve good the that part. Next in line is ‘A’, Apache, specifically Apache2. Follow these instructions to install it.

  1. Run this command to install Apache2 via APT.
    sudo apt install -y apache2

    As you can see, the command installs additional packages that Apache needs.
    ubuntu install lamp

  2. Next, confirm the apache2 service is enabled and active:
    sudo systemctl status apache2

    lamp server ubuntu

  3. Now, let’s make sure that the Apache web server is accessible. Launch a browser and open the URL http://SERVERIPADDRESS/.
    If all is well, you should see the default home page, as shown below.
    install lamp ubuntu 20.04

Step 3: Install MySQL

Now that the web server is up and running, we’ll need a database management system where you can store, organize, and your website’s contents. This is where the ‘M’ in LAMP comes in: MySQL.

  1. Run the below command to install the MySQL server:
    sudo apt install mysql-server

    As you can see, this command will also install the required additional packages.
    lamp ubuntu 20.04

  2. After the MySQL server installation, let’s ensure that the MySQL root account has a password:
    # Connect to MySQL 
    sudo mysql 
    # Set the root user password to Dk03^M$i7xB9. 
    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Dk03^M$i7xB9'; 
    # Exit MySQL 

    ubuntu 20.04 lamp

  3. Next, run the mysql_secure_installation script. This command will prompt you for the password specified in the previous step:
    sudo mysql_secure_installation
  4. The script then prompts you to set up the VALIDATE PASSWORD COMPONENT. This component will make passwords adhere to stricter validation guidelines, essentially banning weak and blank passwords.
    lamp install ubuntu 20.04
  5. For the rest of the questions, answer Y.
    ubuntu 20.04 install lamp
  6. And you’ve completed the MySQL installation.

Step 4: Install PHP

So you now have a web server (Apache2) and a database system for content management (MySQL). You need the ‘P’ in LAMP that handles code and displays dynamic content: PHP.

  1. Run the below command to install the php, libapache2-mod-php, php-mysql. The libapache2-mod-php package allows PHP integration with Apache, and php-mysql facilitates the connection between PHP and MySQL.
    sudo apt install php libapache2-mod-php php-mysql

    lamp stack ubuntu 20.04

  2. After the installation, confirm that PHP is present on the machine:
    php --version

    ubuntu lamp 20.04

  3. Restart the Apache service so that it picks up the current changes:
    sudo systemctl restart apache2
  4. Next, let’s test whether PHP works. We’ll create a PHP page that displays the local system information. Run this command to open a new file called /var/www/html/systeminfo.php in the text editor.
    sudo nano /var/www/html/systeminfo.php
  5. Copy the code below, paste it into the editor, and save the file:
    <?php system("hostname"); ?> 
    <br /> 
    <strong>IP Address:</strong> 
    <?php system("hostname -I"); ?> 
    <br /> 
    <strong>System Information:</strong> 
    <?php system("hostnamectl | grep 'Operating System\|Architecture\|Kernel'"); ?> 
    <br /> 
    <?php system("uptime"); ?> 
    <br /> 
    <strong>Memory Usage (MB):</strong> 
    <?php system("free -m"); ?> 
    <br /> 
    <strong>Disk Usage:</strong> 
    <?php system("df -h"); ?> 

    The file should look like the one below.
    installing lamp on ubuntu 20.04

  6. After saving the file, open the server URL http://SERVERIPADDRESS/systeminfo.php in your browser and see the following information on the webpage.
    install lamp stack
  7. We’ve now confirmed that PHP is installed and working as expected.


In this post, you learned how to install and configure each component of the LAMP stack, such as Apache, MySQL, and PHP.

That’s it! You’ve successfully installed and tested the LAMP stack Ubuntu server and are on your way to starting your web development project.

I enjoy technology and developing websites. Since 2012 I'm running a few of my own websites, and share useful content on gadgets, PC administration and website promotion.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.