CentOS部署Nginx+uWSGI+Flask+Https

1. 添加一个用户

[root@localhost ~]# cat /etc/redhat-release # 查看linux版本
CentOS Linux release 7.7.1908 (Core)

[root@localhost ~]# adduser flask # 添加用户
[root@localhost ~]# passwd flask # 设置密码

[root@localhost ~]# gpasswd -a flask wheel # 将flask加入wheel组
[root@localhost ~]# sudo -iu flask # 切换到flask用户

2. 初始化环境

sudo yum install epel-release # 初始化仓库
sudo yum install gcc nginx # 安装gcc和nginx

3. 安装anaconda

下载地址:https://www.anaconda.com/products/individual

wget https://repo.anaconda.com/archive/Anaconda3-2020.02-Linux-x86_64.sh # 下载anaconda
sh Anaconda3-2020.02-Linux-x86_64.sh # 安装anaconda

按默认设置安装即可,可以简单归纳就是“yes 回车 yes”!

4. 创建python虚拟环境

source anaconda3/bin/activate # 激活anaconda
pip install virtualenv # 安装virtualenv
mkdir ~/myweb # 创建目录
cd ~/myweb
virtualenv myweb # 创建虚拟环境目录
source myweb/bin/activate # 激活新建的虚拟环境

5. 安装flask和配置uwsgi

pip install uwsgi flask # 安装flask和uwsgi
sudo firewall-cmd --permanent --add-port=5000/tcp # 打开防火墙端口,请根据实际打开相应端口
sudo firewall-cmd --reload # 应用防火墙设置

将flask项目复制到myweb目录中,先执行 python manage.py runserver 测试是否可以正常运行,之后在项目目录(myweb)下建立uwsgi配置文件(myweb.ini),并粘贴以下内容。

vi myweb.ini

[uwsgi]
module=manage:app
master=true
processes=2
threads=50
#指向网站目录
chdir=/data/www/myweb/
socket = /data/www/myweb/uwsgi.sock
socket = 127.0.0.1:8000
logto = /data/www/myweb/uwsgi.log
chmod-socket = 660
vacuum = true

文件参数说明如下:

  • module:manage 是项目启动文件 manage.py 去掉扩展名,app 是 manage.py 文件中的变量 app,即 Flask 实例。
  • processes 启动的服务占用2个进程。
  • socket此处包含两个,一个是指定了暴露的端口,另外指定了一个myproject.sock文件保存socker信息。
  • chdir是项目路径地址。
  • logto是日志输出地址。
    设置完成后回到命令行,使用以下命令启动一个uwsgi服务器
    uwsgi --ini myweb.ini
6. 创建自启动Systemd配置

sudo vi /etc/systemd/system/myweb.service

输入下面内容

[Unit]
Description=uWSGI instance to serve Myweb
After=network.target

[Service]
User=flask
WorkingDirectory=/data/www/myweb
Environment="PATH=/data/www/myweb/bin"
ExecStart=/data/www/myweb/bin/uwsgi --ini /data/www/myweb/myweb.ini
Restart=always
Type=notify
NotifyAccess=all
StandardError=syslog
KillSignal=SIGQUIT

[Install]
WantedBy=multi-user.target

sudo systemctl start myweb.service # 启动服务
sudo systemctl enable myweb.service # 开机自启动

7. 安装配置Nginx

yum install nginx
cd /etc/nginx # 进入nginx配置目录
mv nginx.conf nginx.conf_bak # 备份原配置文件
vi nginx.conf # 新建nginx.conf 文件

输入如下内容

error_log /var/log/nginx/error.log;
worker_processes 4;
events { worker_connections 1024; }
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    server {
        listen 9000;
        location / {
            include /etc/nginx/uwsgi_params;
            uwsgi_pass 127.0.0.1:8000;
        }
    }
}

systemctl start nginx # 运行Nginx

如果出现下面错误,可能是http允许访问的端口未设置,需要使用semanage进行配置

[emerg] bind() to 0.0.0.0:9000 failed (13: Permission denied)

安装semanage,直接yum install semanage会报No package semanage available.
执行下面命令:

yum provides semanage

再执行下面命令完成semanage的安装

yum -y install policycoreutils-python.x86_64

使用semanage查看端口

semanage port -l | grep http_port_t

如果Nginx绑定的端口不在其中,使用下面命令添加

semanage port -a -t http_port_t -p tcp 9000

打开防火墙端口

firewall-cmd --add-port 9000/tcp

此时访问网址 http://127.0.0.1:9000 出现502错误,查看日志

cat /var/log/nginx/error.log 
2020/12/17 15:28:41 [crit] 44387#0: *4 connect() to 127.0.0.1:8000 failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", upstream: "uwsgi://127.0.0.1:8000", host: "127.0.0.1:9000"

setsebool -P httpd_can_network_connect 1

执行上面命令后,网站终于可以正常访问了。
最后设置Nginx开机自启动

systemctl enable nginx # 设置Nginx开机自启动

8. 安装 certbot 并获取Let's Encrypt 的免费证书

yum install certbot python2-certbot-nginx

在使用certbot获取证书前,需要做几件事:

  • 将域名指向服务器
  • 将网站端口改为80(实现证书服务器的反向验证)
  • 打开防火墙的http和https服务端口
firewall-cmd --add-service=http --permanent
firewall-cmd --add-service=https --permanent
firewall-cmd --reload
  • Nginx.conf进行简单的设置

nano /etc/nginx/nginx.conf

error_log /var/log/nginx/error.log;
worker_processes 4;
events { worker_connections 1024; }
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    server {
    server_name xxx.xxx.com;    # 可以设置服务器域名了
        listen 80;
        location / {
            include /etc/nginx/uwsgi_params;
            uwsgi_pass 127.0.0.1:8000;
        }
    location ~ /.well-known {
            allow all;
    }    
    }
}

配置好上面内容后,执行下面命令开始获取证书

certbot --nginx -d xxx.xxx.com

当看到 Congratulations 的提示时,表示证书生成成功,并告知证书放在 /etc/letsencrypt/live 目录下

再次修改/etc/nginx/nginx.conf

error_log /var/log/nginx/error.log;
worker_processes 4;
events { worker_connections 1024; }
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    server {
    server_name xxx.xxx.com;
        listen 443 ssl;
        ssl_certificate "/etc/letsencrypt/live/xxx.xxx.com/fullchain.pem"; #cert
        ssl_certificate_key "/etc/letsencrypt/live/xxx.xxx.com/privkey.pem"; #KEY
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;

    location / {
            include /etc/nginx/uwsgi_params;
            uwsgi_pass 127.0.0.1:8000;
        }
    location ~ /.well-known {
            allow all;
    }    
    }
 
    server {
    server_name xxx.xxx.com;
        listen 80;

    location ~ /.well-known {
            allow all;
    }
    location / {
        rewrite ^(.*) https://$host$1 permanent;
    }    
    }

}

测试并重启 nginx:

nginx -t
nginx -s reload
9. Let's Encrypt 证书自动更新

使用下面命令进行证书虚拟更新

certbot renew --dry-run

如果一切正常就可以设置cron任务了

30 4 * * 1 certbot renew --renew-hook "systemctl restart nginx" --quiet > /dev/null 2>&1 &

部署过程到此结束,谢谢观看!

你可能感兴趣的:(CentOS部署Nginx+uWSGI+Flask+Https)