国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Linux系統上Nginx+Python的web.py與Django框架環境

2020-01-04 17:53:22
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了Linux系統上Nginx+Python的web.py與Django框架環境,文中使用fastcgi作為連接,需要的朋友可以參考下
 

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=$1 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 $0 )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 install

2.配置nginx
在server配置項下增加

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;}

這里的3個location配置分別解決了,與python進程通信、django后臺管理端樣式存放、網站樣式存放的問題。對照著apache的配置來看,就很容易明白了

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/$1  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.安裝fastcgi依賴
需要到 http://trac.saddi.com/flup下載安裝,之后fastcgi才能夠正常啟動。

4.啟動django
創建django project的過程我們就不說了,只列出啟動/停止的命令:
啟動:

#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 &

停止:

kill -9 `cat django.pid`

啟動nginx
啟動:

./nginx -p /home/dantezhu/nginx/

停止:

kill -QUIT `cat ../logs/nginx.pid`

重新載入配置:

./nginx -t -c `pwd`/../conf/nginx.confkill -HUP `cat ../logs/nginx.pid`

成功顯示了django的后臺界面:
PPPPPPPPPPPPPPPPPPPPP1

5.部署web.py版
安裝依賴
spawn-cgi
flup
配置nginx
在server配置項下增加

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;}

一個簡單的index.py

#!/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()

并執行:

chmod +x index.py

.啟動web.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 &

停止:

kill -9 `cat webpy.pid`

啟動nginx
加入到rc.local中,自動啟動

/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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长宁区| 建湖县| 新泰市| 益阳市| 松原市| 九龙坡区| 惠州市| 讷河市| 新平| 津市市| 浮山县| 来安县| 班戈县| 科尔| 洪洞县| 页游| 鄂伦春自治旗| 万全县| 成武县| 石棉县| 仁怀市| 庆安县| 池州市| 玛纳斯县| 陇川县| 长葛市| 塔河县| 韶关市| 达拉特旗| 永兴县| 安化县| 高台县| 大悟县| 陵水| 如东县| 乌海市| 顺义区| 石棉县| 乌恰县| 紫阳县| 金寨县|