Django3.0发布有段时间了,支持asgi服务器,有更高的性能。于是尝试在CentOS上部署一个高性能的django-backend。
参考:https://blog.csdn.net/qq_36288025/article/details/82534508
# yum更新yum源
yum update
# 安装Python 3.7依赖
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel libffi-devel gcc make
# 官网下载所需python版本,这里用的是3.7.7版本
wget https://www.python.org/ftp/python/3.7.7/Python-3.7.7.tgz
# 解压缩
tar -zxvf Python-3.7.0.tgz
# 编译
cd Python-3.7.0
./configure
make&&make install
# 配置默认python访问
mv /usr/bin/python /usr/bin/python.bak
ln -s /usr/local/bin/python3 /usr/bin/python
mv /usr/bin/pip /usr/bin/pip.bak
ln -s /usr/local/bin/pip3 /usr/bin/pip
修复yum
将/usr/libexec/urlgrabber-ext-down和/usr/bin/yum中的python改为python2
#! /usr/bin/python2
# A very simple external downloader
# Copyright 2011-2012 Zdenek Pavlas
# 安装supervisord
yum install epel-release
yum install -y supervisor
# 安装nginx
yum install -y pcre-devel zlib-devel
yum install -y openssl openssl-devel
yum install -y nginx
# 安装uvicorn
pip3 install uvicorn
# 安装mysql
yum install mysql-devel
pip3 install mysqlclient
# 安装redis
yum install -y redis
# 安装项目需求库
pip3 install django==3.0.5 django_redis djangorestframework
指定mysql 版本安装的方法,上面的步骤成功或者对版本没需求的可以忽略:
首先去官网下载你需要版本的mysql yum repo
然后使用repo文件更新yum缓存
rpm -ivh mysql80-community-release-el7-3.noarch.rpm
yum clean all
yum makecahe
查看mysql版本
yum repolist all | grep mysql
安装
yum install mysql-community-server
# /etc/my.cnf
[mysqld]
character-set-server=utf8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
max_allowed_packet=500M
生成配置文件
echo_supervisord_conf > /etc/supervisord.conf
修改配置文件
[inet_http_server]
port=0:9001
username=xxxx
password=yyyy
[program: nginx]
command=/usr/sbin/nginx -g 'daemon off;'
directory=/home/appname;
autorestart=true ;
autostart=true ;
stderr_logfile=/home/appname/logs/nginx/error.log ;
stdout_logfile=/home/appname/logs/nginx/stdout.log ;
user=root ;
stopsignal=INT
startsecs=10 ;
startretries=5 ;
stopasgroup=true
[fcgi-program:uvicorn]
directory=/home/appname
socket=unix:///tmp/appname.sock
command=uvicorn --fd 0 --access-log AppName.asgi:application
numprocs=4
process_name=uvicorn-%(process_num)d
stderr_logfile=/home/appname/logs/uvicorn/error.log ;
stdout_logfile=/home/appname/logs/uvicorn/stdout.log ;
注意这里要生成一下日志目录和文件
# mywebsite.conf
upstream myserver {
server unix:/tmp/appname.sock fail_timeout=0;
}
server {
listen 80;
server_name appname.soulcode.cn;
charset utf-8;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
proxy_buffering off;
proxy_pass http://myserver;
}
location /static {
alias /home/appname/static;
}
location /media {
alias /home/appname/media;
}
}
supervisord -c /etc/supervisord.conf
这里如果报502,大概率是权限问题
chmod 777 /tmp/appname.sock