读django文档——nginx + uwsgi 部署django项目

2023-05-16

目录

一、配置uwsgi

二、配置nginx


一、配置uwsgi

本例是在anaconda虚拟环境webenv下面建立的django项目,名字叫webdev。

激活虚拟环境,在该环境里面安装uwsgi。

conda activate webenv
conda install uwsgi

 在django项目webdev目录里创建 uwsgi.ini文件,编辑内容如下。如果遇到目录不存在,比如/var/log/uwsgi,那么自行创建一下。

特别注意一下,如果uwsgi配置文件中配置了 daemonize=/path/to/uwsgi.log (uwsgi服务以守护进程运行),会导致后面sytemctl启动时多次重启而导致启动失败,需改为 logto=/path/to/uwsgi.log

[uwsgi]
# /path/to/your/project (full path)
chdir = /data/webdev

# Django's wsgi file (locate in project.wsgi.py)
module = webdev.wsgi:application

# the virtualenv (full path)
home = /anaconda3/envs/webenv

# set an environment variable
env = DJANGO_SETTINGS_MODULE=webdev.settings

# process-related settings
# the socket (IP:port or  /path/to/your/project/mysite.sock)
socket = 127.0.0.1:8001
# master
master=True
# max number of worker processes
processes = 10
# vacuum means clear environment on exit
vacuum=True
# respawn processes taking more than 20 seconds
harakiri = 20
# respawn processes after serving 5000 requests
max-requests = 5000
# background the process & log
#daemonize = /var/log/uwsgi/webdev.log
logto = /var/log/uwsgi/webdev.log
# /path/to/pid.file (full path)
pidfile = /run/webdev.pid

然后启动。这个是手动的,只是验证可以使用。

uwsgi --ini uwsgi.ini

配置uwsgi开机启动,添加一个systemd服务 /etc/systemd/system/uwsgi-webdev.service 内容。

[Unit]
Description=UWSGI For WEBDEV 
After=syslog.target
 
[Service]
KillSignal=SIGQUIT
ExecStart=/anaconda3/envs/webenv/bin/uwsgi --ini /data/webdev/uwsgi.ini
Restart=always
Type=notify
NotifyAccess=all
StandardError=syslog
 
[Install]
WantedBy=multi-user.target

然后设置开机启动

systemctl daemon-reload 
systemctl start uwsgi-webdev.service 

二、配置nginx

编辑nginx配置文件 /etc/nginx/conf.d/webdev.conf,内容如下。注意里面的 static 、 media 等配置,涉及到django的 STATIC_ROOT目录。

# the upstream component nginx needs to connect to
upstream webdev {
    # 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 192.168.27.7 # substitute your machine's IP address or FQDN
    charset     utf-8;
    access_log  /var/log/nginx/access.log;

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

    # Django media
    location /media  {
        alias /data/webdev/media;  # your Django project's media files - amend as required
    }

    # Django static
    location /static {
        alias /data/webdev/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        include     uwsgi_params; # the uwsgi_params file you installed
        uwsgi_pass  webdev;
        uwsgi_connect_timeout 20;
    }
}

然后重启nginx,访问 IP:8000 验证结果。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

读django文档——nginx + uwsgi 部署django项目 的相关文章

随机推荐