Skip to content

Instantly share code, notes, and snippets.

@jniltinho
Last active December 26, 2015 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jniltinho/7145297 to your computer and use it in GitHub Desktop.
Save jniltinho/7145297 to your computer and use it in GitHub Desktop.
Install Web2py + Nginx + uWSGI
#!/bin/bash
# Author: Nilton OS www.linuxpro.com.br
echo 'setup-web2py-nginx-uwsgi-opensuse.sh'
echo 'Requires OpenSUSE 12.X, 13.X 32/64Bits and installs Nginx + uWSGI + Web2py'
# Check if user has root privileges
if [[ $EUID -ne 0 ]]; then
echo "You must run the script as root or using sudo"
exit 1
fi
SUSE_VERSION=$(cat /etc/issue | awk '{ print $4 }' | head -n1)
# Get Web2py Admin Password
echo -e "Web2py Admin Password: \c "
read PW
echo -e "Set Server Name Ex: www.domain.com : \c "
read SERVER_FQDN
echo -e "Set Server IP (commonly 127.0.0.1 works): \c "
read SERVER_IP
echo "" >>/etc/hosts
echo "$SERVER_IP $SERVER_FQDN" >>/etc/hosts
## Create user Web2py
if [[ $SUSE_VERSION == '12.2' ]]; then
groupadd web2py
useradd -m -g web2py --system -s /bin/sh -c 'Web2py' web2py
else
useradd -m -U --system -s /bin/sh -c 'Web2py' web2py
fi
zypper ar http://download.opensuse.org/repositories/server:/http/openSUSE_${SUSE_VERSION}/ server_http
zypper --no-gpg-checks refresh
zypper up -y
zypper in -y nginx python-xml python-pip unzip sudo git-core python-imaging pcre-devel
zypper in -y gcc python-devel libxml2-devel python-pip unzip python-mysql wget
pip install --upgrade pip
PIPPATH=`which pip`
$PIPPATH install --upgrade uwsgi
# Prepare folders for uwsgi
mkdir -p /etc/uwsgi
mkdir -p /var/log/uwsgi
usermod -G www nginx
mkdir -p /etc/nginx/vhosts.d/
mkdir -p /etc/nginx/ssl/
cd /etc/nginx/ssl
openssl genrsa 1024 > web2py.key
chmod 400 web2py.key
openssl req -new -x509 -nodes -sha1 -days 1780 -key web2py.key > web2py.crt
openssl x509 -noout -fingerprint -text < web2py.crt > web2py.info
echo 'server {
listen YOUR_SERVER_IP:80;
server_name YOUR_SERVER_FQDN;
#to enable correct use of response.static_version
#location ~* /(\w+)/static(?:/_[\d]+\.[\d]+\.[\d]+)?/(.*)$ {
# alias /home/web2py/web2py/applications/$1/static/$2;
# expires max;
#}
location ~* /(\w+)/static/ {
root /home/web2py/web2py/applications/;
#remove next comment on production
#expires max;
}
location / {
#uwsgi_pass 127.0.0.1:9001;
uwsgi_pass unix:/home/web2py/web2py/logs/web2py.socket;
include /etc/nginx/uwsgi_params;
uwsgi_param UWSGI_SCHEME $scheme;
uwsgi_param SERVER_SOFTWARE nginx/$nginx_version;
# proxy_read_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
# proxy_connect_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
# proxy_redirect off;
## Send File Upload via HTTP
client_body_in_file_only clean;
client_body_buffer_size 32K;
client_max_body_size 40M;
sendfile on;
send_timeout 300s;
}
}
server {
listen YOUR_SERVER_IP:443 ssl;
server_name YOUR_SERVER_FQDN;
ssl_certificate /etc/nginx/ssl/web2py.crt;
ssl_certificate_key /etc/nginx/ssl/web2py.key;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_ciphers ECDHE-RSA-AES256-SHA:DHE-RSA-AES256-SHA:DHE-DSS-AES256-SHA:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA;
ssl_protocols SSLv3 TLSv1;
keepalive_timeout 70;
location / {
#uwsgi_pass 127.0.0.1:9001;
uwsgi_pass unix:/home/web2py/web2py/logs/web2py.socket;
include /etc/nginx/uwsgi_params;
uwsgi_param UWSGI_SCHEME $scheme;
uwsgi_param SERVER_SOFTWARE nginx/$nginx_version;
# proxy_read_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
# proxy_connect_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
# proxy_redirect off;
## Send File Upload via HTTP
client_body_in_file_only clean;
client_body_buffer_size 32K;
client_max_body_size 40M;
sendfile on;
send_timeout 300s;
}
}' >/etc/nginx/vhosts.d/web2py.conf
sed -i "s/YOUR_SERVER_IP/$SERVER_IP/g" /etc/nginx/vhosts.d/web2py.conf
sed -i "s/YOUR_SERVER_FQDN/$SERVER_FQDN/g" /etc/nginx/vhosts.d/web2py.conf
# Create configuration file /etc/uwsgi/web2py.ini
echo '[uwsgi]
pythonpath = /home/web2py/web2py/
mount = /=wsgihandler:application
processes = 4
master = true
#harakiri = 60
reload-mercy = 8
cpu-affinity = 1
chmod-socket = 666
socket = /home/web2py/web2py/logs/%n.socket
stats = /home/web2py/web2py/logs/%n.stats.socket
logto = /var/log/uwsgi/%n.log
max-requests = 2000
limit-as = 512
reload-on-as = 256
reload-on-rss = 192
uid = web2py
gid = web2py
cron = 0 0 -1 -1 -1 python /home/web2py/web2py/web2py.py -Q -S welcome -M -R scripts/sessions2trash.py -A -o
no-orphans = true
' >/etc/uwsgi/web2py.ini
wget http://web2py.com/examples/static/web2py_src.zip -O /home/web2py/web2py_src.zip
unzip /home/web2py/web2py_src.zip -d /home/web2py/
rm /home/web2py/web2py_src.zip
cp /home/web2py/web2py/handlers/wsgihandler.py /home/web2py/web2py/
chown -R web2py:web2py /home/web2py
cd /home/web2py/web2py
sudo -u web2py python -c "from gluon.main import save_password; save_password('$PW',443)"
## Daemons /start/stop
echo '#!/bin/sh
# Copyright (c) 2012 SUSE LINUX Products GmbH, Nuernberg, Germany.
#
# Author: James Oakley
#
# /etc/init.d/uwsgi
# and its symbolic link
# /(usr/)sbin/rcuwsgi
#
# LSB compatible service control script; see http://www.linuxbase.org/spec/
#
### BEGIN INIT INFO
# Provides: uwsgi
# Required-Start: $syslog $remote_fs
# Should-Start: $time ypbind smtp
# Required-Stop: $syslog $remote_fs
# Should-Stop: ypbind smtp
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Application Container Server for Networked/Clustered Web Applications
# Description: Application Container Server for Networked/Clustered Web Applications
### END INIT INFO
# Check for missing binaries (stale symlinks should not happen)
UWSGI_BIN=`which uwsgi`
test -x $UWSGI_BIN || { echo "$UWSGI_BIN not installed";
if [ "$1" = "stop" ]; then exit 0;
else exit 5; fi; }
UWSGI_EMPEROR_MODE=true
UWSGI_VASSALS="/etc/uwsgi/"
UWSGI_OPTIONS="--logto /var/log/uwsgi/uwsgi.log"
UWSGI_OPTIONS="$UWSGI_OPTIONS --autoload"
if [ "$UWSGI_EMPEROR_MODE" = "true" ] ; then
UWSGI_OPTIONS="$UWSGI_OPTIONS --emperor $UWSGI_VASSALS"
fi
. /etc/rc.status
rc_reset
case "$1" in
start)
echo -n "Starting uWSGI "
/sbin/startproc $UWSGI_BIN $UWSGI_OPTIONS
rc_status -v
;;
stop)
echo -n "Shutting down uWSGI "
/sbin/killproc $UWSGI_BIN
rc_status -v
;;
try-restart|condrestart)
if test "$1" = "condrestart"; then
echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
fi
$0 status
if test $? = 0; then
$0 restart
else
rc_reset
fi
rc_status
;;
restart)
$0 stop
$0 start
rc_status
;;
force-reload)
echo -n "Reload service uWSGI "
/sbin/killproc -HUP $UWSGI_BIN
rc_status -v
;;
reload)
echo -n "Reload service uWSGI "
/sbin/killproc -HUP $UWSGI_BIN
rc_status -v
;;
status)
echo -n "Checking for service uWSGI "
/sbin/checkproc $UWSGI_BIN
rc_status -v
;;
probe)
echo -n "uWSGI does not support probe "
rc_failed 3
rc_status -v
;;
*)
echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
exit 1
;;
esac
rc_exit '> /etc/init.d/uwsgi
chmod +x /etc/init.d/uwsgi
/etc/init.d/uwsgi start
/etc/init.d/nginx restart
chkconfig --add uwsgi
chkconfig --add nginx
## you can reload uwsgi with
#/etc/init.d/uwsgi restart
## to reload web2py only (without restarting uwsgi)
# touch /etc/uwsgi/web2py.ini
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment