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