Name-based Virtual Hosts on Apache

Virtual Hosts are useful if you want to host multiple domains on one host. Here I present one of multiple ways to achieve this.

step.1. create the directory structure

mkdir /etc/httpd/sites-enabled
mkdir /etc/httpd/sites-available

step.2. add line to the apache configuration file

vim /etc/httpd/conf/httpd.conf
Include /etc/httpd/sites-enabled

step.3. create virtual host configuration file

vim /etc/httpd/sites-available/intranet.conf
<VirtualHost *:80>
  Servername intranet.krolaki.eu
  RewriteEngine on
  RewriteCond %{SERVER_NAME} =www.intranet.krolaki.eu [OR]
  RewriteCond %{SERVER_NAME} =intranet.krolaki.eu
  RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<IfModule mod_ssl.c>
  SSLStaplingCache "shmcb:logs/ssl_stapling(32768)"
  <VirtualHost *:443>
    ServerName intranet.krolaki.eu
    ServerAdmin admin@krolaki.eu
    DocumentRoot /var/www/html/intranet/
    ErrorLog /etc/httpd/logs/intranet-error.log
    CustomLog /etc/httpd/logs/intranet-requests.log combined

    <Directory /var/www/html/intranet/>
      Require all granted
      AllowOverride All
      Options FollowSymLinks MultiViews
      <IfModule mod_dav.c>
        Dav off
      </IfModule>
    </Directory>

    SSLProtocol -all +TLSv1.2 +TLSv1.3
    SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
    SSLHonorCipherOrder off
    SSLSessionTickets off
    Header always set Strict-Transport-Security "max-age=31536000"
    SSLUseStapling on
    Protocols h2 http/1.1

    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/krolaki.eu.crt
    SSLCertificateKeyFile /etc/ssl/private/krolaki.eu.key
  </VirtualHost>
</IfModule>

step.4. create the destination dirs

mkdir -p /var/www/html/krolaki.eu

step.5. create the symbolic link and restart the server

ln -s /etc/httpd/sites-available/intranet.conf /etc/httpd/sites-enabled/
systemctl restart httpd

Leave a Reply

Your email address will not be published. Required fields are marked *