一、安装uwsgi
uwsgi是一个应用服务器,非静态文件的网络请求就必须通过他完成,他也可以充当静态文件服务器,但不是它的强项。
注意:uwsgi必须安装在系统级别的Python环境中,不要安装到虚拟环境中。
uwsgi是使用python编写的,执行下面命令安装。
conda install -c conda-forge uwsgi
conda install -c conda-forge libiconv
##下面先不执行
conda install -c https://conda.binstar.org/travis uwsgi
二、编写uwsgi配置文件
假设项目路径是:/home/hadoop/djtest11
在项目的路径下面,创建一个文件叫做uwsgi.ini的文件,然后填写以下代码:
[uwsgi]
chdir = /home/hadoop/djtest11
#Django的wsgi文件
wsgi-file = djtest11/wsgi.py
#如果django项目运行在某个虚拟环境下,指定 Python虚拟环境的路径
home = /home/hadoop/anaconda3/envs/py3
进程相关的设置
主进程
master = true
最大数量的工作进程
processes = 10
socket文件路径,绝对路径
socket = /home/hadoop/djtest11/djtest11.sock
设置socket的权限
chmod-socket = 666
退出的时候是否清理环境
vacuum = true
然后使用命令看下是否还能启动这个项目。
uwsgi uwsgi.ini
Ctrl + C
退出运行
重要:采用下面命令,让uwsgi在后台运行
uwsgi --ini uwsgi.ini --daemonize uwsgi.log
三、安装nginx
nginx是一个web服务器。用来加载静态文件和接收http请求的。
sudo apt install nginx
nginx
常用命令:- 启动nginx:
nginx
- 关闭nginx:
nginx -s stop
- 启动nginx:
- 重启nginx:
nginx -s reload
四、编写nginx配置文件
在/etc/nginx/conf.d目录下,新建一个文件,叫做djtest11.conf,然后将以下代码粘贴进去。
upstream djtest11 {server unix:///home/hadoop/djtest11/djtest11.sock;
}
配置服务器
server {# 监听的端口号listen 80;# 域名,根据情况修改server_name djtest11.iruiyi.net;charset utf-8;# 最大的文件上传尺寸client_max_body_size 75M;# 静态文件访问的urllocation /static {# 静态文件地址alias /home/hadoop/djtest11/static;}# 最后,发送所有非静态文件请求到django服务器location / {uwsgi_pass djtest11;# uwsgi_params文件地址include /etc/nginx/uwsgi_params;}
}
如果uwsgi是运行在root用户下,也需要修改nginx运行在root用户下
vi /etc/nginx/nginx.conf
###user nginx;
user root;
重启nginx:
service nginx restart
五、测试
http://djtest11.iruiyi.net
https://sqw.iruiyi.net
六、注意事项:
生产环境,要将django的DEBUG改为false
DEBUG = false