pip3安装uwsgi|python3安装|nginx|uwsgi|flask

pip3安装uwsgi|python3安装

环境/软件版本:centos 7.3,yum安装nginx,yum安装mysql,yum安装python3.4

安装pip3

yum install python34-pip

pip3 安装依赖

pip3 install -r requirements

安装python34devel

yum install python34-devel

安装uwsgi

pip3 install uwsgi

编写test.py测试uwsgi,注意python3和python2的测试方法是不一样的。

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"] # python3
    #return ["Hello World"] # python2

启动uwsgi 并访问

测试uwsgi启动项目,项目启动run.py文件代码,

#!usr/bin/python
# -*- coding: utf-8 -*-
from app import app
if __name__ == "__main__":
  app.run()

测试http启动命令,访问端口5000,能够正常访问网站,到此,python3,pip3,flask项目代码,uwsgi,全部测试成功

uwsgi --socket 0.0.0.0:5000 --protocol=http -w run:app

编写uwsgi.ini,以wsgi方式启动uwsgi,此时无法通过web访问的方式测试是否启动,

查看控制台提示即可

uwsgi.ini-uwsgi启动的配置文件

#uwsgi配置文件
[uwsgi]

socket = 127.0.0.1:5000
#socket = /tmp/flask.sock
chmod-socket = 660
vacuum = true
chdir = /home/dc
module = run:app 
wsgi-file = run.py
callable = app

uwsgi启动的linux shell命令,项目在/home/dc下

uwsgi --ini /home/dc/uwsgi.ini

如控制台出现以下提示,八成是成功了

WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x12b9fa0 pid: 2402 (default app)
mountpoint  already configured. skip.
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 2402, cores: 1)

接下来将uwsgi启动shell命令写成系统服务,以便开机启动

在 /usr/lib/systemd/system/   下新建uwsgi.service

我的内容如下,自测成功。

注意,使用绝对路径调用uwsgi

[Unit]
Description=uwsgi project

[Service]
Type=simple
PIDFile=/home/uwsgi.pid                                                      
ExecStart=/usr/bin/uwsgi --ini /home/dc/uwsgi.ini                                 
ExecReload=/usr/bin/uwsgi --ini /home/dc/uwsgi.ini                                 
ExecStop=                                                                          
PrivateTmp=true                                                                
[Install]                                                                          
WantedBy=multi-user.target

启动uwsgi服务,设置开机启动

systemctl start uwsgi
systemctl enable uwsgi

此时,除nginx以外的所有配置全部完成

nginx的配置

在/etc/nginx/conf.d下新建一个uwsgi.conf 

# mysite_nginx.conf

# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name dc.blfly.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    # location /media  {
    # alias /path/to/your/mysite/media;  # end as required
    #}

    location /static {
        alias /home/dc/app/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  localhost:5000;#此处与uwsgi.ini的socket是对应的
        #include     /path/to/your/mysite/uwsgi_params; # the ued
        
    include uwsgi_params;

    #uwsgi_param UWSGI_CHDIR  /home/dc;     #//项目根目录

        #uwsgi_param UWSGI_SCRIPT run:app; 
    }
}

重启nginx,访问80端口,测试网站成功访问

有什么问题,可以在下面留言问我

原文地址:https://www.cnblogs.com/difs/p/9688035.html