Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Before You Start

  • A user with sudo rights: see create a sudo user on Ubuntu.
  • An updated system: sudo apt update && sudo apt upgrade.
  • A domain pointing at the server, if you want HTTPS at the end.

Step 1: Install Apache and Open the Firewall

Allow SSH, or your custom SSH port, before you enable ufw, or the firewall cuts off your session:

sudo apt install apache2
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable

Open http://203.0.113.10 with your server's IP address; the Apache default page should load. More in how to install Apache.

Step 2: Install MySQL

sudo apt install mysql-server
sudo mysql_secure_installation

The script offers password strength checks, removes anonymous users and the test database, and disallows remote root login; answer yes to those. On Ubuntu the MySQL root account uses socket authentication, so sudo mysql opens the SQL shell for local administration without a database password. The details are in how to install MySQL on Ubuntu.

On Debian, install mariadb-server instead and run sudo mariadb-secure-installation.

Step 3: Install PHP

sudo apt install php libapache2-mod-php php-mysql
php -v

libapache2-mod-php is the Apache module that runs PHP inside Apache's own processes: the classic LAMP setup. Installing it switches Apache from the event MPM to prefork. php-mysql adds the extension PHP uses to talk to MySQL. Ubuntu 24.04 ships PHP 8.3 and Ubuntu 26.04 ships PHP 8.5. Paths below use 8.3; replace 8.3 with your version. Versions and extensions are covered in how to install PHP on Ubuntu.

Step 4: Test PHP, Then Delete the Test

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

Open http://203.0.113.10/info.php in a browser. The page shows the PHP version and, under Server API, Apache 2.0 Handler (Apache 2 Handler from PHP 8.4 on); with PHP-FPM it reads FPM/FastCGI. Delete the file right away, because it shows paths and settings to anyone who finds it:

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

Step 5: Create a Database for Your Site

sudo mysql

In the SQL shell:

CREATE DATABASE app;
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'STRONG_PASSWORD';
GRANT ALL PRIVILEGES ON app.* TO 'app_user'@'localhost';
EXIT;

Type the real password here, not on a shell command line, where it would stay in your history and show in the process list. One user per application, limited to its own database, limits the damage if that application is compromised.

Optional: PHP-FPM Instead of mod_php

mod_php needs the prefork MPM, with one Apache process per connection. PHP-FPM runs PHP as a separate service, lets Apache use the event MPM, and allows a separate pool per site. To switch:

sudo apt install php-fpm
sudo a2dismod php8.3 mpm_prefork
sudo a2enmod mpm_event proxy_fcgi setenvif
sudo a2enconf php8.3-fpm
sudo apachectl configtest
sudo systemctl restart apache2

Use a full restart here rather than a reload: the MPM changes, and a2enmod asks for a restart. Tuning is covered in what is PHP-FPM.

Step 6: Add a Site and HTTPS

Give your site its own folder and configuration, as in how to set up Apache virtual hosts, then get a certificate with Certbot Apache. To run WordPress on this stack, continue with how to install WordPress with Apache.

Check the Whole Stack

systemctl status apache2 mysql
sudo ss -tlnp | grep -E ':80 |:3306 '

Both services should be active. MySQL should listen on 127.0.0.1 only, which is Ubuntu's default; do not open port 3306 in the firewall unless another server needs the database.

Was this answer helpful?
Loading...