Django3.0 + nginx + uwsgi 部署

CentOS7.6 下部署Django3.0应用,使用nginx+uwsgi部署:

1. uwsgi部署

pip install uwsgi

在项目的根目录中,新建文件夹 conf, 然后进入conf文件夹,并新建文件 uwsgi.ini, 内容如下:

# mysite_uwsgi.ini file
[uwsgi] # Django-related settings
# the base directory (full path)
chdir=/root/EduOnline
# Django's wsgi file
module=EduOnline.wsgi
# the virtualenv (full path) # process-related settings
# master
master=True
# maximum number of worker processes
processes=5
# the socket (use the full path to be safe
socket=0.0.0.0:8001
# http=0.0.0.0:8001
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum=true
virtualenv =/root/.virtualenvs/eduonline logto=/tmp/mylog.log

  

首先进入文件的根目录要使用命令 uwsgi --http :8000 --module EduOnline.wsgi,然后通过浏览器访问:127.0.0.1:8000 是否能访问首页,如果能访问首页,则说明uwsgi能访问成功,然后通过上面文件的配置,将socket一行注释掉,将http一行释放掉,然后保存并退出,再执行以下命令:

uwsgi -i uwsgi.ini

然后再通过浏览器访问,127.0.0.1:8000,如果能访问到首页,则说明配置文件成功,那么这时如果要用nginx来访问,则需要把socket一行释放掉,http一行注释掉即可,然后保存并执行uwsgi -i uwsgi.ini, 然后查看端口使用处于监听状态:netstat -ntulp |grep 8000, 如果有,则表示这个文件配置成功了

2. 配置nginx

安装nginx步骤

a.  Add Nginx Repository

sudo yum install epel-release

b.  Install Nginx

sudo yum install nginx

c. Start Nginx

sudo systemctl start nginx

d. 设置防火墙

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

最后访问你的ip地址:
http://server_domain_name_or_IP/

进入conf.d文件夹

cd /etc/nginx/conf.d

然后创建文件uc_ningx.conf, 代码如下:

# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 0.0.0.0: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 127.0.0.1 0.0.0.0 47.104.226.120; # 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 /root/EduOnline/media; # 指向django的media目录
} location /static {
alias /root/EduOnline/static; # 指向django的static目录
} # Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include uwsgi_params; # the uwsgi_params file you installed
}
}

  保存并退出,然后重启nginx服务:

service nginx restart

然后输入网址:http://127.0.0.1:8000 如果能访问到首页,则表示nginx配置成功

如果访问出现 css和js样式不对,以及报403 Fobbien 的错误,则表示nginx user权限不够,需要设置nginx.conf的头部user: root

Django3.0 + nginx + uwsgi 部署

上一篇:.Net core2.0+Mysql5.7部署到CentOS7.5完整实践经验


下一篇:.NET Core2.0+MVC 用Redis/Memory+cookie实现的sso单点登录