source: OpenRLabs-Git/deploy/rlabs-docker/web2py-rlabs/scripts/setup-web2py-nginx-uwsgi-centos7.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: 6.5 KB
Line 
1#!/bin/bash
2
3# This script will install web2py with nginx+uwsgi on centos 7
4# This script is based on excellent tutorial by Justin Ellingwood on
5# https://www.digitalocean.com/community/tutorials/how-to-deploy-web2py-python-applications-with-uwsgi-and-nginx-on-centos-7
6
7#
8# Phase 1: First, let's ask a few things
9#
10
11read -p "Enter username under which web2py will be installed [web2py]: " USERNAME
12USERNAME=${USERNAME:-web2py}
13
14read -p "Enter path where web2py will be installed [/opt/web2py_apps]: " WEB2PY_PATH
15WEB2PY_PATH=${WEB2PY_PATH:-/opt/web2py_apps}
16
17read -p "Web2py subdirectory will be called: [web2py]: " WEB2PY_APP
18WEB2PY_APP=${WEB2PY_APP:-web2py}
19
20read -p "Enter your web2py admin password: " WEB2PY_PASS
21
22read -p "Enter your domain name: " YOUR_SERVER_DOMAIN
23
24#  open new user
25useradd -d $WEB2PY_PATH $USERNAME
26
27# if it's not already open, let's create a directory for web2py
28mkdir -p $WEB2PY_PATH
29
30# now let's create a self signed certificate
31cd $WEB2PY_PATH
32
33openssl req -x509 -new -newkey rsa:4096 -days 3652 -nodes -keyout $WEB2PY_APP.key -out $WEB2PY_APP.crt
34
35#
36# phase 2: That was all the input that we needed so let's install the components
37#
38
39echo "Installing necessary components"
40
41# Verify packages are up to date
42yum -y upgrade
43
44# Install required packages
45yum install -y epel-release
46yum install -y python-devel python-pip gcc nginx wget unzip python-psycopg2 MySQL-python
47
48# download and unzip web2py
49
50echo "Downloading web2py"
51
52cd $WEB2PY_PATH
53wget http://web2py.com/examples/static/web2py_src.zip
54unzip web2py_src.zip
55rm web2py_src.zip
56
57# preparing wsgihandler
58chown -R $USERNAME.$USERNAME $WEB2PY_PATH/$WEB2PY_APP
59mv $WEB2PY_PATH/$WEB2PY_APP/handlers/wsgihandler.py $WEB2PY_PATH/$WEB2PY_APP
60
61# now let's install uwsgi
62
63pip install uwsgi
64
65# preparing directories
66mkdir -p /etc/uwsgi/sites
67mkdir -p /var/log/uwsgi
68mkdir -p /etc/nginx/ssl/
69
70#
71#  Phase 3: Ok, everything is installed now so we'll configure things
72#
73
74# Create configuration file for uwsgi in /etc/uwsgi/$WEB2PY_APP.ini
75echo '[uwsgi]
76chdir = WEB2PY_PATH_PLACEHOLDER/WEB2PY_APP_PLACEHOLDER
77module = wsgihandler:application
78
79master = true
80processes = 5
81
82uid = USERNAME_PLACEHOLDER
83socket = /run/uwsgi/WEB2PY_APP_PLACEHOLDER.sock
84chown-socket = USERNAME_PLACEHOLDER:nginx
85chmod-socket = 660
86vacuum = true
87' >/etc/uwsgi/sites/$WEB2PY_APP.ini
88
89sed -i "s@WEB2PY_PATH_PLACEHOLDER@$WEB2PY_PATH@" /etc/uwsgi/sites/$WEB2PY_APP.ini
90sed -i "s@WEB2PY_APP_PLACEHOLDER@$WEB2PY_APP@" /etc/uwsgi/sites/$WEB2PY_APP.ini
91sed -i "s@USERNAME_PLACEHOLDER@$USERNAME@" /etc/uwsgi/sites/$WEB2PY_APP.ini
92
93# Create a daemon configuration file for uwsgi
94cat  > /etc/systemd/system/uwsgi.service <<EOF
95[Unit]
96Description=uWSGI Emperor service
97
98[Service]
99ExecStartPre=/usr/bin/bash -c 'mkdir -p /run/uwsgi; chown USERNAME_PLACEHOLDER:nginx /run/uwsgi'
100ExecStart=/usr/bin/uwsgi --emperor /etc/uwsgi/sites
101Restart=always
102KillSignal=SIGQUIT
103Type=notify
104NotifyAccess=all
105
106[Install]
107WantedBy=multi-user.target
108EOF
109
110sed -i "s@USERNAME_PLACEHOLDER@$USERNAME@" /etc/systemd/system/uwsgi.service
111
112#chmod 777 /etc/systemd/system/uwsgi.service
113
114# create a nginx configuration file
115cat  > /etc/nginx/nginx.conf <<EOF
116# For more information on configuration, see:
117#   * Official English Documentation: http://nginx.org/en/docs/
118#   * Official Russian Documentation: http://nginx.org/ru/docs/
119
120user nginx;
121worker_processes auto;
122error_log /var/log/nginx/error.log;
123pid /run/nginx.pid;
124
125events {
126    worker_connections 1024;
127}
128
129http {
130    log_format  main  '\$remote_addr - \$remote_user [\$time_local] "\$request" '
131                      '\$status \$body_bytes_sent "\$http_referer" '
132                      '"\$http_user_agent" "\$http_x_forwarded_for"';
133
134    access_log  /var/log/nginx/access.log  main;
135
136    sendfile            on;
137    tcp_nopush          on;
138    tcp_nodelay         on;
139    keepalive_timeout   65;
140    types_hash_max_size 2048;
141
142    include             /etc/nginx/mime.types;
143    default_type        application/octet-stream;
144
145    # Load modular configuration files from the /etc/nginx/conf.d directory.
146    # See http://nginx.org/en/docs/ngx_core_module.html#include
147    # for more information.
148    include /etc/nginx/conf.d/*.conf;
149
150    server {
151        listen       80 default_server;
152        listen       [::]:80 default_server;
153        server_name  YOUR_SERVER_DOMAIN_PLACEHOLDER;
154        root         /usr/share/nginx/html;
155
156        # Load configuration files for the default server block.
157        include /etc/nginx/default.d/*.conf;
158
159        location ~* /(\w+)/static/ {
160            root WEB2PY_PATH_PLACEHOLDER/WEB2PY_APP_PLACEHOLDER/applications/;
161        }
162
163        location / {
164            include uwsgi_params;
165            uwsgi_pass unix:/run/uwsgi/WEB2PY_APP_PLACEHOLDER.sock;
166        }
167
168        error_page 404 /404.html;
169            location = /40x.html {
170        }
171
172        error_page 500 502 503 504 /50x.html;
173            location = /50x.html {
174        }
175    }
176   
177    server {
178        listen 443;
179        server_name YOUR_SERVER_DOMAIN_PLACEHOLDER;
180       
181        ssl on;
182        ssl_certificate /etc/nginx/ssl/WEB2PY_APP_PLACEHOLDER.crt;
183        ssl_certificate_key /etc/nginx/ssl/WEB2PY_APP_PLACEHOLDER.key;
184       
185        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
186        ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
187        ssl_prefer_server_ciphers on;
188       
189        location / {
190            include uwsgi_params;
191            uwsgi_pass unix:/run/uwsgi/WEB2PY_APP_PLACEHOLDER.sock;
192        }
193    }
194}
195EOF
196
197sed -i "s@YOUR_SERVER_DOMAIN_PLACEHOLDER@$YOUR_SERVER_DOMAIN@" /etc/nginx/nginx.conf
198sed -i "s@WEB2PY_PATH_PLACEHOLDER@$WEB2PY_PATH@" /etc/nginx/nginx.conf
199sed -i "s@WEB2PY_APP_PLACEHOLDER@$WEB2PY_APP@" /etc/nginx/nginx.conf
200
201#
202# Phase 4: everything is configured now, just a few final touches
203#
204
205# copying certificates to nginx directory
206mv $WEB2PY_PATH/$WEB2PY_APP.crt* /etc/nginx/ssl
207mv $WEB2PY_PATH/$WEB2PY_APP.key* /etc/nginx/ssl
208
209# creating web2py admin password
210cd $WEB2PY_PATH/$WEB2PY_APP
211python -c "from gluon.main import save_password; save_password('$WEB2PY_PASS',443)"
212chown -R $USERNAME.$USERNAME $WEB2PY_PATH/$WEB2PY_APP
213
214# taking care of permissions
215chmod 700 /etc/nginx/ssl
216usermod -a -G $USERNAME nginx
217chmod 710 $WEB2PY_PATH
218
219# enabling daemons
220systemctl start nginx
221systemctl start uwsgi
222systemctl enable nginx
223systemctl enable uwsgi
224
225# If firewall is active make sure these ports are open
226
227firewall-cmd --zone=public --add-port=80/tcp --permanent
228firewall-cmd --zone=public --add-port=443/tcp --permanent
229firewall-cmd --zone=public --add-port=22/tcp --permanent
230firewall-cmd --reload
231
232echo
233echo 'Web2py is now installed on this server!'
234echo
235
Note: See TracBrowser for help on using the repository browser.