]> git.openstreetmap.org Git - nominatim.git/blob - vagrant/Install-on-Ubuntu-18.sh
e36086e19159128eb59c418c4b9559e6a9182c92
[nominatim.git] / vagrant / Install-on-Ubuntu-18.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 #
9 # *Note:* these installation instructions are also available in executable
10 #         form for use with vagrant under vagrant/Install-on-Ubuntu-18.sh.
11 #
12 # Installing the Required Software
13 # ================================
14 #
15 # These instructions expect that you have a freshly installed Ubuntu 18.04.
16 #
17 # Make sure all packages are up-to-date by running:
18 #
19
20     sudo apt update -qq
21
22 # Now you can install all packages needed for Nominatim:
23
24     sudo apt install -y php-cgi
25     sudo apt install -y build-essential cmake g++ libboost-dev libboost-system-dev \
26                         libboost-filesystem-dev libexpat1-dev zlib1g-dev\
27                         libbz2-dev libpq-dev \
28                         postgresql-10-postgis-2.4 \
29                         postgresql-contrib-10 postgresql-10-postgis-scripts \
30                         php-cli php-pgsql php-intl libicu-dev python3-pip \
31                         python3-psutil python3-jinja2 python3-yaml python3-icu git
32
33 # Some of the Python packages that come with Ubuntu 18.04 are too old, so
34 # install the latest version from pip:
35
36     pip3 install --user python-dotenv datrie pyyaml psycopg2-binary
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 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/10/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 10 main start  #DOCS:
84 else                                  #DOCS:
85     sudo systemctl restart postgresql
86 fi                                    #DOCS:
87
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://www.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
131 # Nominatim is now ready to use. You can continue with
132 # [importing a database from OSM data](../admin/Import.md). If you want to set up
133 # a webserver first, continue reading.
134 #
135 # Setting up a webserver
136 # ======================
137 #
138 # The webserver should serve the php scripts from the website directory of your
139 # [project directory](../admin/Import.md#creating-the-project-directory).
140 # This directory needs to exist when being configured.
141 # Therefore set up a project directory and create the website directory:
142
143     mkdir $USERHOME/nominatim-project
144     mkdir $USERHOME/nominatim-project/website
145
146 # The import process will populate the directory later.
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 with
176 #
177
178     sudo a2enconf nominatim
179
180 # and restart apache:
181
182 if [ "x$NOSYSTEMD" == "xyes" ]; then  #DOCS:
183     sudo apache2ctl start             #DOCS:
184 else                                  #DOCS:
185     sudo systemctl restart apache2
186 fi                                    #DOCS:
187
188 # The Nominatim API is now available at `http://localhost/nominatim/`.
189
190 fi   #DOCS:
191
192 #
193 # Option 2: Using nginx
194 # ---------------------
195 #
196 if [ "x$2" == "xinstall-nginx" ]; then #DOCS:
197
198 # Nginx has no native support for php scripts. You need to set up php-fpm for
199 # this purpose. First install nginx and php-fpm:
200
201     sudo apt install -y nginx php-fpm
202
203 # You need to configure php-fpm to listen on a Unix socket.
204
205 #DOCS:```sh
206 sudo tee /etc/php/7.2/fpm/pool.d/www.conf << EOF_PHP_FPM_CONF
207 [www]
208 ; Replace the tcp listener and add the unix socket
209 listen = /var/run/php7.2-fpm.sock
210
211 ; Ensure that the daemon runs as the correct user
212 listen.owner = www-data
213 listen.group = www-data
214 listen.mode = 0666
215
216 ; Unix user of FPM processes
217 user = www-data
218 group = www-data
219
220 ; Choose process manager type (static, dynamic, ondemand)
221 pm = ondemand
222 pm.max_children = 5
223 EOF_PHP_FPM_CONF
224 #DOCS:```
225
226 # Then create a Nginx configuration to forward http requests to that socket.
227
228 #DOCS:```sh
229 sudo tee /etc/nginx/sites-available/default << EOF_NGINX_CONF
230 server {
231     listen 80 default_server;
232     listen [::]:80 default_server;
233
234     root $USERHOME/nominatim-project/website;
235     index search.php index.html;
236     location / {
237         try_files \$uri \$uri/ @php;
238     }
239
240     location @php {
241         fastcgi_param SCRIPT_FILENAME "\$document_root\$uri.php";
242         fastcgi_param PATH_TRANSLATED "\$document_root\$uri.php";
243         fastcgi_param QUERY_STRING    \$args;
244         fastcgi_pass unix:/var/run/php7.2-fpm.sock;
245         fastcgi_index index.php;
246         include fastcgi_params;
247     }
248
249     location ~ [^/]\.php(/|$) {
250         fastcgi_split_path_info ^(.+?\.php)(/.*)$;
251         if (!-f \$document_root\$fastcgi_script_name) {
252             return 404;
253         }
254         fastcgi_pass unix:/var/run/php7.2-fpm.sock;
255         fastcgi_index search.php;
256         include fastcgi.conf;
257     }
258 }
259 EOF_NGINX_CONF
260 #DOCS:```
261
262 #
263 # Enable the configuration and restart Nginx
264 #
265
266 if [ "x$NOSYSTEMD" == "xyes" ]; then  #DOCS:
267     sudo /usr/sbin/php-fpm7.2 --nodaemonize --fpm-config /etc/php/7.2/fpm/php-fpm.conf & #DOCS:
268     sudo /usr/sbin/nginx &            #DOCS:
269 else                                  #DOCS:
270     sudo systemctl restart php7.2-fpm nginx
271 fi                                    #DOCS:
272
273 # The Nominatim API is now available at `http://localhost/`.
274
275
276
277 fi   #DOCS: