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