MeshWorld India Logo MeshWorld.
lamp apache mysql php ubuntu 8 min read

Install the LAMP Stack on Ubuntu 26.04

Vishnu
By Vishnu
| Updated: Apr 26, 2026
Install the LAMP Stack on Ubuntu 26.04

The LAMP stack—Linux, Apache, MySQL, PHP—remains one of the most widely deployed web platforms. It powers WordPress, Drupal, Joomla, and countless custom PHP applications. Ubuntu 26.04 ships stable versions of all components from default repositories, making installation straightforward. This guide focuses on integration: configuring Apache to process PHP, connecting PHP to MySQL, and testing the complete stack.

[!TIP] Real-World Scenario: You’ve got a killer idea for a PHP app, but you’re staring at a blank server. The LAMP stack is the ‘Old Reliable’ of web development—it’s the foundation that powers over 70% of the web. Setting it up on Ubuntu 26.04 is like building a house on solid granite.

TL;DR
  • sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql — install all components
  • sudo mysql_secure_installation — secure MySQL -sudo apache2ctl -M | grep php — verify PHP module is loaded
  • Create info.php to test PHP processing, then delete it
  • Use PDO to connect PHP to MySQL and test the full stack

Prerequisites

Before you start, you need:

  • Ubuntu 26.04 with sudo access
  • Internet connection to download packages

This guide focuses on tying components together. For deeper configuration of individual components, see:

How do I install the LAMP stack?

Install all components in a single command:

bash
sudo apt update
sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql

This installs:

  • Apache 2.4 — web server
  • MySQL Server — database
  • PHP 8.5 — scripting language
  • libapache2-mod-php — Apache PHP module
  • php-mysql — PHP MySQL extension

Both Apache and MySQL start automatically and are enabled at boot. PHP runs as an Apache module, not a persistent service.

Verify services are running:

bash
sudo systemctl status apache2
sudo systemctl status mysql

Both should report active (running).

Secure MySQL immediately:

bash
sudo mysql_secure_installation

Follow the prompts to set a root password, remove anonymous users, and disallow remote root login. Skipping this leaves MySQL in an insecure state.

How do I configure Apache to process PHP?

After installing libapache2-mod-php, Apache can hand .php files to the PHP interpreter. Verify this with a test page.

Confirm PHP Module is Enabled

bash
sudo apache2ctl -M | grep php

The output should include php_module.

Create a PHP Info Test Page

bash
echo '<?php phpinfo(); ?>' | sudo tee /var/www/html/info.php

The phpinfo() function prints the active PHP configuration, confirming Apache is invoking PHP rather than serving raw source.

Restart Apache:

bash
sudo systemctl restart apache2

Test in Browser

Visit http://server-ip/info.php. You should see the PHP information page showing version 8.5, loaded extensions, and configuration directives.

Warning

Delete the test file after verification:

bash
sudo rm /var/www/html/info.php

The phpinfo() page exposes server configuration details that attackers can use for reconnaissance.

If the browser displays raw PHP source instead of the rendered page, Apache isn’t passing files to PHP. Verify libapache2-mod-php is installed and the module is enabled.

How do I connect PHP to MySQL?

The php-mysql package provides mysqli and PDO_MySQL extensions—the two standard PHP interfaces for MySQL communication.

Verify MySQL Extension is Loaded

bash
php -m | grep -i mysql

You should see mysqli, mysqlnd, and pdo_mysql. If not, confirm php-mysql is installed and restart Apache.

Create a Test Database and User

Log in to MySQL as root:

bash
sudo mysql

Create a database and dedicated user:

sql
CREATE DATABASE app_db;
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'StrongPassword123!';
GRANT ALL PRIVILEGES ON app_db.* TO 'app_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

This follows the principle of least privilege—the user has full access only to app_db.

Add Sample Data

bash
mysql -u app_user -p app_db

Enter the password, then:

sql
CREATE TABLE greetings (id INT AUTO_INCREMENT PRIMARY KEY, message VARCHAR(255));
INSERT INTO greetings (message) VALUES ('Hello from LAMP stack');
EXIT;

Write a PHP Database Test Script

Create a script that queries the database:

bash
sudo nano /var/www/html/dbtest.php

Paste this content:

