1 | #!/bin/bash |
---|
2 | echo "This script will: |
---|
3 | 1) Install modules needed to run web2py on Fedora and CentOS/RHEL |
---|
4 | 2) Install Python 3.7 to /opt and recompile wsgi if not provided |
---|
5 | 2) Install web2py in /opt/web-apps/ |
---|
6 | 3) Configure SELinux and iptables |
---|
7 | 5) Create a self signed ssl certificate |
---|
8 | 6) Setup web2py with mod_wsgi |
---|
9 | 7) Create virtualhost entries so that web2py responds for '/' |
---|
10 | 8) Restart Apache. |
---|
11 | |
---|
12 | You should probably read this script before running it. |
---|
13 | |
---|
14 | Although SELinux permissions changes have been made, |
---|
15 | further SELinux changes will be required for your personal |
---|
16 | apps. (There may also be additional changes required for the |
---|
17 | bundled apps.) As a last resort, SELinux can be disabled. |
---|
18 | |
---|
19 | A simple iptables configuration has been applied. You may |
---|
20 | want to review it to verify that it meets your needs. |
---|
21 | |
---|
22 | Finally, if you require a proxy to access the Internet, please |
---|
23 | set up your machine to do so before running this script. |
---|
24 | |
---|
25 | (author: berubejd) |
---|
26 | |
---|
27 | Press ENTER to continue...[ctrl+C to abort]" |
---|
28 | |
---|
29 | read CONFIRM |
---|
30 | |
---|
31 | |
---|
32 | |
---|
33 | ### |
---|
34 | ### Phase 0 - This may get messy. Lets work from a temporary directory |
---|
35 | ### |
---|
36 | |
---|
37 | current_dir=`pwd` |
---|
38 | |
---|
39 | if [ -d /tmp/setup-web2py/ ]; then |
---|
40 | mv /tmp/setup-web2py/ /tmp/setup-web2py.old/ |
---|
41 | fi |
---|
42 | |
---|
43 | mkdir -p /tmp/setup-web2py |
---|
44 | cd /tmp/setup-web2py |
---|
45 | |
---|
46 | ### |
---|
47 | ### Phase 1 - Requirements installation |
---|
48 | ### |
---|
49 | |
---|
50 | echo |
---|
51 | echo " - Installing packages" |
---|
52 | echo |
---|
53 | |
---|
54 | # Verify packages are up to date |
---|
55 | yum update |
---|
56 | |
---|
57 | # Install required packages |
---|
58 | yum install httpd mod_ssl mod_wsgi wget python3 |
---|
59 | |
---|
60 | # Verify we have at least Python 2.5 |
---|
61 | typeset -i version_major |
---|
62 | typeset -i version_minor |
---|
63 | |
---|
64 | version=`rpm --qf %{Version} -q python` |
---|
65 | version_major=`echo ${version} | awk '{split($0, parts, "."); print parts[1]}'` |
---|
66 | version_minor=`echo ${version} | awk '{split($0, parts, "."); print parts[2]}'` |
---|
67 | |
---|
68 | if [ ! ${version_major} -ge 2 -o ! ${version_minor} -ge 5 ]; then |
---|
69 | # Setup 2.6 in /opt - based upon |
---|
70 | # http://markkoberlein.com/getting-python-26-with-django-11-together-on |
---|
71 | |
---|
72 | # Check for earlier Python 2.6 install |
---|
73 | if [ -e /opt/python2.6 ]; then |
---|
74 | # Is Python already installed? |
---|
75 | RETV=`/opt/python2.6/bin/python -V > /dev/null 2>&1; echo $?` |
---|
76 | if [ ${RETV} -eq 0 ]; then |
---|
77 | python_installed='True' |
---|
78 | else |
---|
79 | mv /opt/python2.6 /opt/python2.6-old |
---|
80 | fi |
---|
81 | fi |
---|
82 | |
---|
83 | # Install Python 2.6 if it doesn't exist already |
---|
84 | if [ ! "${python_installed}" == "True" ]; then |
---|
85 | # Install requirements for the Python build |
---|
86 | yum install sqlite-devel zlib-devel |
---|
87 | |
---|
88 | mkdir -p /opt/python2.6 |
---|
89 | |
---|
90 | # Download and install |
---|
91 | wget http://www.python.org/ftp/python/2.6.4/Python-2.6.4.tgz |
---|
92 | tar -xzf Python-2.6.4.tgz |
---|
93 | cd Python-2.6.4 |
---|
94 | ./configure --prefix=/opt/python2.6 --with-threads --enable-shared --with-zlib=/usr/include |
---|
95 | make && make install |
---|
96 | |
---|
97 | cd /tmp/setup-web2py |
---|
98 | fi |
---|
99 | |
---|
100 | # Create links for Python 2.6 |
---|
101 | # even if it was previously installed just to be sure |
---|
102 | ln -s /opt/python2.6/lib/libpython2.6.so /usr/lib |
---|
103 | ln -s /opt/python2.6/lib/libpython2.6.so.1.0 /usr/lib |
---|
104 | ln -s /opt/python2.6/bin/python /usr/local/bin/python |
---|
105 | ln -s /opt/python2.6/bin/python /usr/bin/python2.6 |
---|
106 | ln -s /opt/python2.6/lib/python2.6.so /opt/python2.6/lib/python2.6/config/ |
---|
107 | |
---|
108 | # Update linker for new libraries |
---|
109 | /sbin/ldconfig |
---|
110 | |
---|
111 | # Rebuild wsgi to take advantage of Python 2.6 |
---|
112 | yum install httpd-devel |
---|
113 | |
---|
114 | cd /tmp/setup-web2py |
---|
115 | |
---|
116 | wget http://modwsgi.googlecode.com/files/mod_wsgi-3.3.tar.gz |
---|
117 | tar -xzf mod_wsgi-3.3.tar.gz |
---|
118 | cd mod_wsgi-3.3 |
---|
119 | ./configure --with-python=/usr/local/bin/python |
---|
120 | make && make install |
---|
121 | |
---|
122 | echo "LoadModule wsgi_module modules/mod_wsgi.so" > /etc/httpd/conf.d/wsgi.conf |
---|
123 | |
---|
124 | cd /tmp/setup-web2py |
---|
125 | fi |
---|
126 | |
---|
127 | ### MySQL install untested! |
---|
128 | # Install mysql packages (optional) |
---|
129 | #yum install mysql mysql-server |
---|
130 | |
---|
131 | # Enable mysql to start at boot (optional) |
---|
132 | #chkconfig --levels 235 mysqld on |
---|
133 | #service mysqld start |
---|
134 | |
---|
135 | # Configure mysql security settings (not really optional if mysql installed) |
---|
136 | #/usr/bin/mysql_secure_installation |
---|
137 | |
---|
138 | ### |
---|
139 | ### Phase 2 - Install web2py |
---|
140 | ### |
---|
141 | |
---|
142 | echo |
---|
143 | echo " - Downloading, installing, and starting web2py" |
---|
144 | echo |
---|
145 | |
---|
146 | # Create web-apps directory, if required |
---|
147 | if [ ! -d "/opt/web-apps" ]; then |
---|
148 | mkdir -p /opt/web-apps |
---|
149 | |
---|
150 | chmod 755 /opt |
---|
151 | chmod 755 /opt/web-apps |
---|
152 | fi |
---|
153 | |
---|
154 | cd /opt/web-apps |
---|
155 | |
---|
156 | # Download web2py |
---|
157 | if [ -e web2py_src.zip* ]; then |
---|
158 | rm web2py_src.zip* |
---|
159 | fi |
---|
160 | |
---|
161 | wget http://web2py.com/examples/static/web2py_src.zip |
---|
162 | unzip web2py_src.zip |
---|
163 | mv web2py/handlers/wsgihandler.py web2py/wsgihandler.py |
---|
164 | chown -R apache:apache web2py |
---|
165 | |
---|
166 | ### |
---|
167 | ### Phase 3 - Setup SELinux context |
---|
168 | ### |
---|
169 | |
---|
170 | # Set context for Python libraries if Python 2.6 installed |
---|
171 | if [ -d /opt/python2.6 ]; then |
---|
172 | cd /opt/python2.6 |
---|
173 | chcon -R -t lib_t lib/ |
---|
174 | fi |
---|
175 | |
---|
176 | # Allow http_tmp_exec required for wsgi |
---|
177 | RETV=`setsebool -P httpd_tmp_exec on > /dev/null 2>&1; echo $?` |
---|
178 | if [ ! ${RETV} -eq 0 ]; then |
---|
179 | # CentOS doesn't support httpd_tmp_exec |
---|
180 | cd /tmp/setup-web2py |
---|
181 | |
---|
182 | # Create the SELinux policy |
---|
183 | cat > httpd.te <<EOF |
---|
184 | |
---|
185 | module httpd 1.0; |
---|
186 | |
---|
187 | require { |
---|
188 | type httpd_t; |
---|
189 | class process execmem; |
---|
190 | } |
---|
191 | |
---|
192 | #============= httpd_t ============== |
---|
193 | allow httpd_t self:process execmem; |
---|
194 | EOF |
---|
195 | |
---|
196 | checkmodule -M -m -o httpd.mod httpd.te |
---|
197 | semodule_package -o httpd.pp -m httpd.mod |
---|
198 | semodule -i httpd.pp |
---|
199 | |
---|
200 | fi |
---|
201 | |
---|
202 | # Setup the overall web2py SELinux context |
---|
203 | cd /opt |
---|
204 | chcon -R -t httpd_user_content_t web-apps/ |
---|
205 | |
---|
206 | cd /opt/web-apps/web2py/applications |
---|
207 | |
---|
208 | # Setup the proper context on the writable application directories |
---|
209 | for app in `ls` |
---|
210 | do |
---|
211 | for dir in databases cache errors sessions private uploads |
---|
212 | do |
---|
213 | mkdir ${app}/${dir} |
---|
214 | chown apache:apache ${app}/${dir} |
---|
215 | chcon -R -t tmp_t ${app}/${dir} |
---|
216 | done |
---|
217 | done |
---|
218 | |
---|
219 | |
---|
220 | ### |
---|
221 | ### Phase 4 - Configure iptables |
---|
222 | ### |
---|
223 | |
---|
224 | cd /tmp/setup-web2py |
---|
225 | |
---|
226 | # Create rules file - based upon |
---|
227 | # http://articles.slicehost.com/assets/2007/9/4/iptables.txt |
---|
228 | cat > iptables.rules <<EOF |
---|
229 | *filter |
---|
230 | |
---|
231 | # Allows all loopback (lo0) traffic |
---|
232 | # drop all traffic to 127/8 that doesn't use lo0 |
---|
233 | -A INPUT -i lo -j ACCEPT |
---|
234 | -A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT |
---|
235 | |
---|
236 | # Accepts all established inbound connections |
---|
237 | -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT |
---|
238 | |
---|
239 | # Allows all outbound traffic |
---|
240 | -A OUTPUT -j ACCEPT |
---|
241 | |
---|
242 | # Allows SSH, HTTP, and HTTPS |
---|
243 | # Consider changing the SSH port and/or using rate limiting |
---|
244 | # see http://blog.andrew.net.au/2005/02/16#ipt_recent_and_ssh_attacks |
---|
245 | -A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT |
---|
246 | -A INPUT -p tcp --dport 80 -j ACCEPT |
---|
247 | -A INPUT -p tcp --dport 443 -j ACCEPT |
---|
248 | |
---|
249 | # Allow ping |
---|
250 | -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT |
---|
251 | |
---|
252 | # log iptables denied calls |
---|
253 | -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7 |
---|
254 | |
---|
255 | # Reject all other inbound - default deny unless explicitly allowed policy |
---|
256 | -A INPUT -j REJECT |
---|
257 | -A FORWARD -j REJECT |
---|
258 | |
---|
259 | COMMIT |
---|
260 | EOF |
---|
261 | |
---|
262 | /sbin/iptables -F |
---|
263 | cat iptables.rules | /sbin/iptables-restore |
---|
264 | /sbin/service iptables save |
---|
265 | |
---|
266 | ### |
---|
267 | ### Phase 5 - Setup SSL |
---|
268 | ### |
---|
269 | |
---|
270 | echo |
---|
271 | echo " - Creating a self signed certificate" |
---|
272 | echo |
---|
273 | |
---|
274 | # Verify ssl directory exists |
---|
275 | if [ ! -d "/etc/httpd/ssl" ]; then |
---|
276 | mkdir -p /etc/httpd/ssl |
---|
277 | fi |
---|
278 | |
---|
279 | # Generate and protect certificate |
---|
280 | openssl genrsa 1024 > /etc/httpd/ssl/self_signed.key |
---|
281 | openssl req -new -x509 -nodes -sha1 -days 365 -key /etc/httpd/ssl/self_signed.key > /etc/httpd/ssl/self_signed.cert |
---|
282 | openssl x509 -noout -fingerprint -text < /etc/httpd/ssl/self_signed.cert > /etc/httpd/ssl/self_signed.info |
---|
283 | |
---|
284 | chmod 400 /etc/httpd/ssl/self_signed.* |
---|
285 | |
---|
286 | ### |
---|
287 | ### Phase 6 - Configure Apache |
---|
288 | ### |
---|
289 | |
---|
290 | echo |
---|
291 | echo " - Configure Apache to use mod_wsgi" |
---|
292 | echo |
---|
293 | |
---|
294 | # Create config |
---|
295 | if [ -e /etc/httpd/conf.d/welcome.conf ]; then |
---|
296 | mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.disabled |
---|
297 | fi |
---|
298 | |
---|
299 | cat > /etc/httpd/conf.d/default.conf <<EOF |
---|
300 | |
---|
301 | NameVirtualHost *:80 |
---|
302 | NameVirtualHost *:443 |
---|
303 | |
---|
304 | <VirtualHost *:80> |
---|
305 | WSGIDaemonProcess web2py user=apache group=apache |
---|
306 | WSGIProcessGroup web2py |
---|
307 | WSGIScriptAlias / /opt/web-apps/web2py/wsgihandler.py |
---|
308 | WSGIPassAuthorization On |
---|
309 | |
---|
310 | <Directory /opt/web-apps/web2py> |
---|
311 | AllowOverride None |
---|
312 | Order Allow,Deny |
---|
313 | Deny from all |
---|
314 | <Files wsgihandler.py> |
---|
315 | Allow from all |
---|
316 | </Files> |
---|
317 | </Directory> |
---|
318 | |
---|
319 | AliasMatch ^/([^/]+)/static/(?:_[\d]+.[\d]+.[\d]+/)?(.*) /opt/web-apps/web2py/applications/\$1/static/\$2 |
---|
320 | |
---|
321 | <Directory /opt/web-apps/web2py/applications/*/static> |
---|
322 | Options -Indexes |
---|
323 | Order Allow,Deny |
---|
324 | Allow from all |
---|
325 | </Directory> |
---|
326 | |
---|
327 | <Location /admin> |
---|
328 | Deny from all |
---|
329 | </Location> |
---|
330 | |
---|
331 | <LocationMatch ^/([^/]+)/appadmin> |
---|
332 | Deny from all |
---|
333 | </LocationMatch> |
---|
334 | |
---|
335 | CustomLog /var/log/httpd/access_log common |
---|
336 | ErrorLog /var/log/httpd/error_log |
---|
337 | </VirtualHost> |
---|
338 | |
---|
339 | <VirtualHost *:443> |
---|
340 | SSLEngine on |
---|
341 | SSLCertificateFile /etc/httpd/ssl/self_signed.cert |
---|
342 | SSLCertificateKeyFile /etc/httpd/ssl/self_signed.key |
---|
343 | |
---|
344 | WSGIProcessGroup web2py |
---|
345 | WSGIScriptAlias / /opt/web-apps/web2py/wsgihandler.py |
---|
346 | WSGIPassAuthorization On |
---|
347 | |
---|
348 | <Directory /opt/web-apps/web2py> |
---|
349 | AllowOverride None |
---|
350 | Order Allow,Deny |
---|
351 | Deny from all |
---|
352 | <Files wsgihandler.py> |
---|
353 | Allow from all |
---|
354 | </Files> |
---|
355 | </Directory> |
---|
356 | |
---|
357 | AliasMatch ^/([^/]+)/static/(?:_[\d]+.[\d]+.[\d]+/)?(.*) /opt/web-apps/web2py/applications/\$1/static/\$2 |
---|
358 | |
---|
359 | <Directory /opt/web-apps/web2py/applications/*/static> |
---|
360 | Options -Indexes |
---|
361 | ExpiresActive On |
---|
362 | ExpiresDefault "access plus 1 hour" |
---|
363 | Order Allow,Deny |
---|
364 | Allow from all |
---|
365 | </Directory> |
---|
366 | |
---|
367 | CustomLog /var/log/httpd/access_log common |
---|
368 | ErrorLog /var/log/httpd/error_log |
---|
369 | </VirtualHost> |
---|
370 | |
---|
371 | EOF |
---|
372 | |
---|
373 | # Fix wsgi socket locations |
---|
374 | echo "WSGISocketPrefix run/wsgi" >> /etc/httpd/conf.d/wsgi.conf |
---|
375 | |
---|
376 | # Restart Apache to pick up changes |
---|
377 | service httpd restart |
---|
378 | |
---|
379 | ### |
---|
380 | ### Phase 7 - Setup web2py admin password |
---|
381 | ### |
---|
382 | |
---|
383 | echo |
---|
384 | echo " - Setup web2py admin password" |
---|
385 | echo |
---|
386 | |
---|
387 | cd /opt/web-apps/web2py |
---|
388 | sudo -u apache python -c "from gluon.main import save_password; save_password(raw_input('admin password: '),443)" |
---|
389 | |
---|
390 | ### |
---|
391 | ### Phase 8 - Verify that required services start at boot |
---|
392 | ### |
---|
393 | |
---|
394 | /sbin/chkconfig iptables on |
---|
395 | /sbin/chkconfig httpd on |
---|
396 | |
---|
397 | ### |
---|
398 | ### Phase 999 - Done! |
---|
399 | ### |
---|
400 | |
---|
401 | # Change back to original directory |
---|
402 | cd ${current_directory} |
---|
403 | |
---|
404 | echo " - Complete!" |
---|
405 | echo |
---|