Django 默认使用 WSGI(Python Web Server Gateway )
做为 Web 服务器,通常仅用来做为测试使用,实际生产环境而是使用 uWSGI 和 Nginx 做为服务器。javascript
uWSGI 代码彻底用C编写,效率高、性能稳定,可是处理 静态文件能力较弱,所以实际生产环境中,通常采用 uWSGI + Nginx
二者搭配使用:php
安装:css
pip3 install uWSGI
settings.py 配置html
设置 ALLOWED_HOSTS
:前端
ALLOWED_HOSTS = [ # 加上本机的IP地址 '192.168.xx.xxx', '127.0.0.1', 'localhost' ]
也能够这样设置:java
ALLOWED_HOSTS = ['*']
# 请更换成你服务器 ip 和端口,其中 Celery_Test/wsgi.py 为Django 项目中自带的 wsgi web 服务 hj@hj:~/桌面/Celery_Test$ uwsgi --http 192.168.21.128:8080 --file Celery_Test/wsgi.py --static-map=/static=static
出现以下效果即是运行成功了:python
查看启动端口:mysql
hj@hj:~/桌面/Celery_Test/script$ ps -ef|grep uwsgi hj 17176 5231 0 15:37 pts/1 00:00:00 uwsgi --http 192.168.xx.128:8080 --file Celery_Test/wsgi.py --static-map=/static=static hj 17177 17176 0 15:37 pts/1 00:00:00 uwsgi --http 192.168.xx.128:8080 --file Celery_Test/wsgi.py --static-map=/static=static hj 17206 6554 0 15:38 pts/2 00:00:00 grep --color=auto uwsgi
访问 192.168.21.128:8080
:linux
在项目根目录建立一个名为 uwsgi.ini
的配置文件,配置以下:nginx
[uwsgi] # 项目目录 chdir=/home/hj/桌面/Celery_Test/ # 指定项目的application module=Celery_Test.wsgi:application # 指定sock的文件路径 socket=/home/hj/桌面/Celery_Test/script/uwsgi.sock # 进程个数 workers=5 pidfile=/home/hj/桌面/Celery_Test/script/uwsgi.pid # 指定IP端口 http=192.168.21.128:8080 # 指定静态文件 static-map=/static=/home/hj/桌面/Celery_Test/static # 启动uwsgi的用户名和用户组 uid=root gid=root # 启用主进程 master=true # 自动移除unix Socket和pid文件当服务中止的时候 vacuum=true # 序列化接受的内容,若是可能的话 thunder-lock=true # 启用线程 enable-threads=true # 设置自中断时间 harakiri=30 # 设置缓冲 post-buffering=4096 # 设置日志目录 daemonize=/home/hj/桌面/Celery_Test/script/uwsgi.log
配置好以后,运行 uwsgi --ini uwsgi.ini
启动 uwsgi,出现以下信息即表示启动成功:
hj@hj:~/桌面/Celery_Test$ uwsgi --ini uwsgi.ini [uWSGI] getting INI configuration from uwsgi.ini [uwsgi-static] added mapping for /static => /home/hj/桌面/Celery_Test/static
查看运行状况:
ps ajx|grep uwsgi
效果以下图:
经常使用命令:
uwsgi --ini uwsgi.ini # 启动 # 启动时会生成两个文件,分别为: # PID文件 标识这个程序所处的状态 # SOCK文件 用来和其余程序通讯的 uwsgi --stop uwsgi.pid # 中止 uwsgi --reload uwsgi.ini # 重置
Tips
中止时出现 signal_pidfile()/kill(): No such process [core/uwsgi.c line 1693]
缘由:当前端口进程与 uwsgi.pid
不一致,查看当前端口实际进程 ID,并修改 uwsgi.pid
:
# 根据端口,查看进程 hj@hj:~/桌面/Celery_Test$ sudo netstat -nap | grep 8080 tcp 0 0 192.168.21.128:8080 0.0.0.0:* LISTEN 6943/uwsgi # 修改 uwsgi.pid 的值为 6943,并再从新中止 hj@hj:~/桌面/Celery_Test$ uwsgi --stop script/uwsgi.pid # 查看发现已经成功中止 hj@hj:~/桌面/Celery_Test$ ps ajx|grep uwsgi 5231 14550 14549 5231 pts/1 14549 S+ 1000 0:00 grep --color=auto uwsgi
Linux 中怎么查看端口和进程号
# 加上 sudo # 根据进程 pid 查看端口 lsof -i | grep pid # 根据端口查看进程 lsof -i:port # 根据进程 pid 查看端口 netstat -nap | grep pid # 根据端口查看进程号 netstat -nap | grep port
咱们知道 uWSGI 处理静态文件请求能力比较弱,所以通常实际生产环境中以 动静分离 的方式处理动静请求,即 uWSGI + Nginx。
Nginx 做用还包括负载均衡、反向代理等。
Nginx 的软件包在 Ubuntu 默认软件仓库中可用。 安装很是简单,只需键入如下命令:
sudo apt update udo apt install nginx
查看服务器版本信息:
sudo nginx -v nginx version: nginx/1.14.0 (Ubuntu)
查看服务器状态:
# 两个均可以 sudo systemctl status nginx ps -ef | grep nignx
配置防火墙
打开 80 和 443 端口容许经过防火墙:
hj@hj:~$ sudo ufw allow 'Nginx Full' 防火墙规则已更新 规则已更新(v6)
检查是否更改:
hj@hj:~$ sudo ufw status 状态: 激活 至 动做 来自 - -- -- 22 ALLOW Anywhere 4200 ALLOW Anywhere Nginx Full ALLOW Anywhere 22 (v6) ALLOW Anywhere (v6) 4200 (v6) ALLOW Anywhere (v6) Nginx Full (v6) ALLOW Anywhere (v6)
测试安装
访问:http://192.168.21.128/
:
使用 systemctl 管理 Nginx 服务
您能够像任何其余 systemd 单位同样管理 Nginx 服务:
# 中止Nginx服务 sudo systemctl stop nginx # 再次启动 sudo systemctl start nginx # 从新启动Nginx服务: sudo systemctl restart nginx # 在进行一些配置更改后从新加载 Nginx 服务: $sudo systemctl reload nginx # 若是你想禁用Nginx服务在启动时启动: $sudo systemctl disable nginx # 并从新启用它: $sudo systemctl enable nginx
以CentOS6.x 系统为例
# 备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup # 更换成国内源 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo # 生成缓存 yum makecache
yum -y install nginx
为 Nginx 添加配置文件,Ngnix 默认配置文件加载是在 /etc/nginx/conf.d/
目录下,新建一个配置文件(名字随意),编辑以下:
# 新建到配置文件 conf.d vim /etc/nginx/conf.d/ # 编辑配置文件 server { # 开始配置了 listen 80; # 监听端口 server_name 10.129.xxx.183 ; # 你访问的路径前面的 url名称 access_log /var/log/nginx/access.log main; # Nginx日志配置 charset utf-8; # Nginx编码 gzip on; # 启用压缩,这个的做用就是给用户一个网页,好比3M压缩后1M这样传输速度就会提升不少 gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream; # 支持压缩的类型 error_page 404 /404.html; # 错误页面 error_page 500 502 503 504 /50x.html; # 错误页面 # 指定项目路径 uwsgi location / { # 相似于 Django的 url(r'^admin/', admin.site.urls), include uwsgi_params; # 导入一个Nginx模块他是用来和uWSGI进行通信的 uwsgi_connect_timeout 30; # 设置链接uWSGI超时时间 uwsgi_pass unix:/opt/project_teacher/script/uwsgi.sock; # 指定uwsgi的sock文件全部动态请求就会直接丢给他 } # 指定静态文件路径 location /static/ { alias /opt/project_teacher/teacher/static/; index index.html index.htm; } }
参考:
server { listen 80; server_name 192.168.xx.128 ; access_log /var/log/nginx/access.log main; charset utf-8; gzip on; gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream; error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location / { include uwsgi_params; uwsgi_connect_timeout 30; uwsgi_pass unix:/home/hj/桌面/Celery_Test/script/uwsgi.sock; } location /static/ { alias /home/hj/桌面/Celery_Test/static/; index index.html index.htm
启动 Nginx 服务 /etc/init.d/nginx start
,访问:http://192.168.21.128:8080/app/index/
,效果以下图:
经常使用命令:
/etc/init.d/nginx start # 启动 /etc/init.d/nginx stop # 关闭 # Nginx配置是重启才生效,若修改配置不知道是否对不对,能够用来测试 /etc/init.d/nginx configtest # 生产环境直接 reload就好了,不要 stop start 或者 restart /etc/init.d/nginx reload
配置 Django 静态文件
admin 所需的静态文件都在 Django 的安装内,咱们没有配置指向 Django 的配置文件。
解决办法:
settings.py
,将全部静态文件收集到 static_all
中:# settings.py STATIC_ROOT = os.path.join(BASE_DIR, "static_all") # 收集成功提示(仅供参考) 120 static files copied to '/home/hj/桌面/Celery_Test/static_all'.
static_all
这个目录中(可不建立 static_all):python3 manage.py collectstatic --noinput
/etc/nginx/conf.d/test.ini
中静态文件路径:alias /home/hj/桌面/Celery_Test/static_all;
指定其余地方放置静态文件
# 新建目录 sudo mkdir -vp /var/www/test2/static/ # 赋予权限 sudo chmod 777 /var/www/test2/static/ # 修改项目中 settings.py,指定静态文件路径 STATIC_ROOT = '/var/www/test2/static/' STATIC_URL = '/static/' # 收集静态文件到 /var/www/test2/static/ 中 python3 manage.py collectstatic # 输入 yes,开始收集,从新加载 Nginx 服务