Install a LAMP stack on Ubuntu 24.04
A modern LAMP server on Ubuntu 24.04 is Apache running the event MPM, PHP 8.3 handled by PHP-FPM, and MariaDB, all from the standard Ubuntu packages. The whole stack installs in a few minutes on S4 (2 vCPU, 4 GB RAM, 40 GB NVMe) in Europe or P2 anywhere, and this guide takes you from an empty server to a site with HTTPS.
Self-hosted apps guide, updated 2026-12-03
Why install LAMP by hand instead of using an app image?
The console has a WordPress image, but no general LAMP image, because a LAMP server is a base you shape to your own applications. Starting from the clean Ubuntu 24.04 image means you know which modules are active and nothing is preinstalled that you have to remove. If all you want is WordPress, the WordPress image is quicker; for anything else, follow these steps.
The older way of running PHP inside Apache with mod_php forces the prefork MPM, where every connection holds a whole process. The setup below uses mpm_event plus PHP-FPM, so Apache handles many connections with few threads and PHP runs in its own pool that you can size and restart separately.
Which plan and city should a LAMP server use?
Memory decides the plan. Apache with the event MPM is light; MariaDB wants a few hundred MB for its buffer pool; each busy PHP-FPM worker typically takes 40~80 MB.
- S4 (2 vCPU, 4 GB RAM, 40 GB NVMe, $4.25 a month, Europe only): a handful of PHP sites with modest traffic.
- P2 (1 vCPU, 2 GB RAM, 40 GB NVMe): the same idea in any of the 15 cities, including North America and Asia.
- P4 or S8: shops and forums where the database is busy, or when you host many client sites.
Place the server near your visitors. A shop selling in France does well in Paris, a site for Canadian readers in Toronto. Plans cannot be resized in place, so if you are unsure, choose the bigger one; moving later means a new server and a copy (see moving to a bigger plan).
How do you point the domain and open the firewall?
At your DNS provider create an A record for example.com and www.example.com pointing at the server IPv4, for example 203.0.113.10, plus AAAA records for the IPv6 address if you want visitors over IPv6. The DNS guide has the details. Then allow SSH and web traffic:
ufw allow OpenSSH
ufw allow 80/tcp
ufw allow 443/tcp
ufw enableDo not open port 3306. MariaDB should only listen on localhost; on Ubuntu 24.04 it binds to 127.0.0.1 by default in /etc/mysql/mariadb.conf.d/50-server.cnf, and it is best left that way.
How do you install Apache, PHP 8.3 and MariaDB?
Install everything in one command. Ubuntu 24.04 ships PHP 8.3 and MariaDB 10.11:
apt update && apt -y upgrade
apt -y install apache2 mariadb-server php8.3-fpm php8.3-mysql php8.3-curl php8.3-gd php8.3-mbstring php8.3-xml php8.3-zip php8.3-intlDo not install libapache2-mod-php; it would switch Apache back to prefork. Now connect Apache to PHP-FPM and make sure the event MPM is active:
a2dismod mpm_prefork 2>/dev/null
a2enmod mpm_event proxy_fcgi setenvif rewrite headers
a2enconf php8.3-fpm
systemctl restart apache2 php8.3-fpm
apache2ctl -M | grep mpmThe last command should print mpm_event_module (shared). Test PHP with a temporary file: echo "<?php phpinfo();" > /var/www/html/info.php, open http://203.0.113.10/info.php, look for "Server API: FPM/FastCGI", then delete the file again with rm /var/www/html/info.php.
On Rocky Linux or AlmaLinux the equivalent is dnf install httpd mariadb-server php-fpm php-mysqlnd, followed by systemctl enable --now httpd mariadb php-fpm; the event MPM and the FPM handler are already the default there.
How do you secure MariaDB and create a database?
Run the hardening script once:
mysql_secure_installationOn Ubuntu the root database user signs in through the Unix socket, so you can keep that and answer no to switching root to password login. Answer yes to removing anonymous users, disallowing remote root login and removing the test database. Then create one database and one user per application:
mariadb
CREATE DATABASE shop CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'shop'@'localhost' IDENTIFIED BY 'a-long-random-password';
GRANT ALL PRIVILEGES ON shop.* TO 'shop'@'localhost';
FLUSH PRIVILEGES;
EXIT;For a server with 2 GB of RAM, setting innodb_buffer_pool_size = 256M under [mysqld] in /etc/mysql/mariadb.conf.d/50-server.cnf is a sensible start; raise it on bigger plans, then systemctl restart mariadb.
How do you set up a virtual host for each site?
Give every site its own folder and its own file in /etc/apache2/sites-available/:
mkdir -p /var/www/example.com/public
nano /etc/apache2/sites-available/example.com.conf<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public
<Directory /var/www/example.com/public>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>Enable it, disable the default page and check the syntax before reloading:
a2ensite example.com.conf
a2dissite 000-default.conf
apache2ctl configtest
systemctl reload apache2configtest must answer Syntax OK. AllowOverride All lets applications use their own .htaccess rules; set it to None for sites that do not need them, which is slightly faster.
How do you add HTTPS with certbot?
Install the Apache plugin and request a certificate for both names:
apt -y install certbot python3-certbot-apache
certbot --apache -d example.com -d www.example.comCertbot proves you control the domain over port 80, writes a second virtual host file ending in -le-ssl.conf, and offers to redirect HTTP to HTTPS (accept it). Renewal is handled by a systemd timer; test it with certbot renew --dry-run. More background on certificates is in TLS certificates explained.
Which file permissions keep a PHP site safe?
The safest pattern is that your deploy user owns the code and Apache's www-data user can only read it, with write access limited to upload and cache folders:
adduser --disabled-password deploy
chown -R deploy:www-data /var/www/example.com
find /var/www/example.com -type d -exec chmod 750 {} +
find /var/www/example.com -type f -exec chmod 640 {} +
chmod -R g+w /var/www/example.com/public/uploadsNever use chmod 777; it lets any process on the server change your code. For several unrelated sites, create one PHP-FPM pool per site in /etc/php/8.3/fpm/pool.d/ with its own user and listen socket, so a hole in one application cannot read the files of another. Tune pm.max_children in each pool to the RAM you have.
Should you install phpMyAdmin, and how do you update and back up?
phpMyAdmin is convenient but it is a public login page for your database, and bots scan for it all day. If you need it, keep it off the internet: bind it to a virtual host that only answers on localhost and reach it through an SSH tunnel, ssh -L 8080:127.0.0.1:80 root@203.0.113.10. A desktop SQL client over the same tunnel is often simpler still.
Updates arrive through apt. Turn on automatic security updates with apt -y install unattended-upgrades and run apt update && apt -y upgrade yourself for the rest; PHP minor releases restart FPM automatically.
For backups, dump the databases every night and copy them together with /var/www and /etc/apache2 to another place:
mariadb-dump --all-databases --single-transaction | gzip > /root/db-$(date +%F).sql.gzThe backup add-on ($0.85 per month on S4) adds 7 daily disk copies on top; restoring one is a support ticket naming the server and the day. See backups and restore.
What are the usual LAMP errors and fixes?
- The browser downloads PHP files instead of running them: the FPM handler is not enabled. Run
a2enconf php8.3-fpmand reload Apache. - 503 Service Unavailable: PHP-FPM is stopped or its socket path differs. Check
systemctl status php8.3-fpmand that/run/php/php8.3-fpm.sockexists. - "AH00534: Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe":
libapache2-mod-phpslipped in. Remove it withapt purge libapache2-mod-php8.3. - "Access denied for user": the app user was created for
localhostbut the app connects to127.0.0.1over TCP, or the password differs. Match host and password. - Server slows down and kills processes:
pm.max_childrenis too high for the RAM. Checkjournalctl -k | grep -i oom, lower it, and add swap as in swap and memory.
Questions about this guide
Can I run other PHP versions next to 8.3?
Ubuntu 24.04 ships only PHP 8.3. Other versions come from third party package repositories; each version gets its own FPM service, and each virtual host can point at a different socket.
Is Nginx a better choice than Apache?
Both serve PHP well through PHP-FPM. Apache is handy when applications rely on .htaccess files. For an Nginx setup see Nginx and HTTPS.
Can I send email from the LAMP server?
Outgoing port 25 is opened on request through a support ticket. Many sites instead send through an external SMTP relay on port 587. Reverse DNS for the IPv4 is also set by support.
Does the stack work on Arm plans?
Yes. Apache, MariaDB and PHP 8.3 are standard Ubuntu packages on arm64, so the commands are identical on A4 or A8.
How many sites fit on one LAMP server?
Many small sites fit, since idle virtual hosts cost almost nothing. The limit is the memory used by busy PHP workers and the database, so watch free -h during peaks.
Where are the Apache logs?
In /var/log/apache2/, with one error and access log per site if you set them in the virtual host as shown above.