Skip to content

Commit 7b41bd1

Browse files
psrok1Repumba
andauthored
Move from uwsgi to gunicorn (#735)
Co-authored-by: Tomek Chytry-Trzeciak <75318192+Repumba@users.noreply.github.com>
1 parent a9ba281 commit 7b41bd1

11 files changed

+68
-56
lines changed

deploy/docker/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ RUN mkdir -p /app/uploads/ && \
2828

2929
ENV PYTHONPATH=/app
3030
ENV FLASK_APP=/app/mwdb/app.py
31+
# How many workers gunicorn should spawn by default
3132
WORKDIR /app
3233

3334
CMD ["/app/start.sh"]

docker-compose-dev-karton.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ services:
2626
# NOTE: use gen_vars.sh in order to generate this file
2727
- mwdb-vars.env
2828
environment:
29-
UWSGI_PY_AUTORELOAD: 1
30-
UWSGI_ENABLE_THREADS: 1
29+
HOT_RELOAD: 1
3130
MWDB_MAIL_SMTP: "mailhog:1025"
3231
MWDB_MAIL_FROM: "noreply@mwdb.dev"
3332
MWDB_RECAPTCHA_SITE_KEY: "6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"

docker-compose-dev-remote.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ services:
2525
# NOTE: use gen_vars.sh in order to generate this file
2626
- mwdb-vars.env
2727
environment:
28-
UWSGI_PY_AUTORELOAD: 1
29-
UWSGI_ENABLE_THREADS: 1
28+
HOT_RELOAD: 1
3029
MWDB_MAIL_SMTP: "mailhog:1025"
3130
MWDB_MAIL_FROM: "noreply@mwdb.dev"
3231
MWDB_RECAPTCHA_SITE_KEY: "6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"

docker-compose-dev.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ services:
2525
# NOTE: use gen_vars.sh in order to generate this file
2626
- mwdb-vars.env
2727
environment:
28-
UWSGI_PY_AUTORELOAD: 1
29-
UWSGI_ENABLE_THREADS: 1
28+
HOT_RELOAD: 1
3029
MWDB_MAIL_SMTP: "mailhog:1025"
3130
MWDB_MAIL_FROM: "noreply@mwdb.dev"
3231
MWDB_RECAPTCHA_SITE_KEY: "6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"

docker-compose-e2e.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ services:
2323
MWDB_BASE_URL: http://127.0.0.1
2424
MWDB_ENABLE_RATE_LIMIT: 0
2525
MWDB_ENABLE_REGISTRATION: 1
26-
UWSGI_PROCESSES: 4
2726
MWDB_MAIL_SMTP: "mailhog:1025"
2827
MWDB_MAIL_FROM: "noreply@mwdb.dev"
2928
volumes:

docker-compose-oidc-dev.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ services:
2525
# NOTE: use gen_vars.sh in order to generate this file
2626
- mwdb-vars.env
2727
environment:
28-
UWSGI_PY_AUTORELOAD: 1
29-
UWSGI_ENABLE_THREADS: 1
28+
HOT_RELOAD: 1
3029
MWDB_MAIL_SMTP: "mailhog:1025"
3130
MWDB_MAIL_FROM: "noreply@mwdb.dev"
3231
MWDB_RECAPTCHA_SITE_KEY: "6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"

docker/gunicorn.conf.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import os
2+
3+
wsgi_app = "mwdb.app:app"
4+
bind = "0.0.0.0:8080"
5+
user = "nobody"
6+
reload = bool(int(os.getenv("HOT_RELOAD", "0")))
7+
workers = int(os.getenv("GUNICORN_WORKERS", "4"))

docker/start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ until psql "$MWDB_POSTGRES_URI" -c "\q" ; do
77
done
88

99
echo "Configuring mwdb-core instance"
10-
mwdb-core configure --quiet basic && exec uwsgi --ini /app/uwsgi.ini
10+
mwdb-core configure --quiet basic && exec gunicorn

docker/uwsgi.ini

Lines changed: 0 additions & 11 deletions
This file was deleted.

docs/setup-and-configuration.rst

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,48 @@
11
Setup and configuration
22
=======================
33

4-
Prerequisites
5-
-------------
4+
Installation and configuration with Docker Compose
5+
--------------------------------------------------
6+
7+
The quickest way setup MWDB is to just clone the repository and use Docker-Compose with all batteries included.
8+
9+
.. code-block:: console
10+
11+
$ git clone https://github.com/CERT-Polska/mwdb-core.git
12+
13+
After cloning repository, the first step is to go to the ``mwdb-core`` directory and generate configuration using ``./gen_vars.sh`` script.
14+
Generated variables can be found in mwdb-vars.env.
15+
16+
.. code-block:: console
17+
18+
$ ./gen_vars.sh
19+
Credentials for initial mwdb account:
20+
21+
-----------------------------------------
22+
Admin login: admin
23+
Admin password: la/Z7MsmKA3UxW8Psrk1Opap
24+
-----------------------------------------
25+
26+
Please be aware that initial account will be only set up on the first run. If you already have a database with at least one user, then this setting will be ignored for security reasons. You can always create an admin account manually by executing a command. See "flask create_admin --help" for reference.
27+
28+
Then build images via ``docker-compose build`` and run MWDB via ``docker-compose up -d``.
29+
30+
Your MWDB instance will be available on default HTTP port (80): http://127.0.0.1/
31+
32+
If you want to use Docker Compose for MWDB development, check out :ref:`Developer guide`.
33+
34+
Standalone installation
35+
-----------------------
36+
37+
Step 1.: Prerequisites
38+
~~~~~~~~~~~~~~~~~~~~~~
639

740
MWDB was tested on Debian-based systems, but should work as well on other Linux distributions.
841

942
For production environments, you need to install:
1043

1144

1245
* **PostgreSQL database** (minimum supported version: 12, https://www.postgresql.org/download/linux/debian/)
13-
* **python-ssdeep library dependencies for Python 3** (https://python-ssdeep.readthedocs.io/en/latest/installation.html#id9)
1446

1547
Optionally you can install:
1648

@@ -41,8 +73,8 @@ It's highly recommended to create a fresh `virtualenv <https://docs.python.org/3
4173
4274
The connection string is: ``postgresql://mwdb:mwdb@127.0.0.1:54322/mwdb``
4375

44-
Installation & Configuration
45-
----------------------------
76+
Step 2.: Installation and configuration
77+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4678

4779
The recommended installation method is pip:
4880

@@ -87,14 +119,14 @@ Then, use ``mwdb-core configure`` to provide first configuration for your MWDB s
87119
3) Current directory
88120
: 3
89121
90-
For first installation we recommend to install everything in current folder via ``3`` option. If you want to install MWDB system-wide or locally for user: choose ``1`` or ``2``.
122+
For first installation we recommend to install everything in current folder via ``3`` option. If you want to install MWDB system-wide or locally for user: choose ``1`` or ``2``.
91123

92124
Then, input the connection string for PostgreSQL database. The database must be online and reachable at the time of configuration. After that, you will be asked for path for uploads and instance base URL. If the default value is ok, press Enter:
93125

94126
.. code-block::
95127
96128
PostgreSQL database connection string [postgresql://localhost/mwdb]:
97-
Uploads storage path [./uploads]:
129+
Uploads storage path [./uploads]:
98130
Base public URL of Malwarecage service [http://127.0.0.1]:
99131
100132
Depending on the installation type, your configuration will be stored in ``mwdb.ini`` file and can be changed any time you want:
@@ -136,42 +168,30 @@ And you are done! ``run`` command will start the Flask server:
136168
137169
Your MWDB instance will be available on port 5000 (use ``--port`` to change that): http://127.0.0.1:5000/
138170

139-
.. warning::
140-
141-
Remember to run ``mwdb-core configure`` after each version upgrade to apply database migrations
142-
143-
144-
Alternative setup with Docker Compose
145-
--------------------------------------
171+
Keep in mind that Flask server is meant to be used as development server and **is not suitable for production**.
172+
See also: https://flask.palletsprojects.com/en/2.2.x/server/
146173

147-
The quickest way setup MWDB is to just clone the repository and use Docker-Compose. We recommend this method **only for testing** because it can be a bit more difficult to install extensions and integrate with other services.
148-
149-
.. code-block:: console
150-
151-
$ git clone https://github.com/CERT-Polska/mwdb-core.git
152-
153-
After cloning repository, the first step is to go to the ``mwdb-core`` directory and generate configuration using ``./gen_vars.sh`` script.
174+
.. warning::
154175

155-
.. code-block:: console
176+
In standalone setup, remember to run ``mwdb-core configure`` after each version upgrade to apply database migrations.
156177

157-
$ ./gen_vars.sh
158-
Credentials for initial mwdb account:
178+
Step 3.: Setting up gunicorn and nginx
179+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
159180

160-
-----------------------------------------
161-
Admin login: admin
162-
Admin password: la/Z7MsmKA3UxW8Psrk1Opap
163-
-----------------------------------------
181+
It's recommended to deploy Flask applications using dedicated WSGI server. We highly recommend Gunicorn as it's used
182+
in our Docker images and combine it with Nginx serving as proxy server for best security and performance
164183

165-
Please be aware that initial account will be only set up on the first run. If you already have a database with at least one user, then this setting will be ignored for security reasons. You can always create an admin account manually by executing a command. See "flask create_admin --help" for reference.
184+
.. seealso::
166185

167-
Then build images via ``docker-compose build`` and run MWDB via ``docker-compose up -d``.
186+
https://flask.palletsprojects.com/en/2.2.x/deploying/gunicorn/
168187

169-
Your MWDB instance will be available on default HTTP port (80): http://127.0.0.1/
188+
https://docs.gunicorn.org/en/latest/deploy.html#deploying-gunicorn
170189

171-
If you want to use Docker Compose for MWDB development, check out :ref:`Developer guide`.
190+
Proper configuration files and templates used in our Docker images can be found in `docker directory on our Github repository
191+
<https://github.com/CERT-Polska/mwdb-core/tree/master/docker>`_
172192

173-
Upgrade mwdb-core to latest version
174-
-----------------------------------
193+
Upgrading mwdb-core to latest version
194+
-------------------------------------
175195

176196
For standalone installation (pip-based), upgrade mwdb-core package to the latest version.
177197

0 commit comments

Comments
 (0)