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