source: OpenRLabs-Git/deploy/rlabs-docker/web2py-rlabs/scripts/setup-web2py-nginx-uwsgi-ubuntu.sh @ 42095c5

mainqndtest v1.1.1
Last change on this file since 42095c5 was 42bd667, checked in by David Fuertes <dfuertes@…>, 4 years ago

Historial Limpio

  • Property mode set to 100755
File size: 7.4 KB
Line 
1#!/bin/bash
2echo 'setup-web2py-nginx-uwsgi-ubuntu-precise.sh'
3echo 'Requires Ubuntu > 12.04 or Debian >= 8 and installs Nginx + uWSGI + Web2py'
4# Check if user has root privileges
5if [[ $EUID -ne 0 ]]; then
6   echo "You must run the script as root or using sudo"
7   exit 1
8fi
9# parse command line arguments
10nopassword=0
11nocertificate=0
12while [ "$#" -gt 0 ]; do
13  case "$1" in
14    --no-password) nopassword=1; shift 1;;
15    --no-certificate) nocertificate=1; shift 1;;
16  esac
17done
18# Get Web2py Admin Password
19if [ "$nopassword" -eq 0 ]
20then
21  echo -e "Web2py Admin Password: \c "
22  read -s PW
23  printf "\n"  # fix no new line artifact of "read -s" to avoid cleartext password
24fi
25# Upgrade and install needed software
26apt-get update
27apt-get -y upgrade
28apt-get autoremove
29apt-get autoclean
30apt-get -y install nginx-full
31apt-get -y install build-essential python-dev libxml2-dev python-pip unzip
32pip install setuptools --no-use-wheel --upgrade
33PIPPATH=`which pip`
34$PIPPATH install --upgrade uwsgi
35# Create common nginx sections
36mkdir /etc/nginx/conf.d/web2py
37echo '
38gzip_static on;
39gzip_http_version   1.1;
40gzip_proxied        expired no-cache no-store private auth;
41gzip_disable        "MSIE [1-6]\.";
42gzip_vary           on;
43' > /etc/nginx/conf.d/web2py/gzip_static.conf
44echo '
45gzip on;
46gzip_disable "msie6";
47gzip_vary on;
48gzip_proxied any;
49gzip_comp_level 6;
50gzip_buffers 16 8k;
51gzip_http_version 1.1;
52gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
53' > /etc/nginx/conf.d/web2py/gzip.conf
54# Create configuration file /etc/nginx/sites-available/web2py
55echo 'server {
56        listen          80;
57        server_name     $hostname;
58        ###to enable correct use of response.static_version
59        location ~* ^/(\w+)/static(?:/_[\d]+\.[\d]+\.[\d]+)?/(.*)$ {
60            alias /home/www-data/web2py/applications/$1/static/$2;
61            expires max;
62            ### if you want to use pre-gzipped static files (recommended)
63            ### check scripts/zip_static_files.py and remove the comments
64            # include /etc/nginx/conf.d/web2py/gzip_static.conf;
65        }
66        ###
67
68        ###if you use something like myapp = dict(languages=['en', 'it', 'jp'], default_language='en') in your routes.py
69        #location ~* ^/(\w+)/(en|it|jp)/static/(.*)$ {
70        #    alias /home/www-data/web2py/applications/$1/;
71        #    try_files static/$2/$3 static/$3 =404;
72        #}
73        ###
74       
75        location / {
76            #uwsgi_pass      127.0.0.1:9001;
77            uwsgi_pass      unix:///tmp/web2py.socket;
78            include         uwsgi_params;
79            uwsgi_param     UWSGI_SCHEME $scheme;
80            uwsgi_param     SERVER_SOFTWARE    nginx/$nginx_version;
81
82            ###remove the comments to turn on if you want gzip compression of your pages
83            # include /etc/nginx/conf.d/web2py/gzip.conf;
84            ### end gzip section
85
86            ### remove the comments if you use uploads (max 10 MB)
87            #client_max_body_size 10m;
88            ###
89        }
90}
91server {
92        listen 443 default_server ssl;
93        server_name     $hostname;
94        ssl_certificate         /etc/nginx/ssl/web2py.crt;
95        ssl_certificate_key     /etc/nginx/ssl/web2py.key;
96        ssl_prefer_server_ciphers on;
97        ssl_session_cache shared:SSL:10m;
98        ssl_session_timeout 10m;
99        ssl_ciphers ECDHE-RSA-AES256-SHA:DHE-RSA-AES256-SHA:DHE-DSS-AES256-SHA:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA;
100        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
101        keepalive_timeout    70;
102        location / {
103            #uwsgi_pass      127.0.0.1:9001;
104            uwsgi_pass      unix:///tmp/web2py.socket;
105            include         uwsgi_params;
106            uwsgi_param     UWSGI_SCHEME $scheme;
107            uwsgi_param     SERVER_SOFTWARE    nginx/$nginx_version;
108            ###remove the comments to turn on if you want gzip compression of your pages
109            # include /etc/nginx/conf.d/web2py/gzip.conf;
110            ### end gzip section
111            ### remove the comments if you want to enable uploads (max 10 MB)
112            #client_max_body_size 10m;
113            ###
114        }
115        ###to enable correct use of response.static_version
116        location ~* ^/(\w+)/static(?:/_[\d]+\.[\d]+\.[\d]+)?/(.*)$ {
117            alias /home/www-data/web2py/applications/$1/static/$2;
118            expires max;
119            ### if you want to use pre-gzipped static files (recommended)
120            ### check scripts/zip_static_files.py and remove the comments
121            # include /etc/nginx/conf.d/web2py/gzip_static.conf;
122        }
123        ###
124
125}' >/etc/nginx/sites-available/web2py
126
127ln -s /etc/nginx/sites-available/web2py /etc/nginx/sites-enabled/web2py
128rm /etc/nginx/sites-enabled/default
129mkdir /etc/nginx/ssl
130cd /etc/nginx/ssl
131if [ "$nocertificate" -eq 0 ]
132then
133  openssl genrsa 1024 > web2py.key
134  chmod 400 web2py.key
135  openssl req -new -x509 -nodes -sha1 -days 1780 -key web2py.key > web2py.crt
136  openssl x509 -noout -fingerprint -text < web2py.crt > web2py.info
137fi
138# Prepare folders for uwsgi
139sudo mkdir /etc/uwsgi
140sudo mkdir /var/log/uwsgi
141sudo mkdir /etc/systemd
142sudo mkdir /etc/systemd/system
143
144#uWSGI Emperor
145echo '[Unit]
146Description = uWSGI Emperor
147After = syslog.target
148
149[Service]
150ExecStart = /usr/local/bin/uwsgi --ini /etc/uwsgi/web2py.ini
151RuntimeDirectory = uwsgi
152Restart = always
153KillSignal = SIGQUIT
154Type = notify
155StandardError = syslog
156NotifyAccess = all
157
158[Install]
159WantedBy = multi-user.target
160' > /etc/systemd/system/emperor.uwsgi.service
161
162# Create configuration file /etc/uwsgi/web2py.ini
163echo '[uwsgi]
164
165socket = /tmp/web2py.socket
166pythonpath = /home/www-data/web2py/
167mount = /=wsgihandler:application
168processes = 4
169master = true
170harakiri = 60
171reload-mercy = 8
172cpu-affinity = 1
173stats = /tmp/stats.socket
174max-requests = 2000
175limit-as = 512
176reload-on-as = 256
177reload-on-rss = 192
178uid = www-data
179gid = www-data
180touch-reload = /home/www-data/web2py/routes.py
181cron = 0 0 -1 -1 -1 python /home/www-data/web2py/web2py.py -Q -S welcome -M -R scripts/sessions2trash.py -A -o
182no-orphans = true
183' >/etc/uwsgi/web2py.ini
184
185#Create a configuration file for uwsgi in emperor-mode
186#for Upstart in /etc/init/uwsgi-emperor.conf
187echo '# Emperor uWSGI script
188
189description "uWSGI Emperor"
190start on runlevel [2345]
191stop on runlevel [06]
192##
193#remove the comments in the next section to enable static file compression for the welcome app
194#in that case, turn on gzip_static on; on /etc/nginx/nginx.conf
195##
196#pre-start script
197#    python /home/www-data/web2py/web2py.py -S welcome -R scripts/zip_static_files.py
198#    chown -R www-data:www-data /home/www-data/web2py/*
199#end script
200respawn
201exec uwsgi --master --die-on-term --emperor /etc/uwsgi --logto /var/log/uwsgi/uwsgi.log
202' > /etc/init/uwsgi-emperor.conf
203# Install Web2py
204mkdir /home/www-data
205cd /home/www-data
206wget http://web2py.com/examples/static/web2py_src.zip
207unzip web2py_src.zip
208mv web2py/handlers/wsgihandler.py web2py/wsgihandler.py
209rm web2py_src.zip
210chown -R www-data:www-data web2py
211cd /home/www-data/web2py
212if [ "$nopassword" -eq 0 ]
213then
214   sudo -u www-data python -c "from gluon.main import save_password; save_password('$PW',443)"
215fi
216
217/etc/init.d/nginx start
218systemctl start emperor.uwsgi.service
219systemctl enable emperor.uwsgi.service
220
221echo <<EOF
222you can stop uwsgi and nginx with
223
224  sudo /etc/init.d/nginx stop
225  sudo systemctl stop emperor.uwsgi.service
226 
227and start it with
228
229  sudo /etc/init.d/nginx start
230  systemctl start emperor.uwsgi.service
231
232EOF
233
Note: See TracBrowser for help on using the repository browser.