项目部署uwsgi +Nginx

Nginx代理

在项目中配置Nginx

在项目目录下添加一个config文件夹,后面的配置文件都统一放到该文件夹下(配置文件存放路径不统一会导致输入提示符时运行不成功)

config文件下新建mysite_nginx.conf文件,内容如下(将路径替换成你自己的项目路径或者对应文件):

# mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    # unix:///home/breavo/PyWorkSpace/mysite_code_shuffle/config/eshop.sock
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name xx.xx.xxx.xxx; # 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  {
        # /home/breavo/PyWorkSpace/mysite_code_shuffle/meida;
        alias /path/to/your/mysite/media; # your Django project's media files - amend as required
    }

    location /static {
        # /home/breavo/PyWorkSpace/mysite_code_shuffle/static
        alias /path/to/your/mysite/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/breavo/PyWorkSpace/mysite_code_shuffle/config/uwsgi_params; # the uwsgi_params file you installed
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
在/etc/nginx/sites-enabled中创建一个链接,这样Nginx能够发现并启用我们的配置,运行

sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
1
部署静态文件

在运行Nginx之前,需要将django项目中的所有静态文件放入static文件夹,在mysite/settings.py中添加

STATIC_ROOT = os.path.join(BASE_DIR, "static/")
1
然后运行

python manage.py collectstatic
1
启动(重启)Nginx

sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx restart
1
2
简单测试

往服务器项目的media文件夹里丢一个图片(这里以test.png为例)

访问

xx.xx.xxx.xxx:8000/media/test.png
1


图片正确显示~

至此,静态文件部署算是完成了

Uwsgi安装及配置

接下来我们需要使用Uwsgi来处理项目中的动态请求

基本配置

虚拟环境下运行

pip install uwsgi

在之前创建的config文件夹下新建一个hello.py用来做测试,写入以下内容

# test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"] # python3
    #return ["Hello World"] # python2
1
2
3
4
5
接下来运行

uwsgi --http :8001 --wsgi-file test.py

打开浏览器查看

出现上图,说明Uwsgi已经初步运行成功

运行项目

上述步骤没有出问题的话,我们就可以使用Uwsgi启动项目了,项目根目录下运行

uwsgi --http :8001 --module mysite.wsgi

其中mysite.wsgi(mysite替换成自己的项目名称)会自动搜索项目中的wsgi.py文件

如果正常运行~恭喜

如果浏览器出现Internal Server Error

检查命令提示符是否输入正确
检查是否项目根目录下运行
其他情况,查看命令行输出或者log
至此Uwsgi基本配置算是完成了~~

Nginx + Uwsgi + django

Nginx访问hello.py

现在我们使用socket通信来通过Nginx访问hello.py,(确保Nginx已经启动)config文件夹下运行

uwsgi --socket :8001 --wsgi-file hello.py

好了,就这么现在访问浏览器

xx.xx.xxx.xxx:8000

成功访问~

其中8000是Nginx监听的端口(mysite_nginx.conf文件中配置),–socket :8001是用于socket通信的端口(两个端口都可以替换~)

使用Unix sockets替换端口

编辑mysite_nginx.conf文件,将原来内容修改成如下

server unix:///path/to/your/mysite/mysite.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
1
2
保存后,config文件夹下运行

uwsgi --socket mysite.sock --wsgi-file hello.py

依旧打开浏览器访问xx.xx.xxx.xxx:8000,屏幕出现hello world,说明一切正常

如果出现502 Bad Getway

查看你的Nginx error log(/var/log/nginx/error.log)

如果出现

connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permission
denied)
1
2
则说明是权限问题,运行

uwsgi --socket mysite.sock --wsgi-file hello.py --chmod-socket=666

或者

uwsgi --socket mysite.sock --wsgi-file hello.py --chmod-socket=664 (推荐,但是我用了没成功)

log如果打印了其他问题,请仔细检查配置~

使用Nginx + Uwsgi运行项目

uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664
1
注意这里有个小坑,mysite_nginx.conf中我配置的mysite.sock在config文件夹下,而这条命令需要在项目根目录下运行,这里可以:

修改mysite_nginx.conf下sock文件路径到根目录下
使用配置文件启动(推荐)
编辑配置文件

config文件夹下新建mysite_uwsgi.ini,输入以下内容

# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /path/to/your/project
# Django's wsgi file
module          = project.wsgi
# the virtualenv (full path)
home            = /path/to/virtualenv

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = /path/to/your/project/mysite.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true

stats           = %(chdir)/config/uwsgi.status           

pidfile         = %(chdir)/config/uwsgi.pid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
用自己的路径替换其中对应的配置~

保存后,运行(确保在mysite_uwsgi.ini目录下)

uwsgi --ini mysite_uwsgi.ini

打开浏览器访问xx.xx.xxx.xxx:8000

项目这时候应该完美运行了~~

大功告成~:)
--------------------- 
作者:泡泡茶壶 
来源:CSDN 
原文:https://blog.csdn.net/breavo_raw/article/details/82665978 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值