centos7 部署完整的Django项目(python + mysql + redis + Django +uwsgi + Nginx )

下面是从一个全新的centos系统中部署项目的全部过程

# 配置阿里yum源
yum install -y yum-utils device-mapper-persistent-data lvm2 git
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

yum update -y
yum -y groupinstall "Development tools"
yum install openssl openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel gcc mariadb-devel

cd /usr/local
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
tar -zxvf Python-3.7.0.tgz
cd Python-3.7.0
./configure --prefix=/usr/local/python3
make && make install
ln -s /usr/local/python3/bin/python3.7 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.7 /usr/bin/pip3
pip3 install virtualenv
ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv
mkdir -p /data/env
mkdir -p /data/wwwroot
cd /data/env
virtualenv --python=/usr/bin/python3 kcnet
pip3 install uwsgi
ln -s /usr/local/python3/bin/uwsgi/usr/bin/uwsgi
cd /data/env/kcnet/bin
source activate
# 将项目搬入
pip3 install uwsgi
pip3 install -r request.txt
# 安装MySQL
yum -y install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
yum install -y mysql-server
systemctl start mysqld
systemctl enable mysqld
cat /var/log/mysqld.log |grep A temporary password
mysql -u root -p
alter user root@localhost identified by Pwd9876543210.;
use mysql;
update user set host = % where user = root and host=localhost;
flush privileges;
cd /data/wwwroot/kcnet/
vim kcnet.xml
<uwsgi>
    <socket>127.0.0.1:8000</socket>
    <chdir>/data/wwwroot/kcnet</chdir>
    <module>kcnet.wsgi:application</module>
    <master>true</master>
    <processes>4</processes>
    <vacuum>true</vacuum>
    <daemonize>/data/wwwroot/uwsgi.log</daemonize>
</uwsgi>


# 配置https + nginx
# 配置ssl证书流程
1、购买免费证书(买其它收费证书的没试过,可自己测试)
2、绑定域名,选择手工DNS验证
3、到域名解析处,将记录类型修改成 TXT,记录值修改成购买时提供的记录值后保存
4、签发证书后下载证书放到服务器的指定位置中
5、到域名解析处重新将记录类型修改成 A,记录值修改成自己服务器的ip

cd /home/
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.15.0.tar.gz
cd nginx-1.18.0
# Nginx安装ssl模块 
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd  /usr/local/nginx/conf/
vim nginx.conf

events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    client_max_body_size 20m;
    
     server {
        listen       443 ssl;
        server_name  你的域名;
        ssl_certificate      /usr/local/nginx/cert/cert.pem;
        ssl_certificate_key  /usr/local/nginx/cert/cert.key;
        ssl_session_timeout  5m;
        
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers  on;
        
        location / {
            include /usr/local/nginx/conf/uwsgi_params;
            uwsgi_pass 127.0.0.1:8000;    # 对应/data/wwwroot/kcnet/kcnet.xml中的socket
            uwsgi_read_timeout 60;
        }
        
        location /static/ {
            alias /data/wwwroot/kcnet/static/;
            autoindex on;
        }
        location /media/ {
            alias /data/wwwroot/kcnet/media/;
            autoindex on;
        }
        
    }
    
    server {
        listen       80;
        server_name 你的域名;
        rewrite ^(.*)$ https://$host$1 permanent;

    }
}

# 安装 Redis
cd /usr/local/
wget http://download.redis.io/releases/redis-4.0.6.tar.gz
tar -zxvf redis-4.0.6.tar.gz
cd redis-4.0.6
make && make install
将 /usr/local/redis-4.0.6/redis.conf 中的 daemonize no  修改成为 daemonize yes
mkdir /etc/redis
cp /usr/local/redis-4.0.6/redis.conf /etc/redis/6379.conf
cd /usr/local/redis-4.0.6/utils/
vi redis_init_script
# 在第一行加入如下两行注释,保存退出
# chkconfig:   2345 90 10
# description:  Redis is a persistent key-value database
# 注释的意思是,redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10。
cp /usr/local/redis-4.0.6/utils/redis_init_script /etc/init.d/redisd
cd /etc/init.d
# 设置开机启动
chkconfig redisd on
# 到此已经完全设置Redis开机启动了
# 全局启动命令 service redisd start
# 全局停止命令 service redisd stop

# 这里是手工重启,可自己在项目中写一个fabric文件代码控制启动
cd /usr/local/nginx/sbin/
./nginx
cd /usr/local/nginx/sbin/
./nginx -s reload
cd /data/env/kcnet/bin
source activate
cd /data/wwwroot/kcnet/
python manage.py collectstatice    # vue文件收集
kill -9 `pgrep uwsgi`
uwsgi -x kcnet.xml

# fabric 代码本地控制项目启动
# 在Django项目根目录下新建一个 fabfile.py 文件,依赖自己pip一下下面是代码
import getpass
from invoke import task
from fabric import Connection


@task
def prod():
    c = Connection(host=你的ip, user=root, port=22, password=你的密码)
    # 重启nginx
    with c.cd(/usr/local/nginx/sbin/):
        c.run(./nginx -s reload)
    # 重启uwsgi
    with c.cd(/data/wwwroot/kcnet/):
        c.run(workon kcnet)
        c.run(kill -9 `pgrep uwsgi`)  # 杀掉uwsgi进程
        c.run(/data/env/kcnet/bin/uwsgi -x kcnet.xml)  # 重启

 

centos7 部署完整的Django项目(python + mysql + redis + Django +uwsgi + Nginx )

上一篇:mysql 生成随机数


下一篇:详解MySQL中LIMIT的使用方法