繁体   English   中英

无法在 Ubuntu 16.04 上使用 pip 安装 uwsgi

[英]Unable to install uwsgi with pip on Ubuntu 16.04

我从godaddy那里获得了VPS,并用Ubuntu 16.04构建了一台机器。 我想托管一个 Python (Django) 应用程序。 我成功安装了 Nginix。 不幸的是,我无法在这台机器上使用 pip 安装 Nginix。

当我运行时:

sudo pip install uwsgi

我收到以下错误。

Collecting uwsgi
Downloading uwsgi-2.0.15.tar.gz (795kB)
100% |################################| 798kB 1.1MB/s
Installing collected packages: uwsgi
Running setup.py install for uwsgi ... error
Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-vbfqylz9/uwsgi/setup.py';f=getattr(tokenize, 'open', open) (__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-5ob0jjzo-record/install-record.txt --single-version-externally-managed --compile:
/usr/lib/python3.5/distutils/dist.py:261: UserWarning: Unknown distribution option: 'descriptions'
    warnings.warn(msg)
  running install
  using profile: buildconf/default.ini
detected include path: ['/usr/lib/gcc/x86_64-linux-gnu/5/include', '/usr/local/include', '/usr/lib/gcc/x86_64-linux-gnu/5/include-fixed', '/usr/include/x86_64-linux-gnu', '/usr/include']
Patching "bin_name" to properly install_scripts dir
detected CPU cores: 24
configured CFLAGS: -O2 -I. -Wall -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -DUWSGI_HAS_IFADDRS -DUWSGI_ZLIB -DUWSGI_LOCK_USE_MUTEX -DUWSGI_EVENT_USE_EPOLL -DUWSGI_EVENT_TIMER_USE_TIMERFD -DUWSGI_EVENT_FILEMONITOR_USE_INOTIFY -DUWSGI_VERSION="\"2.0.15\"" -DUWSGI_VERSION_BASE="2" -DUWSGI_VERSION_MAJOR="0" -DUWSGI_VERSION_MINOR="15" -DUWSGI_VERSION_REVISION="0" -DUWSGI_VERSION_CUSTOM="\"\"" -DUWSGI_YAML -DUWSGI_XML -DUWSGI_XML_EXPAT -DUWSGI_PLUGIN_DIR="\".\"" -DUWSGI_DECLARE_EMBEDDED_PLUGINS="UDEP(python);UDEP(gevent);UDEP(ping);UDEP(cache);UDEP(nagios);UDEP(rrdtool);UDEP(carbon);UDEP(rpc);UDEP(corerouter);UDEP(fastrouter);UDEP(http);UDEP(ugreen);UDEP(signal);UDEP(syslog);UDEP(rsyslog);UDEP(logsocket);UDEP(router_uwsgi);UDEP(router_redirect);UDEP(router_basicauth);UDEP(zergpool);UDEP(redislog);UDEP(mongodblog);UDEP(router_rewrite);UDEP(router_http);UDEP(logfile);UDEP(router_cache);UDEP(rawrouter);UDEP(router_static);UDEP(sslrouter);UDEP(spooler);UDEP(cheaper_busyness);UDEP(symcall);UDEP(transformation_tofile);UDEP(transformation_gzip);UDEP(transformation_chunked);UDEP(transformation_offload);UDEP(router_memcached);UDEP(router_redis);UDEP(router_hash);UDEP(router_expires);UDEP(router_metrics);UDEP(transformation_template);UDEP(stats_pusher_socket);" -DUWSGI_LOAD_EMBEDDED_PLUGINS="ULEP(python);ULEP(gevent);ULEP(ping);ULEP(cache);ULEP(nagios);ULEP(rrdtool);ULEP(carbon);ULEP(rpc);ULEP(corerouter);ULEP(fastrouter);ULEP(http);ULEP(ugreen);ULEP(signal);ULEP(syslog);ULEP(rsyslog);ULEP(logsocket);ULEP(router_uwsgi);ULEP(router_redirect);ULEP(router_basicauth);ULEP(zergpool);ULEP(redislog);ULEP(mongodblog);ULEP(router_rewrite);ULEP(router_http);ULEP(logfile);ULEP(router_cache);ULEP(rawrouter);ULEP(router_static);ULEP(sslrouter);ULEP(spooler);ULEP(cheaper_busyness);ULEP(symcall);ULEP(transformation_tofile);ULEP(transformation_gzip);ULEP(transformation_chunked);ULEP(transformation_offload);ULEP(router_memcached);ULEP(router_redis);ULEP(router_hash);ULEP(router_expires);ULEP(router_metrics);ULEP(transformation_template);ULEP(stats_pusher_socket);"
*** uWSGI compiling server core ***
[thread 0][x86_64-linux-gnu-gcc -pthread] core/utils.o
[thread 2][x86_64-linux-gnu-gcc -pthread] core/protocol.o
[thread 3][x86_64-linux-gnu-gcc -pthread] core/socket.o
[thread 4][x86_64-linux-gnu-gcc -pthread] core/logging.o
[thread 7][x86_64-linux-gnu-gcc -pthread] core/master.o
[thread 13][x86_64-linux-gnu-gcc -pthread] core/master_utils.o
[thread 12][x86_64-linux-gnu-gcc -pthread] core/emperor.o
[thread 5][x86_64-linux-gnu-gcc -pthread] core/notify.o
[thread 16][x86_64-linux-gnu-gcc -pthread] core/mule.o
[thread 14][x86_64-linux-gnu-gcc -pthread] core/subscription.o
[thread 10][x86_64-linux-gnu-gcc -pthread] core/stats.o
[thread 15][x86_64-linux-gnu-gcc -pthread] core/sendfile.o
[thread 1][x86_64-linux-gnu-gcc -pthread] core/async.o
[thread 21][x86_64-linux-gnu-gcc -pthread] core/master_checks.o

 ----------------------------------------
