source: OpenRLabs-Git/deploy/rlabs-docker/web2py-rlabs/scripts/setup-web2py-nginx-uwsgi-on-centos.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: 8.9 KB
Line 
1#!/bin/bash
2
3# -------------------------------------------------------------------
4# Description : Installation and basic configuration of web2py,
5#               uWSGI, andNGINX.
6#               in CentOS 5.x GNU/Linux
7#       Usage : Copy the script in /home/username and run it as root,
8#               you may need to allow execution (chmod +x)
9#
10#               WARNING: This script was modified to install compiled
11#               versions of Python and other packages that may be
12#               available at your Centos package repository.
13#               This change was made in order to get the latest
14#               stable libraries available for avoiding compatibility
15#
16#               A bug was reported for the 2.7.3 version of python
17#               here http://bugs.python.org/issue14572
18#               in case you choose to change to VERSION=2.7
19#               (see below) mind that the automatic patch applied
20#               could not work for your environment. By default,
21#               Python 2.6 is installed.
22#
23#               It was originally posted in this web2py-users group
24#               thread: https://groups.google.com/forum/?fromgroups#
25#               !topic/web2py/O4c4Jfr18tM
26#
27#               There are lots of subtleties of ownership, and one
28#               has to take care when installing python 2.6 not to
29#               stop the systems python2.4 from working.
30#
31#               NOTE: The only thing that should need changing for
32#               each installation is the $BASEARCH (base
33#               architecture)
34#               of the machine. This is determined by doing uname -i.
35#               This is needed for the nginx installation.
36#
37#        File : setup-web2py-nginx-uwsgi-on-centos.sh
38#      Author : Peter Hutchinson
39# Modified by : Alan Etkin
40#       Email : spametki@gmail.com
41#   Copyright : web2py
42#        Date : 2013-01-28
43# Disclaimers : This script is provided "as is", without warranty of
44#               any kind.
45#     Licence : BSD
46# -------------------------------------------------------------------
47
48# Retrieve base architecture
49BASEARCH=$(uname -i)
50
51# Get Web2py Admin Password
52echo -e "Enter a password for web2py admin app: \c "
53read  PW
54
55echo 'Install development tools (it should take a while)'
56yum install gcc gdbm-devel readline-devel ncurses-devel zlib-devel \
57bzip2-devel sqlite-devel db4-devel openssl-devel tk-devel bluez-libs-devel
58
59#=================================
60
61# You can change Python and uWSGI options
62# to fit your deployment needs.
63
64# Python options
65PREFIX=2.6
66VERSION=2.6.8
67
68# uWSGI options
69version=uwsgi-1.2.4
70
71echo "Install python $PREFIX without overwriting python 2.4 (no, really, this will take a while too)"
72
73mkdir ~/src
74chmod 777 ~/src
75cd ~/src
76wget http://www.python.org/ftp/python/$VERSION/Python-$VERSION.tgz
77tar xvfz Python-$VERSION.tgz
78cd Python-$VERSION
79
80if [ "$VERSION" == "2.7.3" ]; then
81  echo "Applying patch for sqlite3 bug from post http://bugs.python.org/msg161076"
82  curl -sk https://raw.github.com/gist/2727063/ | patch -p1
83fi
84
85./configure --prefix=/opt/python$PREFIX --with-threads --enable-shared
86make
87
88#The altinstall ensures that python2.4 is left okay
89
90make altinstall
91echo "/opt/python$PREFIX/lib">/etc/ld.so.conf.d/opt-python$PREFIX.conf
92ldconfig
93
94#create alias so that python 2.x can be run with 'python2.x'
95
96alias -p python$PREFIX="/opt/python$/bin/python$PREFIX"
97ln -s /opt/python$PREFIX/bin/python$PREFIX /usr/bin/python$PREFIX
98
99echo 'Install uwsgi' $version
100cd ~
101curl -O http://projects.unbit.it/downloads/$version.tar.gz
102tar zxvf $version.tar.gz
103mkdir /opt/uwsgi-python
104cp -R ./$version/* /opt/uwsgi-python
105cd /opt/uwsgi-python
106
107echo "build using python $PREFIX"
108
109python$PREFIX uwsgiconfig.py --build
110useradd uwsgi
111
112echo "Create and own uwsgi log"
113# Note this log will need emptying from time to time
114
115touch /var/log/uwsgi.log
116chown uwsgi /var/log/uwsgi.log
117
118echo "Install web2py"
119
120cd /opt
121mkdir ./web-apps
122cd ./web-apps
123curl -O http://www.web2py.com/examples/static/web2py_src.zip
124unzip web2py_src.zip
125mv web2py/handlers/wsgihandler.py web2py/wsgihandler.py
126
127echo "Set the ownership for web2py application to uwsgi"
128chown -R uwsgi /opt/web-apps/web2py
129cd /opt/web-apps/web2py
130chmod -R u+rwx ./applications
131
132echo "Now creating the admin password and creating the scaffolding app package"
133sudo -u uwsgi python$PREFIX -c "from gluon.main import save_password;from gluon import widget;save_password('$PW',443);widget.console()"
134
135echo "Now install nginx"
136cd /etc/yum.repos.d
137echo "[nginx]">nginx.repo
138
139echo "baseurl=http://nginx.org/packages/centos/5/$BASEARCH/">>nginx.repo
140echo "gpgcheck=0">>nginx.repo
141echo "enabled=1">>nginx.repo
142yum install nginx
143
144echo "We don't want the defaults, so remove them"
145cd /etc/nginx/conf.d
146mv default.conf default.conf.o
147mv example_ssl.conf example_ssl.conf.o
148
149echo "
150The following configuration files are also needed
151The options for uwsgi are in the following file.
152Other options could be included.
153"
154
155echo "uwsgi_for_nginx.conf"
156
157echo "
158[uwsgi]
159uuid=uwsgi
160pythonpath = /opt/web-apps/web2py
161module = wsgihandler
162socket=127.0.0.1:9001
163harakiri 60
164harakiri-verbose
165enable-threads
166daemonize = /var/log/uwsgi.log
167" > /opt/uwsgi-python/uwsgi_for_nginx.conf
168
169chmod 755 /opt/uwsgi-python/uwsgi_for_nginx.conf
170
171echo "
172The next configuration file is for nginx, and goes in /etc/nginx/conf.d
173It serves the static directory of applications directly. I have not set up
174ssl because I access web2py admin by using ssh tunneling and the web2py rocket server.
175It should be straightforward to set up the ssl server however.
176"
177
178echo "web2py.conf"
179
180echo '
181server {
182  listen 80;
183  server_name $hostname;
184  location ~* /(\w+)/static(?:/_[\d]+\.[\d]+\.[\d]+)?/(.*)$ {
185            alias /opt/web-apps/web2py/applications/$1/static/$2;
186            expires max;
187  }
188  location / {
189    uwsgi_pass 127.0.0.1:9001;
190    include uwsgi_params;
191  }
192}
193
194server {
195  listen 443;
196  server_name $hostname;
197  ssl on;
198  ssl_certificate /etc/nginx/ssl/web2py.cert;
199  ssl_certificate_key /etc/nginx/ssl/web2py.key;
200  location / {
201    uwsgi_pass 127.0.0.1:9001;
202    include uwsgi_params;
203    uwsgi_param UWSGI_SCHEME $scheme;
204  }
205  location ~* /(\w+)/static(?:/_[\d]+\.[\d]+\.[\d]+)?/(.*)$ {
206            alias /opt/web-apps/web2py/applications/$1/static/$2;
207            expires max;
208  }
209}
210' > /etc/nginx/conf.d/web2py.conf
211
212
213echo "Auto-signed ssl certs"
214mkdir /etc/nginx/ssl
215echo "creating a self signed certificate"
216echo "=================================="
217openssl genrsa 1024 > /etc/nginx/ssl/web2py.key
218chmod 400 /etc/nginx/ssl/web2py.key
219openssl req -new -x509 -nodes -sha1 -days 365 -key /etc/nginx/ssl/web2py.key > /etc/nginx/ssl/web2py.cert
220openssl x509 -noout -fingerprint -text < /etc/nginx/ssl/web2py.cert > /etc/nginx/ssl/web2py.info
221
222echo "uwsgi as service"
223
224echo '
225#!/bin/bash
226
227# uwsgi - Use uwsgi to run python and wsgi web apps.
228#
229# chkconfig: - 85 15
230# description: Use uwsgi to run python and wsgi web apps.
231# processname: uwsgi
232
233# author: Roman Vasilyev
234
235# Source function library.
236. /etc/rc.d/init.d/functions
237
238###########################
239PATH=/opt/uwsgi-python:/sbin:/bin:/usr/sbin:/usr/bin
240PYTHONPATH=/home/www-data/web2py
241MODULE=wsgihandler
242PROG=/opt/uwsgi-python/uwsgi
243OWNER=uwsgi
244NAME=uwsgi
245DESC=uwsgi
246DAEMON_OPTS="-s 127.0.0.1:9001 -M 4 -t 30 -A 4 -p 16 -b 32768 -d \
247/var/log/$NAME.log --pidfile /var/run/$NAME.pid --uid $OWNER \
248--ini-paste /opt/uwsgi-python/uwsgi_for_nginx.conf"
249##############################
250
251[ -f /etc/sysconfig/uwsgi ] && . /etc/sysconfig/uwsgi
252
253lockfile=/var/lock/subsys/uwsgi
254
255start () {
256  echo -n "Starting $DESC: "
257  daemon $PROG $DAEMON_OPTS
258  retval=$?
259  echo
260  [ $retval -eq 0 ] && touch $lockfile
261  return $retval
262}
263
264stop () {
265  echo -n "Stopping $DESC: "
266  killproc $PROG
267  retval=$?
268  echo
269  [ $retval -eq 0 ] && rm -f $lockfile
270  return $retval
271}
272
273reload () {
274  echo "Reloading $NAME"
275  killproc $PROG -HUP
276  RETVAL=$?
277  echo
278}
279
280restart () {
281    stop
282    start
283}
284
285rh_status () {
286  status $PROG
287}
288
289rh_status_q() {
290  rh_status >/dev/null 2>&1
291}
292
293case "$1" in
294  start)
295    rh_status_q && exit 0
296    $1
297    ;;
298  stop)
299    rh_status_q || exit 0
300    $1
301    ;;
302  restart|force-reload)
303    $1
304    ;;
305  reload)
306    rh_status_q || exit 7
307    $1
308    ;;
309  status)
310    rh_status
311    ;;
312  *)
313    echo "Usage: $0 {start|stop|restart|reload|force-reload|status}" >&2
314    exit 2
315    ;;
316  esac
317  exit 0
318' > /etc/init.d/uwsgi
319
320chmod 755 /etc/init.d/uwsgi
321chkconfig --add uwsgi
322chkconfig uwsgi on
323
324echo "
325You can test it with
326
327service uwsgi start
328
329and stop it similarly.
330
331Nginx has automatically been set up as a service
332if you want to start it run
333
334service nginx start
335
336You should find the web2py welcome app will be displayed at your web address.
337As they are both services, they should automatically start on a system reboot.
338If you already had a server running, such as apache, you would need to stop
339that and turn its service off before running nginx.
340"
341
342echo "Turning off apache service"
343
344service httpd stop
345chkconfig httpd off
346cd ~
347
348echo "
349Installation complete. You might want to restart your server running
350
351reboot
352
353as superuser
354"
Note: See TracBrowser for help on using the repository browser.