Linux系統(tǒng)上Nginx+Python的web.py與Django框架環(huán)境
1.編譯nginx
在網(wǎng)上買了一本《實(shí)戰(zhàn)nginx-取代Apache的高性能服務(wù)器》,寫的比較淺,主要是些配置方面的東西,不過(guò)卻正是目前我所需要的。由于需要支持https和rewrite,所以除了nginx的源碼之外,又下載了 openssl-0.9.8r.tar.gz 和 pcre-8.12.tar.gz,把他們和nginx-1.0.4.tar.gz放到同一個(gè)目錄。
為了方便編譯,筆者寫了一個(gè)腳本,代碼如下:
#!/bin/bash #============================================================================= #腳本所在絕對(duì)目錄 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' #============================================================================= #目標(biāo)的目錄 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 #============================================================================= #編譯nginx cd $src_nginx_dir chmod 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_dir make && make install
2.配置nginx
在server配置項(xiàng)下增加
location / { #這兩種方法都可以,只不過(guò)spawn-cgi啟動(dòng)的方法不同 #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個(gè)location配置分別解決了,與python進(jìn)程通信、django后臺(tái)管理端樣式存放、網(wǎng)站樣式存放的問(wèn)題。對(duì)照著apache的配置來(lái)看,就很容易明白了
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才能夠正常啟動(dòng)。
4.啟動(dòng)django
創(chuàng)建django project的過(guò)程我們就不說(shuō)了,只列出啟動(dòng)/停止的命令:
啟動(dòng):
#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`
啟動(dòng)nginx
啟動(dòng):
./nginx -p /home/dantezhu/nginx/
停止:
kill -QUIT `cat ../logs/nginx.pid`
重新載入配置:
./nginx -t -c `pwd`/../conf/nginx.conf kill -HUP `cat ../logs/nginx.pid`
成功顯示了django的后臺(tái)界面:
PPPPPPPPPPPPPPPPPPPPP1
5.部署web.py版
安裝依賴
spawn-cgi
flup
配置nginx
在server配置項(xiàng)下增加
location / { #這兩種方法都可以,只不過(guò)spawn-cgi啟動(dòng)的方法不同 #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; }
一個(gè)簡(jiǎn)單的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()
并執(zhí)行:
chmod +x index.py
.啟動(dòng)web.py
啟動(dòng):
#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`
啟動(dòng)nginx
加入到rc.local中,自動(dòng)啟動(dòng)
/home/dantezhu/nginx/sbin/start.sh sudo -u dantezhu /home/dantezhu/htdocs/ngx_django/mysite/start.sh sudo -u dantezhu /home/dantezhu/htdocs/ngx_web/start.sh
相關(guān)文章
Pytorch 使用opnecv讀入圖像由HWC轉(zhuǎn)為BCHW格式方式
這篇文章主要介紹了Pytorch 使用opnecv讀入圖像由HWC轉(zhuǎn)為BCHW格式方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06Python使用smtplib?實(shí)現(xiàn)單發(fā)和群發(fā)郵件驗(yàn)證碼
這篇文章主要介紹了Python使用smtplib?實(shí)現(xiàn)單發(fā)和群發(fā)郵件驗(yàn)證碼,文章通過(guò)使用?smtplib?模塊在?Python?中發(fā)送電子郵件,需要的小伙伴可以參考一下2022-05-05Python3.7 + Yolo3實(shí)現(xiàn)識(shí)別語(yǔ)音播報(bào)功能
這篇文章主要介紹了Python3.7 + Yolo3識(shí)別語(yǔ)音播報(bào)功能,開(kāi)始之前我們先得解析出來(lái)Yolo3的代碼,從而獲取到被識(shí)別出來(lái)的物體標(biāo)簽,具體詳細(xì)過(guò)程跟隨小編一起看看吧2021-12-12python調(diào)試模式無(wú)響應(yīng)解決案例
這篇文章主要介紹了python調(diào)試模式無(wú)響應(yīng)解決案例,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07Python Django使用forms來(lái)實(shí)現(xiàn)評(píng)論功能
這篇文章主要為大家詳細(xì)介紹了Python Django使用forms來(lái)實(shí)現(xiàn)評(píng)論功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08vscode調(diào)試django項(xiàng)目的方法
這篇文章主要介紹了vscode調(diào)試django項(xiàng)目的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08python 高效去重復(fù) 支持GB級(jí)別大文件的示例代碼
今天小編就為大家分享一篇python 高效去重復(fù) 支持GB級(jí)別大文件的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11