1 | #!/bin/bash |
---|
2 | echo 'setup-web2py-nginx-uwsgi-ubuntu-precise.sh' |
---|
3 | echo 'Requires Ubuntu > 12.04 or Debian >= 8 and installs Nginx + uWSGI + Web2py' |
---|
4 | # Check if user has root privileges |
---|
5 | if [[ $EUID -ne 0 ]]; then |
---|
6 | echo "You must run the script as root or using sudo" |
---|
7 | exit 1 |
---|
8 | fi |
---|
9 | # parse command line arguments |
---|
10 | nopassword=0 |
---|
11 | nocertificate=0 |
---|
12 | while [ "$#" -gt 0 ]; do |
---|
13 | case "$1" in |
---|
14 | --no-password) nopassword=1; shift 1;; |
---|
15 | --no-certificate) nocertificate=1; shift 1;; |
---|
16 | esac |
---|
17 | done |
---|
18 | # Get Web2py Admin Password |
---|
19 | if [ "$nopassword" -eq 0 ] |
---|
20 | then |
---|
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 |
---|
24 | fi |
---|
25 | # Upgrade and install needed software |
---|
26 | apt-get update |
---|
27 | apt-get -y upgrade |
---|
28 | apt-get autoremove |
---|
29 | apt-get autoclean |
---|
30 | apt-get -y install nginx-full |
---|
31 | apt-get -y install build-essential python-dev libxml2-dev python-pip unzip |
---|
32 | pip install setuptools --no-use-wheel --upgrade |
---|
33 | PIPPATH=`which pip` |
---|
34 | $PIPPATH install --upgrade uwsgi |
---|
35 | # Create common nginx sections |
---|
36 | mkdir /etc/nginx/conf.d/web2py |
---|
37 | echo ' |
---|
38 | gzip_static on; |
---|
39 | gzip_http_version 1.1; |
---|
40 | gzip_proxied expired no-cache no-store private auth; |
---|
41 | gzip_disable "MSIE [1-6]\."; |
---|
42 | gzip_vary on; |
---|
43 | ' > /etc/nginx/conf.d/web2py/gzip_static.conf |
---|
44 | echo ' |
---|
45 | gzip on; |
---|
46 | gzip_disable "msie6"; |
---|
47 | gzip_vary on; |
---|
48 | gzip_proxied any; |
---|
49 | gzip_comp_level 6; |
---|
50 | gzip_buffers 16 8k; |
---|
51 | gzip_http_version 1.1; |
---|
52 | gzip_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 |
---|
55 | echo '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 | } |
---|
91 | server { |
---|
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 | |
---|
127 | ln -s /etc/nginx/sites-available/web2py /etc/nginx/sites-enabled/web2py |
---|
128 | rm /etc/nginx/sites-enabled/default |
---|
129 | mkdir /etc/nginx/ssl |
---|
130 | cd /etc/nginx/ssl |
---|
131 | if [ "$nocertificate" -eq 0 ] |
---|
132 | then |
---|
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 |
---|
137 | fi |
---|
138 | # Prepare folders for uwsgi |
---|
139 | sudo mkdir /etc/uwsgi |
---|
140 | sudo mkdir /var/log/uwsgi |
---|
141 | sudo mkdir /etc/systemd |
---|
142 | sudo mkdir /etc/systemd/system |
---|
143 | |
---|
144 | #uWSGI Emperor |
---|
145 | echo '[Unit] |
---|
146 | Description = uWSGI Emperor |
---|
147 | After = syslog.target |
---|
148 | |
---|
149 | [Service] |
---|
150 | ExecStart = /usr/local/bin/uwsgi --ini /etc/uwsgi/web2py.ini |
---|
151 | RuntimeDirectory = uwsgi |
---|
152 | Restart = always |
---|
153 | KillSignal = SIGQUIT |
---|
154 | Type = notify |
---|
155 | StandardError = syslog |
---|
156 | NotifyAccess = all |
---|
157 | |
---|
158 | [Install] |
---|
159 | WantedBy = multi-user.target |
---|
160 | ' > /etc/systemd/system/emperor.uwsgi.service |
---|
161 | |
---|
162 | # Create configuration file /etc/uwsgi/web2py.ini |
---|
163 | echo '[uwsgi] |
---|
164 | |
---|
165 | socket = /tmp/web2py.socket |
---|
166 | pythonpath = /home/www-data/web2py/ |
---|
167 | mount = /=wsgihandler:application |
---|
168 | processes = 4 |
---|
169 | master = true |
---|
170 | harakiri = 60 |
---|
171 | reload-mercy = 8 |
---|
172 | cpu-affinity = 1 |
---|
173 | stats = /tmp/stats.socket |
---|
174 | max-requests = 2000 |
---|
175 | limit-as = 512 |
---|
176 | reload-on-as = 256 |
---|
177 | reload-on-rss = 192 |
---|
178 | uid = www-data |
---|
179 | gid = www-data |
---|
180 | touch-reload = /home/www-data/web2py/routes.py |
---|
181 | cron = 0 0 -1 -1 -1 python /home/www-data/web2py/web2py.py -Q -S welcome -M -R scripts/sessions2trash.py -A -o |
---|
182 | no-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 |
---|
187 | echo '# Emperor uWSGI script |
---|
188 | |
---|
189 | description "uWSGI Emperor" |
---|
190 | start on runlevel [2345] |
---|
191 | stop 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 |
---|
200 | respawn |
---|
201 | exec uwsgi --master --die-on-term --emperor /etc/uwsgi --logto /var/log/uwsgi/uwsgi.log |
---|
202 | ' > /etc/init/uwsgi-emperor.conf |
---|
203 | # Install Web2py |
---|
204 | mkdir /home/www-data |
---|
205 | cd /home/www-data |
---|
206 | wget http://web2py.com/examples/static/web2py_src.zip |
---|
207 | unzip web2py_src.zip |
---|
208 | mv web2py/handlers/wsgihandler.py web2py/wsgihandler.py |
---|
209 | rm web2py_src.zip |
---|
210 | chown -R www-data:www-data web2py |
---|
211 | cd /home/www-data/web2py |
---|
212 | if [ "$nopassword" -eq 0 ] |
---|
213 | then |
---|
214 | sudo -u www-data python -c "from gluon.main import save_password; save_password('$PW',443)" |
---|
215 | fi |
---|
216 | |
---|
217 | /etc/init.d/nginx start |
---|
218 | systemctl start emperor.uwsgi.service |
---|
219 | systemctl enable emperor.uwsgi.service |
---|
220 | |
---|
221 | echo <<EOF |
---|
222 | you can stop uwsgi and nginx with |
---|
223 | |
---|
224 | sudo /etc/init.d/nginx stop |
---|
225 | sudo systemctl stop emperor.uwsgi.service |
---|
226 | |
---|
227 | and start it with |
---|
228 | |
---|
229 | sudo /etc/init.d/nginx start |
---|
230 | systemctl start emperor.uwsgi.service |
---|
231 | |
---|
232 | EOF |
---|
233 | |
---|