Only Mookrs

A fugitive from the world.

在 Ubuntu 16.04 上配置 Flask + Nginx + Gunicorn + Supervisor

仅供个人记录,附带了在另一台 Debian 8.0 x86_64 minimal 机器上的一些操作。

Initial Server Setup with Ubuntu 16.04

SSH 登录

'ssh <user>@<ip> -p <port>'

修改 root 密码

passwd root

添加新账户

useradd <user>
passwd <user>
usermod -aG sudo <user>

新建用户目录

mkdir /home/<user>
chown -R <user> /home/<user>

替换 shbash

chsh -s /bin/bash <user>

以上三步一起完成

useradd --create-home --user-group --shell /bin/bash <user>

修改 SSH 端口,禁用 root 登录,禁用密码登录

sudo vi /etc/ssh/sshd_config

找到并修改 Port <数字>PermitRootLogin noPasswordAuthentication no

SSH-Key 登录,本地

ssh-keygen
cat ~/.ssh/id_rsa.pub

服务器

mkdir ~/.ssh
chmod 700 ~/.ssh
vi ~/.ssh/authorized_keys

复制公钥进去

chmod 600 ~/.ssh/authorized_keys

sudo systemctl reload sshd(或 /etc/init.d/ssh restart

防火墙

sudo ufw app list
sudo ufw enable
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw allow <port>

设置语系

sudo locale-gen zh_CN.UTF-8

设置时区(可选)

timedatectl list-timezones
sudo timedatectl set-timezone Asia/Shanghai
timedatectl

修改服务器 hostname,需要编辑 /etc/hostname/etc/hosts 两个文件,/etc/hosts 会提示 sudo: unable to resolve host (none)

编译安装 Python(可选,Debian;Ubuntu 自带 Python 3):

wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tar.xz
tar xJf ./Python-3.5.2.tar.xz
cd ./Python-3.5.2
./configure --prefix=/opt/python3.5
make && sudo make install
sudo ln -fs /opt/python3.5/bin/python3 /usr/bin/python3.5

如果 缺少编译组件,提示 configure: error: no acceptable C compiler found in $PATH

sudo apt-get install build-essential

报错 Ignoring ensurepip failure: pip 8.1.1 requires SSL/TLS

sudo apt-get install libssl-dev openssl

Debian 上编译完的 Python 缺少 sqlite,提示 ImportError: No module named '_sqlite3'

sudo apt-get install libsqlite3-dev

安装 pip(Python 3 是 python3-pip

sudo apt-get install python-pip

安装 virtualenv

sudo pip install virtualenv

安装虚拟环境

virtualenv -p python3 venv

Debian 上是

virtualenv -p python3.5 venv

完整命令实际是

virtualenv --python=<python path> <venv>

How To Install Linux, Nginx, MySQL, PHP (LEMP stack) in Ubuntu 16.04

安装 Nginx

sudo apt-get install nginx
sudo service nginx stop
sudo service nginx start
sudo service nginx restart

配置 Nginx

How To Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu 14.04

How To Set Up uWSGI and Nginx to Serve Python Apps on Ubuntu 14.04

How To Serve Flask Applications with Gunicorn and Nginx on Ubuntu 16.04

sudo vi /etc/nginx/sites-available/<project>
sudo ln -s /etc/nginx/sites-available/<project> /etc/nginx/sites-enabled
sudo nginx -t
sudo service nginx restart

Nginx 示例配置

server {
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /static {
alias /home/<user>/<project>/<app>/static/;
}
}

安装 MySQL

sudo apt-get install mysql-server
sudo mysql_secure_installation

配置 MySQL

mysql -u root -p
CREATE DATABASE myproject CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER myprojectuser@localhost IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON myproject.* TO myprojectuser@localhost;
FLUSH PRIVILEGES;
exit

安装 Gunicorn

(venv) pip install gunicorn

wsgi.py 中有 Flask 实例 app,测试一下

(venv) gunicorn -w 4 -b 127.0.0.1:8000 wsgi:app

结束运行

ps aux | grep gunicorn
kill -9 <pid>

How to Deploy Python WSGI Apps Using Gunicorn HTTP Server Behind Nginx

Gunicorn 配置文件的写法参考

gunicorn.conf 配置文件

import os
import multiprocessing
path_of_current_dir = os.path.dirname(os.path.abspath(__file__))
workers = multiprocessing.cpu_count() * 2 + 1
proc_name = 'gunicorn.<project>'
loglevel = 'info'
pidfile = '{}/run/gunicorn.pid'.format(path_of_current_dir)
accesslog = '{}/logs/gunicorn_acc.log'.format(path_of_current_dir)
errorlog = '{}/logs/gunicorn_err.log'.format(path_of_current_dir)
bind = '127.0.0.1:8000'

python web 部署:nginx + gunicorn + supervisor + flask 部署笔记

在阿里云 CentOS7 中配置基于 Nginx + Supervisor + Gunicorn 的 Flask 项目

Flask Gunicorn Supervisor Nginx 项目部署小总结

Monitoring Processes with Supervisord

配置 Supervisor(不支持 Python 3,安装在虚拟环境以外)

sudo apt-get install supervisor
echo_supervisord_conf > supervisor.conf

编辑 supervisor.conf,在末尾添加如下内容(Supervisor Configuration File

[program:<project>]
command=/home/<user>/<project>/venv/bin/gunicorn -c /home/<user>/<appname>/gunicorn.conf wsgi:app
directory=/home/<user>/<project>
user=<user>
stdout_logfile=/home/<user>/<project>/logs/supervisor_out.log
redirect_stderr=true

Supervisor 的基本命令

supervisord -c supervisor.conf
supervisorctl -c supervisor.conf status
supervisorctl -c supervisor.conf reload
supervisorctl -c supervisor.conf start [all]|[project]
supervisorctl -c supervisor.conf stop [all]|[project]

也可以把 Supervisor 的配置文件放在 /etc/supervisor/conf.d/,这样就不用每次都运行 supervisord 加载配置文件了

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start [all]|[project]

Kickstarting Flask on Ubuntu - Setup and Deployment

How To Set Up Automatic Deployment with Git with a VPS

基于Flask-Angular的项目组网架构与部署

Comments