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