好久沒有寫些東西了,年末了,都很忙
最近的事情也挺多,寫一下開始搞python時作web用的一個配置
最初使用python就是用的python2.7的 而服務器用的centos系統,上面python仍是2.4的版本
多個版本的共存這時就很少說了,網上也有不少寫這個的
下載python2.7的源碼,編譯安裝好後
準備安裝virtualenv html
pip install virtualenv
進入/data/www目錄,建立一個python2.7的環境 前端
virtualenv app -p /usr/bin/python27 cd app
source bin/activate
你會發如今命令行前會多出(app)字樣,這就是建立成功了,在當前環境下運行一下python,你將看到相似於以下的版本信息 python
Python 2.7.3 (default, Jun 7 2012, 21:39:50) [GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2 Type "help", "copyright", "credits" or "license" for more information.
在此運行 linux
pip install supervisor pip install tornado
假設你的tornado源碼在/data/www/app/app目錄下,那麼在/data/www/app/etc目錄下創建supervisord.conf
內容以下 nginx
[unix_http_server] file=/tmp/supervisor.sock ; path to your socket file [supervisord] logfile=/tmp/supervisord.log ; supervisord log file logfile_maxbytes=50MB ; maximum size of logfile before rotation logfile_backups=10 ; number of backed up logfiles loglevel=warn ; info, debug, warn, trace pidfile=/tmp/supervisord.pid ; pidfile location nodaemon=false ; run supervisord as a daemon minfds=1024 ; number of startup file descriptors minprocs=200 ; number of process descriptors user=www ; default user childlogdir=/data/log [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [supervisorctl] serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket [program:tor8000] command=python /data/www/app/app/app.py autostart=true stderr_logfile = /data/log/tornado-8000-stderr.log stdout_logfile = /data/log/tornado-8000-stdout.log [program:tor8001] command=python /data/www/app/app/app.py --port=8001 autostart=true stderr_logfile = /data/log/tornado-8001-stderr.log stdout_logfile = /data/log/tornado-8001-stdout.log
這裏使用supervisor維護兩個進程,分別運行在8000和8001端口上
在/data/www/app目錄下建立supervisord.sh,內容以下 web
#!/bin/bash PID="/tmp/supervisord.pid" CONF="etc/supervisord.conf" stop() { if [ -f $PID ]; then kill `cat -- $PID` rm -f -- $PID echo "stopped" fi } start() { echo "starting" if [ ! -f $PID ]; then supervisord -c $CONF echo "started" fi } case "$1" in start) start ;; stop) stop ;; restart) stop start ;; *) echo "Usage: $0 {start|stop|restart}" esac
給supervisord.sh添加執行權限chown +x supervisord.sh
運行tornado的進程 shell
./supervisord.sh start
配置成功,固然nginx來作下前端代理的配置就很簡單了
nginx.conf內容大體以下: centos
upstream http { server 127.0.0.1:8000; server 127.0.0.1:8001; } server { listen 80; server_name app.com; client_max_body_size 4M; error_page 404 = /404.html; location /404.html { root /data/www/app/app/html; internal; } location ^~ /static/ { root /data/www/app/app; if ($query_string) { expires max; } } location = /favicon.ico { rewrite (.*) /static/favicon.ico; } location = /robots.txt { rewrite (.*) /static/robots.txt; } location ~ .*\.(html|txt|xml) { root /data/www/app/app/html; } location / { proxy_pass_header Server; proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_pass http://http; } }
大功告成,相信不少新手都碰到過這些配置問題,但願個人總結能給你們帶來些方便 bash