flask uwsgi nginx 搭建python项目

uwsgi.ini

[uwsgi]
# 指定项目执行的端口号,用nginx的时候就要配socket
socket=0.0.0.0:5030
protocol=http
# 指定项目所在位置,在使用conda虚拟环境时必须要有这一条
#/root/anaconda3/envs/layermanage/
pythonpath=/home/lizhihua/layermanage
# 指定项目的目录
chdir=/home/lizhihua/layermanage/
# 项目上wsgi.py所在的位置,与settings目录相同
wsgi-file=/home/lizhihua/layermanage/run.py
#  程序内启用的application变量名
callable = app
module = app
# 开启的进程数量
processes=4                          
threads=2
# master :允许主线程存在(true)
master=True
pidfile=/home/lizhihua/layermanage/layermanage.pid
# 日志,uwsgi无法启动时来这查看错误日志
daemonize=/home/lizhihua/layermanage/layermanage.log
buffer-size = 32768
#  当需要连接cassandra时,uwsgi无法启动服务,可以使用该选项设置
#lazy-apps = true

nginx.conf

upstream cloud {
	server 127.0.0.1:5030 max_fails=10 fail_timeout=10s;
}

server {
		listen 80 default_server;
		server_name cloud;
		access_log    /var/log/nginx/layermanage_access.log;

		error_log    /var/log/nginx/layermanage_error.log;

		client_max_body_size 75M;

		tcp_nopush      on;
		tcp_nodelay     on;

		charset utf-8;
		location / {
			proxy_pass  http://cloud;
			include /etc/nginx/uwsgi_params;
			proxy_set_header Host $host:$server_port;
			proxy_set_header X-Real-Ip $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_http_version 1.1;
			proxy_set_header Connection "0";

		}
		# 如果用到静态文件
		location /static {
			alias /home/lizhihua/layermanage/app/static/;
		}

		location ^~ /files/ {
			proxy_pass http://127.0.0.1:5030/files/;
			add_header         'Access-Control-Allow-Origin' '*';
			add_header         "Access-Control-Allow-Credentials" "true";
			add_header         "Access-Control-Allow-Headers" "*";
			add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,PATCH,OPTIONS;
			proxy_set_header   Host             $host;
			proxy_set_header   X-Real-IP        $remote_addr;
			proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
		}
}

restart.sh

#!/bin/bash
flaskconfig="production";
echo "$flaskconfig";
export FLASK_CONFIG="$flaskconfig" #升级为环境变量
source /etc/profile
. /etc/profile
source ~/.bashrc
conda activate layermanage
sleep 3s
pkill -9 -f uwsgi
echo "正在关闭Python服务"
sleep 3s
echo "正在启动Python服务"
a=`pwd`
cd /home/lizhihua/layermanage
uwsgi --ini /home/lizhihua/layermanage/uwsgi.ini
cd ${a}
echo "已完成重启操作"


定时备份日志和数据库

#!/bin/bash

#'''
#* * * * * 执行任务
#第一个* 一小时当中的第几分钟:0-59
#第二个* 一天当中的第几个小时:0-23
#第三个* 一月当中的第几天:1-31
#第四个* 一年当中的第几个月:1-12
#第五个* 一周当中的第几个星期:0-7 0,7都代表周日
#'''
#0 0 * * * sh  /home/lizhihua/layermanage/shell/cron_log.sh  #代表每天0点执行脚本 cron_log.sh

LOGDIR="/home/lizhihua/layermanage"    #当前日志所在目录
DATE=`date -d "yesterday" +"%Y%m%d%H%M%S"`
NEWDIR="/home/lizhihua/layermanage/history"   #新建文件夹history用来放旧日志
mkdir -p  ${NEWDIR}
mv ${LOGDIR}/layermanage.log   ${NEWDIR}/layermanage-${DATE}.log   #将旧日志重新以日期命名
touch /home/lizhihua/layermanage/layermanage.log

number=7
#找出需要删除的日志
delfile=`ls -l -crt  $NEWDIR/*.log | awk '{print $9 }' | head -1`

#判断现在的备份数量是否大于$number
count=`ls -l -crt  $NEWDIR/*.log | awk '{print $9 }' | wc -l`

if [ $count -gt $number ]
then
  # 删除最早生成的备份,只保留number数量的备份
  rm -rf $delfile
  #写删除文件日志
  #echo "delete $delfile" >> $backup_dir/log.txt
fi
echo "备份日志成功,保留【 $number 】个备份"

echo "开始执行 PostgreSql 数据库dbn的备份"
#保存备份个数
#number=7
#备份保存路径
backup_dir="/home/lizhihua/layermanage/dbbackup"
mkdir -p $backup_dir
nowtime=`date +"%Y%m%d%H%M%S"`
export PGPASSWORD=123456
dbname='layermanage'
host='127.0.0.1'
port=5432
username='postgres'
set timeout 1000
/usr/local/pgsql/bin/pg_dump --file "$backup_dir/$dbname-"$nowtime".dump" --host $host --port $port --username $username --dbname $dbname --verbose --blobs --encoding "UTF8"
echo "数据库 $dbname 备份结束!"
echo "删除 $number 天前的 数据库备份文件!"
find $backup_dir -name $dbname"*" -mtime +$number -exec rm -rf {} \;
#找出需要删除的备份
delfile=`ls -l -crt  $backup_dir/*.dump | awk '{print $9 }' | head -1`
#判断现在的备份数量是否大于$number
count=`ls -l -crt  $backup_dir/*.dump | awk '{print $9 }' | wc -l`

if [ $count -gt $number ]
then
  # 删除最早生成的备份,只保留number数量的备份
  rm -rf $delfile
  #写删除文件日志
  #echo "delete $delfile" >> $backup_dir/log.txt
fi

set timeout 1000
echo "【 $number 】天前的数据库备份文件删除完毕!"
echo "备份数据库成功,保留【 $number 】个备份"

#还原
#psql -h 127.0.0.1 -U un -d dbn -f pgsql-dbn-all-2021-05-31+01\:29\:20.dump

你可能感兴趣的:(python,python,flask,linux)