]> git.openstreetmap.org Git - nominatim.git/blob - docs/admin/Deployment-Python.md
4da840086a84e87954f2578ef86e278156a3e131
[nominatim.git] / docs / admin / Deployment-Python.md
1 # Deploying the Nominatim Python frontend
2
3 The Nominatim can be run as a Python-based 
4 [ASGI web application](https://asgi.readthedocs.io/en/latest/). You have the
5 choice between [Falcon](https://falcon.readthedocs.io/en/stable/)
6 and [Starlette](https://www.starlette.io/) as the ASGI framework.
7
8 This section gives a quick overview on how to configure Nginx to serve
9 Nominatim. Please refer to the documentation of
10 [Nginx](https://nginx.org/en/docs/) for background information on how
11 to configure it.
12
13 !!! Note
14     Throughout this page, we assume your Nominatim project directory is
15     located in `/srv/nominatim-project` and you have installed Nominatim
16     using the default installation prefix `/usr/local`. If you have put it
17     somewhere else, you need to adjust the commands and configuration
18     accordingly.
19
20     We further assume that your web server runs as user `www-data`. Older
21     versions of CentOS may still use the user name `apache`. You also need
22     to adapt the instructions in this case.
23
24 ### Installing the required packages
25
26 The recommended way to deploy a Python ASGI application is to run
27 the ASGI runner [uvicorn](https://uvicorn.org/)
28 together with [gunicorn](https://gunicorn.org/) HTTP server. We use
29 Falcon here as the web framework.
30
31 Create a virtual environment for the Python packages and install the necessary
32 dependencies:
33
34 ``` sh
35 sudo apt install virtualenv
36 virtualenv /srv/nominatim-venv
37 /srv/nominatim-venv/bin/pip install SQLAlchemy PyICU psycopg[binary] \
38    psycopg2-binary python-dotenv PyYAML falcon uvicorn gunicorn
39 ```
40
41 ### Setting up Nominatim as a systemd job
42
43 Next you need to set up the service that runs the Nominatim frontend. This is
44 easiest done with a systemd job.
45
46 Create the following file `/etc/systemd/system/nominatim.service`:
47
48 ``` systemd
49 [Unit]
50 Description=Nominatim running as a gunicorn application
51 After=network.target
52 Requires=nominatim.socket
53
54 [Service]
55 Type=simple
56 Environment="PYTHONPATH=/usr/local/lib/nominatim/lib-python/"
57 User=www-data
58 Group=www-data
59 WorkingDirectory=/srv/nominatim-project
60 ExecStart=/srv/nominatim-venv/bin/gunicorn -b unix:/run/nominatim.sock -w 4 -k uvicorn.workers.UvicornWorker nominatim.server.falcon.server:run_wsgi
61 ExecReload=/bin/kill -s HUP $MAINPID
62 StandardOutput=append:/var/log/gunicorn-nominatim.log
63 StandardError=inherit
64 PrivateTmp=true
65 TimeoutStopSec=5
66 KillMode=mixed
67
68 [Install]
69 WantedBy=multi-user.target
70 ```
71
72 This sets up gunicorn with 4 workers (`-w 4` in ExecStart). Each worker runs
73 its own Python process using
74 [`NOMINATIM_API_POOL_SIZE`](../customize/Settings.md#nominatim_api_pool_size)
75 connections to the database to serve requests in parallel.
76
77 Make the new service known to systemd and start it:
78
79 ``` sh
80 sudo systemctl daemon-reload
81 sudo systemctl enable nominatim
82 sudo systemctl start nominatim
83 ```
84
85 This sets the service up, so that Nominatim is automatically started
86 on reboot.
87
88 ### Configuring nginx
89
90 To make the service available to the world, you need to proxy it through
91 nginx. Add the following definition to the default configuration:
92
93 ``` nginx
94 upstream nominatim_service {
95   server unix:/run/nominatim.sock fail_timeout=0;
96 }
97
98 server {
99     listen 80;
100     listen [::]:80;
101
102     root /var/www/html;
103     index /search;
104
105     location / {
106             proxy_set_header Host $http_host;
107             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
108             proxy_set_header X-Forwarded-Proto $scheme;
109             proxy_redirect off;
110             proxy_pass http://nominatim_service;
111     }
112 }
113 ```
114
115 Reload nginx with
116
117 ```
118 sudo systemctl reload nginx
119 ```
120
121 and you should be able to see the status of your server under
122 `http://localhost/status`.