Apache virtual hosts


Didn’t exactly pick this all up today, rather this is my refined experience from setting up numerous virtual hosts (vhost) on numerous servers.

What follows requires at least Apache 2.4.

Useful Apache documentation:
Documentation index for current Apache version
Environment variables
Define variable in configuration file
VirtualHost directive \

A few words on what the configuration below is trying to achieve.

  1. A vhost configuration that can becopied from one serrver (e.g. dev) to another (e.g. production) without changes.
    Essentially this means avoiding hard coding names and paths in vhost configuration as much as possible.

  2. All sites are redirected to use SSL.
    For production sites we use LetsEncrypt for SSL Certificates.
    We currently assume other environments are hosted on personal servers of some sort so we use the default snakeoil certificates.
    Per (1) above we avoid hard-coding paths to these certificates.

  3. A consistent vhost configuration across all servers and all sites.
    For example, we want to make sure that all the sites use the same SSL configuration without having to change that configuration for each vhost (see (1)).

To achieve these aims we use a combination of environment and configuration file variables.

Our approach was developed on Debian, if you are using a distribution that is not based on Ubuntu your files may be in different places - but the approach will be the same.

Environment variables

Add environment variables to the Apache envvars file which can be found in /etc/apache2/envvars.

# add the following to the bottom of /etc/apache2/envvars
## Bespoke IT Solutions variables (prefix BITS)
## NB: Apache must be stopped and started (not restart) for changes here to take effect.
##
## Prefix applied to virtual host domain configuration
## local	A local, presumably dev, environment running on local server. Handled in vhost conf
## XX		Some other environment, change XX as necessary. Would need to be handled in vhost conf file.
## prod		Production
export BITS_SERVERNAME_PREFIX=local
[ "$BITS_SERVERNAME_PREFIX" = "prod" ] && {
	## For production sites, define a prefix for the SSL path
	export BITS_SSL_PATH=/etc/letsencrypt/live
} || {
	## Do not define SSL path prefix for non-prod path
	##
	## Append period to prefix for non-prod environments for correct names in vhost conf
	export BITS_SERVERNAME_PREFIX="${BITS_SERVERNAME_PREFIX}."
}

Note the comment in that code - Apache must be stopped and started for changeshere to take effect, a restart will not do it.

Virtual host configuration

What follows requires the Apache ssl and headers modules be enabled.

Create a virtual host configuration file in /etc/apache2/sites-available with contents similar to:

#
# Virtual Host Configuration
#
# VHost ID used for ordering the configuration files (order can be important)
Define VHostId 		010
# The FQDN
Define VDomain		bespoke-it.solutions
# Apache server name. Prefix defined in /etc/apache2/envvars
Define VServerName 	${BITS_SERVERNAME_PREFIX}${VDomain}
# Document root
Define VHostDir 	/var/www/vhosts/${VServerName}
Define VHostLogDir 	${APACHE_LOG_DIR}/vhosts/${VDomain}
# SSL
<IfDefine BITS_SSL_PATH>
	Define VSSLCertFile	${BITS_SSL_PATH}/${VDomain}/fullchain.pem
	Define VSSLCertKey	${BITS_SSL_PATH}/${VDomain}/privkey.pem
</IfDefine>
<IfDefine !BITS_SSL_PATH>
	Define VSSLCertFile	/etc/ssl/certs/ssl-cert-snakeoil.pem
	Define VSSLCertKey	/etc/ssl/private/ssl-cert-snakeoil.key
</IfDefine>

Include bits-vhost.conf

A few words about this configuration:

Some notes about this configuration:

Comment on this article using form below. Requires email login only for authentication. HTML forbidden, Markdown only.