source: OpenRLabs-Git/deploy/rlabs-docker/web2py-rlabs/scripts/setup-web2py-fedora-ami.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: 9.7 KB
Line 
1echo "This script will:
21) Install modules needed to run web2py on Fedora and CentOS/RHEL
32) Install Python 3.7 to /opt and recompile wsgi if not provided
42) Install web2py in /opt/web-apps/
53) Configure SELinux and iptables
65) Create a self signed ssl certificate
76) Setup web2py with mod_wsgi
87) Create virtualhost entries so that web2py responds for '/'
98) Restart Apache.
10
11You should probably read this script before running it.
12
13Although SELinux permissions changes have been made,
14further SELinux changes will be required for your personal
15apps. (There may also be additional changes required for the
16bundled apps.)  As a last resort, SELinux can be disabled.
17
18A simple iptables configuration has been applied.  You may
19want to review it to verify that it meets your needs.
20
21Finally, if you require a proxy to access the Internet, please
22set up your machine to do so before running this script.
23
24(author: Charles Law as berubejd)
25
26Press ENTER to continue...[ctrl+C to abort]"
27
28read CONFIRM
29
30#!/bin/bash
31
32###
33###  Phase 0 - This may get messy.  Lets work from a temporary directory
34###
35
36current_dir=`pwd`
37
38if [ -d /tmp/setup-web2py/ ]; then
39    mv /tmp/setup-web2py/ /tmp/setup-web2py.old/
40fi
41
42mkdir -p /tmp/setup-web2py
43cd /tmp/setup-web2py
44
45###
46###  Phase 1 - Requirements installation
47###
48
49echo
50echo " - Installing packages"
51echo
52
53# Verify packages are up to date
54yum update
55
56# Install required packages
57yum install httpd mod_ssl mod_wsgi wget python3
58
59# Verify we have at least Python 2.5
60typeset -i version_major
61typeset -i version_minor
62
63version=`rpm --qf %{Version} -q python`
64version_major=`echo ${version} | awk '{split($0, parts, "."); print parts[1]}'`
65version_minor=`echo ${version} | awk '{split($0, parts, "."); print parts[2]}'`
66
67if [ ! ${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
124fi
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
141echo
142echo " - Downloading, installing, and starting web2py"
143echo
144
145# Create web-apps directory, if required
146if [ ! -d "/opt/web-apps" ]; then
147    mkdir -p /opt/web-apps
148
149    chmod 755 /opt
150    chmod 755 /opt/web-apps
151fi
152
153cd /opt/web-apps
154
155# Download web2py
156if [ -e web2py_src.zip* ]; then
157    rm web2py_src.zip*
158fi
159
160wget http://web2py.com/examples/static/web2py_src.zip
161unzip web2py_src.zip
162mv web2py/handlers/wsgihandler.py web2py/wsgihandler.py
163chown -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
170if [ -d /opt/python2.6 ]; then
171    cd /opt/python2.6
172    chcon -R -t lib_t lib/
173fi
174
175# Allow http_tmp_exec required for wsgi
176RETV=`setsebool -P httpd_tmp_exec on > /dev/null 2>&1; echo $?`
177if [ ! ${RETV} -eq 0 ]; then
178    # CentOS doesn't support httpd_tmp_exec
179    cd /tmp/setup-web2py
180
181    # Create the SELinux policy
182cat > httpd.te <<EOF
183module httpd 1.0;
184
185require {
186    type httpd_t;
187    class process execmem;
188}
189
190#============= httpd_t ==============
191allow httpd_t self:process execmem;
192EOF
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
198fi
199
200# Setup the overall web2py SELinux context
201cd /opt
202chcon -R -t httpd_user_content_t web-apps/
203
204cd /opt/web-apps/web2py/applications
205
206# Setup the proper context on the writable application directories
207for app in `ls`
208do
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
215done
216
217
218###
219### Phase 4 - Configure iptables
220###
221
222cd /tmp/setup-web2py
223
224# Create rules file - based upon
225# http://articles.slicehost.com/assets/2007/9/4/iptables.txt
226cat > 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
257COMMIT
258EOF
259
260/sbin/iptables -F
261cat iptables.rules | /sbin/iptables-restore
262/sbin/service iptables save
263
264###
265### Phase 5 - Setup SSL
266###
267
268echo
269echo " - Creating a self signed certificate"
270echo
271
272# Verify ssl directory exists
273if [ ! -d "/etc/httpd/ssl" ]; then
274    mkdir -p /etc/httpd/ssl
275fi
276
277# Generate and protect certificate
278openssl genrsa 1024 > /etc/httpd/ssl/self_signed.key
279openssl req -new -x509 -nodes -sha1 -days 365 -key /etc/httpd/ssl/self_signed.key > /etc/httpd/ssl/self_signed.cert
280openssl x509 -noout -fingerprint -text < /etc/httpd/ssl/self_signed.cert > /etc/httpd/ssl/self_signed.info
281
282chmod 400 /etc/httpd/ssl/self_signed.*
283
284###
285### Phase 6 - Configure Apache
286###
287
288echo
289echo " - Configure Apache to use mod_wsgi"
290echo
291
292# Create config
293if [ -e /etc/httpd/conf.d/welcome.conf ]; then
294    mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.disabled
295fi
296
297cat  > /etc/httpd/conf.d/default.conf <<EOF
298NameVirtualHost *:80
299NameVirtualHost *: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
368EOF
369
370# Fix wsgi socket locations
371echo "WSGISocketPrefix run/wsgi" >> /etc/httpd/conf.d/wsgi.conf
372
373# Restart Apache to pick up changes
374service httpd restart
375
376###
377### Phase 7 - Setup web2py admin password
378###
379
380echo
381echo " - Setup web2py admin password"
382echo
383
384cd /opt/web-apps/web2py
385sudo -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
399cd ${current_directory}
400
401echo " - Complete!"
402echo
Note: See TracBrowser for help on using the repository browser.