Apache 2.4 + Varnish Virtual Host Config Examples

posted in: Apache, Joomla, PHP, SSL, Wordpress | 0

Examples below are from a working Debian 8 + Apache 2.4 + MySQL 5.6 + PHP 5.6 + Varnish web server at Google Cloud Platform – Compute Engine.
I use port 8080 to work with varnish set up on port 80. If you don’t use varnish – change ports in config to :80 instead of :8080.

Example default virtual host config

/your-installation-folder/sites-available/default.conf

NameVirtualHost *:8080
<VirtualHost *:8080>
DocumentRoot /var/www

<Directory "/var/www">
allow from all
Options None
</Directory>

</VirtualHost>

Example php website virtual host config

/your-installation-folder/sites-available/php-website.conf

<VirtualHost *:8080>

DocumentRoot /var/www/php-website/
ServerName php-website.org
ServerAlias www.php-website.org

<Directory "/var/www/php-website/">
allow from all
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
</Directory>

</VirtualHost>

Example wordpress website virtual host config

/your-installation-folder/sites-available/wordpress.conf

<VirtualHost *:8080>
DocumentRoot /var/www/wordpress.org
ServerName wordpress.org
ServerAlias www.wordpress.org

<Directory "/var/www/wordpress.org">
allow from all
Options +FollowSymLinks

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

</Directory>

</VirtualHost>

Example multidomain wordpress website virtual host config

/your-installation-folder/sites-available/multidomain.conf

<VirtualHost *:8080>
DocumentRoot /var/www/domain1.org
ServerName domain1.org
ServerAlias *.domain1.org domain2.org *.domain2.org

<Directory "/var/www/domain1.org">
allow from all
Options +FollowSymLinks

#allow cross-origin access from domain2 to files with domain1 links
<IfModule mod_headers.c>
<FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">
Header add Access-Control-Allow-Origin "http://domain2.org"
</FilesMatch>
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

</Directory>

</VirtualHost>

Example multisite wordpress virtual host config

/your-installation-folder/sites-available/multisite.conf

<VirtualHost *:8080>
DocumentRoot /var/www/domain1.org
ServerName domain1.org
ServerAlias *.domain1.org domain2.org *.domain2.org

<Directory "/var/www/domain1.org">
allow from all
Options +FollowSymLinks

#You should use this ifmodule on multisite only
#If your websites share one theme and all it's settings. Otherwise remove it.
<IfModule mod_headers.c>
<FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">
Header add Access-Control-Allow-Origin "http://domain1.org"
Header add Access-Control-Allow-Origin "http://domain2.org"
</FilesMatch>
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
</IfModule>
# END WordPress
</Directory>

</VirtualHost>

Example joomla virtual host config

/your-installation-folder/sites-available/joomla.conf

<VirtualHost *:8080>
DocumentRoot /var/www/joomla.org
ServerName joomla.org
ServerAlias www.joomla.org

<Directory "/var/www/joomla.org">
allow from all
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
## End - Joomla! core SEF Section.
</Directory>

</VirtualHost>

Example SSL virtual host config with varnish reverse proxy

/your-installation-folder/sites-available/ssl.conf

<IfModule mod_ssl.c>

<VirtualHost *:443>
DocumentRoot /var/www/wordpress.org
ServerName wordpress.org
ServerAlias www.wordpress.org

#forward all https traffic to varnish at port 80 and send back to user response from varnish
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:80/
ProxyPassReverse / http://127.0.0.1:80/
RequestHeader set X-Forwarded-Port "443"
RequestHeader set X-Forwarded-Proto "https"

#ssl settings, point to your certificates
SSLEngine On
SSLCertificateFile /etc/ssl/certificate.crt
SSLCertificateKeyFile /etc/ssl/private.key
SSLCACertificateFile /etc/ssl/ca_bundle.crt

#allow http version of website showing content from your https website
#you also need to add ifmodule to your :8080 virtual host without change
<IfModule mod_headers.c>
<FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">
Header add Access-Control-Allow-Origin "http://wordpress.org"
</FilesMatch>
</IfModule>

<Directory "/var/www/wordpress.org">
allow from all
Options +FollowSymLinks
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
</Directory>

</VirtualHost>

</IfModule>
Follow Sam Tyurenkov:
Hi there, I'm a web-designer, marketing and product manager, business developer. I have created this website and about 25 others. I'm also doing various tasks for IT projects besides websites - like mobile games.