Linux VPS

Configure Apache virtual hosts on Ubuntu and AlmaLinux

Install Apache, host several websites on one server with name-based virtual hosts, enable PHP-FPM and .htaccess, switch to the event MPM and add free HTTPS with Certbot.

IntermediateLinux VPS3 min read

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 (service apache2, 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.html

Add a virtual host

  1. Create the config: on Ubuntu /etc/apache2/sites-available/example.com.conf, on AlmaLinux /etc/httpd/conf.d/example.com.conf.
  2. 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.

  3. On Ubuntu enable the site and mod_rewrite:
    sudo a2ensite example.com.conf && sudo a2enmod rewrite
  4. Test and reload:
    sudo apachectl configtest && sudo systemctl reload apache2

    Use httpd instead of apache2 on 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:

  1. 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
  2. AlmaLinux: install php-fpm, enable it with systemctl enable --now php-fpm; the bundled php.conf already routes PHP to FPM, and event is the default MPM.

Add HTTPS

  1. Install Certbot's Apache plugin: sudo apt install -y certbot python3-certbot-apache (AlmaLinux: enable EPEL, then sudo dnf install -y certbot python3-certbot-apache mod_ssl).
  2. Run sudo certbot --apache -d example.com -d www.example.com and choose the HTTP-to-HTTPS redirect.

What to check afterwards

  • sudo apachectl -S lists 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 ServerName does not match, so Apache uses the first (default) virtual host. Check apachectl -S.
  • 403 Forbidden: missing Require all granted, wrong file permissions, or SELinux contexts. On AlmaLinux run sudo restorecon -Rv /var/www.
  • .htaccess ignored: AllowOverride is None or 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.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.