Django项目部署---命令行方式
一.安装并创建虚拟环境 #取消虚拟环境:deactivate
1.安装virtualenv
pip3.9 install virtualenv
2.创建具体的虚拟环境实例
virtualenv /envs/demo2 –python=python3.9
3.激活虚拟环境
source /envs/demo2/bin/activate 这是linux环境下
\envs\demo2\bin\activate 这是windows环境下
#退出虚拟环境 deactivate
二.创建项目
django-admin startproject demo2
3.安装uwsgi
pip install uwsgi
uwsgi –http :8002 –wsgi-file helloworld.py #如果使用了虚拟环境,这个命令一定要在激活了对应的虚拟环境的状态下执行,不然启用的就是系统环境下的uwsgi
直接运行项目:python manage.py runserver 0.0.0.0:8002
具体案例:
项目目录结构:
day16/
├── .idea/
├── app01/
├── day16/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── wsgi.py
│ └── ...
├── manage.py
├── Monaco.ttf
├── requirements.txt
└── ╩╣╙├modelform╓«╟░╡─app01/
使用心得
1.启动uwsgi –ini uwsgi.ini #此处ini前面是两条短横线,但是显示的是一条,前端展示问题
2.结束所有uwsgi进程 pkill -9 uwsgi
3.查找所有uwsgi进程 ps aux | grep uwsgi
4.如果需要用一个指定的端口号,记得在平台的安全组中放行此端口
5.如果出现502 Bad Gateway,记得去nginx的error.log和uwsgi的log日志中查看是什么报错
PS:我遇到的是诸如权限的问题,利用chatgpt,大致可以解决
nginx配置
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;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name 1.92.108.92 ;
location / {
include uwsgi_params;
uwsgi_pass unix:/www/wwwroot/day16/uwsgi.sock;
}
location /static/ {
alias /www/wwwroot/day16/app01/static/;
}
}
}
uwsgi配置
[uwsgi]
# Django-related settings
module = day16.wsgi:application
# The base directory (full path)
chdir = /www/wwwroot/day16
# The virtualenv (full path)
home = /www/wwwroot/day16/env
# Process-related settings
master = true
processes = 2
# Socket file path
socket = /www/wwwroot/day16/uwsgi.sock
# Permissions for the socket file
chmod-socket = 660
# Set user and group
uid = nginx
gid = nginx
# Clear environment on exit
vacuum = true
# Daemonize the uWSGI process (log to a file)
daemonize = /www/wwwroot/day16/uwsgi.log