本文解釋python如何在nginx+spawn-fcg運(yùn)行
spawn-fcgi是一個(gè)小程序抄伍,作用是管理fast-cgi進(jìn)程,簡(jiǎn)單小巧,原先是屬于lighttpd的一部分晌砾,后來(lái)由于使用比較廣泛烦磁,所以就獨(dú)立項(xiàng)目了.
spawn-fcgi最新下載地址http://redmine.lighttpd.net/news/9
wget http://download.lighttpd.net/spawn-fcgi/releases-1.6.x/spawn-fcgi-1.6.4.tar.gz
tar -zxvf spawn-fcgi-1.6.4.tar.gz
cd spawn-fcgi-1.6.4
./configure #編譯
make
1.部署準(zhǔn)備
Nginx (我裝的是 version: nginx/1.13.5, lnmp安裝,方便) (需要包含 fastcgi 與rewrite 模塊).
查看nginx版本 ./nginx -v
Webpy (是Python的一個(gè)web框架都伪,它非常簡(jiǎn)單,非常強(qiáng)大)
Spawn-fcgi
Flup (它負(fù)責(zé)接受轉(zhuǎn)發(fā)的請(qǐng)求)
2.在網(wǎng)站根目錄下新建index.py 我的根目錄設(shè)置在/home/wwwroot/gpio
#!/usr/bin/env 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()
注意:通過(guò)執(zhí)行chmod +x index. py使您的文件可執(zhí)行陨晶。如果它不是可執(zhí)行的,就會(huì)出現(xiàn)錯(cuò)誤湿刽。
3. Nginx 配置
#我的conf文件位置: cd /usr/local/nginx/nginx.conf 或 cd /usr/local/nginx/vhost/配置文件
location / {
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;
fastcgi_pass 127.0.0.1:9002;
}
#配置靜態(tài)
location /static/ {
root /home/wwwroot/gpio;
if (-f $request_filename) {
rewrite ^/static/(.*)$ /static/$1 break;
}
}
4.Spawn-fcgi
開(kāi)啟 :spawn-fcgi -d /home/wwwroot/gpio -f /home/wwwroot/gpio/index.py -a 127.0.0.1 -p 9002
關(guān)閉: kill `pgrep -f "python /path/to/www/gpio/index.py"`
你可以運(yùn)行ps aux | grep index.py 查看運(yùn)行
注意撮躁,在配置nginx的fastcgi時(shí)可能會(huì)報(bào)錯(cuò)如下:
child exited with 2
解決方法: insert #!/usr/bin/env python into header of index.py
spawn-fcgi child exited with 126
解決方法: chmod +x upload.py
child exited with1
加-n參數(shù) 如spawn-fcgi -d //home/wwwroot/gpio -f /home/wwwroot/gpio/index.py -a 127.0.0.1 -p 9002 -n
5.重啟nginx
如果你使用的是lnmp那就太簡(jiǎn)單了一句即可 lnmp restart
不過(guò)不是lnmp安裝nginx, 進(jìn)入可執(zhí)行的目錄下cd /usr/local/nginx/sbin 輸入命令./nginx -s reload 即可