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
+142 -102
View File
@@ -1,114 +1,104 @@
# Author: Peter Hutchinson #!/bin/bash
# Script for installing Web2py with Nginx and Uwsgi on Centos 5
# Created By Hutchinson
# Modified by spametki
# License: BSD # License: BSD
#
# Installing Web2py with Nginx and Uwsgi on Centos 5 is really tricky. # It was originally posted in this web2py-users group thread:
# https://groups.google.com/forum/?fromgroups#!topic/web2py/O4c4Jfr18tM
# There are lots of subtleties of ownership, and one has to take care # There are lots of subtleties of ownership, and one has to take care
# when installing python 2.6 not to stop the systems python2.4 from working. # when installing python 2.7 not to stop the systems python2.4 from working.
# 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
# the $basearch (base architecture) of the machine. We assume:
basearch=i386 # NOTE: The only thing that should need changing for
# each installation is the $BASEARCH (base architecture) of the machine.
# This is determined by doing uname -i. This is needed for the nginx installation.
# This can be determined by doing 'uname -i'. # Retrieve base architecture
# This is needed for the nginx installation. BASEARCH=$(uname -i)
# There is one script and three configuration files.
# install development tools 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
yum install gcc gdbm-devel readline-devel ncurses-devel zlib-devel bzip2-devel sqlite-devel db4-devel openssl-devel tk-devel bluez-libs-devel echo 'Install python 2.7 without overwriting python 2.4 (no, really, this will take a while too)'
# Install python 2.6 without overwriting python 2.4 VERSION=2.7.3
# ================================================= cd ~
curl -O http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
VERSION=2.6.8 tar -xvfz Python-2.7.3.tgz
mkdir ~/src cd Python-2.7.3
chmod 777 ~/src ./configure --prefix=/opt/python2.7 --with-threads --enable-shared
cd ~/src
wget http://www.python.org/ftp/python/$VERSION/Python-$VERSION.tgz
tar xvfz Python-2.6.8.tgz
cd Python-2.6.8
./configure --prefix=/opt/python2.6 --with-threads --enable-shared
make 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.6/lib">/etc/ld.so.conf.d/opt-python2.6.conf echo "/opt/python2.7/lib">/etc/ld.so.conf.d/opt-python2.7.conf
ldconfig ldconfig
# create alias so that python 2.6 can be run with 'python2.6' echo 'Create alias so that python 2.7 can be run with python2.7'
# =========================================================== 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
# Install uwsgi
# =========
version=uwsgi-1.2.3 version=uwsgi-1.2.3
cd /opt/ cd ~
wget http://projects.unbit.it/downloads/$version.tar.gz curl -O http://projects.unbit.it/downloads/$version.tar.gz
tar -zxvf $version.tar.gz tar -zxvf $version.tar.gz
mv $version/ uwsgi/ mkdir /opt/uwsgi-python
cd uwsgi/ cp -R ./$version/* /opt/uwsgi-python/*
cd /opt/uwsgi-python
# build using python 2.6 echo 'build using python 2.7'
# ====================== python2.7 setup.py build
python2.7 uwsgiconfig.py --build
python2.6 setup.py build
python2.6 uwsgiconfig.py --build
useradd uwsgi useradd uwsgi
# create and own uwsgi log echo 'Create and own uwsgi log'
# ========================
# Note this log will need emptying from time to time # Note this log will need emptying from time to time
echo " ">/var/log/uwsgi.log touch /var/log/uwsgi.log
chown uwsgi /var/log/uwsgi.log chown uwsgi /var/log/uwsgi.log
# Install web2py echo 'Install web2py'
# ==========
cd /opt cd /opt
mkdir web-apps mkdir ./web-apps
cd web-apps cd ./web-apps
wget http://www.web2py.com/examples/static/web2py_src.zip curl -O http://www.web2py.com/examples/static/web2py_src.zip
unzip web2py_src.zip unzip web2py_src.zip
# set the ownership for web2py application to uwsgi echo 'Set the ownership for web2py application to uwsgi'
# ================================================= cd /opt/web-apps/web2py
chown -R uwsgi /opt/web-apps/web2py
cd web2py chmod -R u+rwx ./applications
chown -R uwsgi applications
chmod -R u+wx applications
# Now install nginx
# =================
echo 'Now install nginx'
cd /etc/yum.repos.d cd /etc/yum.repos.d
echo "[nginx]">nginx.repo echo "[nginx]">nginx.repo
echo "baseurl=http://nginx.org/packages/centos/5/"$basearch$"/">>nginx.repo echo "baseurl=http://nginx.org/packages/centos/5/$BASEARCH/">>nginx.repo
echo "gpgcheck=0">>nginx.repo echo "gpgcheck=0">>nginx.repo
echo "enabled=1">>nginx.repo echo "enabled=1">>nginx.repo
yum install nginx yum install nginx
# We don't want the defaults, so remove them echo "We don't want the defaults, so remove them"
# ==========================================
cd /etc/nginx/conf.d cd /etc/nginx/conf.d
mv default.conf default.conf.o mv default.conf default.conf.o
mv example_ssl.conf example_ssl.conf.o mv example_ssl.conf example_ssl.conf.o
# The following configuration files are also needed echo '
# The options for uwsgi are in the following file. The following configuration files are also needed
# It should be placed in /etc/uwsgi. Other options could be included. The options for uwsgi are in the following file.
Other options could be included.
'
echo """ echo 'uwsgi_for_nginx.conf'
echo '
[uwsgi] [uwsgi]
uuid=uwsgi uuid=uwsgi
pythonpath = /opt/web-apps/web2py pythonpath = /opt/web-apps/web2py
module = wsgihandler module = wsgihandler
@@ -117,15 +107,20 @@ harakiri 60
harakiri-verbose harakiri-verbose
enable-threads enable-threads
daemonize = /var/log/uwsgi.log daemonize = /var/log/uwsgi.log
""" > /etc/uwsgi/uwsgi_for_nginx.conf ' > /opt/uwsgi-python/uwsgi_for_nginx.conf
# The next configuration file is for nginx, and goes in /etc/nginx/conf.d chmod 755 /opt/uwsgi-python/uwsgi_for_nginx.conf
# It serves the static diretory of applications directly.
# I have not set up ssl because I access web2py admin by using ssh
# tunneling and the web2py rocket server.
# It should be straightforward to set up the ssl server however.
echo """ echo '
The next configuration file is for nginx, and goes in /etc/nginx/conf.d
It serves the static directory of applications directly. I have not set up
ssl because I access web2py admin by using ssh tunneling and the web2py rocket server.
It should be straightforward to set up the ssl server however.
'
echo 'web2py.conf'
echo '
server { server {
listen 80; listen 80;
server_name $hostname; server_name $hostname;
@@ -138,45 +133,57 @@ server {
} }
} }
#server { server {
# listen 443; listen 443;
# server_name $hostname; server_name $hostname;
# ssl on; ssl on;
# ssl_certificate /etc/nginx/ssl/web2py.crt; ssl_certificate /etc/nginx/ssl/web2py.cert;
# ssl_certificate_key /etc/nginx/ssl/web2py.key; ssl_certificate_key /etc/nginx/ssl/web2py.key;
# location uwsgi_pass 127.0.0.1:9001; location / {
# include uwsgi_params; uwsgi_pass 127.0.0.1:9001;
# uwsgi_param UWSGI_SCHEME $scheme; include uwsgi_params;
#} uwsgi_param UWSGI_SCHEME $scheme;
""" > /etc/nginx/conf.d/web2py.conf }
}
# The final configuration file is only needed if you want to run ' > /etc/nginx/conf.d/web2py.conf
# uwsgi as a service. It should be placed in /etc/init.d
echo """ echo 'Auto-signed ssl certs'
mkdir /etc/nginx/ssl
echo "creating a self signed certificate"
echo "=================================="
openssl genrsa 1024 > /etc/nginx/ssl/web2py.key
chmod 400 /etc/nginx/ssl/web2py.key
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
echo 'uwsgi as service'
echo '
#!/bin/bash #!/bin/bash
# uwsgi - Use uwsgi to run python and wsgi web apps. # uwsgi - Use uwsgi to run python and wsgi web apps.
# #
# chkconfig: - 85 15 # chkconfig: - 85 15
# description: Use uwsgi to run python and wsgi web apps. # description: Use uwsgi to run python and wsgi web apps.
# processname: uwsgi # processname: uwsgi
# author: Roman Vasilyev # author: Roman Vasilyev
# Source function library. # Source function library.
. /etc/rc.d/init.d/functions . /etc/rc.d/init.d/functions
########################### ###########################
PATH=/etc/uwsgi-python:/sbin:/bin:/usr/sbin:/usr/bin PATH=/opt/uwsgi-python:/sbin:/bin:/usr/sbin:/usr/bin
PYTHONPATH=/home/www-data/web2py PYTHONPATH=/home/www-data/web2py
MODULE=wsgihandler MODULE=wsgihandler
prog=/etc/uwsgi-python/uwsgi PROG=/opt/uwsgi-python/uwsgi
OWNER=uwsgi OWNER=uwsgi
# OWNER=nginx ¿?
NAME=uwsgi NAME=uwsgi
DESC=uwsgi DESC=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 --ini-paste /etc/uwsgi-python/uwsgi_for_nginx.conf" 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 /opt/uwsgi-python/uwsgi_for_nginx.conf"
############################## ##############################
[ -f /etc/sysconfig/uwsgi ] && . /etc/sysconfig/uwsgi [ -f /etc/sysconfig/uwsgi ] && . /etc/sysconfig/uwsgi
@@ -185,7 +192,7 @@ lockfile=/var/lock/subsys/uwsgi
start () { start () {
echo -n "Starting $DESC: " echo -n "Starting $DESC: "
daemon $prog $DAEMON_OPTS daemon $PROG $DAEMON_OPTS
retval=$? retval=$?
echo echo
[ $retval -eq 0 ] && touch $lockfile [ $retval -eq 0 ] && touch $lockfile
@@ -194,7 +201,7 @@ start () {
stop () { stop () {
echo -n "Stopping $DESC: " echo -n "Stopping $DESC: "
killproc $prog killproc $PROG
retval=$? retval=$?
echo echo
[ $retval -eq 0 ] && rm -f $lockfile [ $retval -eq 0 ] && rm -f $lockfile
@@ -203,14 +210,14 @@ stop () {
reload () { reload () {
echo "Reloading $NAME" echo "Reloading $NAME"
killproc $prog -HUP killproc $PROG -HUP
RETVAL=$? RETVAL=$?
echo echo
} }
force-reload () { force-reload () {
echo "Reloading $NAME" echo "Reloading $NAME"
killproc $prog -TERM killproc $PROG -TERM
RETVAL=$? RETVAL=$?
echo echo
} }
@@ -221,7 +228,7 @@ restart () {
} }
rh_status () { rh_status () {
status $prog status $PROG
} }
rh_status_q() { rh_status_q() {
@@ -253,5 +260,38 @@ case "$1" in
;; ;;
esac esac
exit 0 exit 0
' > /etc/init.d/uwsgi
""" > /etc/init.d/uwsgi_nginx 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
'