How to Install a LAMP Stack on Ubuntu 22.04+ (Apache, MySQL, PHP)

Set up Apache, MySQL, and PHP on Ubuntu 22.04+ to host dynamic websites with a complete LAMP environment.

Key points to consider:

  • LAMP stands for Linux, Apache, MySQL, and PHP, a widely adopted stack for hosting dynamic websites and web applications.

  • Ubuntu provides official maintained packages for all LAMP components through the apt package manager, ensuring easy installation and updates.

  • Apache is a robust and widely used open source web server that will serve your website content over HTTP or HTTPS.

  • MySQL is a powerful relational database system  for storing structured data such as user profiles, blog entries, or product catalogs.

  • PHP is a server-side scripting language used to process logic and dynamically generate web pages by connecting to MySQL and rendering through Apache.

  • Each component can be installed and tested independently, ensuring modular troubleshooting and step by step verification. 

  • Installing the LAMP on a freshly configured Ubuntu server ensures a clean, secure starting point for your web stack.

  • You can test PHP functionality with a sample .php file and verify that Apache is correctly parsing dynamic content.

  • Secure your MySQL server post installation using the mysql_secure_installation script to remove default vulnerabilities. 

  • Ensure that all services (Apache, MySQL, PHP) are set to start automatically using systemctl , and verify their status with simple test scripts.

Introduction

The LAMP stack-short for Linux, Apache, MySQL, and PHP- is one of the most widely used open source stacks for developing and deploying dynamic web applications. This stack is reliable, and well supported across modern web environments.

In this tutorial, you will learn how to install and configure a full LAMP stack on an Ubuntu server. It assumes you have already completed your initial server setup, including:

  • Creating a non-root user
  • Enabling SSH key authentication
If you have not done so already, please follow our Initial Server Setup with Ubuntu tutorial before proceeding.

We will walk through the installation and configuration each component individually:

  • Apache will be installed as the HTTP server to serve static and dynamic content.

  • MySQL will be used as the relational database backend.

  • PHP will act as the middleware, allowing dynamic content generation and communication with MySQL.

This guide is designed for beginner friendly users and  provides step-by-step commands, example outputs, and verification tips. By the end of this tutorial, you will have a fully functional web server capable of hosting content management systems  like WordPress, web frameworks like Laravel, or custom projects, or your own PHP projects.

Note: This tutorial is intended for Ubuntu 22.04 or newer. Adjustments may be required for other distributions or older versions.

Step-by-Step Instructions

Step 1: Install Apache web server

The first component of the LAMP stack is Apache, a powerful, open source web server used to deliver websites to users over the HTTP/HTTPS protocols. Installing Apache is the foundation for serving both static and dynamic web pages.

  1. Update the package list
    Before installing any new software, it is a good idea to make sure your package list is up to date. Run the following command:
    $ sudo apt update
    This ensures that you are downloading the latest version of Apache available from the Ubuntu repositories.
  2. Install Apache
    Now install the Apache package:
    $ sudo apt install apache2 -y
    The -y flag automatically confirms the installation when prompted. In some systems, Apache will start automatically after installation, but in others (especially newer Ubuntu versions), the service may remain inactive.
  3. Start and enable the Apache service
    If Apache did not start automatically, you can start it manually with the following command:
    $ sudo systemctl start apache2
    To ensure that Apache starts on boot, run:
    $ sudo systemctl enable apache2
    This will create a persistent service entry so the web server starts automatically after system reboots.
  4. Allow Apache through the firewall (if UFW is enabled)
    If your server uses UFW (Uncomplicated Firewall), you need to allow Apache traffic on port 80 (HTTP) and optionally port 443 (HTTPS). Run the following command:
    $ sudo ufw allow 'Apache'
    Or, to allow both HTTP and HTTPS:
    $ sudo ufw allow 'Apache Full'
    Ubuntu UFW firewall uses predefined application profiles to simplify rule management. You can list the available profiles with:
    $ sudo ufw app list
    Expected output (see Fig. 1):

    Fig. 1. UFW lists available application profiles like Apache, Apache Full, and OpenSSH for simplified firewall rule management.
    Here is what each means:
    • Apache – opens port 80 for unencrypted HTTP traffic.
    • Apache Full – opens both port 80 (HTTP) and port 443 (HTTPS).
    • Apache Secure – opens only port 443 for secure HTTPS connections.
    You can verify UFW status and active rules using (see Fig. 2):
    $ sudo ufw status verbose

    Fig. 2. UFW is active and configured to allow Apache (HTTP/HTTPS) and OpenSSH connections.
  5. Verify Apache status
    Check that Apache is active and running:
    $ sudo systemctl status apache2
    You should see a status message indicating that the service is active (running) (see Fig. 3). If not, double check the installation steps or check for error messages using:
    $ sudo journalctl -xeu apache2

    Fig. 3. Apache is running and enabled. The active (running) status confirms the web server is operational and set to start at boot.
  6. Test Apache in your web browser

    Once Apache is installed and running, it is important to confirm that it is properly serving web content. The easiest way to do this is by accessing the default Apache landing page in a web browser. Open any web browser on your local machine and enter the following URL into the address bar:

    http://your_server_ip
    Replace your_server_ip with the actual public IP address of your Ubuntu server. This should bring up the default Apache2 Ubuntu web page.
    If everything is working correctly, you will see a welcome page titled "Apache2 Ubuntu Default Page" (see Fig. 4). This confirms that the web server is correctly installed, running, and accessible over HTTP.

    Fig. 4. Apache2 default welcome page confirms the server is reachable and serving content via HTTP.
    If the page does not load, verify that Apache is active, the firewall allows HTTP traffic on port 80, and that you are using the correct server IP address.

Step 2: Install MySQL Database server

The second component of the LAMP stack is MySQL, an open source relational database management system used to store, retrieve, and manage data for your websites and applications. MySQL is widely supported and integrates seamlessly with Apache and PHP.

  1. Install MySQL
    To install the MySQL server package, run the following command:
    $ sudo apt install mysql-server -y
    The -y flag confirms the installation automatically without prompting for manual approval. Once the package is installed, the MySQL service will be started automatically.
  2. Verify MySQL service status
    Check that the MySQL service is active and running:
    $ sudo systemctl status mysql
    You should see a status line indicating that the service is active (running) (see Fig. 5). If not, start the service manually:
    $ sudo systemctl start mysql
    To enable MySQL to start automatically on boot, run:
    $ sudo systemctl enable mysql

    Fig. 5. MySQL is running and enabled. The active (running) status confirms that the database server is operational and starts on boot.
  3. Run the MySQL secure installation script (optional but recommended)
    To improve the security of your MySQL installation, it is recommended to run the built in security script:
    $ sudo mysql_secure_installation
    This script will prompt you to configure several options, including:
    • Whether to enable the VALIDATE PASSWORD plugin (see Fig. 6).

      Fig. 6. MySQL secure installation script prompts whether to enable the password validation plugin for stronger user password enforcement.
      This step lets you enable the VALIDATE PASSWORD plugin, which checks password strength and enforces complexity rules for database users.
    • Whether to remove anonymous users (see Fig. 7).

      Fig. 7. The script recommends removing anonymous MySQL users to improve server security before deploying to production.
      Anonymous MySQL users allow access without credentials. It is recommended to remove them for better security.
    • Whether to disallow remote root login (see Fig. 8).

      Fig. 8. The MySQL secure installation script recommends disabling remote root login to prevent unauthorized access from external hosts.
      This option improves security by restricting the root user to local connections only.
    • Whether to remove the test database (see Fig. 9).

      Fig. 9. The script offers to remove the default 'test' database, which is accessible to all users and intended only for testing purposes.
      MySQL includes a test database that is accessible to all users. Removing it helps prevent misuse.
    • Whether to reload privilege tables (see Fig. 10).

      Fig 10. Reloading the privilege tables applies all changes immediately without restarting the MySQL service.
      Reloading privilege tables applies all previous changes (like user removals or password rules) immediately.
    You can choose the options based on your security needs. For most setups, accepting the default recommended options is sufficient.
  4. Test MySQL access
    After installing MySQL, verify access to the MySQL shell by running:
    $ sudo mysql
    You should be logged into the MySQL command line interface, which confirms that the server is functioning correctly (see Fig. 11). To exit the MySQL shell, type:
    exit;

    Fig. 11. Logging into the MySQL monitor using sudo mysql confirms the server is running and accessible.

Step 3: Install PHP

