在Linux上使用web2py+uWSGI+Nginx搭建Web服务器

本文介绍在Linux使用Python+Nginx+web2py+uWSGI搭建一个web服务器的过程。

Python 2.7.11

解压安装包

tar -zxvf Python-2.7.11.tgz

cd Python-2.7.11

yum install sqlite-devel

./configure --enable-loadable-sqlite-extensions

 

会提示错误

Python build finished, but the necessary bits to build these modules were not found:

_ssl _tkinter bsddb185

bz2 dl imageop

sunaudiodev
To find the necessary bits, look in setup.py in detect_modules() for the module's name.

 

需要安装相应的依赖包<br><br><br>yum <span class="hljs-keyword">install</span> openssl-devel

继续安装

make

make install

rm /usr/bin/python
ln -s /usr/local/bin/python2.7 /usr/bin/python

 

python<br>Python <span class="hljs-number">2.7</span><span class="hljs-number">.11</span> (<span class="hljs-keyword">default</span>, Feb <span class="hljs-number">2</span> <span class="hljs-number">2016</span>, <span class="hljs-number">14</span>:<span class="hljs-number">33</span>:<span class="hljs-number">40</span>)<br>[GCC <span class="hljs-number">4.4</span><span class="hljs-number">.7</span> <span class="hljs-number">20120313</span> (Red Hat <span class="hljs-number">4.4</span><span class="hljs-number">.7</span><span class="hljs-number">-16</span>)] <span class="hljs-keyword">on</span> linux2<br>Type <span class="hljs-string">"help"</span>, <span class="hljs-string">"copyright"</span>, <span class="hljs-string">"credits"</span> <span class="hljs-keyword">or</span> <span class="hljs-string">"license"</span> <span class="hljs-keyword">for</span> more information.<br>

 

安装Nginx

tar -zxvf nginx-1.8.0.tar.gz 
tar -xzvf zlib-1.2.8.tar.gz
tar -zxvf pcre-8.37.tar.gz
groupadd nginx
useradd 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_module
make
make install

cd /usr/local/nginx
./nginx

安装uWSGI

tar -zxvf uwsgi-latest.tar.gz
cd uwsgi-2.0.12
python uwsgiconfig.py --build
cp uwsgi /usr/sbin

配置uWSGI

创建配置文件/etc/uwsgi/web2py.ini,并在配置文件中输入以下内容。

[uwsgi]
socket = 127.0.0.1:9090
pythonpath = /var/www/html/web2py/
mount = /=wsgihandler:application
processes = 4
master = true
harakiri = 60
reload-mercy = 8
cpu-affinity = 1
stats = /tmp/%n.stats.socket
max-requests = 5000
limit-as = 1024
reload-on-as = 256
reload-on-rss = 192
cron = 0 0 -1 -1 -1 python /var/www/html/web2py/web2py.py -Q -S welcome -M -R scripts/sessions2trash.py -A -o
no-orphans = true
chmod-socket = 666

 

创建uWSGI开关命令。

'#!/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

<span class="hljs-keyword">case</span> <span class="hljs-string">"<span class="hljs-variable">$1</span>"</span> <span class="hljs-keyword">in</span><br>start)<br><span class="hljs-built_in">echo</span> -n <span class="hljs-string">"Starting uWSGI "</span><br>daemon <span class="hljs-variable">$UWSGI_BIN</span> <span class="hljs-variable">$UWSGI_OPTIONS</span> &<br>;;<br>stop)<br><span class="hljs-built_in">echo</span> -n <span class="hljs-string">"Shutting down uWSGI "</span><br>killproc <span class="hljs-variable">$UWSGI_BIN</span><br>;;<br>restart)<br><span class="hljs-variable">$0</span> stop<br><span class="hljs-variable">$0</span> start<br>;;<br>status)<br><span class="hljs-built_in">echo</span> -n <span class="hljs-string">"Checking for service uWSGI "</span><br>status <span class="hljs-variable">$UWSGI_BIN</span><br>;;<br>*)<br><span class="hljs-built_in">echo</span> <span class="hljs-string">"Usage: <span class="hljs-variable">$0</span> {start|stop|status|restart}"</span><br><span class="hljs-built_in">exit</span> 1<br>;;<br><span class="hljs-keyword">esac</span><br><span class="hljs-built_in">exit</span> 0<br>

 

根据上面的开关命令,还需要增加一个uWSGI的日志文件。

mkdir -p /var/log/uwsgi

touch /var/log/uwsgi/uwsgi.log

 

web2py安装

所谓的安装只需要将web2py的包解压到指定目录就可以,从官网可以下载二进制包。

mkdir /var/www/html
unzip web2py_src.zip
mv web2py/handlers/wsgihandler.py web2py/wsgihandler.py
chown -R nginx:nginx web2py
cd web2py
sudo -u nginx python -c "from gluon.main import save_password; save_password('password',443)"

配置NginX

增加一个server模块,监听80端口,将访问使用uWSGI转移到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;
    }

}<br>

 

启动Nginx和uWSGI

注意:web2py本身不需要启动,只用被uWSGI被动调用即可。

/usr/local/nginx/sbin/nginx

/etc/init.d/uwsgi start

以上执行完后,在浏览器访问服务器的IP地址,若需要以下页面则说明部署成功。

在Linux上使用web2py+uWSGI+Nginx搭建Web服务器

相关推荐