Customize specific virtual host¶
Table of Contents
- Templates explained
- Apply Changes
- Further readings
What is vhost-gen¶
vhost-gen is a python script which is able to dynamically generate Apache 2.2, Apache 2.4 and
Nginx virtual host or reverse proxy configuration files.
It is intended to be used by other means of automation such as change of directories or change of listening ports.
Where do I find templates¶
The latest version of vhost-gen templates are shipped in the Devilbox git directory under
How does it work¶
By default new virtual hosts are automatically generated and enabled by vhost-gen and watcherp
using the vanilla templates which are glued into the webserver Docker images. The used templates
are exactly the same as what you will find in
This ensures to have equal and sane default virtual host for all of your projects. If you want to have a different virtual host configuration for a specific project of yours, you can copy a corresponding template into your project directory and adjust it to your needs.
How to apply templates to a specific project¶
Customizing a virtual host via vhost-gen template is generally done in four steps:
- Retrieve or set template directory value in
- Copy webserver template to project template directory
- Adjust template
- Make Devilbox pick up those changes
Let’s assume the following default values and one project named
|Templates to copy from||
Those assumed settings will result in the following directory paths which must be created by you:
|Project directory path||
|Project template path||
1. Retrieve or set template directory value¶
By default the HTTPD_TEMPLATE_DIR value is
.devilbox. This is defined in the
.env file. Feel free to change it to whatever directory name you prefer, but keep in mind that
it will change the Project template path which you need to create yourself.
For this example we will keep the default value for the sake of simplicity:
The HTTPD_TEMPLATE_DIR value is a global setting and will affect all projects.
2. Copy webserver template to project template directory¶
First you need to ensure that the HTTPD_TEMPLATE_DIR exists wihin you project.
# Navigate to the Devilbox directory host> cd /home/user/devilbox # Create template directory in your project host> mkdir ./data/www/project-1/.devilbox
Then you can copy the templates.
host> cp cfg/vhost-gen/*.yml-example-* ./data/www/project-1/.devilbox
You actually only need to copy the template of your chosen webserver (either Apache 2.2, Apache 2.4 or Nginx), however it is good practice to copy all templates and also adjust all templates synchronously. This allows you to change web server versions and still keep your virtual host settings.
3. Adjust template¶
At this stage you can start adjusting the template. Either do that for the webserver version you
have enabled via HTTPD_SERVER:
/home/user/devilbox/data/www/project-1/.devilbox/nginx.yml or do it for all of them
What exactly to change will be explained later.
4. Make Devilbox pick up those changes¶
Whenever you change a project vhost template or the HTTPD_TEMPLATE_DIR value, you need to restart the Devilbox.
It is also possible to do it without a restart which will be explained later.
Before the templates are explained, have a look at the following table to find out what template needs to be in place for what webserver version.
Nginx stable and mainline share the same template as their syntax has no special differences, whereas Apache 2.2 and Apache 2.4 have slight differences in syntax and therefore require two different templates.
Ensure yaml files are valid¶
Pay close attention that you do not use TAB (
\t) characters for indenting the vhost-gen
yaml files. Some editors might automatically indent using TABs, so ensure they are replaced
with spaces. If TAB characters are present, those files become invalid and won’t work.
You can use the bundled
yamllint binary inside the container to validate your config.
# Navigate to the Devilbox directory host> cd /home/user/devilbox # Enter the PHP container host> ./shell.sh # Go to your project's template directory firstname.lastname@example.org in /shared/httpd $ cd project-1/.devilbox # Check the syntax of apache22.yml email@example.com in /shared/httpd/project-1/.devilbox $ yamllint apache22.yml 108:81 error line too long (90 > 80 characters) (line-length) 139:81 error line too long (100 > 80 characters) (line-length) 140:81 error line too long (84 > 80 characters) (line-length) 142:81 error line too long (137 > 80 characters) (line-length)
Long line errors can safely be ignored.
Every uppercase string which begins with
__ and ends by
__ (such as
__PORT__) is a
variable that will be replaced by a value. Variables can contain a string, a multi-line string or
can also be replaced to an empty value.
There are global variables that are determined by the command line arguments of
itself or are elsewhere replaced by the Devilbox webserver container such as:
vHost type variable¶
There are also two variables that will be replaced according to the type of the vhost - either a normal vhost or a reverse proxy vhost.
The Devilbox always uses a normal vhost by default, so the
__VHOST_DOCROOT__ variable will be
replaced by what the
vhost_type.docroot section provides.
vhost_type.rproxy will be ignored and
__VHOST_PROXY__ will be replaced by an empty
All other variables will be replaced by what is provided in the
All subsections of
features: have corresponding variables in the following form:
|Feature directive||Variable name pattern|
As an example, the contents of the
features.php_fpm: section will be replaced into the
Each vhost-gen template has three main yaml directives:
vhost: directive will contain the final resulting virtual host configuration that will
be applied by the webserver. Each of its containing variables will be substituted and its content
will be copied to a webserver configuration file.
By default the
vhost: section has variables from global scope, from the
section and from the
You can also fully hard-code your webserver configuration without any variables. This way you can specify a fully self-brewed webserver configuration. An example for Apache 2.2 could look like this:
vhost: | <VirtualHost *:80> ServerName example.com CustomLog "/var/log/apache/access.log" combined ErrorLog "/var/log/apache/error.log" DocumentRoot "/shared/httpd/project-1/htdocs" <Directory "/shared/httpd/project-1/htdocs"> DirectoryIndex index.php AllowOverride All Options All RewriteEngine on RewriteBase / Order allow,deny Allow from all </Directory> ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/shared/httpd/project-1/htdocs/$1 </VirtualHost>
rproxy. The Devilbox only makes use of
which holds the definition of a normal vhost. Its content will be replaced into the
rproxy section will be ignored and the
__VHOST_RPROXY__ variable will contain an empty
|vHost Type section||Variable to be replaced into|
This section contains directives that will all be replaced into
|Feature section||Variable to be replaced into|
After having edited your vhost-gen template files, you still need to apply these changes. This can be achieved in two ways:
- Rename your project directory back and forth
- Restart the Devilbox
Rename project directory¶
# Navigate to the data directory host> /home/user/devilbox/data/www # Rename your project to something else host> mv project-1 project-1.tmp # Rename your project to its original name host> mv project-1.tmp project-1
If you want to understand what is going on right now, check the docker logs for the web server.
# Navigate to the devilbox directory host> /home/user/devilbox # Check docker logs host> docker-compose logs httpd httpd_1 | vhostgen: [2018-03-18 11:46:52] Adding: project-1.tmp.loc httpd_1 | watcherd: [2018-03-18 11:46:52] [OK] ADD: succeeded: /shared/httpd/project-1.tmp httpd_1 | watcherd: [2018-03-18 11:46:52] [OK] DEL: succeeded: /shared/httpd/project-1 httpd_1 | watcherd: [2018-03-18 11:46:52] [OK] TRIGGER succeeded: /usr/local/apache2/bin/httpd -k restart httpd_1 | vhostgen: [2018-03-18 11:46:52] Adding: project-1loc httpd_1 | watcherd: [2018-03-18 11:46:52] [OK] ADD: succeeded: /shared/httpd/project-1 httpd_1 | watcherd: [2018-03-18 11:46:52] [OK] DEL: succeeded: /shared/httpd/project-1.tmp httpd_1 | watcherd: [2018-03-18 11:46:52] [OK] TRIGGER succeeded: /usr/local/apache2/bin/httpd -k restart
The directory changes have been noticed and a new virtual host has been created. This time however your new vhost-gen template has been read and the changes have applied.
Renaming a project directory will only affect a single project. In case your change the value of HTTPD_TEMPLATE_DIR it will affect all projects and you would have to rename all project directories. In this case it is much faster to just restart the Devilbox.
Restart the Devilbox¶
Stop the Devilbox and start it up again.
Have a look at the following examples which involve customizing vhost-gen templates: