Django + Vue 使用Nginx + uWsgi部署
1.settings.py配置
STATIC_ROOT = os.path.join(BASE_DIR, 'static/dist')
# 导入前后端静态资源后更改即可
DEBUG = True # 为True不容易暴露项目信息,当然也不显示BUG信息
ALLOWED_HOSTS = ['*']
STATIC_URL = '/static/'
2.django端打包静态资源
# 会在static 下生成
[root@dsc1 mydjango]# mkdir -p /deploy/mydjango/static
[root@dsc1 mydjango]# cd /deploy/mydjango
[root@dsc1 mydjango]# python manage.py collectstatic
128 static files copied to '/deploy/mydjango/static/dist'.
3.在uwsgi_conf中写入uwsgi.ini
[root@dsc1 deploy]# mkdir -p /deploy/uwsgi_conf
# vi /deploy/uwsgi_conf/uwsgi.ini
[uwsgi]
# 使用Nginx连接时使用,Django程序所在服务器地址和端口号
socket=127.0.0.1:8000
# 项目目录绝对路径
chdir=/deploy/mydjango
# 项目中wsgi.py文件的目录,相对于项目目录
wsgi-file=opwf/wsgi.py
# 进程数(机器核数的1倍)
processes=4
# 线程数
threads=20
# uwsgi服务器的角色
master=True
# 存放进程编号的文件
pidfile=uwsgi.pid
# 日志文件
daemonize=uwsgi.log
4.打包Vue静态资源
npm run build
提取dist静态资源
将静态资源放置后端 /deploy/mydjango/static/dist 下
文件如下
[root@dsc1 html]# cd /deploy/mydjango/static/dist/
[root@dsc1 dist]# ls
admin index.html static
5.配置ngnix
[root@dsc1 ~]# more /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
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 4096;
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 8080;
server_name 192.168.1.102;
location /static {
alias /deploy/mydjango/static;
}
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
uwsgi_ignore_client_abort on;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 8888;
server_name 192.168.1.102;
location / {
alias /deploy/mydjango/static/dist/;
index index.html,index.htm;
try_files $uri $uri/ /index.html;
}
}
}
6.Nginx启动
systemctl restart nginx # 开启nginx
tail -f /var/log/nginx/access.log # 查看nginx接入日志
tail -f /var/log/nginx/error.log # 查看nginx错误日志
7.uWSGI启动
[root@dsc1 uwsgi_conf]# cd /deploy/uwsgi_conf
# 进入项目目录
uwsgi --ini uwsgi.ini
# 启动uwsgi的 django项目
# http://192.168.1.102:8888/
uwsgi --stop uwsgi.pid
# 关闭uwsgi
tail -f uwsgi.log
# 查看uwsgi日志
ps -ef|grep uwsgi
# 查看uwsgi服务是否启动
netstat -anptu | grep 8888
# 查看8888端口被哪一个程序 占用
8.测试验证
uwsgi --http :8080 --wsgi-file /deploy/mydjango/mydjango/test.py
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2021-06-04 python发邮件附件