Apache HTTP Server is still the web server many applications are written for — especially anything relying on .htaccess files, such as many PHP applications and older CMS installations. With virtual hosts, one Apache instance serves any number of domains, each with its own files, logs and settings. This guide sets up Apache the modern way: the event MPM with PHP-FPM, which uses far less memory than the old prefork + mod_php combination.
If you are starting fresh and do not need .htaccess, Nginx is usually the lighter choice — see hosting multiple websites with Nginx.
Install Apache and PHP-FPM
sudo apt update
sudo apt install apache2 php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip php-intl -y
Switch Apache to the event MPM and connect it to PHP-FPM:
sudo a2dismod php8.3 mpm_prefork 2>/dev/null
sudo a2enmod mpm_event proxy_fcgi setenvif rewrite headers http2 ssl
sudo a2enconf php8.3-fpm
sudo systemctl restart apache2
Replace 8.3 with your PHP version (php -v; Debian 12 uses 8.2). Check:
apachectl -M | grep -E "mpm|proxy_fcgi"
Open the firewall: sudo ufw allow 'Apache Full'.
How Apache organises sites
| Path | Purpose |
|---|---|
/etc/apache2/sites-available/ |
One config file per site |
/etc/apache2/sites-enabled/ |
Symlinks to enabled sites (a2ensite / a2dissite) |
/etc/apache2/conf-available/ |
Shared snippets (a2enconf) |
/etc/apache2/mods-available/ |
Modules (a2enmod) |
/var/log/apache2/ |
Logs |
Create two sites
sudo mkdir -p /var/www/site-a.com/public /var/www/site-b.com/public
echo "<h1>Site A</h1>" | sudo tee /var/www/site-a.com/public/index.html
echo "<h1>Site B</h1>" | sudo tee /var/www/site-b.com/public/index.html
sudo chown -R $USER:www-data /var/www/site-a.com /var/www/site-b.com
/etc/apache2/sites-available/site-a.com.conf:
<VirtualHost *:80>
ServerName site-a.com
ServerAlias www.site-a.com
DocumentRoot /var/www/site-a.com/public
<Directory /var/www/site-a.com/public>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php8.3-fpm.sock|fcgi://localhost"
</FilesMatch>
ErrorLog ${APACHE_LOG_DIR}/site-a.com.error.log
CustomLog ${APACHE_LOG_DIR}/site-a.com.access.log combined
</VirtualHost>
Create site-b.com.conf the same way, changing names and paths. Then:
sudo a2ensite site-a.com site-b.com
sudo a2dissite 000-default
sudo apachectl configtest && sudo systemctl reload apache2
Apache chooses the virtual host by matching the request’s Host header against ServerName and ServerAlias. Requests that match nothing go to the first virtual host loaded — so it is good practice to keep a default host that returns a blank page or 404 for unknown domains, rather than letting them show one of your real sites.
.htaccess: convenient, but costly
AllowOverride All lets .htaccess files change settings per directory — WordPress permalinks, many PHP frameworks and redirect rules depend on it. The cost: Apache checks for .htaccess in every directory on every request.
If you control the configuration, move the rules into the <Directory> block and set AllowOverride None for better performance. For WordPress, the equivalent of its .htaccess is:
<Directory /var/www/site-a.com/public>
AllowOverride None
Require all granted
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</Directory>
Separate PHP pools per site
For isolation, run each site’s PHP as its own user, so one compromised site cannot read another’s files. Create /etc/php/8.3/fpm/pool.d/site-a.conf:
[site-a]
user = sitea
group = sitea
listen = /run/php/site-a.sock
listen.owner = www-data
listen.group = www-data
pm = ondemand
pm.max_children = 8
pm.process_idle_timeout = 30s
Create the user (sudo adduser --system --group sitea), give it ownership of its files, restart PHP-FPM, and point the site’s SetHandler at /run/php/site-a.sock. pm = ondemand keeps no idle PHP processes for quiet sites, saving memory.
HTTPS with Let’s Encrypt
sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache -d site-a.com -d www.site-a.com --redirect
sudo certbot --apache -d site-b.com -d www.site-b.com --redirect
Certbot creates a -le-ssl.conf virtual host on port 443 and adds a redirect from HTTP. Enable HTTP/2 inside the SSL virtual host or globally:
Protocols h2 http/1.1
Security headers and hardening
In /etc/apache2/conf-available/security.conf:
ServerTokens Prod
ServerSignature Off
TraceEnable Off
And per site (or globally with a2enconf):
Header always set X-Content-Type-Options "nosniff"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set X-Frame-Options "SAMEORIGIN"
Block access to hidden files:
<FilesMatch "^\.">
Require all denied
</FilesMatch>
Tune the event MPM
/etc/apache2/mods-available/mpm_event.conf controls how many connections Apache handles. Defaults suit small servers; for a busier one:
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 0
</IfModule>
With PHP-FPM, Apache threads are light; the real memory limit is PHP-FPM’s pm.max_children. Size that to your RAM (see PHP-FPM tuning).
Enable compression and caching:
sudo a2enmod deflate expires
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/webp "access plus 30 days"
ExpiresByType text/css "access plus 30 days"
ExpiresByType application/javascript "access plus 30 days"
</IfModule>
Apache behind Nginx?
Some setups run Nginx in front of Apache: Nginx serves static files and handles connections, Apache runs .htaccess-dependent PHP apps. It works, but it is two web servers to maintain. Choose it only if you really need .htaccess compatibility together with Nginx features.
Troubleshooting
| Problem | Check |
|---|---|
| Every domain shows the same site | ServerName typo; the site is not enabled; DNS points elsewhere |
| PHP code shown as text | SetHandler block missing; proxy_fcgi not enabled |
| 503 Service Unavailable | PHP-FPM not running or wrong socket path |
| 500 after uploading a site | Invalid .htaccess directive — see the site’s error log |
AH00558: Could not reliably determine the server's fully qualified domain name |
Harmless; add ServerName localhost to /etc/apache2/conf-available/servername.conf and enable it |
sudo apachectl -S # list virtual hosts and which file defines them
sudo tail -f /var/log/apache2/site-a.com.error.log