所在位置:
- 首页
- 正文
centos下通过nginx+redis+supervisor+mysql+gunicorn配置Flask网站
我用 nginx + gunicorn + centos 部署 flask 项目的时候遇到了一些问题,在这里做一些分享和总结,希望对大家有帮助
安装常用的包
yum -y update
yum install -y epel-release
yum install -y libffi-devel
yum install -y openssl-devel
yum install -y python-devel mysql-devel python-setuptools
yum install -y gcc
关闭centos 7的防火墙
systemctl status firewalld.service # 查看防火墙状态
systemctl stop firewalld.service # 关闭防火墙
systemctl disable firewalld.service # 禁止防火墙服务器
把代码上传到服务器
scp dir.tar.gz root@ip:/home/user/
安装 redis
yum install -y redis
安装 git
yum install -y git
安装 python-pip
yum install -y python-pip
安装配置 gunicorn
pip install gunicorn==19.10.0
- 在项目根目录 /home/user/project 下新建一个 gunicorn.py 文件,内容如下
import multiprocessing
bind = '127.0.0.1:8000'
workers = multiprocessing.cpu_count() * 2 + 1
# workers = 2
pidfile = '/tmp/blog.pid'
logfile = '/var/log/gunicorn/blog.log'
安装配置 supervisor
pip install supervisor
- 生成配置文件,且放在/etc目录下
echo_supervisord_conf > /etc/supervisord.conf
- 新建文件夹,用来放置配置文件
mkdir /etc/supervisord.d/
- 修改配置文件
vim /etc/supervisord.conf
- 加入以下配置信息
[include]
files = /etc/supervisord.d/*.ini
- 在 /etc/supervisor/con.d 目录下新建一个app的配置文件 app.ini
[program:wsgi]
command=/usr/bin/gunicorn -c /home/user/project/gunicorn.py wsgi:app --user=root
directory=/home/user/project
priority=2
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/home/user/project/app.log
stderr_logfile=/home/user/project/app.err
- 在 /etc/supervisor/con.d 目录下新建一个nginx的配置文件 nginx
[program:nginx]
command=sudo /usr/sbin/nginx
priority=1
startsecs=1
startretries=3
autorestart=true
autostar=true
redirect_stderr=true
pidfile=/tmp/nginx.pid
stdout_logfile=/etc/nginx/nginx.log
stderr_logfile=/etc/nginx/nginx.err
- 在 /etc/supervisor/con.d 目录下新建一个redis的配置文件 redis
[program:redis]
command=sudo /usr/bin/redis-server
priority=1
autostar=true
startsecs=1
startretries=3
autorestart=true
redirect_stderr=true
pidfile=/tmp/redis.pid
stdout_logfile=/etc/redis.log
stderr_logfile=/etc/redis.err
- 启动 supervisord
supervisord -c /etc/supervisord.conf
supervisorctl
安装 nginx
yum install -y nginx
systemctl start nginx.service # 开启nginx服务
systemctl enable nginx.service # 跟随系统启动
- 配置反向代理,在 /etc/nginx/conf.d/ 里新建一个 blog.conf 文件,内容如下
server {
listen 80;
server_name www.52gvim.com 52gvim.com;
location /static/ {
alias /home/user/project/app/static/;
}
location / {
proxy_pass http://127.0.0.1:8000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
安装 mysql
yum install mariadb-server mariadb
systemctl start mariadb # 开启 mysql 服务
systemctl enable mariadb # 跟随系统启动
mysql_secure_installation # 初始化 MySQL 安全策略
- 创建数据库
CREATE DATABASE [IF NOT EXISTS] my_blog DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
- 导入数据库
mysql > use my_blog;
mysql > source my_blog.sql;
- 修改数据库 root 的密码
update mysql.user set password = password('password') where user = 'root';
flush privileges;
安装必需的 pip 包
pip install mysql-python
pip install virtualenv
pip install --upgrade pip
pip install --upgrade setuptools
pip install -r requirement.txt
【下一篇】scrapy爬虫框架的安装和基本使用