Command "/usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-vbfqylz9/uwsgi/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-5ob0jjzo-record/install-record.txt --single-version-externally-managed --compile" 

failed with error code 1 in /tmp/pip-build-vbfqylz9/uwsgi/

这是我的 gcc 版本

Using built-in specs.
COLLECT_GCC=gcc 
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.5' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.5)

我能够使用安装在 Amazon EC2 (Ubuntu 5.4.0-6ubuntu1~16.04.4) 上

`sudo pip install uwsgi`

我在 Ubuntu 16.04 上安装uwsgi时遇到了同样的问题。 康达解决了我的问题。

conda install -c conda-forge uwsgi

您可以尝试通过运行安装 C 编译器和 python 开发文件

apt-get install build-essential python3-dev

然后尝试安装uwsgi

相关文档链接

为公共访问部署 Flask 应用程序需要在应用程序前面安装一个真正的网络服务器。 反过来,这需要一种将网络服务器链接到 Flask 应用程序的机制。

在这篇文章中,我们将安装 uWSGI 包并使用它来将基本的 Flask 应用程序链接到 Nginx 服务器。 作为 uWSGI 安装的一部分,我们将设置一个 Python 虚拟环境来容纳 Flask 应用程序和 uWSGI 包。

为了跟随这篇文章,你应该有一个最新版本的 Python 和 Nginx(见侧栏)。

uWSGI 安装

注意:截至 2019 年 1 月 28 日,这些相同的说明适用于安装 uWSGI 2.0.17.1 版。

我们使用我的 Antsle 创建了一个 Ubuntu 16.04 安装(名为“helium”),我们在其中安装了最新版本的 Python 和 Nginx(请参阅侧边栏)。

我们将在 Python 虚拟环境中安装 uWSGI,因此首先我们设置一个新的虚拟环境并激活它。 注意:当然,您可以在您拥有权限的任何地方设置您的虚拟环境,但我通常的做法是将它们全部放在我的家庭帐户中的一个“venvs”目录下。

$ cd                       # /home/joe is the working directory
$ mkdir venvs              # directory for virtual environments
$ cd venvs                 # 
$ python3 -m venv sam      # make sam virtual environment
$ source sam/bin/activate  # activate the virtual environment

一旦虚拟环境被激活,系统提示就会以虚拟环境的名称为前缀 - (sam)。 检查 Python 版本。 接下来我们使用 which -a 来检查多个 python3 安装:Ubuntu 发行版、我们的 Python 3.7.0 安装和(sam)虚拟环境中的 python3。

(sam) joe@helium:~/venvs$ python --version
Python 3.7.0

(sam) joe@helium:~/venvs$ which -a python3
/home/joe/venvs/sam/bin/python3
/usr/local/bin/python3
/usr/bin/python3

虚拟环境到位后,我们可以安装 Flask 和 uWSGI。 注意:除非您有完整的 Python 安装,否则 uWSGI 安装将失败。 特别是,您需要安装 python3-dev 包和 libssl-dev 包。 (见这篇文章。)

Flask 和 uWSGI 都安装在虚拟环境中。 检查版本。

(sam) joe@helium:~/alex$ pip install flask
(sam) joe@helium:~/alex$ pip install uwsgi


(sam) joe@helium:~/venvs$ which flask
/home/joe/venvs/sam/bin/flask

(sam) joe@helium:~/venvs$ flask --version
Flask 1.0.2
Python 3.7.0 (default, Oct 19 2018, 14:09:51)
[GCC 5.4.0 20160609]

(sam) joe@helium:~/venvs$ which uwsgi
/home/joe/venvs/sam/bin/uwsgi

(sam) joe@helium:~/venvs$ uwsgi --version
2.0.17.1

安装完成:这是计划

现在我们需要的一切都已安装:Python、Nginx、Flask 和 uWSGI。 这就是我们要做的:

  1. 我们在不使用 Nginx 或 uWSGI 的情况下设置并运行了一个简单的 Flask 应用程序。 使用 curl 测试应用程序。

  2. 我们使用 uWSGI 配置文件将基本的 Flask 应用程序连接到 Nginx 服务器。

  3. 作为奖励,我们设置了第二个虚拟环境(“western”),其中包含所有装饰——uWSGI、Flask 应用程序、Nginx 服务等——并同时运行这两个应用程序。

基本的 Flask 应用程序

有许多不同的方式来构建 Flask 应用程序。 我们将使用以下目录结构。

/home/joe
    |
    |-- /alex       (project directory)
        |
        | -- /paa   (application directory)
        .    |
        .    | -- /static  (css, js, etc.)
        .    | -- /templates  (html files)
        .    |
        .    | -- __init__.py
        .    | -- routes.py
        .    | :
        .    | :
        |
        | -- config.py
        | -- run_me.py
        | :
        | :

对于我们将要构建的基本 Flask 应用程序来说,这种结构有点夸张,但它说明了一个简单应用程序的设置(意味着没有蓝图)。 此外,我们使用人为不同的名称创建了一些文件和目录,以便比大多数教程中的依赖关系更清晰一些。

文件:__init__.py(如下所示)

from flask import Flask

ned = Flask(__name__)
ned.config.from_object('config')

注意:在 ned.config.from_object() 调用中仅使用 config.py 文件的根名称(如下所示)。

文件:routes.py(如下所示)

from paa import ned

@ned.route('/')
def slash():
    title = "<title>Ned's Greeting</title>"
    greeting = '<span style="color: blue"><b>Howdy Doodly, Neighbor!'
    return f'{title}\n{greeting}\n'

注意:我们的默认做法是对字符串使用单引号,但“Ned's Greeting”中的撇号需要标题值使用双引号。

文件:run_me.py(如下所示)

from paa import ned
from paa import routes

if __name__ == '__main__':
    ned.run()

注意:Flask 默认为地址为 localhost:5000 的应用程序提供服务,除非在“ned.run()”语句中指定了其他主机和端口。 稍后您将看到,在本示例中使用 uWSGI 和 Nginx 时不需要这些参数。

文件:config.py(如下图)

SECRET_KEY = 'ivegotasecret'

为完整起见,我们在此处包含了 Flask SECRET_KEY 配置值。 在上面显示的示例中没有必要。 但是,在任何真正的 Flask 应用程序(使用 Nginx 和 uWSGI)中,您将在某个时候使用“会话”对象,除非您设置了 SECRET_KEY 配置值,否则您不能这样做。 有关 Flask“会话”对象的完整描述,请参阅此 StackOverflow 问题/答案。

在 Flask 开发环境中测试 Flask 应用程序

为了使用 curl 命令测试 Flask 应用程序,我们需要有两个终端会话 - 一个用于执行 Flask 应用程序,另一个用于执行 curl 命令。

要运行 Flask 应用程序,请在 .../alex 目录中设置:

(sam) joe@helium:~/alex$ export FLASK_APP=run_me.py
(sam) joe@helium:~/alex$ flask run
 * Serving Flask app "run_me.py"
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
.
.
.
[ctl-C to end the application after the curl test]

在另一个终端窗口中,输入 curl 命令:

joe@helium:~$ curl localhost:5000
<title>Ned's Greeting</title>
<span style="color: blue"><b>Howdy Doodly, Neighbor!

现在我们将这个最低限度操作的 Flask 应用程序搁置一边,同时我们将 Nginx 和 uWSGI 放在一起。

设置 uWSGI 参数以使用 Flask 应用程序

uwsgi.ini 文件为将 Python-Flask 应用程序链接到 Nginx 服务器所需的所有参数设置值。

module 参数标识要运行的 Python 文件 (run_me.py) 和 Flask 应用程序对象 (ned)。

主参数是生产环境的标准设置。

进程参数通常默认设置为 5。 要获得最佳设置,需要对负载下的应用程序进行试验。

socket 参数提供了 uWSGI 和 Nginx 之间的套接字连接的名称。 请注意,套接字值也在 Nginx 配置文件中标识。 这些必须匹配才能使 uWSGI 与 Nginx 正确链接 - 一种用于协调应用程序元素的通用机制。