php
<?php
$host     = 'localhost';
$db       = 'app_db';
$user     = 'app_user';
$pass     = 'StrongPassword123!';
$charset  = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
try {
    $pdo = new PDO($dsn, $user, $pass);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $pdo->query('SELECT message FROM greetings');
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        echo htmlspecialchars($row['message']) . "<br>";
    }
} catch (PDOException $e) {
    echo 'Database connection failed: ' . $e->getMessage();
}
?>

This uses PDO (PHP Data Objects), the modern recommended interface for database access.

Test the Full Stack

Visit http://server-ip/dbtest.php. If everything works, the page displays the greeting message from the database.

Information

If the browser displays “Hello from LAMP stack”, then Apache, PHP, and MySQL are all communicating correctly. Your LAMP stack is fully operational.

How do I troubleshoot the stack?

Apache Returns PHP Source Code

Apache isn’t recognizing .php files as PHP scripts:

bash
sudo apt install libapache2-mod-php
sudo a2enmod php8.5
sudo systemctl restart apache2

Database Connection Failed (2002)

PHP cannot reach MySQL. Verify MySQL is running and bound to localhost:

bash
sudo systemctl status mysql
sudo ss -tlnp | grep 3306

MySQL should be listening on 127.0.0.1:3306.

Access Denied for User

The username, password, or host doesn’t match. Check the grants:

sql
SELECT User, Host FROM mysql.user WHERE User = 'app_user';
SHOW GRANTS FOR 'app_user'@'localhost';

Blank Page with No Output

PHP errors may be suppressed. Temporarily enable error display at the top of your script:

php
ini_set('display_errors', 1);
error_reporting(E_ALL);

Check the Apache error log:

bash
sudo tail -n 50 /var/log/apache2/error.log

How do I secure my LAMP server?

Remove Test Files

Delete any testing scripts that leak configuration details:

bash
sudo rm /var/www/html/info.php
sudo rm /var/www/html/dbtest.php

Configure Firewall

Allow only necessary ports:

bash
sudo ufw allow in "Apache Full"
sudo ufw enable
sudo ufw status

The “Apache Full” profile opens HTTP (80) and HTTPS (443).

Enable HTTPS

Serving production sites over plain HTTP is unacceptable. Install a free Let’s Encrypt certificate once you have a domain:

bash
sudo apt install certbot python3-certbot-apache
sudo certbot --apache

Apply Least Privilege to Database Users

Never let web applications log in as MySQL root. Grant each application only the specific privileges on the specific database it needs—exactly as demonstrated above with app_user.

Keep the Stack Patched

Apply security updates regularly:

bash
sudo apt update && sudo apt upgrade

Enable unattended security upgrades for automatic critical patches:

bash
sudo apt install unattended-upgrades

Summary

  • Install LAMP with sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql
  • Secure MySQL immediately with sudo mysql_secure_installation
  • Verify PHP processing with a test page, then delete it
  • Use PDO to connect PHP to MySQL with dedicated, scoped users
  • Remove test files, configure UFW firewall, and enable HTTPS for production
  • Keep components patched with regular updates

FAQ

What is the difference between LAMP and LEMP? LAMP uses Apache; LEMP uses Nginx (the “E” is pronounced “engine-x”). Apache is more flexible with per-directory .htaccess configuration, making it friendlier for traditional PHP apps. Nginx generally uses less memory under high concurrency and performs well as a reverse proxy. Both are fully supported on Ubuntu 26.04.

Can I use MariaDB instead of MySQL? Yes. MariaDB is a community-developed fork of MySQL that is drop-in compatible. Install mariadb-server instead of mysql-server, and PHP’s php-mysql extension works with both. Many distributions recommend MariaDB as the default.

Which PHP version ships with Ubuntu 26.04? Ubuntu 26.04 ships PHP 8.5. Verify with php -v after installation. For different versions, the Ondrej Sury PPA provides multiple PHP versions installable side by side.

How do I serve multiple websites from the same LAMP server? Apache supports virtual hosts. Create separate configuration files in /etc/apache2/sites-available/ for each site, enable with sudo a2ensite sitename.conf, and reload Apache.

Do I need to run mysql_secure_installation on a development machine? Yes. The script sets a root password, removes anonymous accounts, and deletes the test database. Building the habit of hardening MySQL every time prevents accidents when configurations are copied to production servers.