uwsgi部署项目-jiligaula
uwsgi配置文件
[uwsgi] #项目目录 chdir = /usr/local/src/jiliguala # 虚拟环境 home=/usr/local/src/pyENV/jiliguala_ENV/ PYTHONHOME=/usr/local/src/pyENV/jiliguala_ENV/bin/ #启动uwsgi的用户名和用户组 uid = root gid = root #指定项目的application module = jiliguala.wsgi:application #启用主进程 master = True #进程个数 processes = 2 # 使用进程数 threads = 2 使用的线程数 harakiri = 60 # 最大超时时间 max-requests = 5000 # 最大请求数,到了后就会自动重启 #指定socket #socket = 127.0.0.1:3000 #指定http http = 0.0.0.0:8001 socket = /usr/local/src/jiliguala/uwsgi/sock/supercrm.sock #配置pid文件位置 pidfile = /usr/local/src/jiliguala/uwsgi/master.pid # 配置日志文件位置 daemonize = /var/log/uwsgi/jiliguala.log # 服务退出或重启,自动删除pid和socket文件 vacuum = True #序列化接受的内容,如果可能的话 thunder-lock = True #启用线程 enable-threads = True #设置自动中断时间 harakiri = 30 #设置缓冲 post-buffering = 4096
注:须提前配置好虚拟环境,安装uwsgi。
jiliguala.sh启动文件
#!/bin/bash # 配置项目路径(根据实际情况修改) UWSGI_PATH="/usr/local/src/pyENV/jiliguala_ENV/bin/uwsgi" # uwsgi可执行文件路径 INI_PATH="/usr/local/src/jiliguala/uwsgi/uwsgi.ini" # uwsgi配置文件路径 PID_FILE="/usr/local/src/jiliguala/uwsgi/master.pid" # 与uwsgi.ini中的pidfile一致 # 检查uwsgi是否存在 if [ ! -f "$UWSGI_PATH" ]; then echo "错误:uwsgi可执行文件不存在于 $UWSGI_PATH" exit 1 fi # 检查配置文件是否存在 if [ ! -f "$INI_PATH" ]; then echo "错误:uwsgi配置文件不存在于 $INI_PATH" exit 1 fi # 启动服务 start() { if [ -f "$PID_FILE" ] && kill -0 $(cat "$PID_FILE") 2>/dev/null; then echo "服务已在运行中(PID: $(cat "$PID_FILE"))" return 0 fi echo "启动jiliguala服务..." "$UWSGI_PATH" --ini "$INI_PATH" if [ $? -eq 0 ]; then echo "服务启动成功(PID: $(cat "$PID_FILE"))" else echo "服务启动失败,请查看日志" fi } # 停止服务 stop() { if [ ! -f "$PID_FILE" ] || ! kill -0 $(cat "$PID_FILE") 2>/dev/null; then echo "服务未在运行" return 0 fi echo "停止jiliguala服务(PID: $(cat "$PID_FILE"))..." "$UWSGI_PATH" --stop "$PID_FILE" if [ $? -eq 0 ]; then echo "服务已停止" # 清理PID文件(防止残留) if [ -f "$PID_FILE" ]; then rm -f "$PID_FILE" fi else echo "正常停止失败,尝试强制终止..." pkill -f "$UWSGI_PATH" if [ -f "$PID_FILE" ]; then rm -f "$PID_FILE" fi echo "强制终止完成" fi } # 重启服务 restart() { stop # 等待1秒确保进程已终止 sleep 1 start } # 查看状态 status() { if [ -f "$PID_FILE" ] && kill -0 $(cat "$PID_FILE") 2>/dev/null; then echo "服务正在运行(PID: $(cat "$PID_FILE"))" else echo "服务未运行" fi } # 显示帮助 usage() { echo "用法: $0 {start|stop|restart|status}" echo " start - 启动服务" echo " stop - 停止服务" echo " restart - 重启服务" echo " status - 查看服务状态" exit 1 } # 主逻辑 case "$1" in start) start ;; stop) stop ;; restart) restart ;; status) status ;; *) usage ;; esac
注:脚本启动命令 sh jiliguala.sh start/stop/restart/status
systemctl 配置文件
[Unit] Description=jiliguala service managed by custom script # 网络启动后再启动服务 After=network.target [Service] # 服务类型:oneshot 表示一次性命令,适合脚本管理 Type=oneshot # 工作目录(脚本所在目录) WorkingDirectory=/usr/local/src/jiliguala # 启动命令(调用脚本的 start 方法) ExecStart=/bin/bash /usr/local/src/jiliguala/jiliguala.sh start # 停止命令(调用脚本的 stop 方法) ExecStop=/bin/bash /usr/local/src/jiliguala/jiliguala.sh stop # 重启命令(先停止再启动) ExecReload=/bin/bash /usr/local/src/jiliguala/jiliguala.sh restart # 允许服务自行后台运行(脚本中若有后台启动逻辑需开启) RemainAfterExit=yes [Install] # 多用户模式下自启动 WantedBy=multi-user.target
注:vim /etc/systemd/system/jiliguala.service 生效配置文件:systemctl daemon-reload 开机启动:systemctl enable jiliguala
nginx配置文件
# For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 4096; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; server { listen 8000; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { include uwsgi_params; proxy_pass http://127.0.0.1:8001; #uwsgi_pass unix:/usr/local/src/jiliguala/uwsgi/sock/supercrm.sock; } # 静态文件配置:将`/static/`开头的请求映射到STATIC_ROOT目录 location /static/ { alias /usr/local/src/jiliguala/static/; # 注意末尾的斜杠 expires 30d; # 缓存30天,优化性能 } error_page 404 /404.html; location = /404.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } }

浙公网安备 33010602011771号