删除nano

1
2
# 在终端输入删除nano编辑器的命令:
sudo apt-get remove nano

创建用户

1
2
3
4
5
6
7
8
9
sudo adduser xxxxxx

chmod -R 640 /etc/sudoers
sudo vim /etc/sudoers
chmod -R 440 /etc/sudoers

或者使用

sudo visudo

退出并用xxxxxx登录

安装宝塔

1
wget -O install.sh https://download.bt.cn/install/install-ubuntu_6.0.sh && sudo bash install.sh ed8484bec

创建代码仓库密钥

1
2
ssh-keygen -t rsa -b 2048 -C "xxxxxx@example.com"
cat .ssh/id_rsa.pub

下载/安装/配置 conda

1
2
3
4
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-py310_22.11.1-1-Linux-x86_64.sh
source ../.bashrc
conda create -n 环境名称 python=3.x

pip修改源

临时使用

1
pip install -r requestment.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/

将pip源设置为默认源

我们可以通过在用户的Home目录下创建一个pip目录,然后在pip目录下创建一个pip.conf文件来实现将新源设置为默认源:

1
2
3
4
5
mkdir ~/.pip
echo "
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
" > ~/.pip/pip.conf

以上命令将新源写到了pip.conf中,在下一次使用pip的时候,就会使用新源了。我们可以通过以下命令来验证是否设置成功:

1
more ~/.pip/pip.conf

fastapi 安装

1
2
3
4
5
6
7
cd /www/wwwroot/voucher_shubi_apis/
pip install "fastapi[all]"
pip install gunicorn
pip install pyopenssl
pip install python-jose[cryptography]
pip install uvicorn[standard]
pip install uvicorn-worker

其它运营项目安装

1
2
3
sudo apt-get install supervisor
sudo apt-get install redis-server
sudo apt-get install gunicorn

Supervisor 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
sudo vim /etc/supervisor/conf.d/voucher.conf

; 应用部署设置名称
[program: xxx_8000]
; fastapi 使用gunicorn + uvicorn组合运行
command=/home/tonytan/miniconda3/envs/fastapi/bin/gunicorn main:app -w 4 --worker-class uvicorn.workers.UvicornWorker -b 0.0.0.0:8000
; fastapi 使用gunicorn配置文件调用来运行
command=/home/tonytan/miniconda3/envs/fastapi/bin/gunicorn main:app -c gunicorn.conf
; django 使用gunicorn运行wsgi
command=/home/tonytan/miniconda3/envs/voucher/bin/gunicorn -w 4 -b 0.0.0.0:8000 xxxx_admin.wsgi
; django 使用gunicorn + uvicorn + uvicorn-worker组合运行asgi( 需安装 pip install uvicorn uvicorn-worker )
command=/home/tonytan/miniconda3/envs/voucher/bin/gunicorn -w 4 -t 120 -b 0.0.0.0:8000 xxxx_admin.asgi:application -k uvicorn_worker.UvicornWorker
; django 使用gunicorn + uvicorn组合运行asgi
command=/home/tonytan/miniconda3/envs/voucher/bin/gunicorn -w 4 -t 120 -b 0.0.0.0:8000 xxxx_admin.asgi:application -k uvicorn.workers.UvicornWorker
directory=/www/wwwroot/activity_voucher_apis
user = root
redirect_stderr=true
stdout_logfile = /var/log/supervisor/stdout.log
stderr_logfile = /var/log/supervisor/stderr.log
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8

; huey异步任务部署
[program:voucher_huey]
command=/home/tonytan/miniconda3/envs/voucher/bin/python3 manage.py run_huey -w 4
directory = /www/wwwroot/xxxx_admin
user = root
redirect_stderr=true
stdout_logfile = /var/log/supervisor/voucher_stdout.log
stderr_logfile = /var/log/supervisor/voucher_stderr.log
environment=DOMAIN=pro

