improved scripts/setup-web2py-nginx-uwsgi-on-centos.sh, thanks Alan

This commit is contained in:
mdipierro
2012-07-29 10:37:04 -05:00
parent 297b4167e9
commit f2a8bad6fd
2 changed files with 298 additions and 258 deletions
+1 -1
View File
@@ -1 +1 @@
Version 2.00.0 (2012-07-29 10:32:43) dev Version 2.00.0 (2012-07-29 10:37:01) dev
+297 -257
View File
@@ -1,257 +1,297 @@
# Author: Peter Hutchinson #!/bin/bash
# License: BSD
# # Script for installing Web2py with Nginx and Uwsgi on Centos 5
# Installing Web2py with Nginx and Uwsgi on Centos 5 is really tricky. # Created By Hutchinson
# There are lots of subtleties of ownership, and one has to take care # Modified by spametki
# when installing python 2.6 not to stop the systems python2.4 from working. # License: BSD
# Here is a script that does all the installation from a clean start machine.
# The only thing that should need changing for another installation is # It was originally posted in this web2py-users group thread:
# the $basearch (base architecture) of the machine. We assume: # https://groups.google.com/forum/?fromgroups#!topic/web2py/O4c4Jfr18tM
basearch=i386 # There are lots of subtleties of ownership, and one has to take care
# when installing python 2.7 not to stop the systems python2.4 from working.
# This can be determined by doing 'uname -i'.
# This is needed for the nginx installation. # NOTE: The only thing that should need changing for
# There is one script and three configuration files. # each installation is the $BASEARCH (base architecture) of the machine.
# This is determined by doing uname -i. This is needed for the nginx installation.
# install development tools
# Retrieve base architecture
yum install gcc gdbm-devel readline-devel ncurses-devel zlib-devel bzip2-devel sqlite-devel db4-devel openssl-devel tk-devel bluez-libs-devel BASEARCH=$(uname -i)
# Install python 2.6 without overwriting python 2.4 echo 'Install development tools (it should take a while)'
# ================================================= yum install gcc gdbm-devel readline-devel ncurses-devel zlib-devel \
bzip2-devel sqlite-devel db4-devel openssl-devel tk-devel bluez-libs-devel
VERSION=2.6.8
mkdir ~/src echo 'Install python 2.7 without overwriting python 2.4 (no, really, this will take a while too)'
chmod 777 ~/src
cd ~/src VERSION=2.7.3
wget http://www.python.org/ftp/python/$VERSION/Python-$VERSION.tgz cd ~
tar xvfz Python-2.6.8.tgz curl -O http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
cd Python-2.6.8 tar -xvfz Python-2.7.3.tgz
./configure --prefix=/opt/python2.6 --with-threads --enable-shared cd Python-2.7.3
make ./configure --prefix=/opt/python2.7 --with-threads --enable-shared
make
# The altinstall ensures that python2.4 is left okay
# ================================================== echo 'The altinstall ensures that python2.4 is left okay'
make altinstall
make altinstall echo "/opt/python2.7/lib">/etc/ld.so.conf.d/opt-python2.7.conf
echo "/opt/python2.6/lib">/etc/ld.so.conf.d/opt-python2.6.conf ldconfig
ldconfig
echo 'Create alias so that python 2.7 can be run with python2.7'
# create alias so that python 2.6 can be run with 'python2.6' alias -p python2.7="/opt/python2.7/bin/python2.7"
# =========================================================== ln -s /opt/python2.7/bin/python2.7 /usr/bin/python2.7
alias -p python2.6="/opt/python2.6/bin/python2.6" echo 'Install uwsgi'
ln -s /opt/python2.6/bin/python2.6 /usr/bin/python2.6
version=uwsgi-1.2.3
# Install uwsgi cd ~
# ========= curl -O http://projects.unbit.it/downloads/$version.tar.gz
tar -zxvf $version.tar.gz
version=uwsgi-1.2.3 mkdir /opt/uwsgi-python
cd /opt/ cp -R ./$version/* /opt/uwsgi-python/*
wget http://projects.unbit.it/downloads/$version.tar.gz cd /opt/uwsgi-python
tar -zxvf $version.tar.gz
mv $version/ uwsgi/ echo 'build using python 2.7'
cd uwsgi/ python2.7 setup.py build
python2.7 uwsgiconfig.py --build
# build using python 2.6 useradd uwsgi
# ======================
echo 'Create and own uwsgi log'
python2.6 setup.py build # Note this log will need emptying from time to time
python2.6 uwsgiconfig.py --build
useradd uwsgi touch /var/log/uwsgi.log
chown uwsgi /var/log/uwsgi.log
# create and own uwsgi log
# ======================== echo 'Install web2py'
# Note this log will need emptying from time to time
cd /opt
echo " ">/var/log/uwsgi.log mkdir ./web-apps
chown uwsgi /var/log/uwsgi.log cd ./web-apps
curl -O http://www.web2py.com/examples/static/web2py_src.zip
# Install web2py unzip web2py_src.zip
# ==========
echo 'Set the ownership for web2py application to uwsgi'
cd /opt cd /opt/web-apps/web2py
mkdir web-apps chown -R uwsgi /opt/web-apps/web2py
cd web-apps chmod -R u+rwx ./applications
wget http://www.web2py.com/examples/static/web2py_src.zip
unzip web2py_src.zip echo 'Now install nginx'
cd /etc/yum.repos.d
# set the ownership for web2py application to uwsgi echo "[nginx]">nginx.repo
# =================================================
echo "baseurl=http://nginx.org/packages/centos/5/$BASEARCH/">>nginx.repo
cd web2py echo "gpgcheck=0">>nginx.repo
chown -R uwsgi applications echo "enabled=1">>nginx.repo
chmod -R u+wx applications yum install nginx
# Now install nginx echo "We don't want the defaults, so remove them"
# ================= cd /etc/nginx/conf.d
mv default.conf default.conf.o
cd /etc/yum.repos.d mv example_ssl.conf example_ssl.conf.o
echo "[nginx]">nginx.repo
echo '
echo "baseurl=http://nginx.org/packages/centos/5/"$basearch$"/">>nginx.repo The following configuration files are also needed
echo "gpgcheck=0">>nginx.repo The options for uwsgi are in the following file.
echo "enabled=1">>nginx.repo Other options could be included.
yum install nginx '
# We don't want the defaults, so remove them echo 'uwsgi_for_nginx.conf'
# ==========================================
echo '
cd /etc/nginx/conf.d [uwsgi]
mv default.conf default.conf.o uuid=uwsgi
mv example_ssl.conf example_ssl.conf.o pythonpath = /opt/web-apps/web2py
module = wsgihandler
# The following configuration files are also needed socket=127.0.0.1:9001
# The options for uwsgi are in the following file. harakiri 60
# It should be placed in /etc/uwsgi. Other options could be included. harakiri-verbose
enable-threads
echo """ daemonize = /var/log/uwsgi.log
[uwsgi] ' > /opt/uwsgi-python/uwsgi_for_nginx.conf
uuid=uwsgi chmod 755 /opt/uwsgi-python/uwsgi_for_nginx.conf
pythonpath = /opt/web-apps/web2py
module = wsgihandler echo '
socket=127.0.0.1:9001 The next configuration file is for nginx, and goes in /etc/nginx/conf.d
harakiri 60 It serves the static directory of applications directly. I have not set up
harakiri-verbose ssl because I access web2py admin by using ssh tunneling and the web2py rocket server.
enable-threads It should be straightforward to set up the ssl server however.
daemonize = /var/log/uwsgi.log '
""" > /etc/uwsgi/uwsgi_for_nginx.conf
echo 'web2py.conf'
# The next configuration file is for nginx, and goes in /etc/nginx/conf.d
# It serves the static diretory of applications directly. echo '
# I have not set up ssl because I access web2py admin by using ssh server {
# tunneling and the web2py rocket server. listen 80;
# It should be straightforward to set up the ssl server however. server_name $hostname;
location ~* /(\w+)/static/ {
echo """ root /opt/web-apps/web2py/applications/;
server { }
listen 80; location / {
server_name $hostname; uwsgi_pass 127.0.0.1:9001;
location ~* /(\w+)/static/ { include uwsgi_params;
root /opt/web-apps/web2py/applications/; }
} }
location / {
uwsgi_pass 127.0.0.1:9001; server {
include uwsgi_params; listen 443;
} server_name $hostname;
} ssl on;
ssl_certificate /etc/nginx/ssl/web2py.cert;
#server { ssl_certificate_key /etc/nginx/ssl/web2py.key;
# listen 443; location / {
# server_name $hostname; uwsgi_pass 127.0.0.1:9001;
# ssl on; include uwsgi_params;
# ssl_certificate /etc/nginx/ssl/web2py.crt; uwsgi_param UWSGI_SCHEME $scheme;
# ssl_certificate_key /etc/nginx/ssl/web2py.key; }
# location uwsgi_pass 127.0.0.1:9001; }
# include uwsgi_params; ' > /etc/nginx/conf.d/web2py.conf
# uwsgi_param UWSGI_SCHEME $scheme;
#}
""" > /etc/nginx/conf.d/web2py.conf echo 'Auto-signed ssl certs'
mkdir /etc/nginx/ssl
# The final configuration file is only needed if you want to run echo "creating a self signed certificate"
# uwsgi as a service. It should be placed in /etc/init.d echo "=================================="
openssl genrsa 1024 > /etc/nginx/ssl/web2py.key
chmod 400 /etc/nginx/ssl/web2py.key
echo """ openssl req -new -x509 -nodes -sha1 -days 365 -key /etc/nginx/ssl/web2py.key > /etc/nginx/ssl/web2py.cert
openssl x509 -noout -fingerprint -text < /etc/nginx/ssl/web2py.cert > /etc/nginx/ssl/web2py.info
#!/bin/bash
# uwsgi - Use uwsgi to run python and wsgi web apps. echo 'uwsgi as service'
#
# chkconfig: - 85 15 echo '
# description: Use uwsgi to run python and wsgi web apps. #!/bin/bash
# processname: uwsgi
# author: Roman Vasilyev # uwsgi - Use uwsgi to run python and wsgi web apps.
#
# Source function library. # chkconfig: - 85 15
. /etc/rc.d/init.d/functions # description: Use uwsgi to run python and wsgi web apps.
# processname: uwsgi
###########################
PATH=/etc/uwsgi-python:/sbin:/bin:/usr/sbin:/usr/bin # author: Roman Vasilyev
PYTHONPATH=/home/www-data/web2py
MODULE=wsgihandler # Source function library.
prog=/etc/uwsgi-python/uwsgi . /etc/rc.d/init.d/functions
OWNER=uwsgi
# OWNER=nginx ¿? ###########################
NAME=uwsgi PATH=/opt/uwsgi-python:/sbin:/bin:/usr/sbin:/usr/bin
DESC=uwsgi PYTHONPATH=/home/www-data/web2py
DAEMON_OPTS="-s 127.0.0.1:9001 -M 4 -t 30 -A 4 -p 16 -b 32768 -d /var/log/$NAME.log --pidfile /var/run/$NAME.pid --uid $OWNER --ini-paste /etc/uwsgi-python/uwsgi_for_nginx.conf" MODULE=wsgihandler
############################## PROG=/opt/uwsgi-python/uwsgi
OWNER=uwsgi
[ -f /etc/sysconfig/uwsgi ] && . /etc/sysconfig/uwsgi NAME=uwsgi
DESC=uwsgi
lockfile=/var/lock/subsys/uwsgi DAEMON_OPTS="-s 127.0.0.1:9001 -M 4 -t 30 -A 4 -p 16 -b 32768 -d \
/var/log/$NAME.log --pidfile /var/run/$NAME.pid --uid $OWNER \
start () { --ini-paste /opt/uwsgi-python/uwsgi_for_nginx.conf"
echo -n "Starting $DESC: " ##############################
daemon $prog $DAEMON_OPTS
retval=$? [ -f /etc/sysconfig/uwsgi ] && . /etc/sysconfig/uwsgi
echo
[ $retval -eq 0 ] && touch $lockfile lockfile=/var/lock/subsys/uwsgi
return $retval
} start () {
echo -n "Starting $DESC: "
stop () { daemon $PROG $DAEMON_OPTS
echo -n "Stopping $DESC: " retval=$?
killproc $prog echo
retval=$? [ $retval -eq 0 ] && touch $lockfile
echo return $retval
[ $retval -eq 0 ] && rm -f $lockfile }
return $retval
} stop () {
echo -n "Stopping $DESC: "
reload () { killproc $PROG
echo "Reloading $NAME" retval=$?
killproc $prog -HUP echo
RETVAL=$? [ $retval -eq 0 ] && rm -f $lockfile
echo return $retval
} }
force-reload () { reload () {
echo "Reloading $NAME" echo "Reloading $NAME"
killproc $prog -TERM killproc $PROG -HUP
RETVAL=$? RETVAL=$?
echo echo
} }
restart () { force-reload () {
stop echo "Reloading $NAME"
start killproc $PROG -TERM
} RETVAL=$?
echo
rh_status () { }
status $prog
} restart () {
stop
rh_status_q() { start
rh_status >/dev/null 2>&1 }
}
rh_status () {
case "$1" in status $PROG
start) }
rh_status_q && exit 0
$1 rh_status_q() {
;; rh_status >/dev/null 2>&1
stop) }
rh_status_q || exit 0
$1 case "$1" in
;; start)
restart|force-reload) rh_status_q && exit 0
$1 $1
;; ;;
reload) stop)
rh_status_q || exit 7 rh_status_q || exit 0
$1 $1
;; ;;
status) restart|force-reload)
rh_status $1
;; ;;
*) reload)
echo "Usage: $0 {start|stop|restart|reload|force-reload|status}" >&2 rh_status_q || exit 7
exit 2 $1
;; ;;
esac status)
exit 0 rh_status
;;
""" > /etc/init.d/uwsgi_nginx *)
echo "Usage: $0 {start|stop|restart|reload|force-reload|status}" >&2
exit 2
;;
esac
exit 0
' > /etc/init.d/uwsgi
chmod 755 /etc/init.d/uwsgi
chkconfig --add uwsgi
chkconfig uwsgi on
echo '
You can test it with
service uwsgi start
and stop it similarly.
Nginx has automatically been set up as a service
if you want to start it run
service nginx start
You should find the web2py welcome app will be displayed at your web address.
As they are both services, they should automatically start on a system reboot.
If you already had a server running, such as apache, you would need to stop
that and turn its service off before running nginx.
'
echo 'Turning off apache service'
service httpd stop
chkconfig httpd off
echo '
Installation complete. You might want to restart your server running
reboot
as superuser
'