V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
fyyz
V2EX  ›  Python

flask 的网站部署了一天, nginx 依然无法与 uwsgi 通讯,系统 Fedora 23。希望搭建过 Flask 环境的大牛看看。

  •  
  •   fyyz · 2016-01-30 22:40:12 +08:00 · 4768 次点击
    这是一个创建于 3223 天前的主题,其中的信息可能已经有所发展或是发生改变。

    nginx 的配置(开了 81 端口,方便调试):

    [root@localhost ~]# cat /etc/nginx/conf.d/dev.auth.lan.conf 
    server
    {
        listen          81;
        server_name     dev.auth.lan;
        root            /var/web/dev.auth.lan;
    
        location /
        {
            include     uwsgi_params;
            uwsgi_pass  127.0.0.1:8000;
        }
    }
    

    uwsgi 的配置:

    [root@localhost dev.auth.lan]# cat /etc/uwsgi.ini 
    [uwsgi]
    uid = uwsgi 
    gid = uwsgi
    pidfile = /run/uwsgi/uwsgi.pid
    emperor = /etc/uwsgi.d
    stats = /run/uwsgi/stats.sock
    emperor-tyrant = true
    cap = setgid,setuid
    
    [root@localhost ~]# cat /etc/uwsgi.d/dev.auth.lan.ini 
    [uwsgi]
    
    socket = :8000
    
    chdir = /var/web/dev.auth.lan
    wsgi-file = manager.py
    
    master = true
    process = 10
    vacuum = true
    
    daemonize = /var/log/dev.auth.lan.uwsgi.log
    

    nginx 的运行情况(实心圆圈是绿灯):

    [root@localhost ~]# systemctl status nginx -l
    ● nginx.service - The nginx HTTP and reverse proxy server
       Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
       Active: active (running) since 六 2016-01-30 06:03:28 PST; 25min ago
      Process: 936 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
      Process: 861 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
      Process: 848 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
     Main PID: 991 (nginx)
       CGroup: /system.slice/nginx.service
               ├─991 nginx: master process /usr/sbin/ngin
               └─993 nginx: worker proces
    
    1 月 30 06:03:28 localhost.localdomain systemd[1]: Starting The nginx HTTP and reverse proxy server...
    1 月 30 06:03:28 localhost.localdomain nginx[861]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    1 月 30 06:03:28 localhost.localdomain nginx[861]: nginx: configuration file /etc/nginx/nginx.conf test is successful
    1 月 30 06:03:28 localhost.localdomain systemd[1]: nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument
    1 月 30 06:03:28 localhost.localdomain systemd[1]: Started The nginx HTTP and reverse proxy server.
    

    uwsgi 的运行情况(也是绿灯):

    [root@localhost ~]# systemctl status uwsgi -l
    ● uwsgi.service - uWSGI Emperor Service
       Loaded: loaded (/usr/lib/systemd/system/uwsgi.service; enabled; vendor preset: disabled)
       Active: active (running) since 六 2016-01-30 06:32:29 PST; 2s ago
      Process: 2277 ExecStartPre=/bin/chown uwsgi:uwsgi /run/uwsgi (code=exited, status=0/SUCCESS)
      Process: 2274 ExecStartPre=/bin/mkdir -p /run/uwsgi (code=exited, status=0/SUCCESS)
     Main PID: 2281 (uwsgi)
       Status: "The Emperor is governing 1 vassals"
       CGroup: /system.slice/uwsgi.service
               ├─2281 /usr/sbin/uwsgi --ini /etc/uwsgi.ini
               ├─2283 /usr/sbin/uwsgi --ini /etc/uwsgi.ini
               ├─2284 /usr/sbin/uwsgi --ini dev.auth.lan.ini
               └─2285 /usr/sbin/uwsgi --ini dev.auth.lan.ini
    
    1 月 30 06:32:29 localhost.localdomain uwsgi[2281]: your server socket listen backlog is limited to 100 connections
    1 月 30 06:32:29 localhost.localdomain uwsgi[2281]: your mercy for graceful operations on workers is 60 seconds
    1 月 30 06:32:29 localhost.localdomain uwsgi[2281]: mapped 145536 bytes (142 KB) for 1 cores
    1 月 30 06:32:29 localhost.localdomain uwsgi[2281]: *** Operational MODE: single process ***
    1 月 30 06:32:29 localhost.localdomain uwsgi[2281]: *** no app loaded. going in full dynamic mode ***
    1 月 30 06:32:29 localhost.localdomain uwsgi[2281]: *** uWSGI is running in multiple interpreter mode ***
    1 月 30 06:32:29 localhost.localdomain uwsgi[2281]: spawned uWSGI master process (pid: 2284)
    1 月 30 06:32:29 localhost.localdomain uwsgi[2281]: Sat Jan 30 06:32:29 2016 - [emperor] vassal dev.auth.lan.ini has been spawned
    1 月 30 06:32:29 localhost.localdomain uwsgi[2281]: spawned uWSGI worker 1 (pid: 2285, cores: 1)
    1 月 30 06:32:29 localhost.localdomain uwsgi[2281]: Sat Jan 30 06:32:29 2016 - [emperor] vassal dev.auth.lan.ini is ready to accept requests
    

    端口也确实开了:

    [root@localhost ~]# netstat -lpn | grep 8000
    tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      2284/uwsgi
    

    项目目录下也确实有 manager.py

    [root@localhost dev.auth.lan]# pwd
    /var/web/dev.auth.lan
    
    [root@localhost dev.auth.lan]# ls -alh
    总用量 4.0K
    drwxr-xr-x. 3 root root  39 1 月  30 06:36 .
    drwxr-xr-x. 3 root root  26 1 月  30 02:32 ..
    -rwxr-xr-x. 1 root root 132 1 月  30 06:10 manager.py
    drwxr-xr-x. 5 root root  82 1 月  30 05:02 project
    
    [root@localhost dev.auth.lan]# cat manager.py
    #!/usr/bin/env python3
    
    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def index():
        return "<span style='color:red'>I am app 1</span>"
    [root@localhost dev.auth.lan]#
    

    但是用浏览器访问那台服务器的 81 端口,就是报 502 , nginx 的日志(太长,加了换行进去):

    2016/01/30 06:37:59 [crit] 993#0: *25 connect() to 127.0.0.1:8000 failed 
    (13: Permission denied) 
    while connecting to upstream, client: 192.168.124.1, 
    server: dev.auth.lan, request: "GET / HTTP/1.1", 
    upstream: "uwsgi://127.0.0.1:8000", host: "192.168.124.237:81"
    
    第 1 条附言  ·  2016-01-31 00:23:54 +08:00

    @ethego
    直接写 module = manager 依然不行,还是同样的报错,试了很多方法, uwsgi 的报错没了,但是依然是 502 。

    [root@localhost dev.auth.lan]# cat /etc/uwsgi.d/dev.auth.lan.ini 
    [uwsgi]
    
    socket = :8000
    
    chdir = /var/web/dev.auth.lan
    processes = 2
    wsgi-file = /var/web/dev.auth.lan/manager.py
    callable = app
    plugins = python3
    
    master = true
    vacuum = true
    
    第 2 条附言  ·  2016-01-31 00:24:25 +08:00
    [root@localhost dev.auth.lan]# systemctl status uwsgi
    ● uwsgi.service - uWSGI Emperor Service
       Loaded: loaded (/usr/lib/systemd/system/uwsgi.service; enabled; vendor preset: disabled)
       Active: active (running) since 六 2016-01-30 08:24:34 PST; 5s ago
      Process: 4797 ExecStartPre=/bin/chown uwsgi:uwsgi /run/uwsgi (code=exited, status=0/SUCCESS)
      Process: 4793 ExecStartPre=/bin/mkdir -p /run/uwsgi (code=exited, status=0/SUCCESS)
     Main PID: 4801 (uwsgi)
       Status: "The Emperor is governing 1 vassals"
       CGroup: /system.slice/uwsgi.service
               ├─4801 /usr/sbin/uwsgi --ini /etc/uwsgi.ini
               ├─4803 /usr/sbin/uwsgi --ini /etc/uwsgi.ini
               ├─4804 /usr/sbin/uwsgi --ini dev.auth.lan.ini
               ├─4806 /usr/sbin/uwsgi --ini dev.auth.lan.ini
               └─4807 /usr/sbin/uwsgi --ini dev.auth.lan.ini
    
    1 月 30 08:24:35 localhost.localdomain uwsgi[4801]: your mercy for graceful operations on workers is 60 seconds
    1 月 30 08:24:35 localhost.localdomain uwsgi[4801]: mapped 218304 bytes (213 KB) for 2 cores
    1 月 30 08:24:35 localhost.localdomain uwsgi[4801]: *** Operational MODE: preforking ***
    1 月 30 08:24:35 localhost.localdomain uwsgi[4801]: WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x9b93c0 pid: 4804 (default app)
    1 月 30 08:24:35 localhost.localdomain uwsgi[4801]: *** uWSGI is running in multiple interpreter mode ***
    1 月 30 08:24:35 localhost.localdomain uwsgi[4801]: spawned uWSGI master process (pid: 4804)
    1 月 30 08:24:35 localhost.localdomain uwsgi[4801]: Sat Jan 30 08:24:35 2016 - [emperor] vassal dev.auth.lan.ini has been spawned
    1 月 30 08:24:35 localhost.localdomain uwsgi[4801]: spawned uWSGI worker 1 (pid: 4806, cores: 1)
    1 月 30 08:24:35 localhost.localdomain uwsgi[4801]: spawned uWSGI worker 2 (pid: 4807, cores: 1)
    1 月 30 08:24:35 localhost.localdomain uwsgi[4801]: Sat Jan 30 08:24:35 2016 - [emperor] vassal dev.auth.lan.ini is ready to accept requests
    
    第 3 条附言  ·  2016-01-31 10:08:43 +08:00
    Fedora 上还有一个问题,就是默认是开启 SELINUX 的,按照上述步骤做好以后,再关闭 SELINUX 即可让网站运行起来了。

    非常感谢大家的解答。
    13 条回复    2016-02-01 09:10:03 +08:00
    ethego
        1
    ethego  
       2016-01-30 22:45:13 +08:00   ❤️ 1
    *** no app loaded. going in full dynamic mode ***
    看到这玩意没有?
    ethego
        2
    ethego  
       2016-01-30 22:45:44 +08:00
    wsgi 容器里没装东西
    fyyz
        3
    fyyz  
    OP
       2016-01-30 22:55:39 +08:00
    @ethego
    非常感谢你的回复。
    不过,我不是很清楚,你说的这个 wsgi 容器是什么玩意?依照我上面的配置,我应该如何更改呢?
    ethego
        4
    ethego  
       2016-01-30 22:59:28 +08:00
    wsgi-file = manager.py 换成 module = manager 或者 module = manager:app
    fyyz
        5
    fyyz  
    OP
       2016-01-31 00:25:41 +08:00
    直接回复似乎不支持 markdown ,我写 APPEND 里了。
    @ethego
    billlee
        6
    billlee  
       2016-01-31 00:33:33 +08:00   ❤️ 1
    看到有 connect() to 127.0.0.1:8000 failed (13: Permission denied)
    也许是 SELinux 的问题?
    squid157
        7
    squid157  
       2016-01-31 08:43:14 +08:00 via iPhone   ❤️ 1
    @billlee
    我也这么猜。我因为不会配置,都是直接关闭 SELinux
    fyyz
        8
    fyyz  
    OP
       2016-01-31 10:06:55 +08:00
    非常感谢 @billlee @squid157 这两位,当然还有楼上的 @ethego 。目前网站已经成功跑起来了。
    Ansen
        9
    Ansen  
       2016-01-31 10:57:30 +08:00 via iPhone
    uwsgi 你用 http ,不要用 sockt
    sockt 不是这样配置的( nginx )
    jings
        10
    jings  
       2016-01-31 13:21:46 +08:00
    mark
    aabbccli
        11
    aabbccli  
       2016-01-31 15:30:40 +08:00
    用 SOCK 文件的话, NGINX 应该是这样

    upstream first {
    server unix:///var/www/site/site.sock;
    }

    server {
    listen 80;
    server_name localhost;


    location / {

    include /etc/nginx/uwsgi_params;
    uwsgi_pass first;

    }

    }
    fyyz
        12
    fyyz  
    OP
       2016-02-01 09:07:34 +08:00
    @Ansen 为什么不要用 socket ?
    Ansen
        13
    Ansen  
       2016-02-01 09:10:03 +08:00 via iPhone
    @fyyz 你 nginx 配的是 http , uwsgi 用的是 sockt
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3585 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 04:36 · PVG 12:36 · LAX 20:36 · JFK 23:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.