Forked from binderclip/deploy-flask-gunicorn-supervisor-nginx.md
Created
February 1, 2018 05:13
-
-
Save patrickwang96/970211dcad17ef8af56bb9af5c789d34 to your computer and use it in GitHub Desktop.
Revisions
-
binderclip revised this gist
Sep 8, 2015 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -37,6 +37,12 @@ ssh -p 20123 [email protected] -i ~/.ssh/my_rsa # adduser <username> sudo ``` lock 掉用户的密码,使它不能用密码。 ``` # passwd -l <username> ``` 更多的用户管理参考《鸟哥的 Linux 私房菜》里面的 [Linux 账号管理](http://vbird.dic.ksu.edu.tw/linux_basic/0410accountmanager_2.php)。 ### 连接和配置数据库 -
binderclip revised this gist
Sep 1, 2015 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -147,9 +147,9 @@ $ sudo ln -s /etc/nginx/sites-available/ghost.conf /etc/nginx/sites-enabled/ghos 参考: - Ghost 的配置教程 [Basic nginx config](http://support.ghost.org/basic-nginx-config/) - [Deploying Gunicorn](http://docs.gunicorn.org/en/19.3/deploy.html#nginx-configuration),比较复杂的 Gunicorn 文档中的 Nginx 的配置方法 - 使用 `nginx -t` 来检测配置文件是否正确:[nginx configtest vs nginx -t - DEVGET.NET](http://devget.net/nginxapache/nginx-configtest-vs-nginx-t/) ## 其他参考 -
binderclip created this gist
Aug 31, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,168 @@ # Flask Gunicorn Supervisor Nginx 项目部署小总结 ## 服务器的基本连接和配置 ### SSH 连接 使用公钥私钥来登陆而不是账号密码,公钥私钥需要简单的在本地生成一下。Github 的生成 SSH 公钥私钥的教程:[Generating SSH keys](https://help.github.com/articles/generating-ssh-keys/) 可能需要使用 `-i` 参数选择一下本地的私钥,一个示例的 ssh 连接如下: ``` ssh -p 20123 [email protected] -i ~/.ssh/my_rsa ``` 让操作服务器更安全的方法,摘自([谈谈全栈工程师](http://read.douban.com/column/226077/)): 简单来说,大体步骤有以下几点: 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?](http://askubuntu.com/questions/7477/how-can-i-add-a-new-user-as-sudoer-using-the-command-line): ``` # adduser <username> sudo ``` 更多的用户管理参考《鸟哥的 Linux 私房菜》里面的 [Linux 账号管理](http://vbird.dic.ksu.edu.tw/linux_basic/0410accountmanager_2.php)。 ### 连接和配置数据库 在使用数据库之前需要配置下数据库。如果用的是 VPS 可能就在本机上面安装了,如果是云服务的话多是使用专门的数据库服务。下面的是连接数据库服务器并创建数据库的操作。 ``` $ mysql --host=mysql.domain.com --user=username --password=password mysql> CREATE DATABASE db_name CHARACTER SET utf8mb4; ``` 参考:[Connecting to the MySQL Server](https://dev.mysql.com/doc/refman/5.0/en/connecting.html) ## 发布软件的安装和配置 只有 Flask 还不够,还需要其他工具支持。 ### Gunicorn 的配置 ``` (venv)$ pip install gunicorn ``` 首先是单纯运行 Gunicorn,因为是 venv 环境,所以需要先启动 venv 才可以。 下面是启动了 5 个 wooker 的 gunicorn 程序的代码: ``` gunicorn simplecms:app -b localhost:8000 -w 5 ``` 如果想配置更多的运行参数参考[这一篇文章](http://www.simpleapples.com/2015/06/configure-nginx-supervisor-gunicorn-flask/)。 ### 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 错误信息看这里: - [Error Logging in Nginx+Gunicorn+Supervisor+Django - Stack Overflow](http://stackoverflow.com/questions/19076619/error-logging-in-nginxgunicornsupervisordjango) - [python - Logging in Flask on Gunicorn and Supervisor - Stack Overflow](http://stackoverflow.com/questions/28677685/logging-in-flask-on-gunicorn-and-supervisor) Nginx 的 log 信息在 `/var/log/nginx/` 下的 `access.log` 和 `error.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` 参考: - Ghost 的配置教程 [Basic nginx config](http://support.ghost.org/basic-nginx-config/) - [Deploying Gunicorn](http://docs.gunicorn.org/en/19.3/deploy.html#nginx-configuration),比较复杂的 Gunicorn 文档中的 Nginx 的配置方法 - 使用 `nginx -t` 来检测配置文件是否正确:[nginx configtest vs nginx -t - DEVGET.NET](http://devget.net/nginxapache/nginx-configtest-vs-nginx-t/) ## 其他参考 整体的部署教程: - [Flask Deployment on Linux](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvii-deployment-on-linux-even-on-the-raspberry-pi),超赞的教程。有详细的 SSH 连接配置信息、用户设置、数据库初始化和链接、Apache 的配置、程序的初始化和升级管理 - [Kickstarting Flask on Ubuntu - setup and deployment](https://realpython.com/blog/python/kickstarting-flask-on-ubuntu-setup-and-deployment/),也是比较全面,亮点在 Nginx 的配置、Supervisor 的配置、Git Hook 的实用、整个自动化部署 - [Launching your Flask web application on Linux with Gunicorn and Nginx -](http://techarena51.com/index.php/deploy-flask-on-ubuntu/),简单的一个小 Demo 的运行过程,可以参考 - [Deploy Flask App on Ubuntu(Virtualenv Gunicorn Nginx Supervisor) · defshine/flaskblog Wiki](https://github.com/defshine/flaskblog/wiki/Deploy-Flask-App-on-Ubuntu(Virtualenv-Gunicorn-Nginx-Supervisor)),一个实际 Flask Blog 项目的 Deploy 教程,所以更加的简明,项目中还有对应的配置文件可以参考 - [Setting up Django with Nginx, Gunicorn, virtualenv, supervisor and PostgreSQL](http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/),虽然是 Django 的,不过很多还是蛮类似的,写的也不错 - [在阿里云CentOS7中配置基于Nginx+Supervisor+Gunicorn的Flask项目 - simpleapples](http://www.simpleapples.com/2015/06/configure-nginx-supervisor-gunicorn-flask/),也是从什么都没有开始的配置,不过有些过于简单 - [独立 WSGI 容器](http://docs.jinkan.org/docs/flask/deploying/wsgi-standalone.html) Flask 官方文档,几种类似 gunicorn 的容器以及 Nginx 需要做的一点配置 - [部署Web App - 廖雪峰](http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014323392805925d5b69ddad514511bf0391fe2a0df2b0000):主要是 Nginx + Supervisor,讲的原理会比较清晰 - [Flask+Nginx+Gunicorn+Redis+Mysql搭建一个小站](http://codingnow.cn/server/539.html):主要是 Nginx + Gunicorn。Nginx 的配置记录的篇实际记录型的,可以参考;Gunicorn 也有一点配置的信息