Most LAMP tutorials are the same four commands with a screenshot of the Apache welcome page at the end. That gets you something running. It doesn’t get you something you’d want to put a real site on, and it definitely doesn’t prepare you for the things Ubuntu 26.04 does differently: MySQL 8.4 with the old password plugin switched off, PHP 8.5, and a default install path that quietly drags Apache back to the slowest way of running PHP.
So this post builds the whole stack from a clean Ubuntu 26.04.1 LTS machine, one layer at a time, and every command below was run on that machine for this article. The output is what it actually printed, including the errors. By the end you’ll have Apache serving a proper virtual host, PHP 8.5 running through PHP-FPM, MySQL 8.4 with a dedicated app user, and a test page that proves all three are talking to each other.
What you’ll end up with
These are the versions Ubuntu 26.04 ships in its own repositories, no PPAs, no third-party repos. Worth knowing up front, because a lot of older guides still talk about PHP 8.1 and MySQL 8.0.
| Layer | Package | Version on Ubuntu 26.04 |
|---|---|---|
| Linux | Ubuntu 26.04.1 LTS (Resolute) | kernel 6.x |
| Apache | apache2 |
2.4.66 |
| MySQL | mysql-server |
8.4.11 (LTS) |
| PHP | php-fpm |
8.5.4 |
MariaDB 11.8 is also in the repos if you’d rather use it. Everything here works the same way with MariaDB except the authentication details in step 4, and there’s a note about that in the FAQ.
Before you start
You need an Ubuntu 26.04 server or VM, a user with sudo, and a terminal. A 1 GB VPS is enough to follow along, though MySQL alone sat at around 480 MB of memory right after starting on my test box, so 2 GB is more comfortable for anything real. If you’re new to the command line, our list of essential Linux commands covers everything used here.
Start by refreshing the package index:
sudo apt update
Hit:1 http://security.ubuntu.com/ubuntu resolute-security InRelease
Hit:2 http://archive.ubuntu.com/ubuntu resolute InRelease
Hit:3 http://archive.ubuntu.com/ubuntu resolute-updates InRelease
Hit:4 http://archive.ubuntu.com/ubuntu resolute-backports InRelease
Reading package lists...
Building dependency tree...
Reading state information...
95 packages can be upgraded. Run 'apt list --upgradable' to see them.
On a fresh server, run sudo apt upgrade as well before going further. There’s no point building on top of 95 outdated packages.
Step 1: Install Apache
sudo apt install -y apache2
...
Created symlink '/etc/systemd/system/multi-user.target.wants/apache2.service' → '/usr/lib/systemd/system/apache2.service'.
apache2 -v
Server version: Apache/2.4.66 (Ubuntu)
Server built: 2026-07-06T15:33:20
Ubuntu starts Apache and enables it at boot straight away. Check it anyway, it takes two seconds and tells you if something’s already listening on port 80:
systemctl status apache2 --no-pager
● apache2.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/apache2.service; enabled; preset: enabled)
Active: active (running) since Wed 2026-09-30 08:28:59 PKT; 1s ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 1310 (apache2)
Status: "Processing requests..."
Tasks: 55 (limit: 18831)
Memory: 5.9M (peak: 9.6M)
curl -I http://localhost
HTTP/1.1 200 OK
Server: Apache/2.4.66 (Ubuntu)
Content-Length: 10672
Content-Type: text/html
That 200 is the default Ubuntu welcome page. One more thing worth checking now, because it matters in step 5:
sudo apachectl -V | grep -i mpm
Server MPM: event
The MPM (multi-processing module) decides how Apache handles connections. event is the modern, threaded one and it’s what you want. Keep an eye on it, because the most common way of installing PHP silently swaps it for the older prefork. If systemctl is new to you, our systemd service management guide explains everything you’ll see in these status outputs.
Step 2: Open the firewall with UFW
Apache registers application profiles with UFW when it installs, so you don’t have to remember port numbers:
sudo ufw app list
Available applications:
Apache
Apache Full
Apache Secure
Apache is port 80 only, Apache Secure is 443 only, and Apache Full is both. Go with Full, since you’ll want HTTPS later.
If you’re connected over SSH, allow SSH before you enable the firewall. Skip this and you’ll lock yourself out the moment UFW turns on:
sudo ufw allow OpenSSH
sudo ufw allow 'Apache Full'
Rules updated
Rules updated (v6)
sudo ufw --force enable
Firewall is active and enabled on system startup
sudo ufw status
Status: active
To Action From
-- ------ ----
Apache Full ALLOW Anywhere
Apache Full (v6) ALLOW Anywhere (v6)
A small gotcha I hit on the test machine: the OpenSSH profile only exists if openssh-server is installed. My VM didn’t have it, so UFW replied ERROR: Could not find a profile matching 'OpenSSH'. On a cloud server you’ll almost always have it. If you don’t, sudo ufw allow 22/tcp does the same job. Also, some minimal images don’t include ufw at all, in which case sudo apt install ufw first.
For the wider picture on firewalls, SSH and the rest, our Linux server hardening checklist goes much further than a LAMP post should.
Step 3: Install MySQL 8.4
sudo apt install -y mysql-server
mysql --version
mysql Ver 8.4.11-0ubuntu0.26.04.1 for Linux on x86_64 ((Ubuntu))
systemctl status mysql --no-pager
● mysql.service - MySQL Community Server
Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; preset: enabled)
Active: active (running) since Wed 2026-09-30 08:29:34 PKT; 685ms ago
Main PID: 2157 (mysqld)
Status: "Server is operational"
Tasks: 45 (limit: 18831)
Memory: 479M (peak: 480.8M)
MySQL 8.4 is the current long-term support release, and it’s a bigger jump from 8.0 than the version number suggests. More on that in step 4.
Why “mysql -u root” says access denied
Try logging in the way most old tutorials tell you to, and this happens:
mysql -u root -e 'SELECT 1'
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
Nothing is broken. On Ubuntu, the MySQL root account doesn’t use a password at all. It uses the auth_socket plugin, which checks which Linux user is connecting. If you’re root on the system (or using sudo), you’re root in MySQL. If you’re not, you’re not getting in, no matter what password you type:
sudo mysql -e "SELECT user, host, plugin FROM mysql.user;"
user host plugin
debian-sys-maint localhost auth_socket
mysql.infoschema localhost caching_sha2_password
mysql.session localhost caching_sha2_password
mysql.sys localhost caching_sha2_password
root localhost auth_socket
Leave it like that. It’s more secure than a root password, because there’s no password to guess or leak. Use sudo mysql for admin work and give your applications their own users.
Run mysql_secure_installation
This script turns on password strength checking and removes the leftovers MySQL ships for testing. Here’s the run, trimmed a little, with my answers after each prompt:
sudo mysql_secure_installation
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: y
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
Skipping password set for root as authentication with auth_socket is used by default.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
Notice the line Skipping password set for root. That’s the auth_socket thing again, and it’s expected. MEDIUM is a sensible policy level: strong enough to stop password123, not so strict that it rejects every reasonable password.
Step 4: Create a database and a user for your app
Your application should never connect as root. Give it one database and one user that can only touch that database:
sudo mysql -e "CREATE DATABASE lpf_demo CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;"
sudo mysql -e "CREATE USER 'lpf_app'@'localhost' IDENTIFIED BY 'weakpass';"
ERROR 1819 (HY000) at line 1: Your password does not satisfy the current policy requirements
sudo mysql -e "CREATE USER 'lpf_app'@'localhost' IDENTIFIED BY 'Lpf#Demo2026!';"
sudo mysql -e "GRANT ALL PRIVILEGES ON lpf_demo.* TO 'lpf_app'@'localhost';"
That first error is the password policy from the last step doing its job. Use your own strong password, obviously, not the one in this post.
Now log in as the new user and give it something to work with. We’ll read these rows back from PHP in step 8:
mysql -u lpf_app -p lpf_demo
CREATE TABLE posts (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(120) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO posts (title) VALUES
('Apache is serving pages'),
('MySQL 8.4 is storing data'),
('PHP 8.5 is gluing it together');
mysql -t -u lpf_app -p lpf_demo -e 'SELECT * FROM posts'
+----+-------------------------------+---------------------+
| id | title | created_at |
+----+-------------------------------+---------------------+
| 1 | Apache is serving pages | 2026-09-30 08:39:54 |
| 2 | MySQL 8.4 is storing data | 2026-09-30 08:39:54 |
| 3 | PHP 8.5 is gluing it together | 2026-09-30 08:39:54 |
+----+-------------------------------+---------------------+
The MySQL 8.4 change that breaks older tutorials
Plenty of guides, and plenty of older PHP apps, still create users with mysql_native_password. On MySQL 8.4 that fails:
sudo mysql -e "CREATE USER 'legacy_app'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Lpf#Legacy2026!';"
ERROR 1524 (HY000) at line 1: Plugin 'mysql_native_password' is not loaded
sudo mysql -e 'SHOW PLUGINS' | grep -E 'native|sha2'
sha256_password ACTIVE AUTHENTICATION NULL GPL
caching_sha2_password ACTIVE AUTHENTICATION NULL GPL
sha2_cache_cleaner ACTIVE AUDIT NULL GPL
mysql_native_password DISABLED AUTHENTICATION NULL GPL
The plugin still exists in 8.4, it’s just disabled by default. The default for new users is caching_sha2_password, which is what lpf_app got above. PHP’s mysqlnd driver supports it fine, so modern PHP apps don’t care.
And one more thing that trips people up: the old default_authentication_plugin variable is gone entirely.
mysql -u lpf_app -p -e 'SELECT @@default_authentication_plugin'
ERROR 1193 (HY000) at line 1: Unknown system variable 'default_authentication_plugin'
If you have a genuinely old client that can’t do caching_sha2, you can turn the legacy plugin back on with mysql_native_password=ON under [mysqld] in a config file and restarting MySQL. Treat that as a temporary bridge while you upgrade the client, not a setting to copy into every new server.
Step 5: Install PHP 8.5 with PHP-FPM
Here’s the trap I mentioned. Most tutorials tell you to run sudo apt install php libapache2-mod-php. Even plain sudo apt install php does the same thing on a fresh server, because of how the package is built:
apt-cache depends php8.5
php8.5
|Depends: libapache2-mod-php8.5
|Depends: php8.5-fpm
Depends: php8.5-cgi
Depends: php8.5-common
The first choice is libapache2-mod-php8.5, so that’s what apt picks. mod_php runs PHP inside every Apache process, which only works with the old prefork MPM, so installing it switches Apache away from event. Every Apache process then carries a full PHP interpreter, even when it’s just serving a CSS file.
PHP-FPM runs PHP as its own service instead, and Apache hands PHP requests to it over a socket. Apache stays on the lightweight event MPM, PHP gets its own process pool you can tune separately, and you can restart one without the other. So install FPM explicitly, along with the extensions most PHP apps expect:
sudo apt install -y php-fpm php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip php-intl
php -v
PHP 8.5.4 (cli) (built: Sep 2 2026 14:42:18) (NTS)
Copyright (c) The PHP Group
Built by Ubuntu
Zend Engine v4.5.4, Copyright (c) Zend Technologies
with Zend OPcache v8.5.4, Copyright (c), by Zend Technologies
dpkg -l | grep -c libapache2-mod-php
0
That 0 is what you want to see: no mod_php anywhere. OPcache is included and enabled by default, which is a free performance win. The FPM service is already running:
systemctl status php8.5-fpm --no-pager
● php8.5-fpm.service - The PHP 8.5 FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php8.5-fpm.service; enabled; preset: enabled)
Active: active (running) since Wed 2026-09-30 08:41:00 PKT; 236ms ago
Docs: man:php-fpm8.5(8)
Main PID: 13158 (php-fpm8.5)
Status: "Ready to handle connections"
Connect Apache to PHP-FPM
Apache needs two modules to talk FastCGI, plus the small config file that the php8.5-fpm package ships for it:
sudo a2enmod proxy_fcgi setenvif
Considering dependency proxy for proxy_fcgi:
Enabling module proxy.
Enabling module proxy_fcgi.
Module setenvif already enabled
sudo a2enconf php8.5-fpm
Enabling conf php8.5-fpm.
sudo apachectl configtest
Syntax OK
sudo systemctl reload apache2
sudo apachectl -V | grep -i mpm
Server MPM: event
Still on event. That config file sends every .php request to the FPM socket at /run/php/php8.5-fpm.sock. Get into the habit of running apachectl configtest before every reload. It catches a typo before it takes your site down, not after.
Step 6: Set up a virtual host
You could drop files into /var/www/html and call it done. Don’t. A virtual host per site keeps things separate, gives each site its own logs, and is how you’ll add a second site later without untangling the first. I’m using lamp.test as the name, swap in your real domain.
One design choice here that most guides skip: the web root is a public folder inside the site directory. That leaves a place one level up for config files that Apache can never serve by accident. Step 8 uses it.
sudo mkdir -p /var/www/lamp.test/public
sudo chown -R $USER:www-data /var/www/lamp.test
sudo nano /etc/apache2/sites-available/lamp.test.conf
<VirtualHost *:80>
ServerName lamp.test
ServerAlias www.lamp.test
DocumentRoot /var/www/lamp.test/public
<Directory /var/www/lamp.test/public>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/lamp.test-error.log
CustomLog ${APACHE_LOG_DIR}/lamp.test-access.log combined
</VirtualHost>
-Indexes stops Apache listing folder contents when there’s no index file. AllowOverride All lets .htaccess files work, which WordPress, Laravel and most PHP apps rely on for pretty URLs. Enable the site and disable the default one:
sudo a2ensite lamp.test.conf
Enabling site lamp.test.
sudo a2dissite 000-default.conf
Site 000-default disabled.
sudo apachectl configtest
Syntax OK
sudo systemctl reload apache2
echo '<h1>lamp.test is live</h1>' > /var/www/lamp.test/public/index.html
curl -s -H 'Host: lamp.test' http://localhost/
<h1>lamp.test is live</h1>
The -H 'Host: lamp.test' part tells Apache which site you’re asking for, handy for testing before DNS points at the server. In a browser, add the server’s IP and name to your hosts file, or point a real domain’s A record at it.
Step 7: Test PHP
The classic check is a phpinfo page. It’s useful, and it’s also a gift to attackers, so it gets deleted at the end of this post:
echo '<?php phpinfo();' > /var/www/lamp.test/public/info.php
curl -s -H 'Host: lamp.test' http://localhost/info.php | grep -o 'Server API </td><td class="v">[^<]*' | sed 's/<[^>]*>/ /g'
Server API FPM/FastCGI
FPM/FastCGI is the line that matters. If it says Apache 2.0 Handler, you’re on mod_php. In a browser it looks like this, with the loaded config file and the extra ini files listed near the top:

Step 8: Connect PHP to MySQL
This is the part that proves the stack actually works as a stack. Database credentials go in a file outside the web root, so even a broken PHP setup can’t serve them as plain text:
nano /var/www/lamp.test/config.php
<?php
// Lives outside the web root, so Apache can never serve it as a file.
return [
'dsn' => 'mysql:host=localhost;dbname=lpf_demo;charset=utf8mb4',
'user' => 'lpf_app',
'pass' => 'Lpf#Demo2026!',
];
Then a test page in public/dbtest.php that connects with PDO, reads the versions and prints the rows from step 4. The core of it:
<?php
$config = require __DIR__ . '/../config.php';
try {
$pdo = new PDO($config['dsn'], $config['user'], $config['pass'], [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
$mysql = $pdo->query('SELECT VERSION()')->fetchColumn();
$rows = $pdo->query('SELECT id, title, created_at FROM posts ORDER BY id')->fetchAll(PDO::FETCH_ASSOC);
$ok = true;
} catch (PDOException $e) {
$ok = false;
$error = $e->getMessage();
}
// ...then print PHP_VERSION, PHP_SAPI, $mysql and the rows as HTML
I locked the config file down to 640 so only its owner and group can read it, then loaded the page. It failed:
chmod 640 /var/www/lamp.test/config.php
curl -s -o /dev/null -w '%{http_code}n' -H 'Host: lamp.test' http://localhost/dbtest.php
500
sudo tail -n 1 /var/log/apache2/lamp.test-error.log
[Wed Sep 30 08:41:14.537975 2026] [proxy_fcgi:error] [pid 13353:tid 13376] [client ::1:34084] AH01071: Got error 'PHP message: PHP Warning: require(/var/www/lamp.test/config.php): Failed to open stream: Permission denied in /var/www/lamp.test/public/dbtest.php on line 2; PHP message: PHP Fatal error: Uncaught Error: Failed opening required ...
A good one to hit early, because it catches a lot of people. PHP-FPM runs as www-data. The chown $USER:www-data back in step 6 only applied to what existed at that moment. A file I created afterwards got my own group, asif, so with mode 640 www-data couldn’t read it. The fix is to give the file the right group, and to set the setgid bit on the folders so new files inherit www-data automatically from now on:
sudo chgrp www-data /var/www/lamp.test/config.php
sudo chmod 2750 /var/www/lamp.test
sudo chmod 2755 /var/www/lamp.test/public
ls -ld /var/www/lamp.test /var/www/lamp.test/public; ls -l /var/www/lamp.test/config.php
drwxr-s--- 3 asif www-data 4096 Sep 30 08:41 /var/www/lamp.test
drwxr-sr-x 2 asif www-data 4096 Sep 30 08:41 /var/www/lamp.test/public
-rw-r----- 1 asif www-data 215 Sep 30 08:41 /var/www/lamp.test/config.php
curl -s -o /dev/null -w '%{http_code}n' -H 'Host: lamp.test' http://localhost/dbtest.php
200
The s in drwxr-s--- is the setgid bit. If you need finer control than owner and group, say giving a deploy user access without adding it to www-data, that’s a job for Linux ACLs with setfacl. Here’s the working page:

Step 9: Tune PHP for real apps
PHP’s defaults are conservative. A 2 MB upload limit is the one people hit first, usually when a WordPress theme upload fails for no obvious reason. Here’s what FPM was using out of the box. php-fpm8.5 -i prints the same information as phpinfo, but read through FPM’s own config, so you can check it without a web page:
php-fpm8.5 -i | grep -E '^(memory_limit|post_max_size|upload_max_filesize) '
memory_limit => 128M => 128M
post_max_size => 8M => 8M
upload_max_filesize => 2M => 2M
Rather than editing the big php.ini, drop your changes in their own file. Package upgrades won’t touch it, and you can see all your customisations at a glance:
sudo nano /etc/php/8.5/fpm/conf.d/99-lpf.ini
memory_limit = 256M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 60
expose_php = Off
date.timezone = Asia/Karachi
Set date.timezone to your own zone, obviously. expose_php = Off stops PHP advertising its version in response headers. Test the config and restart FPM:
sudo php-fpm8.5 -t
[30-Sep-2026 08:41:33] NOTICE: configuration file /etc/php/8.5/fpm/php-fpm.conf test is successful
sudo systemctl restart php8.5-fpm
php-fpm8.5 -i | grep -E '^(memory_limit|post_max_size|upload_max_filesize) '
memory_limit => 256M => 256M
post_max_size => 64M => 64M
upload_max_filesize => 64M => 64M
Now the part that confuses a lot of people. Check the same setting from the command line and you get a different answer:
php -r 'echo ini_get("upload_max_filesize"), " ", ini_get("memory_limit"), PHP_EOL;'
2M -1
That’s not a failed change. The CLI and FPM have completely separate config trees, /etc/php/8.5/cli/ and /etc/php/8.5/fpm/. Your website uses FPM. The php command, cron jobs and Composer use the CLI one, which has no memory limit at all by default (-1). When a setting “doesn’t work”, check which one you edited.
Step 10: Lock down the basics
A few small changes that are worth doing on every LAMP server.
Stop Apache announcing its version
curl -sI -H 'Host: lamp.test' http://localhost/dbtest.php | grep -i '^server'
Server: Apache/2.4.66 (Ubuntu)
sudo sed -i 's/^ServerTokens OS/ServerTokens Prod/; s/^ServerSignature On/ServerSignature Off/' /etc/apache2/conf-available/security.conf
sudo apachectl configtest && sudo systemctl reload apache2
Syntax OK
curl -sI -H 'Host: lamp.test' http://localhost/dbtest.php | grep -i '^server'
Server: Apache
It won’t stop a determined attacker, but it stops you showing up in every scan that searches for a specific vulnerable version.
Delete the phpinfo page
rm /var/www/lamp.test/public/info.php
curl -s -o /dev/null -w '%{http_code}n' -H 'Host: lamp.test' http://localhost/info.php
404
Do the same with dbtest.php once you’ve finished with it. Both pages print details about your server that nobody else needs to see.
Keep MySQL off the network
sudo ss -tlnp | grep -E ':80 |:3306 |:33060 '
LISTEN 0 151 127.0.0.1:3306 0.0.0.0:* users:(("mysqld",pid=2157,fd=24))
LISTEN 0 70 127.0.0.1:33060 0.0.0.0:* users:(("mysqld",pid=2157,fd=21))
LISTEN 0 511 *:80 *:* users:(("apache2",pid=14063,fd=4),("apache2",pid=14062,fd=4),("apache2",pid=1310,fd=4))
Ubuntu’s MySQL listens on 127.0.0.1 only, so it’s unreachable from outside even if the firewall were off. Apache and PHP are on the same box, so that’s exactly right. Leave bind-address alone unless you really need remote database access, and if you do, open 3306 to one specific IP, never to everyone.
Add HTTPS
Once a real domain points at the server, Certbot’s Apache plugin gets you a free Let’s Encrypt certificate and edits the virtual host for you:
sudo apt install -y certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.com
I didn’t run this part on the test VM, because Let’s Encrypt has to reach a public domain to validate it. The steps, the renewal timer and the mistakes beginners make are all covered in our Let’s Encrypt and Certbot guide. It uses nginx for its examples, but the renewal side is identical for Apache. After that, adding Fail2ban to block brute-force login attempts is a good next step.
Troubleshooting: the errors you’re most likely to see
All of these came up for real while building this server, so the log lines are exactly what you’ll find in yours. The site’s error log is /var/log/apache2/lamp.test-error.log, and searching logs quickly is a skill that pays for itself here.
503 Service Unavailable
sudo systemctl stop php8.5-fpm
curl -s -o /dev/null -w '%{http_code}n' -H 'Host: lamp.test' http://localhost/dbtest.php
503
sudo tail -n 1 /var/log/apache2/lamp.test-error.log
[Wed Sep 30 08:41:34.249974 2026] [proxy_fcgi:error] [pid 13634:tid 13637] [client ::1:58896] AH01079: failed to make connection to backend: localhost
Apache is fine, but nothing is answering on the PHP-FPM socket. Usually FPM has crashed or failed to start after a bad config change. sudo systemctl status php8.5-fpm will tell you which, and sudo php-fpm8.5 -t finds the config error. Start it again and the 503 is gone.
The browser shows your PHP code instead of running it
sudo a2disconf php8.5-fpm && sudo systemctl reload apache2
curl -s -H 'Host: lamp.test' http://localhost/dbtest.php | head -2
<?php
$config = require __DIR__ . '/../config.php';
This one is dangerous, because Apache is serving your source code to anyone who asks. It means Apache doesn’t know to pass .php files to PHP. Either the php8.5-fpm conf isn’t enabled or proxy_fcgi isn’t loaded. Run sudo a2enconf php8.5-fpm and sudo a2enmod proxy_fcgi, then reload. Look at what leaked, too: the path to config.php. That’s exactly why the credentials live outside the web root.
500 Internal Server Error with “Permission denied”
Covered in step 8. PHP-FPM runs as www-data and can’t read a file it needs. Check the group and mode with ls -l, and fix it with chgrp www-data.
“File not found.” and “Primary script unknown”
curl -s -o /dev/null -w '%{http_code}n' -H 'Host: lamp.test' http://localhost/nope.php
404
sudo tail -n 1 /var/log/apache2/lamp.test-error.log
[Wed Sep 30 08:41:34.462844 2026] [proxy_fcgi:error] [pid 13633:tid 13643] [client ::1:58922] AH01071: Got error 'Primary script unknown'
Apache passed the request to FPM, and FPM couldn’t find the file. Either it doesn’t exist, or DocumentRoot points somewhere other than where you put your files. Double check the path in the virtual host.
Access denied for user ‘root’@’localhost’
That’s ERROR 1698 from step 3. Use sudo mysql, not mysql -u root -p.
Setup confirmation: every check, in one place
Before calling this done, I ran every check on the finished server in one go. Here’s what came back.
| Check | Result |
|---|---|
| Apache, MySQL and PHP-FPM running | ✅ all three active |
| All three start at boot | ✅ all three enabled |
| Apache on the event MPM, no mod_php | ✅ mpm_event_module + proxy_fcgi_module loaded |
| PHP handled by FPM | ✅ Server API FPM/FastCGI, socket at /run/php/php8.5-fpm.sock |
| Virtual host is the one answering | ✅ *:80 lamp.test, default site disabled |
| PHP reads from MySQL as the app user | ✅ dbtest.php returns 200 with all 3 rows |
| MySQL root uses auth_socket, app uses caching_sha2 | ✅ confirmed in mysql.user |
| MySQL not exposed to the network | ✅ listening on 127.0.0.1:3306 only |
| PHP tuning applied to FPM | ✅ 256M memory, 64M uploads |
| Version headers hidden | ✅ Server: Apache |
| phpinfo removed | ✅ info.php returns 404 |
| Firewall on | ✅ UFW active, Apache Full allowed |
The raw output from that run:
systemctl is-active apache2 mysql php8.5-fpm
active
active
active
systemctl is-enabled apache2 mysql php8.5-fpm
enabled
enabled
enabled
sudo apachectl -S 2>&1 | grep lamp.test
*:80 lamp.test (/etc/apache2/sites-enabled/lamp.test.conf:1)
sudo apachectl -M | grep -E 'proxy_fcgi|php|mpm'
mpm_event_module (shared)
proxy_fcgi_module (shared)
ls -l /run/php/
total 4
lrwxrwxrwx 1 root root 30 Sep 30 08:40 php-fpm.sock -> /etc/alternatives/php-fpm.sock
-rw-r--r-- 1 root root 5 Sep 30 08:41 php8.5-fpm.pid
srw-rw---- 1 www-data www-data 0 Sep 30 08:41 php8.5-fpm.sock
curl -sI -H 'Host: lamp.test' http://localhost/ | head -3
HTTP/1.1 200 OK
Date: Wed, 30 Sep 2026 03:43:32 GMT
Server: Apache
mysql -u lpf_app -p -e 'SELECT COUNT(*) AS rows_in_posts FROM lpf_demo.posts'
rows_in_posts
3
sudo ufw status
Status: active
To Action From
-- ------ ----
Apache Full ALLOW Anywhere
Apache Full (v6) ALLOW Anywhere (v6)
And the page itself, loaded in a browser on the test machine:

Frequently asked questions
Should I use MariaDB instead of MySQL?
Either works for most PHP apps. Ubuntu 26.04 has MariaDB 11.8 in the same repos, and sudo apt install mariadb-server replaces step 3. The main difference you’ll notice here is authentication: MariaDB’s root uses unix_socket instead of auth_socket (same idea, different name), and MariaDB never switched to caching_sha2_password, so the step 4 plugin errors don’t apply. Pick one and stick with it, because moving a live site between them later is more work than it looks.
Can I still use mod_php?
Yes. sudo apt install libapache2-mod-php works and it’s simpler, since there’s no separate service to manage. You give up the event MPM and the ability to tune or restart PHP separately. For a small internal tool it’s fine. For anything public or busy, PHP-FPM is the better default.
How do I host more than one site?
Repeat step 6 for each domain: its own folder under /var/www, its own .conf file in sites-available, then a2ensite. Apache picks the right site from the Host header. For stronger separation, each site can also get its own PHP-FPM pool running as its own user, so one compromised site can’t read another’s files.
Do I need phpMyAdmin?
No. sudo mysql does everything it does. If you really want a web interface, don’t leave it open on a public server. Put it behind a login and restrict it to your IP, or reach it through an SSH tunnel instead.
Is LAMP still worth learning when LEMP and containers exist?
Very much so. A huge share of the web still runs on Apache and PHP, WordPress included, and .htaccess support is still the reason a lot of apps assume Apache. The skills carry over directly too. Swap Apache for nginx and you’ve got LEMP, same PHP-FPM, same MySQL. If you’re curious about the nginx side, our nginx load balancer guide is a good place to see how it thinks about requests.
Does this work on Ubuntu 24.04?
The steps are the same, but the versions differ: 24.04 ships PHP 8.3 and MySQL 8.0. So replace 8.5 with 8.3 in the FPM package, service and config paths, and the MySQL 8.4 authentication changes in step 4 won’t apply.
Where this leaves you
You’ve got a LAMP stack that’s built the way you’d actually want to run one: Apache on the event MPM, PHP 8.5 in its own FPM pool, MySQL 8.4 with socket-authenticated root and a least-privilege app user, a proper virtual host, credentials outside the web root, and a firewall in front of all of it. More importantly, you’ve seen what each piece looks like when it breaks, which is most of the job.
From here, install your actual application into /var/www/lamp.test/public (or your real domain), add HTTPS, and set up backups. A nightly mysqldump from a cron job is the least you should do. When something misbehaves, the journalctl commands for php8.5-fpm and mysql, plus the site’s Apache error log, will tell you where to look first.