Adding a named site (virtual hosting) to apache

Each website you configure on your apache server gets it’s own small configuration file, which is a major improvement over the old days where one massive file configured all websites on the server. Navigate to the directory /etc/apache2/sites-available and create a new configuration file for your website. You are free to pick a name for your config file, in this example we will configure a site named webshop.

cd/etc/apache2/sites-available
sudo nano webshop.conf

and here is the content of the file, with the following notes:

  • We configure both with and without the www, so we ahve 2 aliases for the site
  • We configure an AllowOverride All, allowing the local site to set all kind of apache options. While this is a handy setting during configuration, you may want to limit what a site can do to your server, especially in hosting situations.
<VirtualHost *:80>

        ServerName www.shop.somedomain.nl
        ServerAlias www.shop.somedomain.nl, shop.somedomain.nl
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/webshop


        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined


        <Directory /var/www/webshop>
                Options FollowSymLinks MultiViews
                AllowOverride All
        </Directory>


</VirtualHost>

Next, we create the actual location on the server that will hold the data, and set default permissions for the webserver

sudo mkdir /var/www/webshop
sudo chown www-data:www-data /var/www/webshop

Finaly, we need to enable the website to tell apache to start responding to requests.

sudo a2ensite webshop
sudo systemctl reload apache2

Many applications like wordpress depend on mod_rewrite to offer you clean url’s in your website. So it may be a good idea to enable that right of the bat too:

sudo a2enmod rewrite
sudo systemctl reload apache2