CentOS下Nginx+fastcgi+python3搭建web.py服务环境

来源:互联网 发布:linux 执行lua脚本 编辑:程序博客网 时间:2024/04/29 11:32

我的系统环境是CentOS release 6.6(Final)

环境依赖软件包

1、python3
2、Nginx1.4(需要包含fastcgi和rewrite模块)
3、web.py-python3(https://github.com/webpy/webpy/tree/python3)
4、Spawn-fcgi 1.6.3
5、Flup6


软件包安装

python3.6安装

[root@localhost home]# wget   https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
[root@localhost home]# tar  xJf  Python-3.6.1.tar.xz
[root@localhost Python-3.6.1]# cd  Python-3.6.1.tar.xz
[root@localhost Python-3.6.1]# ./configure  --prefix=/usr/local/python3 && make && make install
#创建软连接
[root@localhost Python-3.6.1]#  ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[root@localhost Python-3.6.1]#  ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

nginx安装
可以参考CentOS6.6环境中安装Nginx详细过程笔记博客文章
安装spawn-fcgi
[root@localhost home]# wget http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz
[root@localhost home]# tar zxvf spawn-fcgi-1.6.3.tar.gz
[root@localhost spawn-fcgi-1.6.3]# ./configure && make && make install                              
安装flup6
[root@localhost ~]# pip3 install flup6                              
安装web.py
[root@localhost home]# wget https://github.com/webpy/webpy/archive/python3.zip
[root@localhost home]# unzip   webpy-python3.zip
[root@localhost home]# cd  webpy-python3

[root@localhost webpy-python3]# python3  setup.py  install


配置

Nginx配置文件如下:

#user  nobody;worker_processes  1;#error_log  logs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '    #                  '$status $body_bytes_sent "$http_referer" '    #                  '"$http_user_agent" "$http_x_forwarded_for"';    #access_log  logs/access.log  main;    sendfile        on;    #tcp_nopush     on;    #keepalive_timeout  0;    keepalive_timeout  65;    #gzip  on;    server {        listen      80;        server_name  pw.com;        root /home/www/test;        location / {            include fastcgi_params;            fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;            fastcgi_param PATH_INFO $fastcgi_script_name;               fastcgi_pass 127.0.0.1:9002;        }        error_page  500 502 503 504  /50x.html;        location = /50x.html {            root  html;        }        location /static/ {            if (-f $request_filename) {                rewrite ^/static/(.*)$  /static/$1 break;            }        }    }}

在/home/www/demo目录下创建index.py
注意: 需要给代码设置执行权限,chmod +x index.py。
#!/usr/bin/env python3# -*- coding: utf-8 -*-import weburls = ("/.*", "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()

软件启动
Nginx启动
[root@localhost ~]# /usr/local/nginx/nginx
[root@localhost ~]# /usr/local/nginx/nginx  -s reload    #重启nginx配置
[root@localhost ~]# /usr/local/nginx/nginx  -s stop       #停止nginx
Spawn-fcgi启动
注意:你可以随意填写地址和端口信息,但是一定需要和Nginx配置文件相匹配
[root@localhost ~]# spawn-fcgi  -d  /home/www/demo  -f  /home/www/demo/index.py  -a  127.0.0.1 -p 9002
[root@localhost ~]# kill  `pgrep -f "python3 /home/www/demo/index.py"`     #关闭进程


测试
[root@localhost ~]#  curl  localhost
如果打印“hello world!”则表示环境搭建成功




阅读全文
0 0