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