devilbox/docs/vhost-gen/customize-specific-virtual-host.rst

417 lines
15 KiB
ReStructuredText
Raw Normal View History

2018-07-25 06:56:45 +00:00
.. include:: /_includes/all.rst
2018-08-06 19:42:03 +00:00
.. _vhost_gen_customize_specific_virtual_host:
2018-03-17 09:14:50 +00:00
2018-07-06 07:30:52 +00:00
*******************************
Customize specific virtual host
*******************************
2018-03-18 16:14:55 +00:00
**Table of Contents**
.. contents:: :local:
2018-04-01 14:51:46 +00:00
vhost-gen
=========
2018-03-18 16:14:55 +00:00
2018-04-01 14:51:46 +00:00
What is vhost-gen
-----------------
2018-03-18 16:14:55 +00:00
2018-04-01 14:51:46 +00:00
``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.
2018-03-18 16:14:55 +00:00
2018-04-01 14:51:46 +00:00
It is intended to be used by other means of automation such as change of directories or change of
listening ports.
2018-03-18 16:14:55 +00:00
2018-04-01 14:51:46 +00:00
.. seealso::
2018-03-18 16:14:55 +00:00
2018-06-05 02:14:24 +00:00
If you intend to use ``vhost-gen`` for your own projects, have a look at its project page and
2018-06-03 18:45:01 +00:00
its sister projects:
2018-04-01 14:51:46 +00:00
2018-07-25 06:56:45 +00:00
* |ext_lnk_project_vhost_gen|
* |ext_lnk_project_watcherd|
* |ext_lnk_project_watcherp|
2018-04-01 14:51:46 +00:00
Where do I find templates
-------------------------
The latest version of vhost-gen templates are shipped in the Devilbox git directory under
2018-08-06 19:42:03 +00:00
``cfg/vhost-gen/``.
2018-04-01 14:51:46 +00:00
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
2018-08-06 19:42:03 +00:00
are exactly the same as what you will find in ``cfg/vhost-gen/``.
2018-04-01 14:51:46 +00:00
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:
1. Retrieve or set template directory value in ``.env``.
2. Copy webserver template to project template directory
3. Adjust template
4. Make Devilbox pick up those changes
Let's assume the following default values and one project named ``project-1``:
+-------------------------------+-------------------------------------------------------+
| Variable | Value |
+===============================+=======================================================+
| Devilbox path | ``/home/user/devilbox`` |
+-------------------------------+-------------------------------------------------------+
2018-08-06 19:42:03 +00:00
| Templates to copy from | ``/home/user/devilbox/cfg/vhost-gen`` |
2018-04-01 14:51:46 +00:00
+-------------------------------+-------------------------------------------------------+
| Project name | ``project-1`` |
+-------------------------------+-------------------------------------------------------+
| :ref:`env_httpd_template_dir` | ``.devilbox`` (default value) |
+-------------------------------+-------------------------------------------------------+
| :ref:`env_httpd_datadir` | ``./data/www`` (default value) |
+-------------------------------+-------------------------------------------------------+
Those assumed settings will result in the following directory paths which must be created by you:
+-------------------------------+-------------------------------------------------------+
| What | Path |
+===============================+=======================================================+
| Project directory path | ``/home/user/devilbox/data/www/project-1/`` |
+-------------------------------+-------------------------------------------------------+
| Project template path | ``/home/user/devilbox/data/www/project-1/.devilbox/`` |
+-------------------------------+-------------------------------------------------------+
1. Retrieve or set template directory value
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
By default the :ref:`env_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: ``.devilxbox``.
.. note::
2018-06-03 18:45:01 +00:00
The :ref:`env_httpd_template_dir` value is a global setting and will affect all projects.
2018-04-01 14:51:46 +00:00
2. Copy webserver template to project template directory
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
First you need to ensure that the :ref:`env_httpd_template_dir` exists wihin you project.
.. code-block:: bash
2018-06-03 18:45:01 +00:00
# Navigate to the Devilbox directory
host> cd /home/user/devilbox
2018-04-01 14:51:46 +00:00
2018-06-03 18:45:01 +00:00
# Create template directory in your project
host> mkdir ./data/www/project-1/.devilbox
2018-04-01 14:51:46 +00:00
Then you can copy the templates.
.. code-block:: bash
2018-08-06 19:42:03 +00:00
host> cp cfg/vhost-gen/*.yml-example ./data/www/project-1/.devilbox
2018-04-01 14:51:46 +00:00
.. note::
2018-06-03 18:45:01 +00:00
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.
2018-04-01 14:51:46 +00:00
3. Adjust template
^^^^^^^^^^^^^^^^^^
At this stage you can start adjusting the template. Either do that for the webserver version you
have enabled via :ref:`env_httpd_server`:
``/home/user/devilbox/data/www/project-1/.devilbox/apache22.yml``.
``/home/user/devilbox/data/www/project-1/.devilbox/apache24.yml``,
``/home/user/devilbox/data/www/project-1/.devilbox/nginx.yml`` or do it for all of them
synchronously.
.. note:: What exactly to change will be explained later.
4. Make Devilbox pick up those changes
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Whenever you change a project vhost template or the :ref:`env_httpd_template_dir` value,
you need to restart the Devilbox.
.. note:: It is also possible to do it without a restart which will be explained later.
Templates explained
2018-03-18 16:14:55 +00:00
===================
2018-04-01 14:51:46 +00:00
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.
+----------------+------------------+
| Webserver | Template |
+================+==================+
| Apache 2.2 | ``apache22.yml`` |
+----------------+------------------+
| Apache 2.4 | ``apache22.yml`` |
+----------------+------------------+
| Nginx stable | ``nginx.yml`` |
+----------------+------------------+
| Nginx mainline | ``nginx.yml`` |
+----------------+------------------+
.. note::
2018-06-03 18:45:01 +00:00
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.
2018-04-01 14:51:46 +00:00
Ensure yaml files are valid
---------------------------
.. warning::
2018-06-03 18:45:01 +00:00
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.
https://github.com/cytopia/devilbox/issues/142
2018-04-01 14:51:46 +00:00
2018-06-03 18:45:01 +00:00
You can use the bundled ``yamllint`` binary inside the container to validate your config.
2018-04-01 14:51:46 +00:00
.. code-block:: bash
:emphasize-lines: 13-16
2018-06-03 18:45:01 +00:00
# Navigate to the Devilbox directory
host> cd /home/user/devilbox
2018-04-01 14:51:46 +00:00
2018-06-03 18:45:01 +00:00
# Enter the PHP container
host> ./shell.sh
2018-04-01 14:51:46 +00:00
2018-06-03 18:45:01 +00:00
# Go to your project's template directory
devilbox@php-7.0.19 in /shared/httpd $ cd project-1/.devilbox
2018-04-01 14:51:46 +00:00
2018-06-03 18:45:01 +00:00
# Check the syntax of apache22.yml
devilbox@php-7.0.19 in /shared/httpd/project-1/.devilbox $ yamllint apache22.yml
2018-04-01 14:51:46 +00:00
2018-06-03 18:45:01 +00:00
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)
2018-04-01 14:51:46 +00:00
Long line errors can safely be ignored.
Template variables
------------------
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.
Global variables
^^^^^^^^^^^^^^^^
There are `global variables` that are determined by the command line arguments of ``vhost-gen``
itself or are elsewhere replaced by the Devilbox webserver container such as:
* ``__PORT__``
* ``__DEFAULT_VHOST__``
* ``__VHOST_NAME__``
* ``__ACCESS_LOG__``
* ``__ERROR_LOG__``
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.
* ``__VHOST_DOCROOT__``
* ``__VHOST_PROXY__``
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.
The ``vhost_type.rproxy`` will be ignored and ``__VHOST_PROXY__`` will be replaced by an empty
string.
Feature variables
^^^^^^^^^^^^^^^^^
All other variables will be replaced by what is provided in the ``features:`` section.
All subsections of ``features:`` have corresponding variables in the following form:
+-------------------+-----------------------+
| Feature directive | Variable name pattern |
+===================+=======================+
| ``lower_case:`` | ``__UPPER_CASE__`` |
+-------------------+-----------------------+
As an example, the contents of the ``features.php_fpm:`` section will be replaced into the
``__PHP_FPM__`` variable.
Template structure
------------------
Each vhost-gen template has three main yaml directives:
1. ``vhost:``
2. ``vhost_type:``
3. ``features:``
1. ``vhost:``
^^^^^^^^^^^^^
The ``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 ``vhost_type:``
section and from the ``features:`` section.
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:
.. code-block:: yaml
2018-06-03 18:45:01 +00:00
vhost: |
<VirtualHost *:80>
ServerName example.com
2018-04-01 14:51:46 +00:00
2018-06-03 18:45:01 +00:00
CustomLog "/var/log/apache/access.log" combined
ErrorLog "/var/log/apache/error.log"
2018-04-01 14:51:46 +00:00
2018-06-03 18:45:01 +00:00
DocumentRoot "/shared/httpd/project-1/htdocs"
<Directory "/shared/httpd/project-1/htdocs">
DirectoryIndex index.php
2018-04-01 14:51:46 +00:00
2018-06-03 18:45:01 +00:00
AllowOverride All
Options All
2018-04-01 14:51:46 +00:00
2018-06-03 18:45:01 +00:00
RewriteEngine on
RewriteBase /
2018-04-01 14:51:46 +00:00
2018-06-03 18:45:01 +00:00
Order allow,deny
Allow from all
</Directory>
2018-04-01 14:51:46 +00:00
2018-06-03 18:45:01 +00:00
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/shared/httpd/project-1/htdocs/$1
</VirtualHost>
2018-04-01 14:51:46 +00:00
2. ``vhost_type:``
^^^^^^^^^^^^^^^^^^
The ``vhost_type:`` contains ``docroot`` and ``rproxy``. The Devilbox only makes use of ``docroot``
which holds the definition of a normal vhost. Its content will be replaced into the
``__VHOST_DOCROOT__`` variable.
The ``rproxy`` section will be ignored and the ``__VHOST_RPROXY__`` variable will contain an empty
value.
+----------------------+------------------------------+
| vHost Type section | Variable to be replaced into |
+======================+==============================+
| ``docroot:`` | ``__VHOST_DOCROOT__`` |
+----------------------+------------------------------+
| ``rproxy:`` | ``__VHOST_RPROXY__`` (empty) |
+----------------------+------------------------------+
3. ``features:``
^^^^^^^^^^^^^^^^
This section contains directives that will all be replaced into ``vhost:`` variables.
+----------------------+------------------------------+
| Feature section | Variable to be replaced into |
+======================+==============================+
| ``php_fpm:`` | ``__PHP_FPM__`` |
+----------------------+------------------------------+
| ``alias:`` | ``__ALIASES__`` |
+----------------------+------------------------------+
| ``deny:`` | ``__DENIES__`` |
+----------------------+------------------------------+
| ``server_status:`` | ``__SERVER_STATUS__`` |
+----------------------+------------------------------+
| ``xdomain_request:`` | ``__XDOMAIN_REQ__`` |
+----------------------+------------------------------+
2018-03-18 16:14:55 +00:00
.. _custom_vhost_apply_vhost_gen_changes:
2018-04-01 14:51:46 +00:00
Apply Changes
=============
After having edited your vhost-gen template files, you still need to apply these changes.
This can be achieved in two ways:
1. Rename your project directory back and forth
2. Restart the Devilbox
Rename project directory
------------------------
.. code-block:: bash
2018-06-03 18:45:01 +00:00
# Navigate to the data directory
host> /home/user/devilbox/data/www
2018-04-01 14:51:46 +00:00
2018-06-03 18:45:01 +00:00
# Rename your project to something else
host> mv project-1 project-1.tmp
2018-04-01 14:51:46 +00:00
2018-06-03 18:45:01 +00:00
# Rename your project to its original name
host> mv project-1.tmp project-1
2018-04-01 14:51:46 +00:00
If you want to understand what is going on right now, check the docker logs for the web server.
.. code-block:: bash
2018-06-03 18:45:01 +00:00
# Navigate to the devilbox directory
host> /home/user/devilbox
2018-04-01 14:51:46 +00:00
2018-06-03 18:45:01 +00:00
# Check docker logs
host> docker-compose logs httpd
2018-04-01 14:51:46 +00:00
2018-06-03 18:45:01 +00:00
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
2018-04-01 14:51:46 +00:00
2018-06-03 18:45:01 +00:00
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
2018-04-01 14:51:46 +00:00
**What happened?**
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.
.. note::
2018-06-03 18:45:01 +00:00
Renaming a project directory will only affect a single project. In case your change the
value of :ref:`env_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.
2018-04-01 14:51:46 +00:00
Restart the Devilbox
--------------------
Stop the Devilbox and start it up again.
Further readings
================
.. seealso::
2018-06-03 18:45:01 +00:00
Have a look at the following examples which involve customizing vhost-gen templates:
2018-03-18 16:14:55 +00:00
2018-08-06 19:42:03 +00:00
* :ref:`vhost_gen_virtual_host_templates`
* :ref:`vhost_gen_example_add_sub_domains`