]> git.openstreetmap.org Git - nominatim.git/blob - vagrant/Install-on-Ubuntu-20.sh
adapt Ubunut-20 vagrant file to triple webserver config
[nominatim.git] / vagrant / Install-on-Ubuntu-20.sh
1 #!/bin/bash
2 #
3 # hacks for broken vagrant box      #DOCS:
4 sudo rm -f /var/lib/dpkg/lock       #DOCS:
5 sudo update-locale LANG=en_US.UTF-8 #DOCS:
6 export APT_LISTCHANGES_FRONTEND=none #DOCS:
7 export DEBIAN_FRONTEND=noninteractive #DOCS:
8
9 # *Note:* these installation instructions are also available in executable
10 #         form for use with vagrant under vagrant/Install-on-Ubuntu-20.sh.
11 #
12 # Installing the Required Software
13 # ================================
14 #
15 # These instructions expect that you have a freshly installed Ubuntu 20.04.
16 #
17 # Make sure all packages are are up-to-date by running:
18 #
19
20 #DOCS:    :::sh
21     sudo apt-get \ #DOCS:
22         -o DPkg::options::="--force-confdef" -o DPkg::options::="--force-confold" \ #DOCS:
23         --allow-downgrades --allow-remove-essential --allow-change-held-packages \ #DOCS:
24         -fuy install grub-pc #DOCS:
25     sudo apt update -qq
26
27 # Now you can install all packages needed for Nominatim:
28
29     sudo apt install -y php-cgi
30     sudo apt install -y build-essential cmake g++ libboost-dev libboost-system-dev \
31                         libboost-filesystem-dev libexpat1-dev zlib1g-dev \
32                         libbz2-dev libpq-dev libproj-dev \
33                         postgresql-server-dev-12 postgresql-12-postgis-3 \
34                         postgresql-contrib postgresql-12-postgis-3-scripts \
35                         php php-pgsql php-intl \
36                         python3-psycopg2 git
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 # To be able to copy and paste instructions from this manual, export
59 # user name and home directory now like this:
60 #
61     export USERNAME=vagrant        #DOCS:    export USERNAME=nominatim
62     export USERHOME=/home/vagrant  #DOCS:    export USERHOME=/srv/nominatim
63 #
64 # **Never, ever run the installation as a root user.** You have been warned.
65 #
66 # Make sure that system servers can read from the home directory:
67
68     chmod a+x $USERHOME
69
70 # Setting up PostgreSQL
71 # ---------------------
72 #
73 # Tune the postgresql configuration, which is located in 
74 # `/etc/postgresql/12/main/postgresql.conf`. See section *Postgres Tuning* in
75 # [the installation page](../admin/Installation.md#postgresql-tuning)
76 # for the parameters to change.
77 #
78 # Restart the postgresql service after updating this config file.
79
80     sudo systemctl restart postgresql
81
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 git://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     cd $USERHOME
119     mkdir build
120     cd build
121     cmake $USERHOME/Nominatim
122     make
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 # Option 1: Using Apache
132 # ----------------------
133 #
134 if [ "x$2" == "xinstall-apache" ]; then #DOCS:
135 #
136 # Apache has a PHP module that can be used to serve Nominatim. To install them
137 # run:
138
139     sudo apt install -y apache2 libapache2-mod-php
140
141 # You need to create an alias to the website directory in your apache
142 # configuration. Add a separate nominatim configuration to your webserver:
143
144 #DOCS:```sh
145 sudo tee /etc/apache2/conf-available/nominatim.conf << EOFAPACHECONF
146 <Directory "$USERHOME/build/website">
147   Options FollowSymLinks MultiViews
148   AddType text/html   .php
149   DirectoryIndex search.php
150   Require all granted
151 </Directory>
152
153 Alias /nominatim $USERHOME/build/website
154 EOFAPACHECONF
155 #DOCS:```
156
157 #
158 # Then enable the configuration and restart apache
159 #
160
161     sudo a2enconf nominatim
162     sudo systemctl restart apache2
163
164 # You need to create a minimal configuration file that tells nominatim
165 # where it is located on the webserver:
166
167 #DOCS:```sh
168 tee settings/local.php << EOF
169 <?php
170  @define('CONST_Website_BaseURL', '/nominatim/');
171 EOF
172 #DOCS:```
173
174 # The Nominatim API is now available at `http://localhost/nominatim/`.
175
176 fi   #DOCS:
177
178 #
179 # Option 2: Using nginx
180 # ---------------------
181 #
182 if [ "x$2" == "xinstall-nginx" ]; then #DOCS:
183
184 # Nginx has no native support for php scripts. You need to set up php-fpm for
185 # this purpose. First install nginx and php-fpm:
186
187     sudo apt install -y nginx php-fpm
188
189 # You need to configure php-fpm to listen on a Unix socket.
190
191 #DOCS:```sh
192 sudo tee /etc/php/7.4/fpm/pool.d/www.conf << EOF_PHP_FPM_CONF
193 [www]
194 ; Replace the tcp listener and add the unix socket
195 listen = /var/run/php7.4-fpm.sock
196
197 ; Ensure that the daemon runs as the correct user
198 listen.owner = www-data
199 listen.group = www-data
200 listen.mode = 0666
201
202 ; Unix user of FPM processes
203 user = www-data
204 group = www-data
205
206 ; Choose process manager type (static, dynamic, ondemand)
207 pm = ondemand
208 pm.max_children = 5
209 EOF_PHP_FPM_CONF
210 #DOCS:```
211
212 # Then create a Nginx configuration to forward http requests to that socket.
213
214 #DOCS:```sh
215 sudo tee /etc/nginx/sites-available/default << EOF_NGINX_CONF
216 server {
217     listen 80 default_server;
218     listen [::]:80 default_server;
219
220     root $USERHOME/build/website;
221     index search.php index.html;
222     location / {
223         try_files \$uri \$uri/ @php;
224     }
225
226     location @php {
227         fastcgi_param SCRIPT_FILENAME "\$document_root\$uri.php";
228         fastcgi_param PATH_TRANSLATED "\$document_root\$uri.php";
229         fastcgi_param QUERY_STRING    \$args;
230         fastcgi_pass unix:/var/run/php7.4-fpm.sock;
231         fastcgi_index index.php;
232         include fastcgi_params;
233     }
234
235     location ~ [^/]\.php(/|$) {
236         fastcgi_split_path_info ^(.+?\.php)(/.*)$;
237         if (!-f \$document_root\$fastcgi_script_name) {
238             return 404;
239         }
240         fastcgi_pass unix:/var/run/php7.4-fpm.sock;
241         fastcgi_index search.php;
242         include fastcgi.conf;
243     }
244 }
245 EOF_NGINX_CONF
246 #DOCS:```
247
248 #
249 # Enable the configuration and restart Nginx
250 #
251
252     sudo systemctl restart php7.4-fpm nginx
253
254 # The Nominatim API is now available at `http://localhost/`.
255
256
257
258 fi   #DOCS: