Install a LAMP stack on Ubuntu 24.04 and 22.04: Apache2, PHP 8, MySQL and phpMyAdmin
Apache2, PHP 8, MySQL or MariaDB and phpMyAdmin on Ubuntu 24.04 LTS or 22.04 LTS: the complete setup step by step, including how to secure it afterwards.
Do you want to install Apache2, PHP 8, MySQL and phpMyAdmin, in other words a complete LAMP stack, on your Ubuntu 24.04 LTS or Ubuntu 22.04 LTS server? This guide walks you through the whole setup step by step, from the web server to a secured database login.
One advantage over Debian: Ubuntu carries both database servers in its package sources, mysql-server as well as mariadb-server, so the choice is yours. Debian ships MariaDB only. This guide covers both variants, all remaining steps are identical.
Prerequisites
You need SSH access with root privileges (or a user with sudo permissions) on a current Ubuntu system. Ubuntu 24.04 LTS "Noble Numbat" and Ubuntu 22.04 LTS "Jammy Jellyfish" are recommended. Ubuntu 20.04 LTS reached the end of its regular support in May 2025 and should no longer be used in production.
Start by refreshing the package list and installing any pending updates:
apt update && apt upgrade -y
Install Apache2 and the base packages
Install the web server together with the helper packages you will need in the steps that follow:
apt install -y nano curl wget unzip ca-certificates apt-transport-https lsb-release gnupg software-properties-common apache2
Then open http://<your-server-ip>/ in your browser. If the Apache2 default page shows up, the web server is already running.
If a firewall is active on the server, open the web server ports:
ufw allow "Apache Full"
Install PHP 8
Which PHP version you get from the package sources depends on your distribution. That is why a command with a fixed version number such as apt install php8.3 fails on Ubuntu 22.04 LTS. This overview shows the version shipped with each release (as of July 2026):
| Distribution | PHP version from the package sources |
|---|---|
| Ubuntu 24.04 LTS | PHP 8.3 |
| Ubuntu 22.04 LTS | PHP 8.1 |
Use the meta packages without a version number instead. They automatically pull in the version that matches your distribution and behave the same way on every Ubuntu:
apt install -y php php-cli php-common php-curl php-gd php-intl php-mbstring php-mysql php-opcache php-readline php-xml php-zip php-bz2 libapache2-mod-php
If you prefer to run PHP through FPM rather than the Apache module, the same principle applies. Here too the meta package reliably gets you there, while a versioned name such as php8.3-fpm only exists on Ubuntu 24.04 LTS:
apt install -y php-fpm
To see which version actually got installed, run:
php -v
If you need a version your Ubuntu release does not carry (PHP 8.4, for example), add the well known PHP repository maintained by Ondřej Surý:
add-apt-repository ppa:ondrej/php
apt update
add-apt-repository stores the signing key automatically as its own keyring file and assigns it to exactly this package source. The once common apt-key command is deprecated and should no longer be used, because a key added that way would be valid for every package source on the system.
Only through this package source do the versioned package names become available for all Ubuntu releases. After that, install the PHP version you want along with the usual extensions:
apt install -y php8.4 php8.4-cli php8.4-common php8.4-curl php8.4-gd php8.4-intl php8.4-mbstring php8.4-mysql php8.4-opcache php8.4-readline php8.4-xml php8.4-zip php8.4-bz2 libapache2-mod-php8.4
If several PHP versions end up running side by side, define which one Apache2 should use:
a2dismod php8.3
a2enmod php8.4
systemctl restart apache2
Use php -v to check which version is active on the command line.
Install and secure the database server
Ubuntu offers you both of the common database servers, in these releases:
| Package | Ubuntu 24.04 LTS | Ubuntu 22.04 LTS |
|---|---|---|
| mysql-server | MySQL 8.0.46 | MySQL 8.0.46 |
| mariadb-server | available | available |
For MySQL, the original from Oracle:
apt install -y mysql-server mysql-client
mysql_secure_installation
For MariaDB, the server that grew out of MySQL and speaks the same protocol and the same query language:
apt install -y mariadb-server mariadb-client
mysql_secure_installation
Install only one of the two variants, because both occupy the same port. For the remaining steps of this guide it makes no difference which one you picked.
The mysql_secure_installation script asks several questions. Current MariaDB versions first ask whether to switch to unix_socket authentication. That option makes sense, because the database user root can then only log in as the system user root. Answer all remaining questions (remove anonymous users, disallow remote login for root, drop the test database, reload the privileges) with y. Set a strong database password as well.
Then check whether the service is running. The name of the systemd unit depends on the server you chose:
systemctl status mysql
systemctl status mariadb
Install phpMyAdmin
You have two options for phpMyAdmin. The phpmyadmin package sits in the package sources of every current Ubuntu version and installs in a single step:
apt install phpmyadmin -y
Convenient, but the version often lags behind what the vendor has released. If you want the current release, download phpMyAdmin directly instead. That is exactly the route the following steps describe:
cd /usr/share && wget https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.zip -O phpmyadmin.zip && unzip phpmyadmin.zip && rm phpmyadmin.zip && mv phpMyAdmin-*-all-languages phpmyadmin && chmod -R 0755 phpmyadmin
Then create the configuration file:
cp /usr/share/phpmyadmin/config.sample.inc.php /usr/share/phpmyadmin/config.inc.php
phpMyAdmin needs a random value as its blowfish_secret, which encrypts the session data stored in the cookie. Generate such a value:
openssl rand -base64 32
Then enter it in the configuration file:
nano /usr/share/phpmyadmin/config.inc.php
The relevant line reads:
$cfg['blowfish_secret'] = 'ENTER_THE_GENERATED_VALUE_HERE';
Connect phpMyAdmin to Apache2
Create a dedicated Apache2 configuration file for phpMyAdmin:
nano /etc/apache2/conf-available/phpmyadmin.conf
The content for phpmyadmin.conf:
#PHPMyAdmin Apache2 configuration
Alias /phpmyadmin /usr/share/phpmyadmin
<Directory /usr/share/phpmyadmin>
Options SymLinksIfOwnerMatch
DirectoryIndex index.php
</Directory>
<Directory /usr/share/phpmyadmin/templates>
Require all denied
</Directory>
<Directory /usr/share/phpmyadmin/libraries>
Require all denied
</Directory>
<Directory /usr/share/phpmyadmin/setup/lib>
Require all denied
</Directory>
Enable the configuration, reload Apache2 and create the temporary directory with the right permissions:
a2enconf phpmyadmin && systemctl reload apache2 && mkdir -p /usr/share/phpmyadmin/tmp/ && chown -R www-data:www-data /usr/share/phpmyadmin/tmp/
Create a database user
Log in to the database as the system user root:
mysql -u root
Now create your own database user. Do not use "root", "admin", "user" or "username" as the name, pick a username of your own and a long, random password:
#IMPORTANT! REPLACE USER AND PASSWORD WITH YOUR OWN CREDENTIALS.
CREATE USER 'USER'@'localhost' IDENTIFIED BY 'PASSWORD';
GRANT ALL PRIVILEGES ON *.* TO 'USER'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
For individual applications you should additionally create users with restricted privileges that may only access their own database:
CREATE DATABASE myapp;
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'PASSWORD';
GRANT ALL PRIVILEGES ON myapp.* TO 'appuser'@'localhost';
FLUSH PRIVILEGES;
You can leave the database console at any time with:
exit
Test the LAMP stack
Open phpMyAdmin at http://<your-server-ip>/phpmyadmin and log in with the user you just created. If the login works, Apache2, PHP and your database server are working together correctly.
You can also verify the PHP integration with a short test file:
echo "<?php phpinfo();" > /var/www/html/info.php
After opening http://<your-server-ip>/info.php, make sure you delete the file again, because it reveals details about your server configuration:
rm /var/www/html/info.php
Security notes for production use
A freshly installed LAMP stack is not secured yet. You should add these points:
- phpMyAdmin is a popular target. Restrict access to your own IP address by using
Require ip 203.0.113.10in the Directory block instead of leaving access open. - Set up HTTPS so that passwords are not transmitted in plain text. Without encryption your credentials can be read on the way to the server.
- Keep phpMyAdmin up to date. Since you installed it manually,
apt upgradedoes not update it automatically. - Create regular database backups and store them off the server.
Common errors and fixes
The browser downloads the PHP file instead of executing it: The PHP module is not active in Apache2. Enable it with a2enmod php8.4 and restart the web server with systemctl restart apache2.
phpMyAdmin reports "The configuration file now needs a secret passphrase": The value for blowfish_secret is missing in config.inc.php. Generate it with openssl rand -base64 32 and enter it there.
"Access denied for user" when logging in to phpMyAdmin: After mysql_secure_installation, the database user root normally uses unix_socket authentication and therefore cannot log in through the web interface. Use the additional user you created.
Apache2 no longer starts after a configuration change: Check the configuration with apachectl configtest, the output names the file and the line of the error.
Frequently asked questions
Should I install MySQL or MariaDB on Ubuntu?
Which PHP version does Ubuntu ship?
How do I get a newer PHP version than the one Ubuntu provides?
Why does the browser download the PHP file instead of executing it?
Why can I not log in to phpMyAdmin as root?
2022-2026 KernelHost GmbH. All rights reserved. This guide is protected by copyright. Republishing it on other websites, in whole, in part or in edited form, is not permitted without our written consent. Quoting with a source credit and a link is expressly welcome.

