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 |
---|
49 | BASEARCH=$(uname -i) |
---|
50 | |
---|
51 | # Get Web2py Admin Password |
---|
52 | echo -e "Enter a password for web2py admin app: \c " |
---|
53 | read PW |
---|
54 | |
---|
55 | echo 'Install development tools (it should take a while)' |
---|
56 | yum install gcc gdbm-devel readline-devel ncurses-devel zlib-devel \ |
---|
57 | bzip2-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 |
---|
65 | PREFIX=2.6 |
---|
66 | VERSION=2.6.8 |
---|
67 | |
---|
68 | # uWSGI options |
---|
69 | version=uwsgi-1.2.4 |
---|
70 | |
---|
71 | echo "Install python $PREFIX without overwriting python 2.4 (no, really, this will take a while too)" |
---|
72 | |
---|
73 | mkdir ~/src |
---|
74 | chmod 777 ~/src |
---|
75 | cd ~/src |
---|
76 | wget http://www.python.org/ftp/python/$VERSION/Python-$VERSION.tgz |
---|
77 | tar xvfz Python-$VERSION.tgz |
---|
78 | cd Python-$VERSION |
---|
79 | |
---|
80 | if [ "$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 |
---|
83 | fi |
---|
84 | |
---|
85 | ./configure --prefix=/opt/python$PREFIX --with-threads --enable-shared |
---|
86 | make |
---|
87 | |
---|
88 | #The altinstall ensures that python2.4 is left okay |
---|
89 | |
---|
90 | make altinstall |
---|
91 | echo "/opt/python$PREFIX/lib">/etc/ld.so.conf.d/opt-python$PREFIX.conf |
---|
92 | ldconfig |
---|
93 | |
---|
94 | #create alias so that python 2.x can be run with 'python2.x' |
---|
95 | |
---|
96 | alias -p python$PREFIX="/opt/python$/bin/python$PREFIX" |
---|
97 | ln -s /opt/python$PREFIX/bin/python$PREFIX /usr/bin/python$PREFIX |
---|
98 | |
---|
99 | echo 'Install uwsgi' $version |
---|
100 | cd ~ |
---|
101 | curl -O http://projects.unbit.it/downloads/$version.tar.gz |
---|
102 | tar zxvf $version.tar.gz |
---|
103 | mkdir /opt/uwsgi-python |
---|
104 | cp -R ./$version/* /opt/uwsgi-python |
---|
105 | cd /opt/uwsgi-python |
---|
106 | |
---|
107 | echo "build using python $PREFIX" |
---|
108 | |
---|
109 | python$PREFIX uwsgiconfig.py --build |
---|
110 | useradd uwsgi |
---|
111 | |
---|
112 | echo "Create and own uwsgi log" |
---|
113 | # Note this log will need emptying from time to time |
---|
114 | |
---|
115 | touch /var/log/uwsgi.log |
---|
116 | chown uwsgi /var/log/uwsgi.log |
---|
117 | |
---|
118 | echo "Install web2py" |
---|
119 | |
---|
120 | cd /opt |
---|
121 | mkdir ./web-apps |
---|
122 | cd ./web-apps |
---|
123 | curl -O http://www.web2py.com/examples/static/web2py_src.zip |
---|
124 | unzip web2py_src.zip |
---|
125 | mv web2py/handlers/wsgihandler.py web2py/wsgihandler.py |
---|
126 | |
---|
127 | echo "Set the ownership for web2py application to uwsgi" |
---|
128 | chown -R uwsgi /opt/web-apps/web2py |
---|
129 | cd /opt/web-apps/web2py |
---|
130 | chmod -R u+rwx ./applications |
---|
131 | |
---|
132 | echo "Now creating the admin password and creating the scaffolding app package" |
---|
133 | sudo -u uwsgi python$PREFIX -c "from gluon.main import save_password;from gluon import widget;save_password('$PW',443);widget.console()" |
---|
134 | |
---|
135 | echo "Now install nginx" |
---|
136 | cd /etc/yum.repos.d |
---|
137 | echo "[nginx]">nginx.repo |
---|
138 | |
---|
139 | echo "baseurl=http://nginx.org/packages/centos/5/$BASEARCH/">>nginx.repo |
---|
140 | echo "gpgcheck=0">>nginx.repo |
---|
141 | echo "enabled=1">>nginx.repo |
---|
142 | yum install nginx |
---|
143 | |
---|
144 | echo "We don't want the defaults, so remove them" |
---|
145 | cd /etc/nginx/conf.d |
---|
146 | mv default.conf default.conf.o |
---|
147 | mv example_ssl.conf example_ssl.conf.o |
---|
148 | |
---|
149 | echo " |
---|
150 | The following configuration files are also needed |
---|
151 | The options for uwsgi are in the following file. |
---|
152 | Other options could be included. |
---|
153 | " |
---|
154 | |
---|
155 | echo "uwsgi_for_nginx.conf" |
---|
156 | |
---|
157 | echo " |
---|
158 | [uwsgi] |
---|
159 | uuid=uwsgi |
---|
160 | pythonpath = /opt/web-apps/web2py |
---|
161 | module = wsgihandler |
---|
162 | socket=127.0.0.1:9001 |
---|
163 | harakiri 60 |
---|
164 | harakiri-verbose |
---|
165 | enable-threads |
---|
166 | daemonize = /var/log/uwsgi.log |
---|
167 | " > /opt/uwsgi-python/uwsgi_for_nginx.conf |
---|
168 | |
---|
169 | chmod 755 /opt/uwsgi-python/uwsgi_for_nginx.conf |
---|
170 | |
---|
171 | echo " |
---|
172 | The next configuration file is for nginx, and goes in /etc/nginx/conf.d |
---|
173 | It serves the static directory of applications directly. I have not set up |
---|
174 | ssl because I access web2py admin by using ssh tunneling and the web2py rocket server. |
---|
175 | It should be straightforward to set up the ssl server however. |
---|
176 | " |
---|
177 | |
---|
178 | echo "web2py.conf" |
---|
179 | |
---|
180 | echo ' |
---|
181 | server { |
---|
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 | |
---|
194 | server { |
---|
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 | |
---|
213 | echo "Auto-signed ssl certs" |
---|
214 | mkdir /etc/nginx/ssl |
---|
215 | echo "creating a self signed certificate" |
---|
216 | echo "==================================" |
---|
217 | openssl genrsa 1024 > /etc/nginx/ssl/web2py.key |
---|
218 | chmod 400 /etc/nginx/ssl/web2py.key |
---|
219 | openssl req -new -x509 -nodes -sha1 -days 365 -key /etc/nginx/ssl/web2py.key > /etc/nginx/ssl/web2py.cert |
---|
220 | openssl x509 -noout -fingerprint -text < /etc/nginx/ssl/web2py.cert > /etc/nginx/ssl/web2py.info |
---|
221 | |
---|
222 | echo "uwsgi as service" |
---|
223 | |
---|
224 | echo ' |
---|
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 | ########################### |
---|
239 | PATH=/opt/uwsgi-python:/sbin:/bin:/usr/sbin:/usr/bin |
---|
240 | PYTHONPATH=/home/www-data/web2py |
---|
241 | MODULE=wsgihandler |
---|
242 | PROG=/opt/uwsgi-python/uwsgi |
---|
243 | OWNER=uwsgi |
---|
244 | NAME=uwsgi |
---|
245 | DESC=uwsgi |
---|
246 | DAEMON_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 | |
---|
253 | lockfile=/var/lock/subsys/uwsgi |
---|
254 | |
---|
255 | start () { |
---|
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 | |
---|
264 | stop () { |
---|
265 | echo -n "Stopping $DESC: " |
---|
266 | killproc $PROG |
---|
267 | retval=$? |
---|
268 | echo |
---|
269 | [ $retval -eq 0 ] && rm -f $lockfile |
---|
270 | return $retval |
---|
271 | } |
---|
272 | |
---|
273 | reload () { |
---|
274 | echo "Reloading $NAME" |
---|
275 | killproc $PROG -HUP |
---|
276 | RETVAL=$? |
---|
277 | echo |
---|
278 | } |
---|
279 | |
---|
280 | restart () { |
---|
281 | stop |
---|
282 | start |
---|
283 | } |
---|
284 | |
---|
285 | rh_status () { |
---|
286 | status $PROG |
---|
287 | } |
---|
288 | |
---|
289 | rh_status_q() { |
---|
290 | rh_status >/dev/null 2>&1 |
---|
291 | } |
---|
292 | |
---|
293 | case "$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 | |
---|
320 | chmod 755 /etc/init.d/uwsgi |
---|
321 | chkconfig --add uwsgi |
---|
322 | chkconfig uwsgi on |
---|
323 | |
---|
324 | echo " |
---|
325 | You can test it with |
---|
326 | |
---|
327 | service uwsgi start |
---|
328 | |
---|
329 | and stop it similarly. |
---|
330 | |
---|
331 | Nginx has automatically been set up as a service |
---|
332 | if you want to start it run |
---|
333 | |
---|
334 | service nginx start |
---|
335 | |
---|
336 | You should find the web2py welcome app will be displayed at your web address. |
---|
337 | As they are both services, they should automatically start on a system reboot. |
---|
338 | If you already had a server running, such as apache, you would need to stop |
---|
339 | that and turn its service off before running nginx. |
---|
340 | " |
---|
341 | |
---|
342 | echo "Turning off apache service" |
---|
343 | |
---|
344 | service httpd stop |
---|
345 | chkconfig httpd off |
---|
346 | cd ~ |
---|
347 | |
---|
348 | echo " |
---|
349 | Installation complete. You might want to restart your server running |
---|
350 | |
---|
351 | reboot |
---|
352 | |
---|
353 | as superuser |
---|
354 | " |
---|