
1.编译Nginx
在网上买了一本《实战Nginx-取代Apache的高性能服务器》,写的比较浅,主要是些配置方面的东西,不过却正是目前我所需要的。由于需要支持https和rewrite,所以除了Nginx的源码之外,又下载了 openssl-0.9.8r.tar.gz 和 pcre-8.12.tar.gz,把他们和Nginx-1.0.4.tar.gz放到同一个目录。
为了方便编译,笔者写了一个脚本,代码如下:
#!/bin/bash #=============================================================================#脚本所在绝对目录abs_path(){ local path= local basename=$( basename $path ) local dirname=$( dirname $path ) cd $dirname if [ -h $basename ]; then path=$( readlink $basename ) abs_path $path else pwd fi} #=============================================================================#依赖的目录src_base_dir=$( abs_path location / { #这两种方法都可以,只不过spawn-cgi启动的方法不同 #fastcgi_pass 127.0.0.1:9002; fastcgi_pass unix:webpy.sock; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param query_STRING $query_string; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE Nginx/$Nginx_version; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_name $server_name; fastcgi_param SERVER_PROTOCol $server_protocol; fastcgi_param SCRIPT_filename $fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_script_name;} )src_openssl_dir=$src_base_dir'/openssl-0.9.8r'src_pcre_dir=$src_base_dir'/pcre-8.12'src_Nginx_dir=$src_base_dir'/Nginx-1.0.4' #=============================================================================#目标的目录dest_base_dir=$src_base_dir'/release'dest_Nginx_dir=$dest_base_dir'/Nginx' #=============================================================================#把所有的tar.gz解压find . -name "*.tar.gz" | xargs -IX tar zxvf X #=============================================================================#编译Nginxcd $src_Nginx_dirchmod u+x ./configure./configure --with-http_stub_status_module --with-http_ssl_module --with-openssl=$src_openssl_dir --with-pcre=$src_pcre_dir --prefix=$dest_Nginx_dirmake && make install2.配置Nginx
在server配置项下增加
WsgiPythonEggs /tmp<VirtualHost *> Servername fuload.qq.com WsgiScriptAlias / /home/dantezhu/htdocs/fuload/conf/setting.wsgi <Directory /> Options FollowSymlinks AllowOverrIDe Order allow,deny Allow from all </Directory> <Directory "/home/dantezhu/htdocs/fuload/mysite"> Order Deny,Allow Deny from all </Directory> Alias /admin_media "/usr/local/lib/python2.7/site-packages/django/contrib/admin/media" <Directory "/usr/local/lib/python2.7/site-packages/django/contrib/admin/media"> Order allow,deny Options Indexes Allow from all IndexOptions FancyIndexing </Directory> #AliasMatch /site_media/(.*\.(CSS|gif|png|jpg|jpeg)) /home/dantezhu/htdocs/fuload/media/ Alias /site_media /home/dantezhu/htdocs/fuload/media/ <Directory "/home/dantezhu/htdocs/fuload/media/"> Order allow,deny Options Indexes Allow from all IndexOptions FancyIndexing </Directory></VirtualHost>
这里的3个location配置分别解决了,与python进程通信、django后台管理端样式存放、网站样式存放的问题。对照着apache的配置来看,就很容易明白了
#python manage.py runfcgi daemonize=true pIDfile=`pwd`/django.pID host=127.0.0.1 port=9001 maxrequests=1 &python manage.py runfcgi daemonize=true pIDfile=`pwd`/django.pID socket=/home/dantezhu/Nginx/sbin/django.sock maxrequests=1 &
3.安装fastcgi依赖
需要到 http://trac.saddi.com/flup下载安装,之后fastcgi才能够正常启动。
4.启动django
创建django project的过程我们就不说了,只列出启动/停止的命令:
启动:
kill -9 `cat django.pID`
停止:
./Nginx -p /home/dantezhu/Nginx/
启动Nginx
启动:
kill -QUIT `cat ../logs/Nginx.pID`
停止:
./Nginx -t -c `pwd`/../conf/Nginx.confkill -HUP `cat ../logs/Nginx.pID`
重新载入配置:
location / { #这两种方法都可以,只不过spawn-cgi启动的方法不同 #fastcgi_pass 127.0.0.1:9002; fastcgi_pass unix:webpy.sock; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param query_STRING $query_string; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE Nginx/$Nginx_version; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_name $server_name; fastcgi_param SERVER_PROTOCol $server_protocol; fastcgi_param SCRIPT_filename $fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_script_name;}成功显示了django的后台界面:
PPPPPPPPPPPPPPPPPPPPP1
5.部署web.py版
安装依赖
spawn-cgi
flup
配置Nginx
在server配置项下增加
#!/usr/bin/python# -*- Coding: utf-8 -*- import web urls = ("/.*","hello")app = web.application(urls,globals()) class hello: def GET(self): return 'Hello,world!' if __name__ == "__main__": web.wsgi.runwsgi = lambda func,addr=None: web.wsgi.runfcgi(func,addr) app.run()一个简单的index.py
chmod +x index.py
并执行:
#spawn-fcgi -P `pwd`/webpy.pID -f /home/dantezhu/htdocs/ngx_web/index.py -a 127.0.0.1 -p 9002 &spawn-fcgi -P `pwd`/webpy.pID -f /home/dantezhu/htdocs/ngx_web/index.py -s /home/dantezhu/Nginx/sbin/webpy.sock &
.启动web.py
启动:
kill -9 `cat webpy.pID`
停止:
/home/dantezhu/Nginx/sbin/start.shsudo -u dantezhu /home/dantezhu/htdocs/ngx_django/mysite/start.shsudo -u dantezhu /home/dantezhu/htdocs/ngx_web/start.sh
启动Nginx
加入到rc.local中,自动启动
以上是内存溢出为你收集整理的Linux系统上Nginx+Python的web.py与Django框架环境全部内容,希望文章能够帮你解决Linux系统上Nginx+Python的web.py与Django框架环境所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)