How to Install LAMP Stack on Ubuntu 26.04
When you spin up a new server to run a PHP application or a CMS such as WordPress, Joomla, or Drupal, the LAMP stack is one of the most familiar starting points. LAMP stands for Linux, Apache, MySQL, and PHP, and the four pieces work together to serve dynamic web pages over HTTP.
This guide explains how to install and configure a complete LAMP stack on Ubuntu 26.04. By the end you will have Apache serving HTTP traffic, MySQL 8.4 running as the database, and PHP 8.5 processing dynamic pages through mod_php.
Quick Reference
| Task | Command |
|---|---|
| Update package index | sudo apt update |
| Install Apache | sudo apt install apache2 |
| Install MySQL | sudo apt install mysql-server |
| Install PHP with Apache | sudo apt install php libapache2-mod-php php-mysql |
| Secure MySQL | sudo mysql_secure_installation |
| Restart Apache | sudo systemctl restart apache2 |
| Test Apache config | sudo apache2ctl configtest |
| Allow HTTP/HTTPS | sudo ufw allow 'Apache Full' |
| Enable a site | sudo a2ensite example.com |
| Disable default site | sudo a2dissite 000-default |
Prerequisites
Before installing the stack, make sure you have:
- A server running Ubuntu 26.04 with a user with sudo privileges
. - The UFW firewall enabled. If you have not configured it yet, see ufw Command in Linux: Uncomplicated Firewall Reference
.
Step 1: Install Apache
Apache is in the default Ubuntu 26.04 repositories. Refresh the package index and install it:
Terminal
sudo apt update
sudo apt install apache2
The service starts automatically after the install. Confirm it is running:
Terminal
sudo systemctl status apache2
The output shows the service as active (running). If UFW is enabled, allow HTTP and HTTPS through the firewall:
Terminal
sudo ufw allow 'Apache Full'
Open http://your_server_ip in a browser. You should see the default Apache welcome page, which confirms that Apache is serving requests.
For an in-depth walkthrough of the install and the directory layout, see How to Install Apache on Ubuntu 26.04
.
Step 2: Install MySQL
The database tier handles persistence. Install the MySQL server package:
Terminal
sudo apt install mysql-server
Once the install completes, run the security script. It walks you through removing anonymous users, disabling remote root login, and dropping the test database:
Terminal
sudo mysql_secure_installation
Answer Y to each hardening question. The validate password component is optional; skip it if you plan to manage credentials yourself.
To verify that the service is running:
Terminal
sudo systemctl status mysql
For details on creating users and granting privileges, see How to Install MySQL on Ubuntu 26.04
.
Step 3: Install PHP
Apache integrates with PHP through the libapache2-mod-php module, which lets Apache run PHP code in-process. Install PHP, the Apache module, and the MySQL driver:
Terminal
sudo apt update
sudo apt install php libapache2-mod-php php-mysql
On Ubuntu 26.04 this pulls in PHP 8.5. Confirm the version:
Terminal
php -v
The output starts with PHP 8.5.x. Restart Apache so the PHP module loads:
Terminal
sudo systemctl restart apache2
If you need extra extensions such as php-curl, php-gd, php-mbstring, php-xml, or php-zip, install them now:
Terminal
sudo apt install php-curl php-gd php-mbstring php-xml php-zip
For information on additional modules and switching PHP versions, see How to Install PHP on Ubuntu 26.04
.
Step 4: Configure an Apache Virtual Host
The default Apache configuration serves files from /var/www/html. For a real site you usually want a dedicated document root and a virtual host. Create the directory and a small placeholder:
Terminal
sudo mkdir -p /var/www/example.com
sudo chown -R $USER:$USER /var/www/example.com
echo "<h1>example.com is live</h1>" > /var/www/example.com/index.html
Create a new virtual host file:
Terminal
sudo nano /etc/apache2/sites-available/example.com.conf
Paste the following content. Replace example.com with your domain or your server IP if you do not have a domain yet:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin [email protected]
DocumentRoot /var/www/example.com
<Directory /var/www/example.com>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
AllowOverride All lets you use per-directory .htaccess files, which is required by many PHP applications such as WordPress for clean URL rewrites. Enable the site and disable the default one:
Terminal
sudo a2ensite example.com
sudo a2dissite 000-default
Make sure mod_rewrite is enabled if your application uses it:
Terminal
sudo a2enmod rewrite
Test the configuration before reloading Apache:
Terminal
sudo apache2ctl configtest
The output should end with Syntax OK. Reload Apache to apply the change:
Terminal
sudo systemctl reload apache2
Step 5: Test the PHP Setup
Create a PHP info file to confirm that Apache executes PHP:
Terminal
echo "<?php phpinfo();" | sudo tee /var/www/example.com/info.php
Open http://example.com/info.php (or http://your_server_ip/info.php) in a browser. You should see the PHP information page that lists the PHP version, loaded extensions, and configuration directives. If the browser downloads the file or shows the source code, the PHP module is not loaded; reinstall libapache2-mod-php and restart Apache.
Once you confirm PHP is working, remove the info file. It exposes details about your environment that should not be publicly readable:
Terminal
sudo rm /var/www/example.com/info.php
Step 6: Test the MySQL Connection from PHP
To confirm that PHP can talk to MySQL, create a test database and user:
Terminal
sudo mysql
In the MySQL prompt, run:
CREATE DATABASE lamp_test;
CREATE USER 'lamp_user'@'localhost' IDENTIFIED BY 'changeme';
GRANT ALL PRIVILEGES ON lamp_test.* TO 'lamp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Warning
changeme with a strong value, and do not commit credentials to version control. Store secrets in environment variables or a .env file outside the repository.Create a small connection test:
Terminal
sudo nano /var/www/example.com/db-test.php
Add the following content:
<?php
$mysqli = new mysqli("localhost", "lamp_user", "changeme", "lamp_test");
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
echo "Connected to MySQL " . $mysqli->server_info;
$mysqli->close();
Open http://example.com/db-test.php. The page prints Connected to MySQL 8.4.x when the credentials and the PHP MySQL driver are working. Remove the file once you confirm the connection:
Terminal
sudo rm /var/www/example.com/db-test.php
Troubleshooting
Apache shows the default page after editing the virtual host
The default site is still enabled. Run sudo a2dissite 000-default, then sudo a2ensite example.com, and reload Apache.
PHP files download instead of executing
The Apache PHP module is not loaded. Reinstall it with sudo apt install --reinstall libapache2-mod-php, enable it with sudo a2enmod php8.5, and restart Apache.
AllowOverride All is ignored
The <Directory> block in the virtual host must include AllowOverride All. Confirm that the path matches your DocumentRoot and run sudo apache2ctl configtest after the change.
.htaccess rewrites do not work
The rewrite module is not enabled. Run sudo a2enmod rewrite and restart Apache. Verify that the <Directory> block sets AllowOverride All.
MySQL connection refused from PHP
Confirm that the php-mysql package is installed, that the user has been granted privileges on the target database, and that the password in the PHP code matches the one used in CREATE USER.
FAQ
What is the difference between LAMP and LEMP?
LAMP uses Apache as the web server, while LEMP uses Nginx. Apache supports .htaccess overrides and mod_php, which makes shared hosting and many PHP CMS workflows simpler. Nginx is event-driven and tends to use less memory under high concurrency. For an Nginx-based stack, see How to Install LEMP Stack on Ubuntu 26.04
.
Can I use MariaDB instead of MySQL?
Yes. MariaDB is a drop-in replacement for MySQL and uses the same client tools, the same protocol, and the same SQL syntax for the cases covered in this guide.
Should I use mod_php or PHP-FPM with Apache?
mod_php is the simplest setup and is fine for many sites. For higher concurrency or when you run multiple PHP versions, switch to PHP-FPM with mpm_event and proxy_fcgi. The php-fpm package and the proxy_fcgi Apache module handle the connection.
How do I add HTTPS to the site?
Install Certbot and request a Let’s Encrypt certificate for your domain. The Certbot Apache plugin updates the virtual host automatically and sets up redirection from HTTP to HTTPS.
Next Steps
You now have a working LAMP stack on Ubuntu 26.04. From here you can deploy a PHP application, create additional Apache virtual hosts
for more sites, or add HTTPS using Let’s Encrypt.




