refactor(移除生产部署文件)
This commit is contained in:
@@ -113,141 +113,8 @@ flask db upgrade
|
||||
flask init-db
|
||||
```
|
||||
|
||||
+ 项目启动
|
||||
|
||||
## 服务器部署
|
||||
### wsgi
|
||||
默认的 flask 程序启动一个应用,一个应用只能用于测试环境下。生成环境采用 gunicorn 开启多进行+多线程启动程序,可以提升程序的并发量。详细配置可以查看 gunicorn 的官方文档,本项目的配置文件请查看 `gunicorn.conf.py` 。
|
||||
|
||||
```python
|
||||
# filename: gunicorn.conf.py
|
||||
import os
|
||||
import multiprocessing
|
||||
|
||||
bind = '0.0.0.0:8000' # 默认部署地址,如果用了nginx的反向代理,建议改成 127.0.0.1:8000
|
||||
backlog = 512
|
||||
chdir = os.path.dirname(os.path.abspath(__file__))
|
||||
timeout = 30
|
||||
worker_class = 'sync'
|
||||
|
||||
workers = multiprocessing.cpu_count() * 2 + 1 # 开启进程数为 CPU核心数 * 2 + 1
|
||||
threads = 2 # 每个进程开启两个线程
|
||||
loglevel = 'info' # 日志的等级
|
||||
access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"'
|
||||
# 日志存放位置
|
||||
if not os.path.exists('logs'):
|
||||
os.mkdir('logs')
|
||||
|
||||
accesslog = os.path.join(chdir, "logs/gunicorn_access.log")
|
||||
errorlog = os.path.join(chdir, "logs/gunicorn_error.log")
|
||||
|
||||
```
|
||||
|
||||
因为是在服务器部署,而且一般做服务器部署的时候都会采用虚拟环境 + 命令行启动。为了能在命令行模式下从虚拟环境启动程序,所以需要专门编写一个 shell 文件进行启动。详细看看 `start.sh`
|
||||
|
||||
```shell
|
||||
cd /home/ubuntu/pear-admin-flask # 进入到项目的根目录
|
||||
source venv/bin/activate # 激活虚拟环境
|
||||
exec gunicorn -c gunicorn.conf.py "applications:create_app('development')" # 运行 gunicorn 指令启动程序
|
||||
```
|
||||
|
||||
注意:如果使用的是全局环境,就可以不用激活虚拟环境。
|
||||
|
||||
|
||||
|
||||
### 守护进程
|
||||
|
||||
程序在服务器环境下,一般是 7*24 小时不间断运行。有时候服务器会因为一些特殊原因宕机自动重启,或者是晚上定时重启电脑让内存处于最佳状态下。在这种情况下如果想要只要是电脑在正常运行,程序就提供服务,就需要开启守护进程。
|
||||
|
||||
|
||||
|
||||
使用下面的命令安装Supervisor:
|
||||
```shell script
|
||||
$ sudo apt install supervisor
|
||||
```
|
||||
|
||||
编辑配置文件
|
||||
|
||||
```shell
|
||||
$ sudo vim /etc/supervisor/conf.d/pear.conf
|
||||
```
|
||||
|
||||
写入项目配置
|
||||
```shell script
|
||||
[program:pear]
|
||||
command=bash /home/ubuntu/pear-admin-flask/start.sh
|
||||
user=root
|
||||
autostart=true
|
||||
autorestart=true
|
||||
stopasgroup=true
|
||||
killasgroup=true
|
||||
```
|
||||
|
||||
管理项目进程
|
||||
```shell script
|
||||
$ sudo supervisorctl
|
||||
```
|
||||
|
||||
|
||||
|
||||
注意:关于 supervisor 的具体功法参考 [官方文档](http://supervisord.org/index.html)
|
||||
|
||||
|
||||
|
||||
### Nginx
|
||||
|
||||
gunicorn 虽然提供了 Web服务器的功能,但是功能比较局限,拓展性也不强。而 Nginx 就能很好的弥补这部分的不足。所以在服务器一般采用 Nginx + gunicorn 的模式做项目部署。
|
||||
|
||||
Nginx 是一个高性能的 HTTP 和 反向代理 web服务器。在后期的项目性能优化中,可以提供非常多的帮助。例如可以对返回的网页、静态文件进行压缩提升页面的加载速度,当性能达到瓶颈之后可以通过负载均衡让后端直接可以通过加机器解决问题。
|
||||
|
||||
|
||||
|
||||
使用下面的命令安装Nginx
|
||||
|
||||
```shell
|
||||
$ sudo apt install nginx
|
||||
```
|
||||
|
||||
可以直接在 Nginx 的默认配置文件(/etc/nginx/nginx.conf)中写入程序配置,但通常情况下,为了便于组织,我们可以在/etc/nginx/sites-enabled/或是/etc/nginx/conf.d/目录下为我们的Flask程序创建单独的Nginx配置文件。
|
||||
|
||||
```shell
|
||||
$ sudo rm /etc/nginx/sites-enabled/default # 删除默认的示例
|
||||
$ sudo vi /etc/nginx/sites-enabled/pear # 添加自己的示例
|
||||
```
|
||||
|
||||
|
||||
|
||||
> /etc/nginx/sites-enabled/pear
|
||||
|
||||
```shell
|
||||
server {
|
||||
listen 80 default_server; # 监听 80 端口
|
||||
# server_name example.com; # 域名解析
|
||||
access_log /var/log/nginx/access.log;
|
||||
error_log /var/log/nginx/error.log;
|
||||
|
||||
location / {
|
||||
proxy_pass http://127.0.0.1:8000; # 转发的地址,即Gunicorn运行的地址
|
||||
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;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
更新配置文件后,我们可以通过下面的命令来测试语法正确性:
|
||||
|
||||
```shell
|
||||
$ sudo nginx -t
|
||||
```
|
||||
|
||||
如果一切正常,那么现在可以重启Nginx让配置生效:
|
||||
|
||||
```shell
|
||||
$ sudo service nginx restart
|
||||
```
|
||||
|
||||
当使用反向代理服务器后,Gunicorn不需要再监听外部请求,而是直接监听本地机的某个端口。我们可以使用默认值,即本地机的8000端口,默认绑定 `127.0.0.1:8000` 。
|
||||
|
||||
```bash
|
||||
flask run
|
||||
```
|
||||
@@ -1,16 +1,29 @@
|
||||
import os
|
||||
|
||||
from flask import Flask
|
||||
from flask import Flask, Blueprint
|
||||
from common.flask_uploads import configure_uploads
|
||||
|
||||
from common.utils.upload import photos
|
||||
from extensions import init_plugs
|
||||
from applications.view import init_view
|
||||
from applications.api import init_api
|
||||
import config
|
||||
|
||||
api_bp: Blueprint = Blueprint('api', __name__, url_prefix='/api/v1')
|
||||
|
||||
def create_app():
|
||||
from .rights import register_rights_api
|
||||
from .system import register_system_api
|
||||
from .users import register_users_api
|
||||
|
||||
register_rights_api(api_bp)
|
||||
register_users_api(api_bp)
|
||||
register_system_api(api_bp)
|
||||
|
||||
|
||||
def init_api(app: Flask) -> None:
|
||||
app.register_blueprint(api_bp)
|
||||
|
||||
|
||||
def create_app() -> Flask:
|
||||
app = Flask('pear-admin-flask')
|
||||
|
||||
# 引入数据库配置
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
from flask import Blueprint, Flask
|
||||
|
||||
api_bp = Blueprint('api', __name__, url_prefix='/api/v1')
|
||||
|
||||
from .rights import register_rights_api
|
||||
from .system import register_system_api
|
||||
from .users import register_users_api
|
||||
|
||||
register_rights_api(api_bp)
|
||||
register_users_api(api_bp)
|
||||
register_system_api(api_bp)
|
||||
|
||||
|
||||
def init_api(app: Flask):
|
||||
app.register_blueprint(api_bp)
|
||||
@@ -1,19 +0,0 @@
|
||||
import os
|
||||
import multiprocessing
|
||||
|
||||
bind = '0.0.0.0:8000'
|
||||
backlog = 512
|
||||
chdir = os.path.dirname(os.path.abspath(__file__))
|
||||
timeout = 30
|
||||
worker_class = 'sync'
|
||||
|
||||
workers = multiprocessing.cpu_count() * 2 + 1
|
||||
threads = 2
|
||||
loglevel = 'info'
|
||||
access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"'
|
||||
|
||||
if not os.path.exists('logs'):
|
||||
os.mkdir('logs')
|
||||
|
||||
accesslog = os.path.join(chdir, "logs/gunicorn_access.log")
|
||||
errorlog = os.path.join(chdir, "logs/gunicorn_error.log")
|
||||
Reference in New Issue
Block a user