How to Install Apache, MySQL, PHP (LAMP Stack) on Ubuntu 26.04

Updated on 23 April, 2026
Install and configure the LAMP stack with Apache, MySQL, and PHP on an Ubuntu 26.04 Vultr server for hosting dynamic web applications and CMS platforms.
How to Install Apache, MySQL, PHP (LAMP Stack) on Ubuntu 26.04 header image

LAMP is a web application stack consisting of Linux, Apache, MySQL, and PHP. Apache serves as the HTTP web server, MySQL provides the relational database backend, and PHP processes server-side scripts to generate dynamic content. Together, these components form a widely used platform for hosting content management systems, e-commerce applications, and custom web services.

This article explains how to install the LAMP stack on an Ubuntu 26.04 server, configure Apache to process PHP requests through PHP-FPM, secure the server with firewall rules and a Let's Encrypt SSL certificate, and test the stack with a sample PHP application that queries a MySQL database.

Prerequisites

Before you begin, you need to:

Install Apache

The default Ubuntu 26.04 APT repositories include the Apache web server package. The following steps install Apache and verify that the service is running.

  1. Update the APT package index.

    console
    $ sudo apt update
    
  2. Install Apache.

    console
    $ sudo apt install apache2 -y
    
  3. Enable Apache to start automatically at boot time.

    console
    $ sudo systemctl enable apache2
    
  4. Start the Apache service.

    console
    $ sudo systemctl start apache2
    
  5. Verify that Apache is active and running.

    console
    $ sudo systemctl status apache2
    

    The output should display active (running), confirming that the Apache web server is operational.

Install MySQL

MySQL is included in the default Ubuntu 26.04 repositories. The following steps install the database server, enable the service, and run the secure installation script to harden the default configuration.

  1. Install the MySQL server package.

    console
    $ sudo apt install mysql-server -y
    
  2. Enable MySQL to start automatically at boot time.

    console
    $ sudo systemctl enable mysql
    
  3. Start the MySQL service.

    console
    $ sudo systemctl start mysql
    
  4. Verify that MySQL is active and running.

    console
    $ sudo systemctl status mysql
    

    The output should display active (running), confirming that the MySQL server is operational.

  5. Run the MySQL secure installation script to remove insecure defaults.

    console
    $ sudo mysql_secure_installation
    

    Respond to each prompt as described below:

    • VALIDATE PASSWORD COMPONENT: Enter Y and press Enter to activate password validation.
    • Password Validation Policy: Enter 2 to require strong passwords.
    • Remove anonymous users?: Enter Y to delete anonymous database accounts.
    • Disallow root login remotely?: Enter Y to restrict root access to the local server.
    • Remove test database and access to it?: Enter Y to drop the default test database.
    • Reload privilege tables now?: Enter Y to refresh the privilege tables and apply all changes.
  6. Open the MySQL server configuration file.

    console
    $ sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
    
  7. Add the following directive under the [mysqld] section to activate the mysql_native_password authentication plugin.

    ini
    mysql_native_password=ON
    

    Save and close the file.

  8. Restart MySQL to load the updated configuration.

    console
    $ sudo systemctl restart mysql
    
  9. Access the MySQL console as the root system user.

    console
    $ sudo mysql
    
  10. Set a strong password for the root account. Replace your_strong_password with a secure password that meets your validation policy.

    sql
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_strong_password';
    
  11. Reload the privilege tables.

    sql
    mysql> FLUSH PRIVILEGES;
    
  12. Exit the MySQL console.

    sql
    mysql> EXIT;
    

Install PHP

PHP-FPM (FastCGI Process Manager) enables Apache to process PHP scripts by forwarding requests through a Unix socket. The following steps install PHP, PHP-FPM, and essential modules for the LAMP stack.

  1. Install PHP, PHP-FPM, and common extensions.

    console
    $ sudo apt install php php-fpm php-mysql php-cli libapache2-mod-php -y
    

    The command installs the following packages:

    • php-fpm: Manages PHP worker processes for handling web server requests.
    • php-mysql: Enables PHP to connect to MySQL databases.
    • php-opcache: Caches precompiled PHP scripts in memory for faster execution.
    • php-cli: Provides the PHP command-line interpreter.
    • libapache2-mod-php: Enables Apache to process PHP scripts directly.
  2. Confirm the installed PHP version.

    console
    $ php -v
    

    Your output should be similar to the one below:

    PHP 8.5.2 (cli) (built: Jan 21 2026 17:35:28) (NTS)
    Copyright (c) The PHP Group
    Built by Ubuntu
    Zend Engine v4.5.2, Copyright (c) Zend Technologies
        with Zend OPcache v8.5.2, Copyright (c), by Zend Technologies
  3. Enable PHP-FPM to start automatically at boot time.

    console
    $ sudo systemctl enable php8.5-fpm
    
  4. Start the PHP-FPM service.

    console
    $ sudo systemctl start php8.5-fpm
    
  5. Verify that PHP-FPM is active and running.

    console
    $ sudo systemctl status php8.5-fpm
    

    The output should display active (running), confirming that PHP-FPM is operational.

Configure Apache with PHP-FPM

Apache requires additional modules to forward PHP requests to PHP-FPM. The following steps enable the required modules and activate the PHP-FPM configuration.

  1. Enable the Apache proxy and environment modules.

    console
    $ sudo a2enmod proxy_fcgi setenvif
    
  2. Enable the PHP-FPM configuration for Apache.

    console
    $ sudo a2enconf php8.5-fpm
    
  3. Restart Apache to apply the module changes.

    console
    $ sudo systemctl restart apache2
    

Set Up Firewall Rules

Uncomplicated Firewall (UFW) is active by default on Ubuntu 26.04. The following steps open ports for HTTP and HTTPS traffic.

  1. Allow HTTP traffic on port 80.

    console
    $ sudo ufw allow 80/tcp
    
  2. Allow HTTPS traffic on port 443.

    console
    $ sudo ufw allow 443/tcp
    

Configure Apache Virtual Host

Apache uses virtual host configurations to serve content based on the requested domain name. The following steps remove the default configuration, create a virtual host for your domain, and connect it to PHP-FPM.

  1. Unlink the default Apache virtual host configuration.

    console
    $ sudo a2dissite 000-default.conf
    
  2. Create a new virtual host configuration file. Replace app.example.com with your actual domain.

    console
    $ sudo nano /etc/apache2/sites-available/app.example.com.conf
    
  3. Add the following configuration to the file. Replace app.example.com with your actual domain.

    ini
    <VirtualHost *:80>
        ServerAdmin webmaster@app.example.com
        ServerName app.example.com
        DocumentRoot /var/www/app.example.com
    
        <Directory /var/www/app.example.com>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
    
        <FilesMatch \.php$>
            SetHandler "proxy:unix:/var/run/php/php8.5-fpm.sock|fcgi://localhost/"
        </FilesMatch>
    
        ErrorLog ${APACHE_LOG_DIR}/app.example.com_error.log
        CustomLog ${APACHE_LOG_DIR}/app.example.com_access.log combined
    </VirtualHost>
    

    Save and close the file.

    Within the configuration:

    • ServerName: Defines the domain that triggers this virtual host.
    • DocumentRoot: Sets the directory from which Apache serves files.
    • <FilesMatch \.php$>: Forwards all PHP file requests to the PHP-FPM socket for processing.
    • ErrorLog and CustomLog: Store virtual host-specific error and access logs.
  4. Create the web root directory for the virtual host.

    console
    $ sudo mkdir -p /var/www/app.example.com
    
  5. Enable the new virtual host configuration.

    console
    $ sudo a2ensite app.example.com.conf
    
  6. Test the Apache configuration for syntax errors.

    console
    $ sudo apache2ctl configtest
    

    The output should display Syntax OK.

  7. Restart Apache to activate the virtual host.

    console
    $ sudo systemctl restart apache2
    

Secure the Server with SSL

SSL certificates encrypt communication between the client browser and the Apache web server over HTTPS. The following steps install Certbot and generate a trusted Let's Encrypt SSL certificate for your domain.

  1. Install the Certbot Let's Encrypt client and the Apache plugin.

    console
    $ sudo apt install certbot python3-certbot-apache -y
    
  2. Generate an SSL certificate for your domain. Replace app.example.com with your actual domain.

    console
    $ sudo certbot --apache -d app.example.com --agree-tos
    

    Follow the on-screen prompts to complete the certificate generation. Certbot automatically updates the Apache virtual host configuration to enable HTTPS.

  3. Verify that the automatic renewal process is configured correctly.

    console
    $ sudo certbot renew --dry-run
    

Test the LAMP Stack

A working LAMP stack allows PHP to query data from MySQL and return dynamic content through Apache. The following steps create a sample database, insert a test record, and build a PHP application that retrieves and displays the data in a web browser.

  1. Log in to the MySQL console as the root user.

    console
    $ mysql -u root -p
    

    Enter the root password you configured earlier.

  2. Create a new database named example_db.

    sql
    mysql> CREATE DATABASE example_db;
    
  3. Create a database user named example_user with a strong password. Replace strong_password with your desired password.

    sql
    mysql> CREATE USER 'example_user'@'localhost' IDENTIFIED BY 'strong_password';
    
  4. Grant the user full privileges on the example_db database.

    sql
    mysql> GRANT ALL PRIVILEGES ON example_db.* TO 'example_user'@'localhost';
    
  5. Reload the privilege tables.

    sql
    mysql> FLUSH PRIVILEGES;
    
  6. Switch to the example_db database.

    sql
    mysql> USE example_db;
    
  7. Create a table named messages with an auto-incrementing ID and a text column.

    sql
    mysql> CREATE TABLE messages (
             id INT AUTO_INCREMENT PRIMARY KEY,
             content VARCHAR(255) NOT NULL
             );
    
  8. Insert a sample record into the table.

    sql
    mysql> INSERT INTO messages (content) VALUES ('Hello World from LAMP Stack');
    
  9. Verify that the record exists.

    sql
    mysql> SELECT * FROM messages;
    

    Your output should be similar to the one below:

    +----+-----------------------------+
    | id | content                     |
    +----+-----------------------------+
    |  1 | Hello World from LAMP Stack |
    +----+-----------------------------+
    1 row in set (0.00 sec)
  10. Exit the MySQL console.

    sql
    mysql> EXIT;
    
  11. Create a PHP test file in the web root directory.

    console
    $ sudo nano /var/www/app.example.com/test.php
    
  12. Add the following PHP code to the file. Replace strong_password with the example_user password you set earlier.

    php
    <?php
    $servername = "localhost";
    $username = "example_user";
    $password = "strong_password";
    $dbname = "example_db";
    
    $conn = new mysqli($servername, $username, $password, $dbname);
    
    if ($conn->connect_error) {
        die("Database connection failed: " . $conn->connect_error);
    }
    
    $sql = "SELECT content FROM messages";
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        echo "<h1>" . htmlspecialchars($row["content"]) . "</h1>";
    } else {
        echo "<h1>No records found.</h1>";
    }
    
    $conn->close();
    ?>
    

    Save and close the file.

    This script connects to the example_db database, queries the messages table, and displays the result as an HTML heading.

  13. Access the test page in a web browser. Replace app.example.com with your actual domain.

    https://app.example.com/test.php

    The browser displays the message Hello World from LAMP Stack, confirming that Apache, MySQL, and PHP are working together.

Conclusion

You have deployed and secured the LAMP stack on an Ubuntu 26.04 server. Apache serves web requests, PHP-FPM processes dynamic content, and MySQL manages the application data. This stack supports multiple virtual hosts and integrates with frameworks such as Laravel and WordPress. For more configuration options, refer to the following resources:

Comments