LAMP — Linux, Apache, MariaDB/MySQL, PHP — is the classic stack for PHP applications like WordPress, Drupal and Moodle. Apache’s .htaccess support makes it the easiest choice for applications that ship their own rewrite rules. This guide uses PHP-FPM, the modern and faster way to run PHP with Apache.
Ubuntu and Debian
1. Install the packages
apt update
apt install -y apache2 mariadb-server php-fpm php-mysql php-curl php-gd \
php-mbstring php-xml php-zip php-intl php-opcache
2. Connect Apache to PHP-FPM
a2enmod proxy_fcgi setenvif rewrite headers
a2enconf php*-fpm
systemctl restart apache2
3. Secure MariaDB and create a database
mariadb-secure-installation
mariadb -e "CREATE DATABASE app CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'app'@'localhost' IDENTIFIED BY 'a-long-random-password';
GRANT ALL ON app.* TO 'app'@'localhost';"
4. Create a virtual host
mkdir -p /var/www/example.com/public
chown -R www-data:www-data /var/www/example.com
/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>
a2ensite example.com
a2dissite 000-default
apache2ctl configtest && systemctl reload apache2
AllowOverride All lets applications use .htaccess (needed for WordPress permalinks).
5. Firewall and HTTPS
ufw allow 'Apache Full'
apt install -y certbot python3-certbot-apache
certbot --apache -d example.com -d www.example.com --redirect
AlmaLinux and Rocky Linux
dnf install -y httpd mariadb-server php php-fpm php-mysqlnd php-gd php-mbstring \
php-xml php-intl php-opcache
systemctl enable --now httpd mariadb php-fpm
mariadb-secure-installation
firewall-cmd --permanent --add-service={http,https}
firewall-cmd --reload
On this family, PHP-FPM is wired into Apache automatically through /etc/httpd/conf.d/php.conf. Virtual hosts go in /etc/httpd/conf.d/example.com.conf (same <VirtualHost> block; logs in /var/log/httpd/). Keep SELinux enforcing and, if the app writes files, label writable directories:
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/example.com/public/uploads(/.*)?"
restorecon -Rv /var/www/example.com
For HTTPS: dnf install -y certbot python3-certbot-apache && certbot --apache.
Verify
echo "<?php echo 'PHP ' . PHP_VERSION;" > /var/www/example.com/public/info.php
curl http://example.com/info.php
rm /var/www/example.com/public/info.php
Tune for production
- Use the event MPM with PHP-FPM (default on Debian/Ubuntu when
mod_phpisn’t installed):a2dismod mpm_prefork && a2enmod mpm_event. - PHP settings in
/etc/php/*/fpm/php.ini:memory_limit,upload_max_filesize,post_max_size,expose_php = Off. - FPM workers sized to RAM — see WordPress VPS performance tuning.
- Hide versions:
ServerTokens ProdandServerSignature Offin Apache’s security config. - Compression and caching:
a2enmod deflate expires.
LAMP or LEMP?
Nginx (the “E” in LEMP) is lighter under high concurrency and excellent as a reverse proxy; Apache is more convenient when applications rely on .htaccess. Both are fine for most sites. The Nginx variant is covered in installing PHP and MySQL on a VPS.
Next steps
- Host several sites: multiple websites on one VPS.
- Harden the server: secure web hosting on Linux.
- Database details: installing MariaDB.
A 2 GB Ubuntu VPS or Debian VPS runs a LAMP stack for a small site comfortably.