当前位置: 代码迷 >> 综合 >> django+uwsgi+nginx部署配置篇
  详细解决方案

django+uwsgi+nginx部署配置篇

热度:0   发布时间:2023-12-15 23:12:44.0

django+uwsgi+nginx部署配置篇

  • 说明
      • 安装Python3.8
      • 安装Python虚拟环境
        • 列出虚拟环境列表
        • 创建新虚拟环境
        • 启动/ 切换虚拟环境
        • 删除虚拟环境
        • 离开虚拟环境
    • 创建环境
    • 测试环境
      • 启动django项目,测试一下项目能否正常跑动
      • 测试uwsgi能否正常跑动
      • 测试nginx能否正常跑动
        • nginx配置如下
        • 验证nginx配置
        • 启动nginx

说明

1.本项目所有流程均是本人自己摸索验证成功后在发帖,如有问题,请私信或留言一起讨论.
2.本帖项目数据库,redis等环境均在开发时已经装好
3.本帖所有成功点均在本地虚拟机测试通过
注:nginx与uwsgi的安装请参照其他大神的帖子。我踩坑太多,不知道怎么写

安装Python3.8

sudo apt-get python3.8

安装Python虚拟环境

sudo apt install virtualenvsudo apt install virtualenvwrapper

列出虚拟环境列表

lsvirtualenv

创建新虚拟环境

mkvirtualenv [name]

启动/ 切换虚拟环境

workon [name]

删除虚拟环境

rmvirtualenv [name]

离开虚拟环境

deactivate

创建环境

#创建项目文件夹
mkdir /www/project
#创建虚拟环境
mkvirtualenv [project]
#再加载虚拟环境中安装Python环境
pip install -i https://pypi.douban.com/simple/ -r project.txt
#安装uwsgi
sudo apt-get uwsgi
#安装nginx
sudo apt-get nginx

测试环境

启动django项目,测试一下项目能否正常跑动


python manage.py runserver

测试uwsgi能否正常跑动

#创建ini文件
cd /www/project
vim uwsgi.ini
#将下面配置zhantie进你的ini
[uwsgi]
;socket = 127.0.0.1:8000
http  = 127.0.0.1:8000
chdir = /www/project
wsgi-file = project/wsgi.py
# 保存后启动uwsgi
uwsgi uwsgi.ini

测试nginx能否正常跑动

cd  /etc/nginx
sudo vim nginx.conf
#修改 include /etc/nginx/mime.types;,将#去掉,避免项目无法正常加载css# include /etc/nginx/mime.types;--> include /etc/nginx/mime.types;
#在http中添加如下代码server {listen       80;server_name  1;autoindex       on;   #是否允许访问目录location / {proxy_pass http://127.0.0.1:8000;}location /static {alias  /www/project/static; # 1. alias方式配置文件夹,django静态文件夹位置}}
}

nginx配置如下

worker_processes auto;
pid /run/nginx.pid;
#include /etc/nginx/modules-enabled/*.conf;events {worker_connections 768;# multi_accept on;
}http {### Basic Settings##sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 2048;# server_tokens off;# server_names_hash_bucket_size 64;# server_name_in_redirect off;include /etc/nginx/mime.types;default_type application/octet-stream;### SSL Settings##ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLEssl_prefer_server_ciphers on;### Logging Settings##access_log /var/log/nginx/access.log;error_log /var/log/nginx/error.log;### Gzip Settings##gzip on;# gzip_vary on;# gzip_proxied any;# gzip_comp_level 6;# gzip_buffers 16 8k;# gzip_http_version 1.1;# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;### Virtual Host Configs###include /etc/nginx/conf.d/*.conf;#include /etc/nginx/sites-enabled/*;server {listen       80;server_name  1;autoindex       on;   #是否允许访问目录location / {proxy_pass http://127.0.0.1:8000;}location /static {alias  /www/project/static; # 1. alias方式配置文件夹}}
}#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

验证nginx配置

sudo nginx -t 

启动nginx

sudo nginx -c /etc/nginx/nginx.conf