Skip to content

Instantly share code, notes, and snippets.

@binderclip
Last active March 22, 2023 09:17
  • Star 58 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save binderclip/f6b6f5ed4d71fa64c7c5 to your computer and use it in GitHub Desktop.
Flask Gunicorn Supervisor Nginx 项目部署小总结

Flask Gunicorn Supervisor Nginx 项目部署小总结

服务器的基本连接和配置

SSH 连接

使用公钥私钥来登陆而不是账号密码,公钥私钥需要简单的在本地生成一下。Github 的生成 SSH 公钥私钥的教程:Generating SSH keys

可能需要使用 -i 参数选择一下本地的私钥,一个示例的 ssh 连接如下:

ssh -p 20123 username@domain.com -i ~/.ssh/my_rsa

让操作服务器更安全的方法,摘自(谈谈全栈工程师):

简单来说,大体步骤有以下几点:

  1. 新建一个用户,以后都不要用 root 登录了。
  2. 使用 SSH 的名值对的登录方法,禁用用户名密码的登录方法。
  3. 禁用 root 账户通过 SSH 登录。
  4. 安装一个防火墙。
  5. 安装 Fail2Ban,杜绝字典攻击

新建用户

这里用选择这样的操作来新建一个用户,主要是强制添加了一个用户目录。然后再去设置用户的密码。

# useradd -m -d /home/myname myname
# passwd myname

添加 sudo 权限,参考 sudo - How can I add a new user as sudoer using the command line?

# adduser <username> sudo

lock 掉用户的密码,使它不能用密码。

# passwd -l <username>

更多的用户管理参考《鸟哥的 Linux 私房菜》里面的 Linux 账号管理

连接和配置数据库

在使用数据库之前需要配置下数据库。如果用的是 VPS 可能就在本机上面安装了,如果是云服务的话多是使用专门的数据库服务。下面的是连接数据库服务器并创建数据库的操作。

$ mysql --host=mysql.domain.com --user=username --password=password
mysql> CREATE DATABASE db_name CHARACTER SET utf8mb4;

参考:Connecting to the MySQL Server

发布软件的安装和配置

只有 Flask 还不够,还需要其他工具支持。

Gunicorn 的配置

(venv)$ pip install gunicorn

首先是单纯运行 Gunicorn,因为是 venv 环境,所以需要先启动 venv 才可以。

下面是启动了 5 个 wooker 的 gunicorn 程序的代码:

gunicorn simplecms:app -b localhost:8000 -w 5

如果想配置更多的运行参数参考这一篇文章

Supervisor 的配置

首先是一个配置文件,写在 /etc/supervisor/conf.d 路径下面创建 simplecms.conf 文件:

[program:simplecms]
command = /home/simplecms/simple-cms/venv/bin/gunicorn simplecms:app -b localhost:8000 -w 5
directory = /home/simplecms/simple-cms
user = simplecms

这里面因为 gunicorn 是安装在 venv 里面的,所以需要写全路径才能够启动。

添加了一些 log 输出之后的:

[program:simplecms]
command = /home/simplecms/simple-cms/venv/bin/gunicorn simplecms:app -b 0.0.0.0:8001 -w 5 --log-file /tmp/gunicorn.log
directory = /home/simplecms/simple-cms
stdout_logfile = /tmp/supervisor.log
user = simplecms

如何 log 错误信息看这里:

Nginx 的 log 信息在 /var/log/nginx/ 下的 access.logerror.log

supervisord 似乎默认是启动的,可以 ps -aux | grep supervisord 检测一下。

之后是几个常用的操作:

$ superviosrctl reload      # 重新加载配置文件
$ superviosrctl update
$ superviosrctl start xxx
$ superviosrctl stop xxx
$ superviosrctl status xxx
$ superviosrctl help        # 查看更多命令

Nginx 的配置

只是最简单的先配置出来了,之后可能还需要有更多的配置。

配置文件在 /etc/nginx/sites-available/ 下面,配置完了之后软链接一份到 /etc/nginx/sites-enabled/ghost.conf 下面。记得把默认的 default.conf 删掉以免冲突。

一个很简单的配置文件:

server {
listen 80;
server_name example.com;

location / {
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   Host      $http_host;
    proxy_pass         http://127.0.0.1:2368;
}
}

做软链接:

$ sudo ln -s /etc/nginx/sites-available/ghost.conf /etc/nginx/sites-enabled/ghost.conf

开启关闭服务有两种方法:

  1. $ sudo service nginx restart
  2. $ sudo /etc/init.d/nginx restart

参考:

其他参考

整体的部署教程:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment