Centos8下django项目部署 nginx+uwsgi的教程

linux itxz 5年前 (2020-11-19) 483次浏览 已收录 0个评论

1.虚拟环境virtualenv安装

1.安装virtualenv

2.创建目录,把项目文件传过来

3.创建独立运行环境-命名

4.进入虚拟环境

5.在虚拟环境中安装第三方库,导入需要的环境(导出命令:pip3 freeze > packages.txt)

 pip3 install <a href="http://www.itxz.com/?tag=django" title="查看更多关于django的文章" target="_blank">django</a>==2.11   #此时pip3的包都会安装到venv1环境下,venv1是针对Myproject创建的
 pip3 install -r packages.txt

6.退出venv1环境

7. virtualenv是如何创建“独立”的Python运行环境的呢?原理很简单,就是把系统Python复制一份到virtualenv的环境,
用命令source venv/bin/activate进入一个virtualenv环境时,virtualenv会修改相关环境变量,让命令python和pip均指向当前的virtualenv环境。

2.django配置

1.settings.py

DEBUG = False #debug改为false
 
ALLOWED_HOSTS = ['*'] # 访问地址改为 “*” 表示所有
 
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static")
 #<a href="http://www.itxz.com/?tag=nginx" title="查看更多关于nginx的文章" target="_blank">nginx</a>访问的目录 放到了之前static的上一级目录,可以自定义 需要写绝对路径
STATIC_URL = '/static/' 
STATICFILES_DIRS=[ os.path.join(BASE_DIR,"static"),]
 
MEDIA_URL = '/archive/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'archive') 
#用户上传的静态文件,如:头像

配置完成后运行python manage.py collectstatic 加载静态文件至STATIC_ROOT 目录

2.urls.py

from <a href="http://www.itxz.com/?tag=django" title="查看更多关于django的文章" target="_blank">django</a>.urls import path,re_path
from <a href="http://www.itxz.com/?tag=django" title="查看更多关于django的文章" target="_blank">django</a>.conf import settings
from <a href="http://www.itxz.com/?tag=django" title="查看更多关于django的文章" target="_blank">django</a>.views.static import serve
  
urlpatterns = [
   re_path(r'^archive/(?P<path>.*)$', serve, 
{'document_root': settings.MEDIA_ROOT}, name='archive'), #用户上传的文件
 path('favicon.ico', serve,{'path': 'img/favicon.ico','document_root':settings.STATIC_ROOT}),
 #全局favicon.ico图标
]

3.安装和配置uwsgi

1.进入虚拟环境venv1,安装uwsgi(最好虚拟环境外也安装一下)

2.配置启动文件(放到哪个目录都可以,我放到venv1下了)
uwsgi支持ini、xml等多种配置方式,本文以 ini 为例, 在/etc/目录下新建uwsgi.ini,添加如下配置:

#添加配置选择
[<a href="http://www.itxz.com/?tag=uwsgi" title="查看更多关于uwsgi的文章" target="_blank">uwsgi</a>]
#配置和<a href="http://www.itxz.com/?tag=nginx" title="查看更多关于nginx的文章" target="_blank">nginx</a>连接的socket连接
socket=127.0.0.1:8000
#http=0.0.0.0:8000 #http连接
#配置项目路径,项目的所在目录
chdir = /opt/My/Myproject
 
#配置wsgi接口模块文件路径,也就是wsgi.py这个文件所在的目录名
wsgi-file = Myproject/wsgi.py
#配置启动的进程数
processes=4
#配置每个进程的线程数
threads=2
#配置启动管理主进程
master=True
#虚拟环境目录
home=/opt/My/venv1
#配置存放主进程的进程号文件(我注释了,据说和supervisor的日志冲突)
#pidfile=uwsgi.pid
 
#配置dump日志记录 (同上)
#daemonize=uwsgi.log

3.指定配置文件启动

4.安装和配置nginx

1.centos8安装nginx(直接yum安装)

2.配置nginx.conf

user nginx;
worker_processes 2; #进程数
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
 
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
 
events {
worker_connections 1024;
}
 
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
 
access_log /var/log/nginx/access.log main;
 
sendfile on;
tcp_nopush on;
tcp_nodelay on;
 
keepalive_timeout 65;
types_hash_max_size 2048;
 
include /etc/nginx/mime.types;
default_type application/octet-stream;
 
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
#include /etc/nginx/conf.d/*.conf;
 
server {
listen 80;#监听端口
#listen [::]:80 default_server;
server_name 192.168.3.119;# 域名或者IP
#root /usr/share/nginx/html;
 
# Load configuration files for the default server block.
#include /etc/nginx/default.d/*.conf;
charset utf-8;
 
location /static {
alias /opt/My/static; #静态文件地址(STATIC_ROOT)
 
}
 
location / {
include uwsgi_params;
uwsgi_pass 0.0.0.0:8000; #项目端口号
uwsgi_param UWSGI_SCRIPT Myproject.wsgi; #项目wsgi.py目录
uwsgi_param UWSGI_CHDIR /opt/My/Myproject; #项目目录
}
 
}
 
}

3.启动nginx

5.安装和配置supervisor

1.安装supervisor

2.通过命令生成配置文件到etc目录(可以自定义)

3.在配置文件末尾添加如下代码

[program:myname] #任务名
 command=/opt/my/venv1/bin/uwsgi --ini /opt/my/venv1/uwsgi.ini
 #执行的命令 运行uwsgi。 uwsgi是虚拟环境内的
 
 [program:nginx] 
 command=/usr/sbin/nginx #运行nginx

4.启动supervisor

supervisord -c /etc/supervisord.conf #启动supervisor
supervisorctl -c /etc/supervisord.conf #进入supervisor交互界面

5.supervisor命令

start myname #启动 \
stop myname #停止 >> 可以写任务名称或者 all 表示全部
restart myname #重启 /


IT学者 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Centos8下django项目部署 nginx+uwsgi的教程
喜欢 (0)

您必须 登录 才能发表评论!