]> git.openstreetmap.org Git - nominatim.git/blob - vagrant/Install-on-Ubuntu-20.sh
add base framework for library reference
[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 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                         nlohmann-json3-dev postgresql-12-postgis-3 \
27                         postgresql-contrib-12 postgresql-12-postgis-3-scripts \
28                         php-cli php-pgsql php-intl 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 *Postgres Tuning* in
82 # [the installation page](../admin/Installation.md#postgresql-tuning)
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 # a webserver first, continue reading.
137 #
138 # Setting up a webserver
139 # ======================
140 #
141 # The webserver should serve the php scripts from the website directory of your
142 # [project directory](../admin/Import.md#creating-the-project-directory).
143 # This directory needs to exist when being configured.
144 # Therefore set up a project directory and create a website directory:
145
146     mkdir $USERHOME/nominatim-project
147     mkdir $USERHOME/nominatim-project/website
148
149 # The import process will populate the directory later.
150
151 #
152 # Option 1: Using Apache
153 # ----------------------
154 #
155 if [ "x$2" == "xinstall-apache" ]; then #DOCS:
156 #
157 # Apache has a PHP module that can be used to serve Nominatim. To install them
158 # run:
159
160     sudo apt install -y apache2 libapache2-mod-php
161
162 # You need to create an alias to the website directory in your apache
163 # configuration. Add a separate nominatim configuration to your webserver:
164
165 #DOCS:```sh
166 sudo tee /etc/apache2/conf-available/nominatim.conf << EOFAPACHECONF
167 <Directory "$USERHOME/nominatim-project/website">
168   Options FollowSymLinks MultiViews
169   AddType text/html   .php
170   DirectoryIndex search.php
171   Require all granted
172 </Directory>
173
174 Alias /nominatim $USERHOME/nominatim-project/website
175 EOFAPACHECONF
176 #DOCS:```
177
178 #
179 # Then enable the configuration and restart apache
180 #
181
182     sudo a2enconf nominatim
183 if [ "x$NOSYSTEMD" == "xyes" ]; then  #DOCS:
184     sudo apache2ctl start             #DOCS:
185 else                                  #DOCS:
186     sudo systemctl restart apache2
187 fi                                    #DOCS:
188
189 # The Nominatim API is now available at `http://localhost/nominatim/`.
190
191 fi   #DOCS:
192
193 #
194 # Option 2: Using nginx
195 # ---------------------
196 #
197 if [ "x$2" == "xinstall-nginx" ]; then #DOCS:
198
199 # Nginx has no native support for php scripts. You need to set up php-fpm for
200 # this purpose. First install nginx and php-fpm:
201
202     sudo apt install -y nginx php-fpm
203
204 # You need to configure php-fpm to listen on a Unix socket.
205
206 #DOCS:```sh
207 sudo tee /etc/php/7.4/fpm/pool.d/www.conf << EOF_PHP_FPM_CONF
208 [www]
209 ; Replace the tcp listener and add the unix socket
210 listen = /var/run/php-fpm-nominatim.sock
211
212 ; Ensure that the daemon runs as the correct user
213 listen.owner = www-data
214 listen.group = www-data
215 listen.mode = 0666
216
217 ; Unix user of FPM processes
218 user = www-data
219 group = www-data
220
221 ; Choose process manager type (static, dynamic, ondemand)
222 pm = ondemand
223 pm.max_children = 5
224 EOF_PHP_FPM_CONF
225 #DOCS:```
226
227 # Then create a Nginx configuration to forward http requests to that socket.
228
229 #DOCS:```sh
230 sudo tee /etc/nginx/sites-available/default << EOF_NGINX_CONF
231 server {
232     listen 80 default_server;
233     listen [::]:80 default_server;
234
235     root $USERHOME/nominatim-project/website;
236     index search.php index.html;
237     location / {
238         try_files \$uri \$uri/ @php;
239     }
240
241     location @php {
242         fastcgi_param SCRIPT_FILENAME "\$document_root\$uri.php";
243         fastcgi_param PATH_TRANSLATED "\$document_root\$uri.php";
244         fastcgi_param QUERY_STRING    \$args;
245         fastcgi_pass unix:/var/run/php-fpm-nominatim.sock;
246         fastcgi_index index.php;
247         include fastcgi_params;
248     }
249
250     location ~ [^/]\.php(/|$) {
251         fastcgi_split_path_info ^(.+?\.php)(/.*)$;
252         if (!-f \$document_root\$fastcgi_script_name) {
253             return 404;
254         }
255         fastcgi_pass unix:/var/run/php-fpm-nominatim.sock;
256         fastcgi_index search.php;
257         include fastcgi.conf;
258     }
259 }
260 EOF_NGINX_CONF
261 #DOCS:```
262
263 # If you have some errors, make sure that php-fpm-nominatim.sock is well under
264 # /var/run/ and not under /var/run/php. Otherwise change the Nginx configuration
265 # to /var/run/php/php-fpm-nominatim.sock.
266 #
267 # Enable the configuration and restart Nginx
268 #
269
270 if [ "x$NOSYSTEMD" == "xyes" ]; then  #DOCS:
271     sudo /usr/sbin/php-fpm7.4 --nodaemonize --fpm-config /etc/php/7.4/fpm/php-fpm.conf & #DOCS:
272     sudo /usr/sbin/nginx &            #DOCS:
273 else                                  #DOCS:
274     sudo systemctl restart php7.4-fpm nginx
275 fi                                    #DOCS:
276
277 # The Nominatim API is now available at `http://localhost/`.
278
279
280
281 fi   #DOCS: