]> git.openstreetmap.org Git - nominatim.git/blob - vagrant/Install-on-Ubuntu-22.sh
b170daad4e0ab22055e4ea4f94bb2ffa2e82e319
[nominatim.git] / vagrant / Install-on-Ubuntu-22.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-22.sh.
10 #
11 # Installing the Required Software
12 # ================================
13 #
14 # These instructions expect that you have a freshly installed Ubuntu 22.04.
15 #
16 # Make sure all packages are up-to-date by running:
17 #
18
19     sudo apt update -qq
20
21 # Now you can install all packages needed for Nominatim:
22
23     sudo apt 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                         postgresql-server-dev-14 postgresql-14-postgis-3 \
27                         postgresql-contrib-14 postgresql-14-postgis-3-scripts \
28                         php-cli php-pgsql php-intl libicu-dev python3-dotenv \
29                         python3-psycopg2 python3-psutil python3-jinja2 \
30                         python3-icu python3-datrie python3-sqlalchemy \
31                         python3-asyncpg git
32
33 #
34 # System Configuration
35 # ====================
36 #
37 # The following steps are meant to configure a fresh Ubuntu installation
38 # for use with Nominatim. You may skip some of the steps if you have your
39 # OS already configured.
40 #
41 # Creating Dedicated User Accounts
42 # --------------------------------
43 #
44 # Nominatim will run as a global service on your machine. It is therefore
45 # best to install it under its own separate user account. In the following
46 # we assume this user is called nominatim and the installation will be in
47 # /srv/nominatim. To create the user and directory run:
48 #
49 #     sudo useradd -d /srv/nominatim -s /bin/bash -m nominatim
50 #
51 # You may find a more suitable location if you wish.
52 #
53 # The following instructions assume you are logged in as this user.
54 # You can also switch to the user with:
55 #
56 #     sudo -u nominatim bash
57 #
58 # To be able to copy and paste instructions from this manual, export
59 # user name and home directory now like this:
60 #
61 if [ "x$USERNAME" == "x" ]; then #DOCS:
62     export USERNAME=vagrant        #DOCS:    export USERNAME=nominatim
63     export USERHOME=/home/vagrant  #DOCS:    export USERHOME=/srv/nominatim
64 fi                                 #DOCS:
65 #
66 # **Never, ever run the installation as a root user.** You have been warned.
67 #
68 # Make sure that system servers can read from the home directory:
69
70     chmod a+x $USERHOME
71
72 # Setting up PostgreSQL
73 # ---------------------
74 #
75 # Tune the postgresql configuration, which is located in 
76 # `/etc/postgresql/14/main/postgresql.conf`. See section *Postgres Tuning* in
77 # [the installation page](../admin/Installation.md#postgresql-tuning)
78 # for the parameters to change.
79 #
80 # Restart the postgresql service after updating this config file.
81
82 if [ "x$NOSYSTEMD" == "xyes" ]; then  #DOCS:
83     sudo pg_ctlcluster 14 main start  #DOCS:
84 else                                  #DOCS:
85     sudo systemctl restart postgresql
86 fi                                    #DOCS:
87 #
88 # Finally, we need to add two postgres users: one for the user that does
89 # the import and another for the webserver which should access the database
90 # for reading only:
91 #
92
93     sudo -u postgres createuser -s $USERNAME
94     sudo -u postgres createuser www-data
95
96 #
97 # Installing Nominatim
98 # ====================
99 #
100 # Building and Configuration
101 # --------------------------
102 #
103 # Get the source code from Github and change into the source directory
104 #
105 if [ "x$1" == "xyes" ]; then  #DOCS:    :::sh
106     cd $USERHOME
107     git clone --recursive https://github.com/openstreetmap/Nominatim.git
108     cd Nominatim
109 else                               #DOCS:
110     cd $USERHOME/Nominatim         #DOCS:
111 fi                                 #DOCS:
112
113 # When installing the latest source from github, you also need to
114 # download the country grid:
115
116 if [ ! -f data/country_osm_grid.sql.gz ]; then       #DOCS:    :::sh
117     wget -O data/country_osm_grid.sql.gz https://nominatim.org/data/country_grid.sql.gz
118 fi                                 #DOCS:
119
120 # The code must be built in a separate directory. Create this directory,
121 # then configure and build Nominatim in there:
122
123     mkdir $USERHOME/build
124     cd $USERHOME/build
125     cmake $USERHOME/Nominatim
126     make
127     sudo make install
128
129 # Nominatim is now ready to use. You can continue with
130 # [importing a database from OSM data](../admin/Import.md). If you want to set up
131 # a webserver first, continue reading.
132 #
133 # Setting up a webserver
134 # ======================
135 #
136 # The webserver should serve the php scripts from the website directory of your
137 # [project directory](../admin/Import.md#creating-the-project-directory).
138 # This directory needs to exist when being configured.
139 # Therefore set up a project directory and create a website directory:
140
141     mkdir $USERHOME/nominatim-project
142     mkdir $USERHOME/nominatim-project/website
143
144 # The import process will populate the directory later.
145
146 #
147 # Option 1: Using Apache
148 # ----------------------
149 #
150 if [ "x$2" == "xinstall-apache" ]; then #DOCS:
151 #
152 # Apache has a PHP module that can be used to serve Nominatim. To install them
153 # run:
154
155     sudo apt install -y apache2 libapache2-mod-php
156
157 # You need to create an alias to the website directory in your apache
158 # configuration. Add a separate nominatim configuration to your webserver:
159
160 #DOCS:```sh
161 sudo tee /etc/apache2/conf-available/nominatim.conf << EOFAPACHECONF
162 <Directory "$USERHOME/nominatim-project/website">
163   Options FollowSymLinks MultiViews
164   AddType text/html   .php
165   DirectoryIndex search.php
166   Require all granted
167 </Directory>
168
169 Alias /nominatim $USERHOME/nominatim-project/website
170 EOFAPACHECONF
171 #DOCS:```
172
173 #
174 # Then enable the configuration and restart apache
175 #
176
177     sudo a2enconf nominatim
178 if [ "x$NOSYSTEMD" == "xyes" ]; then  #DOCS:
179     sudo apache2ctl start             #DOCS:
180 else                                  #DOCS:
181     sudo systemctl restart apache2
182 fi                                    #DOCS:
183
184 # The Nominatim API is now available at `http://localhost/nominatim/`.
185
186 fi   #DOCS:
187
188 #
189 # Option 2: Using nginx
190 # ---------------------
191 #
192 if [ "x$2" == "xinstall-nginx" ]; then #DOCS:
193
194 # Nginx has no native support for php scripts. You need to set up php-fpm for
195 # this purpose. First install nginx and php-fpm:
196
197     sudo apt install -y nginx php-fpm
198
199 # You need to configure php-fpm to listen on a Unix socket.
200
201 #DOCS:```sh
202 sudo tee /etc/php/8.1/fpm/pool.d/www.conf << EOF_PHP_FPM_CONF
203 [www]
204 ; Replace the tcp listener and add the unix socket
205 listen = /var/run/php-fpm-nominatim.sock
206
207 ; Ensure that the daemon runs as the correct user
208 listen.owner = www-data
209 listen.group = www-data
210 listen.mode = 0666
211
212 ; Unix user of FPM processes
213 user = www-data
214 group = www-data
215
216 ; Choose process manager type (static, dynamic, ondemand)
217 pm = ondemand
218 pm.max_children = 5
219 EOF_PHP_FPM_CONF
220 #DOCS:```
221
222 # Then create a Nginx configuration to forward http requests to that socket.
223
224 #DOCS:```sh
225 sudo tee /etc/nginx/sites-available/default << EOF_NGINX_CONF
226 server {
227     listen 80 default_server;
228     listen [::]:80 default_server;
229
230     root $USERHOME/nominatim-project/website;
231     index search.php index.html;
232     location / {
233         try_files \$uri \$uri/ @php;
234     }
235
236     location @php {
237         fastcgi_param SCRIPT_FILENAME "\$document_root\$uri.php";
238         fastcgi_param PATH_TRANSLATED "\$document_root\$uri.php";
239         fastcgi_param QUERY_STRING    \$args;
240         fastcgi_pass unix:/var/run/php-fpm-nominatim.sock;
241         fastcgi_index index.php;
242         include fastcgi_params;
243     }
244
245     location ~ [^/]\.php(/|$) {
246         fastcgi_split_path_info ^(.+?\.php)(/.*)$;
247         if (!-f \$document_root\$fastcgi_script_name) {
248             return 404;
249         }
250         fastcgi_pass unix:/var/run/php-fpm-nominatim.sock;
251         fastcgi_index search.php;
252         include fastcgi.conf;
253     }
254 }
255 EOF_NGINX_CONF
256 #DOCS:```
257
258 # If you have some errors, make sure that php-fpm-nominatim.sock is well under
259 # /var/run/ and not under /var/run/php. Otherwise change the Nginx configuration
260 # to /var/run/php/php-fpm-nominatim.sock.
261 #
262 # Enable the configuration and restart Nginx
263 #
264
265 if [ "x$NOSYSTEMD" == "xyes" ]; then  #DOCS:
266     sudo /usr/sbin/php-fpm8.1 --nodaemonize --fpm-config /etc/php/8.1/fpm/php-fpm.conf & #DOCS:
267     sudo /usr/sbin/nginx &            #DOCS:
268 else                                  #DOCS:
269     sudo systemctl restart php8.1-fpm nginx
270 fi                                    #DOCS:
271
272 # The Nominatim API is now available at `http://localhost/`.
273
274
275
276 fi   #DOCS: