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