1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 读django文档——nginx + uwsgi 部署django项目

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

时间:2020-09-12 10:43:07

相关推荐

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

目录

一、配置uwsgi

二、配置nginx

一、配置uwsgi

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

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

conda activate webenvconda 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 variableenv = 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# mastermaster=True# max number of worker processesprocesses = 10# vacuum means clear environment on exitvacuum=True# respawn processes taking more than 20 secondsharakiri = 20# respawn processes after serving 5000 requestsmax-requests = 5000# background the process & log#daemonize = /var/log/uwsgi/webdev.loglogto = /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=SIGQUITExecStart=/anaconda3/envs/webenv/bin/uwsgi --ini /data/webdev/uwsgi.iniRestart=alwaysType=notifyNotifyAccess=allStandardError=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 toupstream webdev {# server unix:///path/to/your/mysite/mysite.sock; # for a file socketserver 127.0.0.1:8001; # for a web port socket (we'll use this first)}# configuration of the serverserver {# the port your site will be served onlisten8000;# the domain name it will serve forserver_name 192.168.27.7 # substitute your machine's IP address or FQDNcharsetutf-8;access_log /var/log/nginx/access.log;# max upload sizeclient_max_body_size 75M; # adjust to taste# Django medialocation /media {alias /data/webdev/media; # your Django project's media files - amend as required}# Django staticlocation /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 / {includeuwsgi_params; # the uwsgi_params file you installeduwsgi_pass webdev;uwsgi_connect_timeout 20;}}

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。