部署Django网站
参考文献https://blog.csdn.net/u012145252/article/details/82147440
环境检查
centos8 python3 django3.1.3
设置selinux:
将SELINUX=enforcing 修改为 SELINUX=disabled 状态。
vi /etc/selinux/config
#SELINUX=enforcing
SELINUX=disabled
修改pip源
mkdir ~/.pip
vim ~/.pip/pip.conf
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
安装Django
pip3 install Django==3.1.3
python3 -m pip install Pillow
#修改openssl版本
vim /etc/crypto-policies/back-ends/opensslcnf.config
CipherString = @SECLEVEL=2:kEECDH:kRSA:kEDH:kPSK:kDHEPSK:kECDHEPSK:-aDSS:-3DES:!DES:!RC4:!RC2:!IDEA:-SEED:!eNULL:!aNULL:!MD5:-SHA384:-CAMELLIA:-ARIA:-AESCCM8
Ciphersuites = TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256:TLS_AES_128_CCM_SHA256
# MinProtocol = TLSv1.2
# 这里把1.2改成1.1
MinProtocol = TLSv1
MaxProtocol = TLSv1.1
安装nginx
yum -y install nginx
systemctl enable nginx
安装uWsgi
pip3 install uwsgi
安装成功后测试是否成功
在项目目录下
python manage.py runserver 0.0.0.0:8000
启动正常之后再测试uwsgi启动
uwsgi --http :8000 --module BaseWeb.wsgi
也正常之后可以继续下一步
将uwsgi_params文件拷贝到项目文件夹下。
uwsgi_params文件一般在/etc/nginx/目录下
在项目目录下创建myweb.conf,并在/etc/nginx/conf.d/下创建软连接
upstream django {
#socket用来与uwsgi连接通讯
server unix:///path/to/your/mysite/mysite.socket;
# for a file socket
#server 127.0.0.1:8083;
# for a web port socket (we‘ll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name luffy.tielemao.com;
# substitute your machine‘s IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/BaseWeb/media;
# your Django project‘s media files - amend as required
}
location /static {
alias /home/BaseWeb/static;
# your Django project‘s static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/BaseWeb/uwsgi_params;
# the uwsgi_params file you installed
}
}
创建软连接
cd /etc/nginx/conf.d/
ln -s /home/BaseWeb/myweb.conf myweb.conf
django部署static文件
在项目文件夹下,建立好静态文件目录:
mkdir static
在项目文件夹下,建立一个media媒体文件目录(用于存放图片音乐等文件做测试)
mkdir media
拷贝一个图片到media中后面有用
django的setting文件中,添加一行:
STATIC_ROOT = os.path.join(BASE_DIR, “static/”)
运行
python manage.py collectstatic
重新加载nginx进行测试
先进行检测,看之前的配置文件有无错误。sudo /usr/sbin/nginx -t
重新加载nginx让软链接的myweb.conf生效。sudo /usr/sbin/nginx -s reload
http://192.168.111.133:8000/media/long.png
上传Django网站文件
uwsgi --socket /home/BaseWeb/BaseWeb.sock --wsgi-file test-uwsgi.py
制作uwsgi.ini文件
# uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
# 指定运行目录,其实就是项目的根目录
chdir = /home/BaseWeb
# Django‘s wsgi file
# 导入django项目的wsgi模块
module = BaseWeb.wsgi:application
# 载入wsgi-file
wsgi-file = BaseWeb/wsgi.py
# the virtualenv (full path)
# 配置虚拟环境python相关依赖包所在路径
#home = /home/operation/work/py3env
# 补充,uwsgi启动时的用户和用户组,注意要按你自己的实际权限进行配置
# 我这边是因为项目属主是operation,而operation我设置了添加进了nginx组
# 但这种情况下uid仍不能设置nginx,除非你的项目目录所有文件属主都改为了nginx
uid = root
gid = nginx
# process-related settings
# 开启master主进程
master = true
# maximum number of worker processes
# 开启多少个进程数,workers项也等同processes
# threads项则是设置运行线程,测试倒不用设置上线程
processes = 4
# the socket (use the full path to be safe)
# 设置使用的socket端口或socket地址
# socket = 0.0.0.0:8000
# 上面的socket建议配置成一个luffy.socket文件后使用nginx来连接uWSGI运行,不然容易报socket的请求头错误和权限错误等。
socket = /home/BaseWeb/BaseWeb.socket
# ... with appropriate permissions - may be needed
# 配置生成的sock文件的权限
chmod-socket = 664
# clear environment on exit
# 退出时清空环境,其实就是将自动生成的luffy.sock和相关pid文件给干掉。
vacuum = true