InstallationGuidelines/Linux/Server/ApachePostgresqlDebian8: install-eden-apache-postgis-debian8.sh

File install-eden-apache-postgis-debian8.sh, 17.6 KB (added by arminr, 6 years ago)
Line 
1#!/bin/bash
2
3# Script to turn a generic Jessie box into an Eden server
4# with apache & PostgreSQL
5# - tunes PostgreSQL for 512Mb RAM (e.g. Amazon Micro (free tier))
6# - run pg1024 to tune for 1Gb RAM (e.g. Amazon Small or greater)
7
8#configuration for specific installation - relevant for database cleaning script
9database_name="sahana"
10database_port="5432"
11database_user="sahana"
12database_password="sahana"
13
14#versions of external libraries
15reportlab_version="3.2.0"
16Shapely_version="1.5.13"
17xlrd_version="0.9.4"
18
19#configuration of apache24 config files
20extension='.conf'
21use_proxy="false"
22http_proxy_host="10.240.20.164"
23http_proxy_port="8080"
24https_proxy_host="10.240.20.164"
25https_proxy_port="8080"
26
27#apache confs
28hostname=""
29DOMAIN=""
30hostname_error="sahana_error"
31hostname_access="sahana_access"
32GRANT=""
33
34if [ $use_proxy == 'true' ]; then
35 #set proxy env for wget from shell
36 export http_proxy=http://$http_proxy_host:$http_proxy_port
37 export https_proxy=http://$https_proxy_host:$https_proxy_port
38
39 #for git access behind proxy
40 git config --global http.proxy http://$http_proxy_host:$http_proxy_port
41 git config --global https.proxy http://$https_proxy_host:$https_proxy_port
42fi
43
44#########
45# Standard debian packages
46#########
47# Update debian system
48apt-get update
49apt-get -y upgrade
50apt-get clean
51# Install Admin Tools
52apt-get -y install unzip psmisc mlocate telnet lrzsz vim elinks-lite rcconf htop sudo p7zip dos2unix curl
53apt-get clean
54# Git
55apt-get -y install git-core
56apt-get clean
57# Email
58apt-get -y install exim4-config exim4-daemon-light
59apt-get clean
60
61#########
62# Python debian packages
63#########
64# Install Libraries
65apt-get -y install libgeos-c1
66# Install Python
67#apt-get -y install python2.7
68apt-get -y install python-dev
69# 100 Mb of diskspace due to deps, so only if you want an advanced shell
70#apt-get -y install ipython
71apt-get clean
72apt-get -y install python-lxml python-setuptools python-dateutil
73apt-get clean
74apt-get -y install python-serial
75#apt-get -y install python-imaging python-reportlab
76apt-get -y install python-imaging
77apt-get -y install python-matplotlib
78apt-get -y install python-requests
79apt-get -y install python-xlwt
80apt-get -y install build-essential
81apt-get clean
82
83#########
84# External packages
85#########
86# Upgrade ReportLab for Percentage support
87#apt-get remove -y python-reportlab
88wget --no-check-certificate http://pypi.python.org/packages/source/r/reportlab/reportlab-$reportlab_version.tar.gz
89tar zxvf reportlab-$reportlab_version.tar.gz
90cd reportlab-$reportlab_version
91python setup.py install
92cd ..
93
94# Upgrade Shapely for Simplify enhancements
95#apt-get remove -y python-shapely
96apt-get -y install libgeos-dev
97wget --no-check-certificate http://pypi.python.org/packages/source/S/Shapely/Shapely-$Shapely_version.tar.gz
98tar zxvf Shapely-$Shapely_version.tar.gz
99cd Shapely-$Shapely_version
100python setup.py install
101cd ..
102
103# Upgrade XLRD for XLS import support
104#apt-get remove -y python-xlrd
105wget --no-check-certificate http://pypi.python.org/packages/source/x/xlrd/xlrd-$xlrd_version.tar.gz
106tar zxvf xlrd-$xlrd_version.tar.gz
107cd xlrd-$xlrd_version
108python setup.py install
109cd ..
110
111#########
112# Web2Py
113#########
114apt-get -y install libodbc1
115# Install Web2Py
116# Add system user
117adduser --system --disabled-password web2py
118addgroup web2py
119cd /home
120# Delete old web2py version
121rm web2py_src.zip
122# Load actual web2py version
123wget --no-check-certificate http://www.web2py.com/examples/static/web2py_src.zip
124unzip web2py_src.zip
125ln -s /home/web2py ~
126cp -f /home/web2py/handlers/wsgihandler.py /home/web2py
127
128# Define the routes configuration
129cat << EOF > "/home/web2py/routes.py"
130#!/usr/bin/python
131default_application = 'eden'
132default_controller = 'default'
133default_function = 'index'
134routes_onerror = [
135 ('eden/400', '!'),
136 ('eden/401', '!'),
137 ('eden/509', '!'),
138 ('eden/*', '/eden/errors/index'),
139 ('*/*', '/eden/errors/index'),
140 ]
141EOF
142
143# Configure Matplotlib
144mkdir /home/web2py/.matplotlib
145chown web2py /home/web2py/.matplotlib
146echo "os.environ['MPLCONFIGDIR'] = '/home/web2py/.matplotlib'" >> /home/web2py/wsgihandler.py
147sed -i 's|TkAgg|Agg|' /etc/matplotlibrc
148
149##############
150# Sahana Eden
151##############
152# Install Sahana Eden
153cd web2py
154cd applications
155# @ToDo: Stable branch
156git clone git://github.com/flavour/eden.git
157# Fix permissions
158chown web2py ~web2py
159chown web2py ~web2py/applications/admin/cache
160chown web2py ~web2py/applications/admin/cron
161chown web2py ~web2py/applications/admin/databases
162chown web2py ~web2py/applications/admin/errors
163chown web2py ~web2py/applications/admin/sessions
164chown web2py ~web2py/applications/eden
165chown web2py ~web2py/applications/eden/cache
166chown web2py ~web2py/applications/eden/cron
167mkdir -p ~web2py/applications/eden/databases
168chown web2py ~web2py/applications/eden/databases
169mkdir -p ~web2py/applications/eden/errors
170chown web2py ~web2py/applications/eden/errors
171chown web2py ~web2py/applications/eden/models
172mkdir -p ~web2py/applications/eden/sessions
173chown web2py ~web2py/applications/eden/sessions
174chown web2py ~web2py/applications/eden/static/fonts
175chown web2py ~web2py/applications/eden/static/img/markers
176mkdir -p ~web2py/applications/eden/static/cache/chart
177chown web2py -R ~web2py/applications/eden/static/cache
178mkdir -p ~web2py/applications/eden/uploads/gis_cache
179mkdir -p ~web2py/applications/eden/uploads/images
180mkdir -p ~web2py/applications/eden/uploads/tracks
181chown web2py ~web2py/applications/eden/uploads
182chown web2py ~web2py/applications/eden/uploads/gis_cache
183chown web2py ~web2py/applications/eden/uploads/images
184chown web2py ~web2py/applications/eden/uploads/tracks
185ln -s /home/web2py/applications/eden /home/web2py
186ln -s /home/web2py/applications/eden ~
187
188## Add Scheduler config
189cat << EOF > "/home/web2py/run_scheduler.py"
190#!/usr/bin/env python
191# -*- coding: utf-8 -*-
192
193import os
194import sys
195
196if '__file__' in globals():
197 path = os.path.dirname(os.path.abspath(__file__))
198 os.chdir(path)
199else:
200 path = os.getcwd() # Seems necessary for py2exe
201
202sys.path = [path]+[p for p in sys.path if not p==path]
203
204# import gluon.import_all ##### This should be uncommented for py2exe.py
205import gluon.widget
206from gluon.shell import run
207
208# Start Web2py Scheduler -- Note the app name is hardcoded!
209if __name__ == '__main__':
210 run('eden',True,True,None,False,"from gluon import current; current._scheduler.loop()")
211EOF
212
213#########
214# Apache2 configuration
215#########
216apt-get -y install libapache2-mod-wsgi
217a2enmod rewrite
218a2enmod deflate
219a2enmod headers
220a2enmod expires
221# Enable Basic Authentication for WebServices
222sed -i 's|</IfModule>|WSGIPassAuthorization On|' /etc/apache2/mods-enabled/wsgi.conf
223echo "</IfModule>" >> /etc/apache2/mods-enabled/wsgi.conf
224# Prevent Memory leaks from killing servers
225sed -i 's|MaxRequestsPerChild 0|MaxRequestsPerChild 300|' /etc/apache2/apache2.conf
226# Tune for smaller RAM setups
227sed -i 's|MinSpareServers 5|MinSpareServers 3|' /etc/apache2/apache2.conf
228sed -i 's|MaxSpareServers 10|MaxSpareServers 6|' /etc/apache2/apache2.conf
229# Activate apache root - disabled as default since debian 8!
230sed -i 's|Require all denied|#Require all denied|' /etc/apache2/apache2.conf
231apache2ctl restart
232# Holding Page for Maintenance windows
233cat << EOF > "/var/www/maintenance.html"
234<html><body><h1>Site Maintenance</h1>Please try again later...</body></html>
235EOF
236
237/etc/init.d/apache2 restart
238
239# -----------------------------------------------------------------------------
240# Further apache2 web server configuration
241# -----------------------------------------------------------------------------
242echo "Setting up Web server"
243# Disable default site
244rm -f /etc/apache2/sites-enabled/000-default.conf
245# Define VirtualHost for production site
246cat << EOF > "/etc/apache2/sites-available/production$extension"
247<VirtualHost *:80>
248 ServerName $hostname.$DOMAIN
249 ServerAdmin webmaster@$DOMAIN
250 DocumentRoot /home/web2py/applications
251 WSGIScriptAlias / /home/web2py/wsgihandler.py
252 ## Edit the process and the maximum-requests to reflect your RAM
253 WSGIDaemonProcess web2py user=web2py group=web2py home=/home/web2py processes=4 maximum-requests=100
254 RewriteEngine On
255 # Stop GoogleBot from slowing us down
256 RewriteRule .*robots\.txt$ /eden/static/robots.txt [L]
257 # extract desired cookie value from multiple-cookie HTTP header
258 #RewriteCond %{HTTP_COOKIE} registered=([^;]+)
259 # check that cookie value is correct
260 #RewriteCond %1 ^yes$
261 #RewriteRule ^/$ /eden/ [R,L]
262 #RewriteRule ^/$ /eden/static/index.html [R,L]
263 RewriteCond %{REQUEST_URI} !/phpmyadmin(.*)
264 RewriteCond %{REQUEST_URI} !/eden/(.*)
265 RewriteRule /(.*) /eden/$1 [R]
266 ### static files do not need WSGI
267 <LocationMatch "^(/[\w_]*/static/.*)">
268 Order Allow,Deny
269 Allow from all
270 SetOutputFilter DEFLATE
271 BrowserMatch ^Mozilla/4 gzip-only-text/html
272 BrowserMatch ^Mozilla/4\.0[678] no-gzip
273 BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
274 SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
275 Header append Vary User-Agent env=!dont-vary
276 ExpiresActive On
277 ExpiresByType text/html "access plus 1 day"
278 ExpiresByType text/javascript "access plus 1 week"
279 ExpiresByType text/css "access plus 2 weeks"
280 ExpiresByType image/ico "access plus 1 month"
281 ExpiresByType image/gif "access plus 1 month"
282 ExpiresByType image/jpeg "access plus 1 month"
283 ExpiresByType image/jpg "access plus 1 month"
284 ExpiresByType image/png "access plus 1 month"
285 ExpiresByType application/x-shockwave-flash "access plus 1 month"
286 </LocationMatch>
287 ### everything else goes over WSGI
288 <Location "/">
289 $GRANT
290 WSGIProcessGroup web2py
291 </Location>
292 ErrorLog /var/log/apache2/$hostname_error.log
293 LogLevel warn
294 CustomLog /var/log/apache2/$hostname_access.log combined
295</VirtualHost>
296EOF
297
298# Enable production site
299a2ensite production
300apache2ctl restart
301
302# Define maintenance site
303cat << EOF > "/etc/apache2/sites-available/maintenance$extension"
304<VirtualHost *:80>
305 ServerName $hostname.$DOMAIN
306 ServerAdmin webmaster@$DOMAIN
307 DocumentRoot /var/www
308 RewriteEngine On
309 RewriteRule ^/(.*) /maintenance.html
310 <Location "/">
311 $GRANT
312 </Location>
313 ErrorLog /var/log/apache2/maintenance_error.log
314 LogLevel warn
315 CustomLog /var/log/apache2/maintenance_access.log combined
316</VirtualHost>
317EOF
318
319############
320# PostgreSQL
321############
322apt-get update
323apt-get -y install postgresql-9.4 python-psycopg2 postgresql-9.4-postgis ptop
324
325# Tune PostgreSQL
326cat << EOF >> "/etc/sysctl.conf"
327## Increase Shared Memory available for PostgreSQL
328# 512Mb
329kernel.shmmax = 279134208
330# 1024Mb (may need more)
331#kernel.shmmax = 552992768
332kernel.shmall = 2097152
333EOF
334sysctl -w kernel.shmmax=279134208 # For 512 MB RAM
335#sysctl -w kernel.shmmax=552992768 # For 1024 MB RAM
336sysctl -w kernel.shmall=2097152
337
338sed -i 's|#track_counts = on|track_counts = on|' /etc/postgresql/9.4/main/postgresql.conf
339sed -i 's|#autovacuum = on|autovacuum = on|' /etc/postgresql/9.4/main/postgresql.conf
340# 512Mb RAM:
341sed -i 's|shared_buffers = 28MB|shared_buffers = 56MB|' /etc/postgresql/9.4/main/postgresql.conf
342sed -i 's|#effective_cache_size = 128MB|effective_cache_size = 256MB|' /etc/postgresql/9.4/main/postgresql.conf
343sed -i 's|#work_mem = 1MB|work_mem = 2MB|' /etc/postgresql/9.4/main/postgresql.conf
344# If 1Gb+ RAM, activate post-install via pg1024 script
345
346#####################
347# Adopted management scripts from old cherokee installation
348#####################
349cat << EOF > "/usr/local/bin/maintenance"
350#!/bin/sh
351# Script to activate/deactivate the maintenance site
352# Can provide the option 'off' to disable the maintenance site
353if [ "\$1" != "off" ]; then
354 # Stop the Scheduler
355 killall python
356 # Deactivate the Production Site
357 a2dissite production
358 # Activate the Maintenance Site
359 a2ensite maintenance
360else
361 # Deactivate the Maintenance Site
362 a2dissite maintenance
363 # Activate the Production Site
364 a2ensite production
365 # Start the Scheduler
366 cd ~web2py && sudo -H -u web2py nohup python web2py.py -K eden -Q >/dev/null 2>&1 &
367fi
368apache2ctl restart
369EOF
370chmod +x /usr/local/bin/maintenance
371#####################
372cat << EOF > "/usr/local/bin/backup"
373#!/bin/sh
374mkdir /var/backups/eden
375chown postgres /var/backups/eden
376NOW=\$(date +"%Y-%m-%d")
377su -c - postgres "pg_dump -c sahana > /var/backups/eden/sahana-\$NOW.sql"
378#su -c - postgres "pg_dump -Fc gis > /var/backups/eden/gis.dmp"
379OLD=\$(date --date='7 day ago' +"%Y-%m-%d")
380rm -f /var/backups/eden/sahana-\$OLD.sql
381mkdir /var/backups/eden/uploads
382tar -cf /var/backups/eden/uploads/uploadsprod-\$NOW.tar -C /home/web2py/applications/eden ./uploads
383bzip2 /var/backups/eden/uploads/uploadsprod-\$NOW.tar
384rm -f /var/backups/eden/uploads/uploadsprod-\$OLD.tar.bz2
385EOF
386chmod +x /usr/local/bin/backup
387#####################
388cat << EOF > "/usr/local/bin/compile"
389#!/bin/bash
390/etc/init.d/apache2 stop
391cd ~web2py
392python web2py.py -S eden -M -R applications/eden/static/scripts/tools/compile.py
393/etc/init.d/apache2 start
394EOF
395chmod +x /usr/local/bin/compile
396#####################
397cat << EOF > "/usr/local/bin/pull"
398#!/bin/sh
399/etc/init.d/apache2 stop
400cd ~web2py/applications/eden
401sed -i 's/settings.base.migrate = False/settings.base.migrate = True/g' models/000_config.py
402git reset --hard HEAD
403git pull
404rm -rf compiled
405cd ~web2py
406sudo -H -u web2py python web2py.py -S eden -M -R applications/eden/static/scripts/tools/noop.py
407cd ~web2py/applications/eden
408sed -i 's/settings.base.migrate = True/settings.base.migrate = False/g' models/000_config.py
409cd ~web2py
410python web2py.py -S eden -M -R applications/eden/static/scripts/tools/compile.py
411/etc/init.d/apache2 start
412EOF
413chmod +x /usr/local/bin/pull
414#####################
415cat << EOF > "/usr/local/bin/migrate"
416#!/bin/sh
417/etc/init.d/apache2 stop
418cd ~web2py/applications/eden
419sed -i 's/settings.base.migrate = False/settings.base.migrate = True/g' models/000_config.py
420rm -rf compiled
421cd ~web2py
422sudo -H -u web2py python web2py.py -S eden -M -R applications/eden/static/scripts/tools/noop.py
423cd ~web2py/applications/eden
424sed -i 's/settings.base.migrate = True/settings.base.migrate = False/g' models/000_config.py
425cd ~web2py
426python web2py.py -S eden -M -R applications/eden/static/scripts/tools/compile.py
427/etc/init.d/apache2 start
428EOF
429chmod +x /usr/local/bin/migrate
430#####################
431cat << EOF > "/usr/local/bin/revert"
432#!/bin/sh
433git reset --hard HEAD
434EOF
435chmod +x /usr/local/bin/revert
436cat << EOF > "/usr/local/bin/w2p"
437#!/bin/sh
438cd ~web2py
439python web2py.py -S eden -M
440EOF
441chmod +x /usr/local/bin/w2p
442#####################
443cat << EOF2 > "/usr/local/bin/clean"
444#!/bin/sh
445/etc/init.d/apache2 stop
446cd ~web2py/applications/eden
447rm -rf databases/*
448rm -f errors/*
449rm -rf sessions/*
450rm -rf uploads/*
451pkill -f "postgres: $database_user $database_name"
452echo "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '$database_name';" > /tmp/delete_current_connections.sql
453su - postgres -c "psql -q -p $database_port -f /tmp/delete_current_connections.sql"
454su - postgres -c "dropdb -p $database_port $database_name"
455echo "database dropped"
456sed -i 's/settings.base.migrate = False/settings.base.migrate = True/g' models/000_config.py
457sed -i 's/settings.base.prepopulate = 0/#settings.base.prepopulate = 0/g' models/000_config.py
458rm -rf compiled
459su - postgres -c "createdb -O $database_user -p $database_port -E UTF8 $database_name -T template0"
460#su -c - postgres "createlang plpgsql -d sahana"
461su - postgres -c "psql -q -p $database_port -d $database_name -f /usr/share/postgresql/9.4/contrib/postgis-2.1/postgis.sql"
462su - postgres -c "psql -q -p $database_port -d $database_name -f /usr/share/postgresql/9.4/contrib/postgis-2.1/spatial_ref_sys.sql"
463su - postgres -c "psql -q -p $database_port -d $database_name -c 'grant all on geometry_columns to $database_user;'"
464#maybe new to postgis 2.x
465su - postgres -c "psql -q -p $database_port -d $database_name -c 'grant all on geography_columns to $database_user;'"
466su - postgres -c "psql -q -p $database_port -d $database_name -c 'grant all on spatial_ref_sys to $database_user;'"
467su - postgres -c "psql -q -p $database_port -d $database_name -c 'ALTER DATABASE $database_name OWNER TO $database_user;'"
468cd ~web2py
469sudo -H -u web2py python web2py.py -S eden -M -R applications/eden/static/scripts/tools/noop.py
470cd ~web2py/applications/eden
471sed -i 's/settings.base.migrate = True/settings.base.migrate = False/g' models/000_config.py
472sed -i 's/#settings.base.prepopulate = 0/settings.base.prepopulate = 0/g' models/000_config.py
473cd ~web2py
474python web2py.py -S eden -M -R applications/eden/static/scripts/tools/compile.py
475/etc/init.d/apache2 start
476#sudo -H -u web2py python web2py.py -S eden -M -R /home/data/import.py
477EOF2
478chmod +x /usr/local/bin/clean
479#####################
480cat << EOF > "/usr/local/bin/pg1024"
481#!/bin/sh
482sed -i 's|kernel.shmmax = 279134208|#kernel.shmmax = 279134208|' /etc/sysctl.conf
483sed -i 's|#kernel.shmmax = 552992768|kernel.shmmax = 552992768|' /etc/sysctl.conf
484sysctl -w kernel.shmmax=552992768
485sed -i 's|shared_buffers = 56MB|shared_buffers = 160MB|' /etc/postgresql/9.4/main/postgresql.conf
486sed -i 's|effective_cache_size = 256MB|effective_cache_size = 512MB|' /etc/postgresql/9.4/main/postgresql.conf
487sed -i 's|work_mem = 2MB|work_mem = 4MB|' /etc/postgresql/9.4/main/postgresql.conf
488/etc/init.d/postgresql restart
489EOF
490chmod +x /usr/local/bin/pg1024
491#####################
492cat << EOF > "/usr/local/bin/pg512"
493#!/bin/sh
494sed -i 's|#kernel.shmmax = 279134208|kernel.shmmax = 279134208|' /etc/sysctl.conf
495sed -i 's|kernel.shmmax = 552992768|#kernel.shmmax = 552992768|' /etc/sysctl.conf
496sysctl -w kernel.shmmax=279134208
497sed -i 's|shared_buffers = 160MB|shared_buffers = 56MB|' /etc/postgresql/9.4/main/postgresql.conf
498sed -i 's|effective_cache_size = 512MB|effective_cache_size = 256MB|' /etc/postgresql/9.4/main/postgresql.conf
499sed -i 's|work_mem = 4MB|work_mem = 2MB|' /etc/postgresql/9.4/main/postgresql.conf
500/etc/init.d/postgresql restart
501EOF
502chmod +x /usr/local/bin/pg512
503apt-get clean
504
505# END