如何折腾我的Flask博客VirzzBlog

admin 2022年5月17日11:28:11评论35 views字数 5997阅读19分59秒阅读模式

简单总结一下我的博客是如何折腾的

本博客程序及环境:Nginx + MySQL + Flask + uWSGI + Supervisor + SSL

Python 環境

Python2.7

我反正是習慣用這個版本

sudo apt-get install python-dev build-essential

PIP

Python插件安裝管理工具

安裝

sudo apt-get install python-pip

更新

sudo pip install --upgrade pip

Virtualenv

Python依賴虛擬環境

安裝

sudo pip install --upgrade virtualenv

配置環境

可以本地調試或者綫上部署都可以配置一個依賴虛擬環境,防止依賴混亂

新建 網站程序 目錄

mkdir web-flask
cd web-flask

生成 虛擬環境 目錄

virtualenv venv

使用当前命令行状态进入虚拟环境

source venv/bin/activate

通過清单文件【requirements.txt】一次性安装Flask和他的相关依赖

pip install -r requirements.txt

Flask

一套基於Python的web框架,至於怎麼玩,Baidu or Google!

我是通過研究 https://github.com/isislab/CTFd 源碼來學習的

主要分四個達模塊

  • utils – 公共函數集合
  • models – 數據庫模型
  • admin – 後臺
  • view – 前臺

然後是兩個靜態文件夾

  • templates – 模板html
  • static - css、js、img等

最後是

  • __init__ – 這個不知道怎麼翻譯(意會吧)
  • config – 配置
  • manage – 本地測試啟動入口

關於 Flask 可以去看文檔,或者我以後有空再說

MySQL

也就那麼回事

sudo apt-get install mysql-server mysql-client

不同環境不同命令、自己看著辦

uWSGI

实现了WSGI协议一個web服務器。

安裝

pip install uwsgi

配置文件 uwsgi.ini

[uwsgi]
# 任意未使用的端口,也可以是文件模式
socket = 127.0.0.1:9000
# 主進程模式開啟
master = true
# 網站程序根目錄
chdir = /home/virink/flask-web 
# 模塊 manage.py
module = manage 
# 應用變量
callable = app 
# 進程數
processes = 4 
# 線程數
threads = 2 
#stats = 127.0.0.1:9191 # 狀態顯示?大概是
daemonize = /home/virink/flask-web/logs/uwsgi.log # 日誌

Flask啟動文件例子manage.py

#!/usr/bin/env python
from ICore import create_app
app = create_app()
# 一下可有可無
if __name__ == '__main__':
    app.run(debug=True, host="0.0.0.0", port=8000)

Supervisor

安裝

sudo apt-get install supervisor

配置文件

Supervisor 的全局的配置文件位置在 /etc/supervisor/supervisor.conf

我們可以在 /etc/supervisor/conf.d/ 添加一個新的 *.conf 文件

[program:flask-web]
# 启动命令入口
command=/home/virink/flask-web/venv/bin/uwsgi /home/virink/flask-web/config.ini
# 命令程序所在目录
directory=/home/virink/flask-web
#运行命令的用户名
user=web-data
# 自動啟動
autostart=true
# 自動重啟
autorestart=true
#日志地址
stdout_logfile=/home/virink/flask-web/logs/uwsgi_supervisor.log

操作

// 启动服务
sudo service supervisor start
// 终止服务
sudo service supervisor stop

停止服務之後 uwsgi 是不會關閉的,所以要更新flask的時候要 kill 了所有的 uwsgi 進程

我寫了個腳本

restart.sh

#!/bin/bash
service supervisor stop && kill -9 $(ps -e|grep uwsgi |awk '{print $1}') && service supervisor start

運行腳本就好了

chmod +x restart.sh
sudo ./restart.sh

Nginx

安裝

sudo apt-get install nginx

配置

不要去改动默认的 nginx.conf

而是修改配置文件 /etc/nginx/sites-available/default

server {
    listen  80;
    server_name domain; #公网地址

    location / {
        include      uwsgi_params;
        # 指向uwsgi 所应用的内部地址,所有请求将转发给uwsgi 处理
        uwsgi_pass   127.0.0.1:9000;  
        uwsgi_param UWSGI_PYHOME /home/virink/flask-web/venv; # 指向虚拟环境目录
        uwsgi_param UWSGI_CHDIR  /home/virink/flask-web; # 指向网站根目录
        uwsgi_param UWSGI_SCRIPT manage:app; # 指定启动程序
    }
}

然後重啟 sudo service nginx restart

提醒

如果不是改動default而是添加一個新的配置文件xxx

需要把該文件鏈接到 sites-enabled 文件夾

創建文件/etc/nginx/sites-enabled/xxx鏈接到/etc/nginx/sites-available/xxx

ln -s /etc/nginx/sites-enabled/xxx /etc/nginx/sites-available/xxx

到這裡就基本折騰好了

SSL

最近聽說 基於SSLhttps比較流行,所以....

安装 & 生成Let’s Encrypt证书

运行时保證80端口可以访问以及拥有root权限

apt-get update
apt-get -y install git
git clone https://github.com/letsencrypt/letsencrypt
cd letsencrypt
sudo ./letsencrypt-auto certonly --standalone -d yourdomain.com -d www.yourdomain.com

or

sudo apt-get install letsencrypt 
letsencrypt certonly --standalone -d yourdomain.com -d www.yourdomain.com

输入邮箱,

Agree Let’s Encrypt的TOS,

证书成功,存放位置为 /etc/letsencrypt/live/yourdomain.com/fullchain.pem

配置Nginx

修改 listen 80;listen 443 ssl;

然后添加

ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;

location / 中添加 uwsgi_param UWSGI_SCHEME https;

配置 Let’s Encrypt 自动续期

确保/etc/letsencrypt/renewal/yourdomain.com.conf文件的几个重要参数正常

email = [email protected]
tos = True

接下來的我还不会、、、Orz, 大概是利用 crontab

sudo crontab -u root -e
* * * */1 * /usr/sbin/service nginx stop;/usr/bin/letsencrypt renew;/usr/sbin/service nginx start

强制使用https访问 或者 換域名

因為大多數人是不會輸入https://www.virzz.com而是輸入http://www.virzz.com

所以有必要開一個80端口進行rewrite重定向,

另外,換域名和換目錄也可以用rewrite重定向,

我上一個 Blog 用的是 Typecho ,文章訪問格式是 www.old.com/分類/文章

而我新的 Blogwww.yourdomain.com/文章

flask_nginx

server {  
    listen  80;
    listen 443 ssl;
    server_name www.yourdomain.com;
    server_name www.old.com;
    charset     utf-8;

    # 如果不是 **www.yourdomain.com** 則跳轉到 **https://www.yourdomain.com**
    if ($host != 'www.yourdomain.com' ) {
        rewrite ^/(\w+)/(.*)$ https://www.yourdomain.com/$2 permanent;
        # ^/(\w+)/(.*)$ 匹配 目錄/文章, 取文章
        # permanent 指 301 永久重定向
    }

    # 如果不是 **https** 則跳轉到 **https://www.yourdomain.com**
    if ($scheme != 'https' ) {
        rewrite ^/(.*)$ https://www.yourdomain.com/$1 permanent;
    }

    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;

    access_log /home/virink/virzz/logs/access_log;
    error_log  /home/virink/virzz/logs/error_log;

    uwsgi_connect_timeout 300;

    location / {
        include      uwsgi_params;
        uwsgi_param UWSGI_SCHEME https; # 使用HTTPS模式
        uwsgi_pass   127.0.0.1:9000;  
        uwsgi_param UWSGI_PYHOME /home/virink/flask-web/venv;
        uwsgi_param UWSGI_CHDIR  /home/virink/flask-web;
        uwsgi_param UWSGI_SCRIPT manage:app;
    }
}

搞定、收功、、不成功的話、一起交流、、

因為我現在的方式是兩個不同的配置文件

https_flask_web

server {
    listen  443 ssl;
    server_name www.yourdomain.com;
    charset     utf-8;

    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;

    access_log /home/virink/virzz/logs/access_log;
    error_log  /home/virink/virzz/logs/error_log;

    uwsgi_connect_timeout 300;

    location / {
        include      uwsgi_params;
        uwsgi_param UWSGI_SCHEME https;
        uwsgi_pass   127.0.0.1:9000;  
        uwsgi_param UWSGI_PYHOME /home/virink/flask_web/venv;
        uwsgi_param UWSGI_CHDIR  /home/virink/flask_web;
        uwsgi_param UWSGI_SCRIPT manage:app;
    }
}

http_flask_web

server {
    listen  80;
    server_name www.yourdomain.com;
    server_name www.old.com;

    if ($host != 'www.yourdomain.com' ) {
        rewrite ^/(\w+)/(.*)$ https://www.yourdomain.com/$2 permanent;
    }
    if ($scheme != 'https' ) {
        rewrite ^/(.*)$ https://www.yourdomain.com/$1 permanent;
    }

    charset     utf-8;

    location / {
        root /var/www/;
    }

    error_page 404 https://www.virzz.com;
}

結束語

關於本文

折騰這個blog画的時間蠻久的,不過總算弄出來了。

期間不知道 百度、Google了多少回,各種文章的方法反復測試。

為了然其他像我這樣不得門而入的童鞋不走冤枉路,特此寫了篇文章。

為何用Flask

至於為什麼不用PHP是因為我想裝個逼~~PHP的MVC不好玩,也不會用框架,各種麻煩

Python開發挺好玩的吧、我加了幾個不錯的插件~~

發文章的同時、可以同步發微博和推特、並且實時推送文章鏈接送到Baidu【百度站長-鏈接提交】

關於開源

最後、暫時還不打算開源、比較開發基礎不太好、代碼淩亂沉余、真心不好意思開源污染大夥的眼睛。

真的結束了

Over

FROM : virzz.com | Author:Virink

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年5月17日11:28:11
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   如何折腾我的Flask博客VirzzBloghttp://cn-sec.com/archives/1013012.html

发表评论

匿名网友 填写信息