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 | |
---|
11 | read -p "Enter username under which web2py will be installed [web2py]: " USERNAME |
---|
12 | USERNAME=${USERNAME:-web2py} |
---|
13 | |
---|
14 | read -p "Enter path where web2py will be installed [/opt/web2py_apps]: " WEB2PY_PATH |
---|
15 | WEB2PY_PATH=${WEB2PY_PATH:-/opt/web2py_apps} |
---|
16 | |
---|
17 | read -p "Web2py subdirectory will be called: [web2py]: " WEB2PY_APP |
---|
18 | WEB2PY_APP=${WEB2PY_APP:-web2py} |
---|
19 | |
---|
20 | read -p "Enter your web2py admin password: " WEB2PY_PASS |
---|
21 | |
---|
22 | read -p "Enter your domain name: " YOUR_SERVER_DOMAIN |
---|
23 | |
---|
24 | # open new user |
---|
25 | useradd -d $WEB2PY_PATH $USERNAME |
---|
26 | |
---|
27 | # if it's not already open, let's create a directory for web2py |
---|
28 | mkdir -p $WEB2PY_PATH |
---|
29 | |
---|
30 | # now let's create a self signed certificate |
---|
31 | cd $WEB2PY_PATH |
---|
32 | |
---|
33 | openssl 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 | |
---|
39 | echo "Installing necessary components" |
---|
40 | |
---|
41 | # Verify packages are up to date |
---|
42 | yum -y upgrade |
---|
43 | |
---|
44 | # Install required packages |
---|
45 | yum install -y epel-release |
---|
46 | yum install -y python-devel python-pip gcc nginx wget unzip python-psycopg2 MySQL-python |
---|
47 | |
---|
48 | # download and unzip web2py |
---|
49 | |
---|
50 | echo "Downloading web2py" |
---|
51 | |
---|
52 | cd $WEB2PY_PATH |
---|
53 | wget http://web2py.com/examples/static/web2py_src.zip |
---|
54 | unzip web2py_src.zip |
---|
55 | rm web2py_src.zip |
---|
56 | |
---|
57 | # preparing wsgihandler |
---|
58 | chown -R $USERNAME.$USERNAME $WEB2PY_PATH/$WEB2PY_APP |
---|
59 | mv $WEB2PY_PATH/$WEB2PY_APP/handlers/wsgihandler.py $WEB2PY_PATH/$WEB2PY_APP |
---|
60 | |
---|
61 | # now let's install uwsgi |
---|
62 | |
---|
63 | pip install uwsgi |
---|
64 | |
---|
65 | # preparing directories |
---|
66 | mkdir -p /etc/uwsgi/sites |
---|
67 | mkdir -p /var/log/uwsgi |
---|
68 | mkdir -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 |
---|
75 | echo '[uwsgi] |
---|
76 | chdir = WEB2PY_PATH_PLACEHOLDER/WEB2PY_APP_PLACEHOLDER |
---|
77 | module = wsgihandler:application |
---|
78 | |
---|
79 | master = true |
---|
80 | processes = 5 |
---|
81 | |
---|
82 | uid = USERNAME_PLACEHOLDER |
---|
83 | socket = /run/uwsgi/WEB2PY_APP_PLACEHOLDER.sock |
---|
84 | chown-socket = USERNAME_PLACEHOLDER:nginx |
---|
85 | chmod-socket = 660 |
---|
86 | vacuum = true |
---|
87 | ' >/etc/uwsgi/sites/$WEB2PY_APP.ini |
---|
88 | |
---|
89 | sed -i "s@WEB2PY_PATH_PLACEHOLDER@$WEB2PY_PATH@" /etc/uwsgi/sites/$WEB2PY_APP.ini |
---|
90 | sed -i "s@WEB2PY_APP_PLACEHOLDER@$WEB2PY_APP@" /etc/uwsgi/sites/$WEB2PY_APP.ini |
---|
91 | sed -i "s@USERNAME_PLACEHOLDER@$USERNAME@" /etc/uwsgi/sites/$WEB2PY_APP.ini |
---|
92 | |
---|
93 | # Create a daemon configuration file for uwsgi |
---|
94 | cat > /etc/systemd/system/uwsgi.service <<EOF |
---|
95 | [Unit] |
---|
96 | Description=uWSGI Emperor service |
---|
97 | |
---|
98 | [Service] |
---|
99 | ExecStartPre=/usr/bin/bash -c 'mkdir -p /run/uwsgi; chown USERNAME_PLACEHOLDER:nginx /run/uwsgi' |
---|
100 | ExecStart=/usr/bin/uwsgi --emperor /etc/uwsgi/sites |
---|
101 | Restart=always |
---|
102 | KillSignal=SIGQUIT |
---|
103 | Type=notify |
---|
104 | NotifyAccess=all |
---|
105 | |
---|
106 | [Install] |
---|
107 | WantedBy=multi-user.target |
---|
108 | EOF |
---|
109 | |
---|
110 | sed -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 |
---|
115 | cat > /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 | |
---|
120 | user nginx; |
---|
121 | worker_processes auto; |
---|
122 | error_log /var/log/nginx/error.log; |
---|
123 | pid /run/nginx.pid; |
---|
124 | |
---|
125 | events { |
---|
126 | worker_connections 1024; |
---|
127 | } |
---|
128 | |
---|
129 | http { |
---|
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 | } |
---|
195 | EOF |
---|
196 | |
---|
197 | sed -i "s@YOUR_SERVER_DOMAIN_PLACEHOLDER@$YOUR_SERVER_DOMAIN@" /etc/nginx/nginx.conf |
---|
198 | sed -i "s@WEB2PY_PATH_PLACEHOLDER@$WEB2PY_PATH@" /etc/nginx/nginx.conf |
---|
199 | sed -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 |
---|
206 | mv $WEB2PY_PATH/$WEB2PY_APP.crt* /etc/nginx/ssl |
---|
207 | mv $WEB2PY_PATH/$WEB2PY_APP.key* /etc/nginx/ssl |
---|
208 | |
---|
209 | # creating web2py admin password |
---|
210 | cd $WEB2PY_PATH/$WEB2PY_APP |
---|
211 | python -c "from gluon.main import save_password; save_password('$WEB2PY_PASS',443)" |
---|
212 | chown -R $USERNAME.$USERNAME $WEB2PY_PATH/$WEB2PY_APP |
---|
213 | |
---|
214 | # taking care of permissions |
---|
215 | chmod 700 /etc/nginx/ssl |
---|
216 | usermod -a -G $USERNAME nginx |
---|
217 | chmod 710 $WEB2PY_PATH |
---|
218 | |
---|
219 | # enabling daemons |
---|
220 | systemctl start nginx |
---|
221 | systemctl start uwsgi |
---|
222 | systemctl enable nginx |
---|
223 | systemctl enable uwsgi |
---|
224 | |
---|
225 | # If firewall is active make sure these ports are open |
---|
226 | |
---|
227 | firewall-cmd --zone=public --add-port=80/tcp --permanent |
---|
228 | firewall-cmd --zone=public --add-port=443/tcp --permanent |
---|
229 | firewall-cmd --zone=public --add-port=22/tcp --permanent |
---|
230 | firewall-cmd --reload |
---|
231 | |
---|
232 | echo |
---|
233 | echo 'Web2py is now installed on this server!' |
---|
234 | echo |
---|
235 | |
---|