This article describes how to build a web server using Python + Nginx + web2py + uWSGI in Linux. Python 2.7.11
Decompress the installation package
tar -zxvf Python-2.7.11.tgzcd Python-2.7.11yum install sqlite-devel./configure --enable-loadable-sqlite-extensions
An error is prompted.
Python build finished, but the necessary bits to build these modules were not found:_ssl _tkinter bsddb185bz2 dl imageopsunaudiodevTo find the necessary bits, look in setup.py in detect_modules() for the module's name.
Install the corresponding dependency package
yum install openssl-devel
Follow the instructions in the dependency package. For details, refer to this document.
Continue Installation
makemake installrm /usr/bin/pythonln -s /usr/local/bin/python2.7 /usr/bin/python
python Python 2.7.11 (default, Feb 2 2016, 14:33:40) [GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2 Type "help", "copyright", "credits" or "license" for more information.
Install Nginx
tar -zxvf nginx-1.8.0.tar.gz tar -xzvf zlib-1.2.8.tar.gztar -zxvf pcre-8.37.tar.gzgroupadd nginxuseradd nginx./configure \--prefix=/usr/local/nginx \--user=nginx \--group=nginx \--with-pcre=/opt/web/pcre-8.37 \--with-zlib=/opt/web/zlib-1.2.8 \--with-http_addition_module \--with-http_realip_modulemakemake installcd /usr/local/nginx./nginx
Install uWSGI
tar -zxvf uwsgi-latest.tar.gzcd uwsgi-2.0.12python uwsgiconfig.py --buildcp uwsgi /usr/sbin
Configure uWSGI
Create a configuration file/etc/uwsgi/web2py.ini
And enter the following content in the configuration file.
[uwsgi]socket = 127.0.0.1:9090pythonpath = /var/www/html/web2py/mount = /=wsgihandler:applicationprocesses = 4master = trueharakiri = 60reload-mercy = 8cpu-affinity = 1stats = /tmp/%n.stats.socketmax-requests = 5000limit-as = 1024reload-on-as = 256reload-on-rss = 192cron = 0 0 -1 -1 -1 python /var/www/html/web2py/web2py.py -Q -S welcome -M -R scripts/sessions2trash.py -A -ono-orphans = truechmod-socket = 666
Create uWSGI SwitchCommand.
'#!/bin/sh'# Autor: Nilton OS -- www.linuxpro.com.br'#'#'### 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'### END INIT INFO
'# Source function library. . /etc/rc.d/init.d/functions
'# 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="--enable-threads --logto /var/log/uwsgi/uwsgi.log" lockfile=/var/lock/subsys/uwsgi
UWSGI_OPTIONS="$UWSGI_OPTIONS --autoload"
if [ "$UWSGI_EMPEROR_MODE" = "true" ] ; then UWSGI_OPTIONS="$UWSGI_OPTIONS --emperor $UWSGI_VASSALS" fi
case "$1" in start) echo -n "Starting uWSGI " daemon $UWSGI_BIN $UWSGI_OPTIONS & ;; stop) echo -n "Shutting down uWSGI " killproc $UWSGI_BIN ;; restart) $0 stop $0 start ;; status) echo -n "Checking for service uWSGI " status $UWSGI_BIN ;; *) echo "Usage: $0 {start|stop|status|restart}" exit 1 ;; esac exit 0
According to the preceding switch command, you also need to add a uWSGI log file.
mkdir -p /var/log/uwsgitouch /var/log/uwsgi/uwsgi.log
Install web2py
For the so-called installation, you only need to extract the web2py package to the specified directory and download the binary package from the official website.
mkdir /var/www/htmlunzip web2py_src.zipmv web2py/handlers/wsgihandler.py web2py/wsgihandler.pychown -R nginx:nginx web2pycd web2pysudo -u nginx python -c "from gluon.main import save_password; save_password('password',443)"
Configure NginX
Add a server module, listen to port 80, and transfer access using uWSGI to web2py.
server { listen 80; server_name YOUR_SERVER_FQDN;
'#to enable correct use of response.static_version location ~* /(\w+)/static(?:/_[\d]+\.[\d]+\.[\d]+)?/(.*)$ { alias /var/www/html/web2py/applications/$1/static/$2; expires max; } location / { uwsgi_pass 127.0.0.1:9090; uwsgi_pass unix:///var/www/html/web2py/logs/web2py.socket; include /etc/nginx/uwsgi_params; }
}
Start Nginx and uWSGI
Note: web2py itself does not need to be started. It can only be passively called by uWSGI.
/usr/local/nginx/sbin/nginx/etc/init.d/uwsgi start
After the preceding steps are completed, access the IP address of the server in the browser. If the following page is required, the deployment is successful.