Skip to content
On this page

Apache2

Usage

Site configuration files can be found in /etc/apache2/sites-available directory. There are symlinked to /etc/apache2/sites-enabled when enabled.

Updating configs

Never restart Apache2 if not necessary! Site configs can be updated with reload command.

To reload configs execute:

bash
service apache2 reload
service apache2 reload
1

Adding new site config

Create new config to /etc/apache2/sites-available directory. To enable new site execute a2ensite config-name.conf and reload Apache.

Disabling site config

To disable the config use a2disite config-name.conf command and reload Apache.

Redirect to HTTPS

Add new VirtualHost block to your apache config. Redirects all traffic from http://sub.example.com to https://sub.example.com.

apache
# Redirect to https://sub.example.com/
<VirtualHost *:80>
	ServerName sub.example.com
	Redirect permanent / https://sub.example.com/
</VirtualHost>
# Redirect to https://sub.example.com/
<VirtualHost *:80>
	ServerName sub.example.com
	Redirect permanent / https://sub.example.com/
</VirtualHost>
1
2
3
4
5

New virtual host for sub domain

Add new VirtualHost block to your apache config.

apache
<IfModule mod_ssl.c>
	<VirtualHost *:443>
		ServerName sub.example.com
		DocumentRoot /home/user/www

		<Directory /home/user/www>
			Require all granted

			# Allows overriding apache options with .htaccess file (recommended)
			AllowOverride all

			# Hide file structure (optional)
			# Options -Indexes
		</Directory>

		# Alias (optional)
		# Alias /images /home/user/path/to/images

		SSLCertificateFile /etc/letsencrypt/live/sub.example.com/fullchain.pem
		SSLCertificateKeyFile /etc/letsencrypt/live/sub.example.com/privkey.pem
		SSLCertificateChainFile /etc/letsencrypt/live/sub.example.com/chain.pem
		Include /etc/letsencrypt/options-ssl-apache.conf
	</VirtualHost>
</IfModule>
<IfModule mod_ssl.c>
	<VirtualHost *:443>
		ServerName sub.example.com
		DocumentRoot /home/user/www

		<Directory /home/user/www>
			Require all granted

			# Allows overriding apache options with .htaccess file (recommended)
			AllowOverride all

			# Hide file structure (optional)
			# Options -Indexes
		</Directory>

		# Alias (optional)
		# Alias /images /home/user/path/to/images

		SSLCertificateFile /etc/letsencrypt/live/sub.example.com/fullchain.pem
		SSLCertificateKeyFile /etc/letsencrypt/live/sub.example.com/privkey.pem
		SSLCertificateChainFile /etc/letsencrypt/live/sub.example.com/chain.pem
		Include /etc/letsencrypt/options-ssl-apache.conf
	</VirtualHost>
</IfModule>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

Virtualhost proxy

Create proxy from localhost:<PORT> to a sub domain with HTTPS.

apache
<VirtualHost *:443>
	ServerName proxy.example.com

	ProxyRequests On
	ProxyPreserveHost On

	ProxyPass / http://127.0.0.1:<PORT>/
	ProxyPassReverse / http://127.0.0.1:<PORT>/

	<Proxy *>
		Order deny,allow
		Allow from all
	</Proxy>

	SSLProxyEngine on
	SSLCertificateFile /etc/letsencrypt/live/proxy.example.com/fullchain.pem
	SSLCertificateKeyFile /etc/letsencrypt/live/proxy.example.com/privkey.pem
	SSLCertificateChainFile /etc/letsencrypt/live/proxy.example.com/chain.pem
	Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
<VirtualHost *:443>
	ServerName proxy.example.com

	ProxyRequests On
	ProxyPreserveHost On

	ProxyPass / http://127.0.0.1:<PORT>/
	ProxyPassReverse / http://127.0.0.1:<PORT>/

	<Proxy *>
		Order deny,allow
		Allow from all
	</Proxy>

	SSLProxyEngine on
	SSLCertificateFile /etc/letsencrypt/live/proxy.example.com/fullchain.pem
	SSLCertificateKeyFile /etc/letsencrypt/live/proxy.example.com/privkey.pem
	SSLCertificateChainFile /etc/letsencrypt/live/proxy.example.com/chain.pem
	Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20