不使用 sudo 启动 uWSGI 服务器

Starting uWSGI server without sudo

我正在尝试使用 uWSGI 部署我的 Flask 应用程序,但如果没有 sudo,我似乎无法做到这一点。

这是我的启动脚本:

#!/bin/bash
set -v
set -e
cd /var/hpit/hpit_services
/var/hpit/hpit_services/env/bin/uwsgi --http [::]:80 --master --module wsgi --callable app --processes 4 --daemonize ../log/uwsgi.log --pidfile ../server_pid_file.pid
echo server started

这是我在日志中得到的:

*** Starting uWSGI 2.0.9 (64bit) on [Mon Jan 26 15:53:26 2015] ***
compiled with version: 4.8.2 on 23 January 2015 20:35:44
os: Linux-3.18.1-x86_64-linode50 #1 SMP Tue Jan 6 12:14:10 EST 2015
nodename: <<blocked out>>
machine: x86_64
clock source: unix
detected number of CPU cores: 2
current working directory: /var/hpit/hpit_services
writing pidfile to ../server_pid_file.pid
detected binary path: /var/hpit/hpit_services/env/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
your processes number limit is 7962
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
bind(): Permission denied [core/socket.c line 764]

Core/socket.c 第 764 行有这个:

if (bind(serverfd, (struct sockaddr *) &uws_addr, addr_len) != 0) {
    if (errno == EADDRINUSE) {
        uwsgi_log("probably another instance of uWSGI is running on the same address (%s).\n", socket_name);
    }
    uwsgi_error("bind()");
    uwsgi_nuclear_blast();
    return -1;
}

但是我没有uWSGI实例运行。这似乎是一个权限问题。我对 /var/hpit、/var/hpit/hpit_services(应用程序的位置)和 /var/hpit/log 的权限是 bsauer:www-data。我的用户我们 bsauer.

如果我将 sudo -E 附加到启动脚本中调用 uWSGI 二进制文件的行,它似乎可以正常启动,但我读到服务器不应以 sudo 启动。我在工作中继承了这个系统管理员角色,对这一切有点陌生。

这是我的 hunches/musings:

感谢您的帮助,如有必要,我可以提供更多详细信息。

编辑

奇怪的是,我的 /var/hpit/log/uwsgi.log 文件属于 bsauer:bsauer,而不是 bsauer:www-data 或 www-data:www-data期待...

EDIT2

好的,从页面底部的http://projects.unbit.it/uwsgi/wiki/Example看,问题似乎是运行在端口80上。我将其更改为8080,但仍然是运行 作为 bsauer,我不认为我想要。

据我所知,这是我想出来的,如果有人想把它放在更清晰的系统管理员语言中,我会很乐意编辑。

毕竟解决方案与日志无关。问题是默认的 HTTP 端口 80 被系统保护,只有 root 可以绑定到该端口。没有 sudo,它不会让你绑定。绑定到另一个端口,如端口 8080,工作正常。

我想绑定到端口 80,但仍然 运行 服务器作为 www-data,所以我最终关注了此页面的最底部:http://projects.unbit.it/uwsgi/wiki/Example。基本上,端口 80 的套接字是共享的,uWSGI 可以先作为 sudo 访问它,然后下降到 www-data 到 运行 服务器。

在调用 uWSGI 二进制文件之前,我仍然不得不使用 sudo -E,因为它需要 root 权限来更改 uWSGI 的用户和组 ID,但没关系,因为最终结果是服务器 运行 s 在非常受限的 www-data 用户中。

最后,我的服务器起始行是: sudo -E /var/hpit/hpit_services/env/bin/uwsgi --shared-socket [::]:80 --http =0 --uid 33 --gid 33 --master --module wsgi --callable app --processes 4 --daemonize ../log/uwsgi.log --pidfile ../server_pid_file.pid