Debian Codegraph @

contrib/docker/apache-server/README.rst

raw
1 ====================
2 Apache Docker Server
3 ====================
4
5 This directory contains code for running a Mercurial hgweb server via mod_wsgi
6 with the Apache HTTP Server and protected from scraper bots with Anubis inside
7 a Docker container.
8
9 .. important::
10
11 This container is intended for testing purposes only: it is
12 **not** meant to be suitable for production use.
13
14 Building Image
15 ==============
16
17 The first step is to build a Docker image containing all the required
18 software::
19
20 $ docker build -t hg-apache .
21
22 .. important::
23
24 You should rebuild the image whenever the content of this directory
25 changes. Rebuilding after pulling or when you haven't run the container
26 in a while is typically a good idea.
27
28 Running the Server
29 ==================
30
31 To run the container, you'll execute something like::
32
33 $ docker run --init --rm -it -p 8000:80 hg-apache
34
35 If you aren't a Docker expert:
36
37 * ``--init`` will run an init inside the container so we can run multiple
38 processes at once (i.e. both Apache HTTP Server and Anubis)
39 * ``--rm`` will remove the container when it stops (so it doesn't clutter
40 your system)
41 * ``-i`` will launch the container in interactive mode so stdin is attached
42 * ``-t`` will allocate a pseudo TTY
43 * ``-p 8000:80`` will publish port ``80`` on the container to port ``8000``
44 on the host, allowing you to access the HTTP server on the host interface.
45 * ``hg-apache`` is the container image to run. This should correspond to what
46 we build with ``docker build``.
47
48 When starting the container, you should see some start-up actions (including a
49 Mercurial health check) and some output saying Apache and Anubis have started.
50 There will be messages related to Anubis configuration and random key
51 generation, but they are expected, since it was configured to simply run
52 successfully and protect one hgweb instance.
53
54 Now if you load ``http://localhost:8000/`` (or whatever interface Docker
55 is using), you should see hgweb running!
56
57 For your convenience, we've created an empty repository available at
58 ``/repo``. Feel free to populate it with ``hg push``.
59
60 Customizing the Server
61 ======================
62
63 This Docker container uses fairly minimal configuration and often relies on the
64 default values while making sure the software works together well. There are
65 very few advanced features, and the configuration that is present is hopefully
66 pretty self-explanatory. This should make it easier for people to understand
67 the principles behind the moving parts and how they all come together, and to
68 experiment with adding any new features without breaking anything.
69
70 Customizing the WSGI Dispatcher And Mercurial Config
71 ----------------------------------------------------
72
73 By default, the Docker environment installs a custom ``hgweb.wsgi``
74 file (based on the example in ``contrib/hgweb.wsgi``). The file
75 is installed into ``/var/hg/htdocs/hgweb.wsgi``.
76
77 A default hgweb configuration file is also installed. The ``hgwebconfig``
78 file from this directory is installed into ``/var/hg/htdocs/config``.
79
80 You have a few options for customizing these files.
81
82 The simplest is to hack up ``hgwebconfig`` and ``entrypoint.sh`` in
83 this directory and to rebuild the Docker image. This has the downside
84 that the Mercurial working copy is modified and you may accidentally
85 commit unwanted changes.
86
87 The next simplest is to copy this directory somewhere, make your changes,
88 then rebuild the image. No working copy changes involved.
89
90 The preferred solution is to mount a host file into the container and
91 overwrite the built-in defaults.
92
93 For example, say we create a custom hgweb config file in ``~/hgweb``. We
94 can start the container like so to install our custom config file::
95
96 $ docker run -v ~/hgweb:/var/hg/htdocs/config ...
97
98 You can do something similar to install a custom WSGI dispatcher::
99
100 $ docker run -v ~/hgweb.wsgi:/var/hg/htdocs/hgweb.wsgi ...
101
102 Managing Repositories
103 ---------------------
104
105 Repositories are served from ``/var/hg/repos`` by default. This directory
106 is configured as a Docker volume. This means you can mount an existing
107 data volume container in the container so repository data is persisted
108 across container invocations. See
109 https://docs.docker.com/userguide/dockervolumes/ for more.
110
111 Alternatively, if you just want to perform lightweight repository
112 manipulation, open a shell in the container::
113
114 $ docker exec -it <container> /bin/bash
115
116 Then run ``hg init``, etc to manipulate the repositories in ``/var/hg/repos``.
117
118 Anubis Configuration Settings
119 -----------------------------
120
121 The newest and likely the most experimental part in this whole setup is Anubis,
122 which is described as "Web AI Firewall Utility", and in our case it protects
123 hgweb from the excessive scraping done by various bots online (mostly
124 AI-related, but not only those).
125
126 The current configuration file for Anubis, ``/etc/anubis/hgweb.env``, is
127 created inside ``entrypoint.sh``. It's purposefully short and uses the default
128 bot policy and doesn't use any advanced features, mostly because currently it's
129 difficult to make any assumptions about the future situation, and also because
130 this is just an example setup not meant for production.
131
132 Probably the best advice for maintaining a widely-accessible hgweb instance is
133 to keep Anubis package up-to-date and either rely on its defaults, or maintain
134 your own policy file and continuously monitor the logs.
135
136 See https://anubis.techaro.lol/docs/admin/installation for more on the
137 available settings.
138
139 Apache HTTP Server Settings
140 ---------------------------
141
142 There is ``vhost.conf`` file in this directory that gets copied to
143 ``/etc/apache2/sites-available/hg.conf`` in the container.
144
145 One feature that is worth mentioning is serving the files in the /static/
146 directory. It is possible to serve these files via hgweb, but that is not
147 recommended, since any web server software is much better at it. So there are
148 configuration directives related to that in the ``vhost.conf`` file.
149
150 But while we do serve the static files via Apache HTTP Server, it's worth
151 mentioning that they are also protected with Anubis (and get proxied through
152 it). Depending on your use-case, this might not be what you want. If you want
153 to serve the static files to HTTP clients quickly and without any checks, you
154 need to move the directives into the first ``VirtualHost`` (that proxies the
155 requests to Anubis) and use ``ProxyPass`` with ``!`` directive to not
156 reverse-proxy the /static/ directory. See
157 https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypass for more.
158
159 mod_wsgi Configuration Settings
160 -------------------------------
161
162 mod_wsgi settings can be controlled with the following environment
163 variables (defined in ``Dockerfile`` and used in ``vhost.conf``).
164
165 WSGI_PROCESSES
166 Number of WSGI processes to run.
167 WSGI_THREADS
168 Number of threads to run in each WSGI process
169 WSGI_MAX_REQUESTS
170 Maximum number of requests each WSGI process may serve before it is
171 reaped.
172
173 See https://modwsgi.readthedocs.io/en/master/configuration-directives/WSGIDaemonProcess.html
174 for more on these settings.
175
176 .. note::
177
178 The default is to use 1 thread per process. The reason is that Mercurial
179 doesn't perform well in multi-threaded mode due to the GIL. Most people
180 run a single thread per process in production for this reason, so that's
181 what we default to.