在Linux上使用web2py_uwsgi_nginx搭建web服務器
來自: http://www.cnblogs.com/shenfeng/p/nginx_web2py.html
本文介紹在Linux使用Python+Nginx+web2py+uWSGI搭建一個web服務器的過程。
</div>
Python 2.7.11
解壓安裝包
tar -zxvf Python-2.7.11.tgzcd Python-2.7.11
yum install sqlite-devel
./configure --enable-loadable-sqlite-extensions</pre>
會提示錯誤
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.</pre>
需要安裝相應的依賴包
yum install openssl-devel按照依賴包操作,具體可以參照這篇 文檔繼續安裝
makemake install
rm /usr/bin/python
ln -s /usr/local/bin/python2.7 /usr/bin/python</pre>
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.
</div>
安裝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 installcd /usr/local/nginx ./nginx</pre>
安裝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</pre>
創建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</pre>
'# 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
根據上面的開關命令,還需要增加一個uWSGI的日志文件。
mkdir -p /var/log/uwsgitouch /var/log/uwsgi/uwsgi.log</pre>
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;</pre>
'#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; }}
啟動Nginx和uWSGI
注意:web2py本身不需要啟動,只用被uWSGI被動調用即可。
/usr/local/nginx/sbin/nginx/etc/init.d/uwsgi start</pre>
以上執行完后,在瀏覽器訪問服務器的IP地址,若需要以下頁面則說明部署成功。
![]()
版權說明:camash原創,轉載請注明出處 http://www.cnblogs.com/shenfeng/
--EOF--
</div>本文由用戶 Willie21P 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!