Installing Apache, MariaDB and PHP
The classic web stack still runs a large share of the internet. On a dedicated machine it can be sized to the hardware instead of to a shared plan.
Which versions will I get?
Those shipped by your distribution, which receive security fixes for the life of the release. On Debian 13 that means Apache 2.4, MariaDB 11.8 and PHP 8.4; Ubuntu 24.04 ships PHP 8.3 and MariaDB 10.11; AlmaLinux and Rocky Linux 10 offer PHP 8.3 with newer versions as application streams. Unless an application needs a specific release, the distribution packages are the lowest-maintenance choice.
How do I install it on Debian or Ubuntu?
One apt line installs the three parts plus the PHP extensions most applications want. Use PHP-FPM rather than the old Apache module: it runs PHP in its own pool of processes and lets Apache use the efficient event worker.
apt update apt install apache2 mariadb-server php-fpm php-mysql php-curl php-gd php-intl php-mbstring php-xml php-zip a2enmod proxy_fcgi setenvif rewrite headers http2 a2enconf php*-fpm systemctl restart apache2
And on AlmaLinux or Rocky Linux?
The package names differ slightly, and services are not started automatically. Open the web ports in firewalld as well.
dnf install httpd mariadb-server php-fpm php-mysqlnd php-gd php-intl php-mbstring php-xml
systemctl enable --now httpd mariadb php-fpm
firewall-cmd --permanent --add-port={80,443}/tcp
firewall-cmd --reload
On this family Apache talks to PHP-FPM through a socket configured in /etc/httpd/conf.d/php.conf, and SELinux is enforcing. If the application must connect to a remote API or database, allow it with setsebool -P httpd_can_network_connect 1.
How do I secure the database?
Run the bundled hardening script, then create one database user per application instead of using root. MariaDB on Debian and Ubuntu lets the system root user log in through the local socket without a password, which is safe as long as nothing else uses that account.
mariadb-secure-installation mariadb -e "CREATE DATABASE shop_prod CHARSET utf8mb4; CREATE USER 'shop'@'localhost' IDENTIFIED BY 'long-random-secret'; GRANT ALL ON shop_prod.* TO 'shop'@'localhost';"
Leave MariaDB listening on localhost only, its default. If an application on another server needs it, connect through a WireGuard tunnel rather than opening port 3306 to the internet.
How do I add a site with HTTPS?
Create a virtual host for the domain, point the domain's A and AAAA records at your server, then request a free certificate with certbot. The certificate renews itself through a timer.
cat > /etc/apache2/sites-available/shop.example.org.conf <<'EOF'
<VirtualHost *:80>
ServerName shop.example.org
DocumentRoot /var/www/shop/public
<Directory /var/www/shop/public>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
EOF
a2ensite shop.example.org && systemctl reload apache2
apt install python3-certbot-apache
certbot --apache -d shop.example.org
How should I tune it for a dedicated machine?
Size PHP-FPM and MariaDB to the memory you have, which is the main advantage over shared hosting. A rough plan for a server with 64 GB DDR5:
| Component | Setting | Starting value |
|---|---|---|
| MariaDB | innodb_buffer_pool_size | About half of RAM if the database lives here, so 32G |
| PHP-FPM | pm.max_children | Free RAM for PHP divided by average process size, often 80 to 150 |
| PHP | opcache.memory_consumption | 256 to 512 MB for large codebases |
| Apache | MaxRequestWorkers (event MPM) | 400 or more; threads are cheap |
Measure the average PHP process size under real traffic with ps -C php-fpm8.4 -o rss= (adjust the version) and adjust. Keep the slow query log on with long_query_time = 1; most performance work on a LAMP site starts there.
Which server should a LAMP site run on?
For most sites, a machine with fast cores and NVMe drives matters more than core count. ESSENTIAL-S with the AMD Ryzen 7 9700X suits a busy shop or forum at $46.20 a month, and BUSINESS-2 2024 adds more memory for a large database. Sites that outgrow one machine usually split the database onto its own server first; see High-traffic websites for the next steps.
Machines for LAMP websites
| Model | Processor | Cores | Memory | Drives | Port | Per month |
|---|---|---|---|---|---|---|
| ESSENTIAL-S | AMD Ryzen 7 9700X | 8 | 64 GB | 2x 512 GB SSD NVMe | 1 Gbps unmetered | $46.20 |
| ESSENTIAL-M | AMD Ryzen 9 9900X | 12 | 64 GB | 2x 512 GB SSD NVMe | 1 Gbps unmetered | $70.80 |
| BUSINESS-1 2024 | AMD EPYC 4244P | 6 | 32 GB | 2x 960 GB SSD NVMe | 3 Gbps unmetered | $81.60 |
| BUSINESS-2 2024 | AMD EPYC 4344P | 8 | 64 GB | 2x 960 GB SSD NVMe | 3 Gbps unmetered | $109.80 |
Related pages
First steps with a new server
The first hour after delivery: log in, check the hardware matches the order, update, add a user, lock down SSH and set a firewall.
Read moreHigh-traffic websites
Machines with fast cores and guaranteed 3 to 5 Gbps ports for news sites, forums, APIs and busy content platforms.
Read moreE-commerce shops
Single machines and two node setups for WooCommerce, Magento, PrestaShop and headless shops that must stay fast at checkout.
Read moreDebian
Debian 13 or 12: a lean, conservative base that stays out of the way on machines you want to forget about.
Read moreQuestions and answers
Should I use mod_php or PHP-FPM?
PHP-FPM. It runs PHP in separate pools and lets Apache use the event worker, which handles many more connections with less memory.
Is MySQL or MariaDB installed?
The guide uses MariaDB, the default in Debian and Enterprise Linux. MySQL can be installed instead from its own repository if an application requires it.
Where does the HTTPS certificate come from?
Point the domain at your server, install certbot with its Apache plugin and run certbot --apache -d with your domain. Renewal runs automatically.
Is a hosting control panel part of the server?
Licensed panels are not included with the rental. You can install free panels or manage sites directly as shown here.
How much memory should MariaDB use?
On a server dedicated to the site and its database, start the InnoDB buffer pool at about half of RAM and adjust from the slow query log.