]> git.openstreetmap.org Git - nominatim.git/blob - vagrant/Install-on-Ubuntu-20.sh
switch Ubuntu installation scripts to Python frontend
[nominatim.git] / vagrant / Install-on-Ubuntu-20.sh
1 #!/bin/bash -e
2 #
3 # hacks for broken vagrant box      #DOCS:
4 sudo rm -f /var/lib/dpkg/lock       #DOCS:
5 export APT_LISTCHANGES_FRONTEND=none #DOCS:
6 export DEBIAN_FRONTEND=noninteractive #DOCS:
7
8 # *Note:* these installation instructions are also available in executable
9 #         form for use with vagrant under vagrant/Install-on-Ubuntu-20.sh.
10 #
11 # Installing the Required Software
12 # ================================
13 #
14 # These instructions expect that you have a freshly installed Ubuntu 20.04.
15 #
16 # Make sure all packages are up-to-date by running:
17 #
18
19     sudo apt-get update -qq
20
21 # Now you can install all packages needed for Nominatim:
22
23     sudo apt-get install -y build-essential cmake g++ libboost-dev libboost-system-dev \
24                         libboost-filesystem-dev libexpat1-dev zlib1g-dev \
25                         libbz2-dev libpq-dev liblua5.3-dev lua5.3 lua-dkjson \
26                         nlohmann-json3-dev postgresql-12-postgis-3 \
27                         postgresql-contrib-12 postgresql-12-postgis-3-scripts \
28                         libicu-dev python3-dotenv \
29                         python3-psycopg2 python3-psutil python3-jinja2 python3-pip \
30                         python3-icu python3-datrie python3-yaml git
31
32 # Some of the Python packages that come with Ubuntu 20.04 are too old, so
33 # install the latest version from pip:
34
35     pip3 install --user sqlalchemy asyncpg
36
37
38 #
39 # System Configuration
40 # ====================
41 #
42 # The following steps are meant to configure a fresh Ubuntu installation
43 # for use with Nominatim. You may skip some of the steps if you have your
44 # OS already configured.
45 #
46 # Creating Dedicated User Accounts
47 # --------------------------------
48 #
49 # Nominatim will run as a global service on your machine. It is therefore
50 # best to install it under its own separate user account. In the following
51 # we assume this user is called nominatim and the installation will be in
52 # /srv/nominatim. To create the user and directory run:
53 #
54 #     sudo useradd -d /srv/nominatim -s /bin/bash -m nominatim
55 #
56 # You may find a more suitable location if you wish.
57 #
58 # The following instructions assume you are logged in as this user.
59 # You can also switch to the user with:
60 #
61 #     sudo -u nominatim bash
62 #
63 # To be able to copy and paste instructions from this manual, export
64 # user name and home directory now like this:
65 #
66 if [ "x$USERNAME" == "x" ]; then #DOCS:
67     export USERNAME=vagrant        #DOCS:    export USERNAME=nominatim
68     export USERHOME=/home/vagrant  #DOCS:    export USERHOME=/srv/nominatim
69 fi                                 #DOCS:
70 #
71 # **Never, ever run the installation as a root user.** You have been warned.
72 #
73 # Make sure that system servers can read from the home directory:
74
75     chmod a+x $USERHOME
76
77 # Setting up PostgreSQL
78 # ---------------------
79 #
80 # Tune the postgresql configuration, which is located in 
81 # `/etc/postgresql/12/main/postgresql.conf`. See section *Tuning the PostgreSQL database*
82 # in [the installation page](../admin/Installation.md#tuning-the-postgresql-database)
83 # for the parameters to change.
84 #
85 # Restart the postgresql service after updating this config file.
86
87 if [ "x$NOSYSTEMD" == "xyes" ]; then  #DOCS:
88     sudo pg_ctlcluster 12 main start  #DOCS:
89 else                                  #DOCS:
90     sudo systemctl restart postgresql
91 fi                                    #DOCS:
92 #
93 # Finally, we need to add two postgres users: one for the user that does
94 # the import and another for the webserver which should access the database
95 # for reading only:
96 #
97
98     sudo -u postgres createuser -s $USERNAME
99     sudo -u postgres createuser www-data
100
101 #
102 # Installing Nominatim
103 # ====================
104 #
105 # Building and Configuration
106 # --------------------------
107 #
108 # Get the source code from Github and change into the source directory
109 #
110 if [ "x$1" == "xyes" ]; then  #DOCS:    :::sh
111     cd $USERHOME
112     git clone --recursive https://github.com/openstreetmap/Nominatim.git
113     cd Nominatim
114 else                               #DOCS:
115     cd $USERHOME/Nominatim         #DOCS:
116 fi                                 #DOCS:
117
118 # When installing the latest source from github, you also need to
119 # download the country grid:
120
121 if [ ! -f data/country_osm_grid.sql.gz ]; then       #DOCS:    :::sh
122     wget -O data/country_osm_grid.sql.gz https://nominatim.org/data/country_grid.sql.gz
123 fi                                 #DOCS:
124
125 # The code must be built in a separate directory. Create this directory,
126 # then configure and build Nominatim in there:
127
128     mkdir $USERHOME/build
129     cd $USERHOME/build
130     cmake $USERHOME/Nominatim
131     make
132     sudo make install
133
134 # Nominatim is now ready to use. You can continue with
135 # [importing a database from OSM data](../admin/Import.md). If you want to set up
136 # the API frontend first, continue reading.
137 #
138 # Setting up the Python frontend
139 # ==============================
140 #
141 # Some of the Python packages in Ubuntu are too old. Therefore run the
142 # frontend from a Python virtualenv with current packages.
143 #
144 # To set up the virtualenv, run:
145
146 #DOCS:```sh
147 sudo apt-get install -y virtualenv
148 virtualenv $USERHOME/nominatim-venv
149 $USERHOME/nominatim-venv/bin/pip install SQLAlchemy PyICU psycopg[binary] \
150               psycopg2-binary python-dotenv PyYAML falcon uvicorn gunicorn
151 #DOCS:```
152
153 # Next you need to create a systemd job that runs Nominatim on gunicorn.
154 # First create a systemd job that manages the socket file:
155
156 #DOCS:```sh
157 sudo tee /etc/systemd/system/nominatim.socket << EOFSOCKETSYSTEMD
158 [Unit]
159 Description=Gunicorn socket for Nominatim
160
161 [Socket]
162 ListenStream=/run/nominatim.sock
163 SocketUser=www-data
164
165 [Install]
166 WantedBy=multi-user.target
167 EOFSOCKETSYSTEMD
168 #DOCS:```
169
170 # Then create the service for Nominatim itself.
171
172 #DOCS:```sh
173 sudo tee /etc/systemd/system/nominatim.service << EOFNOMINATIMSYSTEMD
174 [Unit]
175 Description=Nominatim running as a gunicorn application
176 After=network.target
177 Requires=nominatim.socket
178
179 [Service]
180 Type=simple
181 Environment="PYTHONPATH=/usr/local/lib/nominatim/lib-python/"
182 User=www-data
183 Group=www-data
184 WorkingDirectory=$USERHOME/nominatim-project
185 ExecStart=$USERHOME/nominatim-venv/bin/gunicorn -b unix:/run/nominatim.sock -w 4 -k uvicorn.workers.UvicornWorker nominatim.server.falcon.server:run_wsgi
186 ExecReload=/bin/kill -s HUP \$MAINPID
187 StandardOutput=append:/var/log/gunicorn-nominatim.log
188 StandardError=inherit
189 PrivateTmp=true
190 TimeoutStopSec=5
191 KillMode=mixed
192
193 [Install]
194 WantedBy=multi-user.target
195 EOFNOMINATIMSYSTEMD
196 #DOCS:```
197
198 # Activate the services:
199
200 if [ "x$NOSYSTEMD" != "xyes" ]; then  #DOCS:
201     sudo systemctl daemon-reload
202     sudo systemctl enable nominatim.socket
203     sudo systemctl start nominatim.socket
204     sudo systemctl enable nominatim.service
205 fi                                    #DOCS:
206
207
208 # Setting up a webserver
209 # ======================
210 #
211 # The webserver is only needed as a proxy between the public interface
212 # and the gunicorn service.
213 #
214 # The frontend will need configuration information from the project
215 # directory, which will be populated later
216 # [during the import process](../admin/Import.md#creating-the-project-directory)
217 # Already create the project directory itself now:
218
219
220     mkdir $USERHOME/nominatim-project
221
222
223 # Option 1: Using Apache
224 # ----------------------
225 #
226 if [ "x$2" == "xinstall-apache" ]; then #DOCS:
227 # First install apache itself and enable the proxy module:
228
229     sudo apt-get install -y apache2
230     sudo a2enmod proxy_http
231
232 # To set up proxying for Apache add the following configuration:
233
234 #DOCS:```sh
235 sudo tee /etc/apache2/conf-available/nominatim.conf << EOFAPACHECONF
236 ProxyPass /nominatim "unix:/run/nominatim.sock|http://localhost/"
237 EOFAPACHECONF
238 #DOCS:```
239
240 #
241 # Then enable the configuration and restart apache
242 #
243
244     sudo a2enconf nominatim
245 if [ "x$NOSYSTEMD" == "xyes" ]; then  #DOCS:
246     sudo apache2ctl start             #DOCS:
247 else                                  #DOCS:
248     sudo systemctl restart apache2
249 fi                                    #DOCS:
250
251 # The Nominatim API is now available at `http://localhost/nominatim/`.
252
253 fi   #DOCS:
254
255 #
256 # Option 2: Using nginx
257 # ---------------------
258 #
259 if [ "x$2" == "xinstall-nginx" ]; then #DOCS:
260
261 # First install nginx itself:
262
263     sudo apt-get install -y nginx
264
265 # Then create a Nginx configuration to forward http requests to that socket.
266
267 #DOCS:```sh
268 sudo tee /etc/nginx/sites-available/default << EOF_NGINX_CONF
269 server {
270     listen 80 default_server;
271     listen [::]:80 default_server;
272
273     root $USERHOME/nominatim-project/website;
274     index /search;
275
276     location /nominatim/ {
277             proxy_set_header Host \$http_host;
278             proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
279             proxy_set_header X-Forwarded-Proto \$scheme;
280             proxy_redirect off;
281             proxy_pass http://unix:/run/nominatim.sock:/;
282     }
283 }
284 EOF_NGINX_CONF
285 #DOCS:```
286
287 # Enable the configuration and restart Nginx
288 #
289
290 if [ "x$NOSYSTEMD" == "xyes" ]; then  #DOCS:
291     sudo /usr/sbin/nginx &            #DOCS:
292 else                                  #DOCS:
293     sudo systemctl restart nginx
294 fi                                    #DOCS:
295
296 # The Nominatim API is now available at `http://localhost/`.
297
298
299
300 fi   #DOCS: