django+drf+nginx+uwsgi+socket项目部署
准备centos 7服务器一台 后端代码 前端代码
- 在/var/www/目录下创建项目文件夹,例如创建house_statistic_query_dev如下
[root@lidong www]# pwd
/var/www
[root@lidong www]# ls
house_statistic_query_dev
- 将前端项目,后端代码项目放到house_statistic_query_dev里,并创建配置文件夹(dev_config)
[root@lidong house_statistic_query_dev]# pwd
/var/www/house_statistic_query_dev
[root@lidong house_statistic_query_dev]# ll
total 4
drwxr-xr-x. 4 root root 105 Nov 19 09:03 dev_config
drwxr-xr-x. 14 root root 282 Nov 17 03:27 house_statistic_query_backend
drwxr-xr-x. 10 root root 4096 Nov 19 07:09 house_statistic_query_front
- 在dev_config文件夹下创建uwsgi文件夹,venv虚拟环境
[root@lidong dev_config]# pwd
/var/www/house_statistic_query_dev/dev_config
[root@lidong dev_config]# ll
total 12
drwxr-xr-x. 2 root root 70 Nov 23 02:04 uwsgi # make uwsgi
drwxr-xr-x. 6 root root 92 Nov 17 03:26 venv #执行python -m venv venv完成创建
- 激活虚拟环境
[root@lidong ~]# source /var/www/house_statistic_query_dev/dev_config/venv/bin/activate
(venv) [root@lidong ~]#
- 进入项目目录,检测该Django项目目录是否能够正确启动
(venv) [root@lidong house_statistic_query_backend]# python3 manage.py runserver 172.16.10.239:8833
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
November 29, 2021 - 15:31:28
Django version 3.0.7, using settings 'settings.dev'
Starting development server at http://172.16.10.239:8833/
Quit the server with CONTROL-C.
-
如果DEBUG = True,浏览器访问你应看到: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BHcZutUF-1638441513904)(https://note.youdao.com/yws/res/f/WEBRESOURCE6b3a56b56e4e775950cbd34bad9a5fdf)] -
如果DEBUG = False,浏览器访问你应看到: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Q1Ocv6l1-1638441513905)(WEBRESOURCEbb914ee4bf717a913010113f427bf330)]
- 在uwsgi文件夹下创建uwsgi.ini文件
[uwsgi]
# 如果我们想直接将uwsgi用作服务,直接暴露在公网那么就使用http;
# 如果有单独的服务器,由服务器将请求转发给uwsgi处理,并且使用http协议,那么此时使用http-socket
# http的端口,http 一般是作为独立部署的选项; http-socket 在前端webserver不支持uwsgi时使用,
# nginx的端口,前端webserver支持uwsgi, 则直接使用socket即可(tcp or unix)
#http= :8081
socket=:8082
http-keepalive=1
# 项目所在的目录
chdir=/var/www/house_statistic_query_dev/house_statistic_query_backend
# wsgi所在的位置(django项目的wsgi.py文件)
module=house_statistic_query_backend.wsgi:application
# 替换uwsgi.py文件下的os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'house_bi.settings')
env = DJANGO_SETTINGS_MODULE=settings.dev
# 设置静态文件
# static-map = /ui=/var/www/house_statistic_query_dev/house_statistic_query_backend/static
# 启动主进程
master=true
# 进程个数
workers=3
# 在每个worker而不是master中加载应用
lazy-apps=true
# 每个进程最大请求个数
max-requests=2000
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 自动移除unix Socket 和 Pid 文件 当服务停止的时候
vacuum=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 序列化接受的内容,如果可能的话
thunder-lock = true
buffer-size = 65536
# 进程个数
processes=4
threads=2
chmod-socket=664
pidfile=/var/www/house_statistic_query_dev/dev_config/uwsgi/uwsgi.pid
#日志所在位置
logto=/var/www/house_statistic_query_dev/dev_config/uwsgi/uwsgi.log
注意:当连接为socket通信时,不需要设置static-map对应的静态文件路径,因为此时使用外部浏览器将无法直接访问,会提示类似于“缓存区(buffer)超过最大长度”类似的问题。故所以需要nginx来转发,因为nginx服务器和后端可以用socket连接,故需要先访问uwsgi服务器,再请求后端服务器
- 使用uwsgi启动django服务器,会生成四个文件如下。
(venv) [root@lidong uwsgi]# uwsgi -d --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
(venv) [root@lidong uwsgi]# ll
total 400
-rw-r-----. 1 root root 136957 Dec 1 06:31 --ini
-rw-r--r--. 1 root root 1710 Dec 1 06:31 uwsgi.ini
-rw-r-----. 1 root root 5119 Nov 29 07:49 uwsgi.log
-rw-rw-rw-. 1 root root 5 Nov 29 07:50 uwsgi.pid
srw-rw-r--. 1 root root 0 Dec 1 06:31 uwsgi.sock
- 查看是否启动成功,出现该网页无法访问则为启动成功
- 下载nginx,并配置nginx.conf文件如下,并启动nginx
- 方法一:配置,对应uwsgi.ini文件中的uwsgi.sock文件路径
- uwsgi_pass unix:/var/www/house_statistic_query_dev/dev_config/uwsgi/uwsgi.sock;
user root;
worker_processes 2;
error_log /software/nginx/logs/error.log warn;
#pid /software/nginx/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
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 /software/nginx/logs/access.log main;
sendfile on;
keepalive_timeout 1800;
server {
listen 80;
server_name 172.16.10.239; # 服务器 ip 或是域名
charset utf-8; # 字符集
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 3;
gzip_types text/plain text/css application/xml application/javascript application/x-javascript text/javascript;
location / {
root /var/www/house_statistic_query_dev/house_statistic_query_front/dist;
add_header Cache-Control no-cache;
add_header Access-Control-Allow-Oriain '*';
index html index.html;
try_files $uri $uri/ /index.html;
}
location /ui{
alias /var/www/house_statistic_query_dev/house_statistic_query_backend/static;
index index.html index.htm;
}
location /api/ {
# include uwsgi_params;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
client_body_buffer_size 10M;
client_max_body_size 10G;
proxy_buffers 65536 4k;
proxy_read_timeout 216000;
proxy_connect_timeout 216000;
proxy_send_timeout 216000;
send_timeout 216000;
proxy_next_upstream error timeout http_404;
uwsgi_send_timeout 1800;
uwsgi_read_timeout 1800;
uwsgi_pass unix:/var/www/house_statistic_query_dev/dev_config/uwsgi/uwsgi.sock;
uwsgi_param UWSGI_SCRIPT house_statistic_query_backend.wsgi;
uwsgi_param UWSGI_CHDIR /var/www/house_statistic_query_dev/house_statistic_query_backend;
}
location /admin {
include uwsgi_params;
uwsgi_pass unix:/var/www/house_statistic_query_dev/dev_config/uwsgi/uwsgi.sock;
uwsgi_param UWSGI_SCRIPT house_statistic_query_backend.wsgi;
uwsgi_param UWSGI_CHDIR /var/www/house_statistic_query_dev/house_statistic_query_backend;
}
}
}
- 方法一:如果在uwsgi.ini文件中没有配置uwsgi.sock文件,可以使用ip代替
- uwsgi_pass 172.16.10.239:8082;
user root;
worker_processes 2;
error_log /software/nginx/logs/error.log warn;
#pid /software/nginx/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
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 /software/nginx/logs/access.log main;
sendfile on;
keepalive_timeout 1800;
server {
listen 80;
server_name 172.16.10.239; # 服务器 ip 或是域名
charset utf-8; # 字符集
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 3;
gzip_types text/plain text/css application/xml application/javascript application/x-javascript text/javascript;
location / {
root /var/www/house_statistic_query_dev/house_statistic_query_front/dist;
add_header Cache-Control no-cache;
add_header Access-Control-Allow-Oriain '*';
index html index.html;
try_files $uri $uri/ /index.html;
}
location /ui{
alias /var/www/house_statistic_query_dev/house_statistic_query_backend/static;
index index.html index.htm;
}
location /api/ {
include uwsgi_params;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
client_body_buffer_size 10M;
client_max_body_size 10G;
proxy_buffers 65536 4k;
proxy_read_timeout 216000;
proxy_connect_timeout 216000;
proxy_send_timeout 216000;
send_timeout 216000;
proxy_next_upstream error timeout http_404;
uwsgi_send_timeout 1800;
uwsgi_read_timeout 1800;
# uwsgi_pass unix:/var/www/house_statistic_query_dev/dev_config/uwsgi/uwsgi.sock;
uwsgi_pass 172.16.10.239:8082;
uwsgi_param UWSGI_SCRIPT house_statistic_query_backend.wsgi;
uwsgi_param UWSGI_CHDIR /var/www/house_statistic_query_dev/house_statistic_query_backend;
}
location /admin {
include uwsgi_params;
# uwsgi_pass unix:/var/www/house_statistic_query_dev/dev_config/uwsgi/uwsgi.sock;
uwsgi_param UWSGI_SCRIPT house_statistic_query_backend.wsgi;
uwsgi_param UWSGI_CHDIR /var/www/house_statistic_query_dev/house_statistic_query_backend;
uwsgi_pass 172.16.10.239:8082;
}
}
}
- 检查是否配置成功,此时静态文件可以正常加载。
- 检查是否与后端连接成功,检查登陆功能。
|