gunicorn.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 并行工作进程数
workers = 4
# 指定每个工作者的线程数
threads = 2
# 监听内网端口5000
bind = '127.0.0.1:5000'
# 设置守护进程,将进程交给supervisor管理
daemon = 'false'
# 工作模式协程
worker_class = 'gevent'
# 设置最大并发量
worker_connections = 2000
# 设置进程文件目录
pidfile = '/var/run/gunicorn.pid'
# 设置访问日志和错误信息日志路径
accesslog = '/var/log/gunicorn_acess.log'
errorlog = '/var/log/gunicorn_error.log'
# 设置日志记录水平
loglevel = 'warning'

Nginx配置

wenzhou.trust-will.com

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
upstream apis {
least_conn;
server 127.0.0.1:8000;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
server 127.0.0.1:8003;
}

location / {
proxy_pass http://apis;
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;
}

文档配置

Gitbook安装(django)

npm 版本尽量不要选择最新版,按照管理工具 n

命令行输入 n, 列出当前有的版本,选择一个不是最新的

初始化

1
gitbook init

docs 不能访问问题(fastapi)

1
2
3
4
5
6
7
8
9
10
11
12
sudo vim /home/tonytan/miniconda3/envs/fastapi/lib/python3.9/site-packages/fastapi/openapi/docs.py

注释20·22行
增加:
swagger_js_url: str = "/static/swagger-ui/swagger-ui-bundle.js",
swagger_css_url: str = "/static/swagger-ui/swagger-ui.css",
swagger_favicon_url: str = "/static/swagger-ui/favicon-32x32.png",

注释82-83行
增加:
redoc_js_url: str = "/static/redoc/bundles/redoc.standalone.js",
redoc_favicon_url: str = "/static/redoc/favicon.png",

文档更新同步(fastapi)

  • 本地运行程序
1
uvicorn main:app --reload
  • 浏览器打开 127.0.0.1:800/openapi.json
  • 将文件另存到 ./static/openapi.json
  • git更新提交

错误处理

admin 登录成功跳转报CSRF错误(django)

  • seetings.py 中增加 CSRF_TRUSTED_ORIGINS = ['https://www.02405.com', 'https://02405.com'], 主要要加httphttps前缀

jwt报错

  • jwt和pyjwt冲突, 要卸载jwt
1
2
3
4
5
6
7
8
# jwt卸载命令
pip uninstall jwt

# 保险起见,将PyJWT一同卸载
pip uninstall PyJWT

# 重新安装PyJWT
pip install PyJWT

针对django报错mysql得升级8的问题

Django提示mysq版本Q过低: django db. utils NotSuppotedError: MySQL 8 or later is required (found 5.7.26).
因为mysql5.7及以下是免费的数据库,8.0之后是收费的。使用免费的数据库更放心,使用上没有什么区别。这个提示只是Django的版本
检测提示,把它注释掉就好了。

在pycharm中 搜索路径 :

1
django\db\backends\base\base.py   "小提示pycharm中搜索快捷键为 Ctrl+shift+n 苹果系统除外"

把如下代码注释了就行

1
2
3
4
5
6
7
8
def init_connection_state(self):
Initialize the database connection settings.
global RAN_ DB_ VERSION_ CHECK
if self.alias not in RAN_ DB_ VERSION_ CHECK:
# self.check_database_version_supported( )
RAN_DB_VERSION_CHECK.add(self.alias )

"""就注释一句话就行"""

huey定时任务时间错误处理

huey定时任务运行的时间,如果没有处理会使用UTC时间。 比上海时间完几个小时,会导致原定于晚上的任务白天才会执行。
更新方法:

1
2
3
4
5
6
7
8
# 在django的settings.py中,配置huey

# >>>>>>>> HUEY 设置 <<<<<<<<<
from huey import RedisHuey
from redis import ConnectionPool

pool = ConnectionPool(host='localhost', port=6379, max_connections=100)
HUEY = RedisHuey(f'{API_ENV}_FENZHANG_ADMIN', connection_pool=pool, utc=False)

其中, utc=False就是让huey不用UTC时间,已系统时间为准。

之后就可以重新运行项目了