The final component of the LAMP stack is PHP, a widely used server side scripting language designed for web development. PHP is used to generate dynamic page content and interact with databases like MySQL, making it essential for many websites and applications.

  1. Install PHP and required modules
    Begin by installing PHP and common modules needed for Apache integration and MySQL database access:
    $ sudo apt install php libapache2-mod-php php-mysql -y
    • php – the core PHP package.
    • libapache2-mod-php – enables Apache to process PHP files.
    • php-mysql – provides MySQL support in PHP applications.
    • The -y flag confirms all prompts automatically.
  2. Verify PHP installation
    Once installation is complete, check the installed PHP version to confirm that PHP is set up correctly:
    $ php -v
    This command should return the installed PHP version and configuration details, such as (see Fig. 12):

    Fig. 12. Checking installed PHP version using php -v. Output confirms PHP 8.3.6 is correctly installed and functioning.
  3. Create a PHP info test page
    To test PHP functionality with Apache, create a simple test page:
    $ sudo nano /var/www/html/info.php
    Add the following contents to the file:
    <?php phpinfo(); ?>
    Save and exit the editor (Ctrl+O, Enter, then Ctrl+X). 
    Important: restart Apache to make sure it picks up the PHP module:
    $ sudo systemctl restart apache2
    This ensures Apache will correctly execute the new PHP file. Without this restart, some systems may return the file as plain text.
  4. Test PHP in your browser
    Open your web browser and navigate to your server's IP address followed by /info.php:
    http://your_server_ip/info.php
    You should see the PHP information page, which confirms PHP is working correctly with Apache (see Fig. 13).

    Fig. 13. PHP info page confirms Apache is correctly executing PHP.

Step 4: Install common PHP extensions (optional)

Many PHP applications and frameworks rely on additional modules to function properly. You can install the most commonly required extensions with:

$ sudo apt install php-cli php-curl php-mbstring php-xml php-zip -y

Here is what each module does:

  • php-cli – allows running PHP scripts via the terminal (used in cron jobs, Artisan commands, etc.).

  • php-curl – enables HTTP requests, used by APIs and SDKs.

  • php-mbstring – handles multibyte strings like UTF-8 (critical for internationalization).

  • php-xml – provides XML parsing functionality for DOM and SimpleXML.

  • php-zip – enables working with .zip archive files in PHP.

These modules are commonly needed by:

  • Laravel

  • WordPress

  • phpMyAdmin

  • Custom applications that interact with API's or use ZIP/JSON/XML parsing

Step 5: Changing Apache DirectoryIndex (optional)

By default, Apache prioritizes serving index.html when a user visits a directory. If you plan to serve dynamic websites written in PHP, it is recommended to prioritize index.php instead. This ensures PHP applications are served even when an index.html file is present.

  1. Open the dir.conf file
    Apache directory index order is defined in the following configuration file:
    $ sudo nano /etc/apache2/mods-enabled/dir.conf
  2. Move index.php to the front
    In the opened file, update the line so that index.php is the first entry (see Fig. 14):

    Fig. 14. Apache dir.conf file modified to prioritize index.php over index.html and other default files when serving directory requests.
    This change tells Apache to look for index.php before any other default file when serving a directory.
  3. Save and close the file
    Press Ctrl+O to write the file, Enter to confirm, and Ctrl+X to exit the editor.
  4. Restart Apache
    To apply the configuration change, restart the Apache service:
    $ sudo systemctl restart apache2
    This ensures Apache reloads its configuration with the updated directory index preference.

Summary

In this tutorial, you successfully installed and configured a fully functional LAMP stack on an Ubuntu server. You installed and tested each component individually:

  • Apache was configured to serve both static and dynamic content over HTTP/HTTPS.

  • MySQL was installed and secured using mysql_secure_installation to protect against common vulnerabilities.

  • PHP was installed and integrated with Apache and MySQL, enabling dynamic server side scripting.

  • You verified each service using terminal commands and browser tests to ensure proper operation.

You also explored optional but recommended enhancements:

  • Installed common PHP extensions such as php-curl, php-xml, and php-mbstring, which are essential for many modern web applications like Laravel, WordPress, and phpMyAdmin.

  • Changed Apache DirectoryIndex behavior to prioritize index.php over index.html, improving compatibility with dynamic PHP-based sites.

Note: for fast development setups or testing environments, you can use a one command installation method:

$ sudo apt install lamp-server^

This command installs Apache, MySQL, PHP, and common modules automatically. However, it skips security hardening and service configuration steps, so it is not recommended for production use.

With your LAMP stack ready, you can now begin hosting your own websites, deploying CMS platforms like WordPress, or building custom PHP applications on a secure and reliable Linux server foundation.