Centos8-django项目部署 nginx+uwsgi

1.虚拟环境virtualenv安装

1.安装virtualenv
 pip3 install virtualenv 
2.创建目录,把项目文件传过来 mkdir My cd My
3.创建独立运行环境-命名 virtualenv --no-site-packages --python=python3 venv1 #创建独立的环境,并且指定解释器是python3
4.进入虚拟环境 source venv1/bin/activate #此时进入虚拟环境(venv1)
5.在虚拟环境中安装第三方库,导入需要的环境(导出命令:pip3 freeze > packages.txt) pip3 install django==2.11 #此时pip3的包都会安装到venv1环境下,venv1是针对Myproject创建的
pip3 install -r packages.txt
6.退出venv1环境
deactivate
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") #nginx访问的目录 放到了之前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 django.urls import path,re_path
from django.conf import settings
from django.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(最好虚拟环境外也安装一下)
 (venv1) [root@localhost ~]# pip3 install uwsgi

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

    #添加配置选择
    [uwsgi]
    #配置和nginx连接的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.指定配置文件启动
    uwsgi --ini  /opt/My/venv1/uwsgi.ini

 

4.安装和配置nginx

1.centos8安装nginx(直接yum安装)
yum install -y nginx

2.配置nginx.conf(cd /etc/nginx)

   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

   /usr/sbin/nginx

 

5.安装和配置supervisor

1.安装supervisor
pip3 install supervisor # 之前需要python2的环境才可以安装 现在直接pip3安装就可以

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

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 #重启 /
posted @ 2020-05-06 21:10  hean-i  阅读(1913)  评论(0编辑  收藏  举报
jQuery火箭图标返回顶部代码