Apache virtual hosts let one server host many websites, each with its own domain, document root and logs. Apache picks the right site by reading the domain the visitor requested. This guide installs Apache and sets up virtual hosts on both Debian-style and RHEL-style systems.
Before you start
- A Linux VPS with root or sudo access.
- Domains with A records pointing to the server.
- Ports 80 and 443 open in the firewall.
On cPanel servers, virtual hosts are generated automatically – do not edit them by hand. This guide is for self-managed servers.
Install Apache
- Ubuntu/Debian:
sudo apt install -y apache2(serviceapache2, configs in/etc/apache2/). - AlmaLinux/Rocky:
sudo dnf install -y httpd && sudo systemctl enable --now httpd(configs in/etc/httpd/).
Create the site folder
sudo mkdir -p /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/example.com/public_html
echo 'It works' > /var/www/example.com/public_html/index.htmlAdd a virtual host
- Create the config: on Ubuntu
/etc/apache2/sites-available/example.com.conf, on AlmaLinux/etc/httpd/conf.d/example.com.conf. - Add:
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com/public_html <Directory /var/www/example.com/public_html> AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined </VirtualHost>On AlmaLinux replace
${APACHE_LOG_DIR}with/var/log/httpd. - On Ubuntu enable the site and mod_rewrite:
sudo a2ensite example.com.conf && sudo a2enmod rewrite - Test and reload:
sudo apachectl configtest && sudo systemctl reload apache2Use
httpdinstead ofapache2on AlmaLinux.
AllowOverride All enables .htaccess files, which most PHP apps expect.
Use the event MPM with PHP-FPM
The event MPM handles many connections with less memory than prefork. Pair it with PHP-FPM:
- Ubuntu:
sudo apt install -y php-fpm sudo a2dismod php* mpm_prefork; sudo a2enmod mpm_event proxy_fcgi setenvif sudo a2enconf php*-fpm sudo systemctl restart apache2 - AlmaLinux: install
php-fpm, enable it withsystemctl enable --now php-fpm; the bundledphp.confalready routes PHP to FPM, and event is the default MPM.
Add HTTPS
- Install Certbot's Apache plugin:
sudo apt install -y certbot python3-certbot-apache(AlmaLinux: enable EPEL, thensudo dnf install -y certbot python3-certbot-apache mod_ssl). - Run
sudo certbot --apache -d example.com -d www.example.comand choose the HTTP-to-HTTPS redirect.
What to check afterwards
sudo apachectl -Slists every virtual host and which is the default.- Each domain loads its own site over HTTPS.
- Logs for each site appear in their own files.
Troubleshooting
- Every domain shows the same site: the
ServerNamedoes not match, so Apache uses the first (default) virtual host. Checkapachectl -S. - 403 Forbidden: missing
Require all granted, wrong file permissions, or SELinux contexts. On AlmaLinux runsudo restorecon -Rv /var/www. - .htaccess ignored:
AllowOverrideisNoneor mod_rewrite is disabled. - PHP downloads instead of running: PHP-FPM is not running or the proxy_fcgi config is not enabled.
FAQ about Apache virtual hosts
How many virtual hosts can Apache run?
Hundreds or more; the limit is server resources, not Apache.
What is the difference between name-based and IP-based virtual hosts?
Name-based hosts share one IP and are selected by domain name (with SNI for HTTPS). IP-based hosts need a separate IP per site and are rarely needed today.
Should I use Apache or Nginx?
Apache is ideal when you rely on .htaccess. See the comparison guide for details.