chmod-socket 参数应该提供对套接字的“uWSGI 用户”访问。 文档中指定了值 664,但它对我们不起作用,因此我们在此处将其显示为 666,这对我们有用。

当 uWSGI 服务器终止时,vacuum 参数指示 uWSGI 删除 Unix 套接字。

uid 和 gid 参数标识运行 uWSGI 服务器的用户和组。

die-on-term 参数指示 uWSGI 在应用程序退出时“粗暴地重新加载所有工作进程和主进程”。

uwsgi.ini 参数文件如下所示:

[uwsgi]
module=run_me:ned

master = true
processes = 5

socket = baker.sock
chmod-socket = 666  
vacuum = true

uid = joe
gid = www-data

die-on-term = true

设置 Nginx 以使用 uWSGI 参数

下面显示的 Nginx 配置文件通过 include 和 uwsgi_pass 参数与 uWSGI 服务器链接。

/etc/nginx/conf.d/helium.conf

server {
    listen 8181;
    server_name localhost;

    location / {
        include   uwsgi_params;
        uwsgi_pass  unix:/home/joe/alex/baker.sock;
    }
}

/etc/nginx/conf.d/helium.conf 文件包含在从 Nginx.org 站点安装 Nginx 服务器附带的标准“启动器”配置文件的最后一行中。

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


    http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/helium.conf;
}

运行 Nginx 和 uWSGI 服务器

重启 Nginx 服务器:

$ sudo service nginx restart

为了为.../alex/paa 应用程序正确启动uWSGI,您必须激活sam 虚拟环境,然后运行uWSGI 服务器。

--ini uwsgi.ini 参数将 uWSGI 定向到我们的 .ini 文件。

--daemonize uwsgi.log 参数将此 uWSGI 服务器实例作为守护进程运行,并指示服务器将其日志输出写入 uwsgi.log。

--safe-pidfile /tmp/alex.pid 参数将这个 uWSGI 服务器进程的 pid 保存到文件“/tmp/alex.pid”中。 该 pid 编号用于“正常”重新加载 uWSGI 服务器或停止它。 (见这个。)

(sam) joe@helium:~/alex$ uwsgi --ini uwsgi.ini --daemonize uwsgi.log --safe-pidfile /tmp/alex.pid

这是从命令行显示的带有“curl localhost:8181”的问候语:

(sam) joe@helium:~$ curl localhost:8181
<title>Ned's Greeting</title>
<span style="color: blue"><b>Howdy Doodly, Neighbor!</b></span>

我们有了它:Flask 应用程序通过 uWSGI 连接到 Nginx。

双倍乐趣

但我们忍不住要检查一下。

将 uWSGI 安装到 Python 虚拟环境中的一个直接含义是,我们应该能够从不同的虚拟环境运行多个 Flask 应用程序——每个应用程序都通过自己的 uWSGI 连接到 Nginx。 事实证明,我们可以。

我们只是要勾画这个实现,因为它非常简单。 我们创建了第二个名为“western”的虚拟环境,并像以前一样安装了 Flask 和 uWSGI。 我们为 Flask 应用程序创建了一个名为“scifi”的项目目录和一个“noir”目录。 下面的列表显示了西方/科幻/黑色设置中的替换。 与上面的 sam/alex/paa 设置进行比较。

科幻/黑色/初始化.py

from flask import Flask

hitchcock = Flask(__name__)
hitchcock.config.from_object('config')

科幻/黑色/views.py

from noir import hitchcock

@hitchcock.route('/')
def slash():
    title = f"<title>Confusion Cinema</title>"
    greeting = f'<span style="color: blue"><b>Some like hot, psycho birds!</b></span>\n'
    return f'{title}\n{greeting}\n'

科幻/run_fi.py

from noir import hitchcock
from noir import views

if __name__ == '__main__':
    hitchcock.run()

科幻/配置.py

SECRET_KEY = 'ivegotasecret'

科幻/uwsgi.ini

[uwsgi]
module=run_fi:hitchcock

master = true
processes = 5

socket = marilyn.sock
chmod-socket = 666
vacuum = true

uid = joe
gid = www-data

die-on-term = true

我们通过单独的端口为它们提供服务来区分这两个单独的 Flask 应用程序。 uWSGI 服务器负责将 Flask 应用程序链接到 Nginx 服务器的正确端口。

server {
   listen 8181;
   server_name localhost;

    location / {
        include   uwsgi_params;
        uwsgi_pass  unix:/home/joe/alex/baker.sock;
    }
}

server {
    listen 8080;
    server_name localhost;

    location / {
        include   uwsgi_params;
        uwsgi_pass  unix:/home/joe/scifi/marilyn.sock;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM