CentOS8让uwsgi开机自动启动django(无需登录,无需手动)

baidu了好几天,折腾了好几天,终于让uwsgi能在CentOS8下开机自动启动Django网站了

 

网上说的:

/etc/init.d/???.sh

chkconfig --add ???.sh

 

这种↑方法,不行!!

没研究,不知道是不是CentOS8版本原因

 

 

今天看了下Systemd的介绍,终于以.service的方式搞定。

解决方案分两步:

  1、新建自己的 xxx.service(位置:/etc/systemd/system)

    

[Unit]
Description=uwsgi-xxx-support
After=network.target
Before=nginx.service

[Service]
ExecStart=/usr/sbin/xxx.sh
ExecReload=/bin/kill -HUP ( ps -ep | grep uwsgi)
Type=forking


[Install]
WantedBy=multi-user.target
~                              

注意:: Type 要设置为forking,因为xxx.sh执行完之后是要退出的,继续运行的是uwsgi进程,默认的simple是启动不了的,状态会一直是inactive(dead)

建 好 之 后:$  systemctl enable xxx.service

  2、建自己的xxx.sh脚本(我放在/usr/sbin)

         

#!/bin/sh 
  
venvwrap="virtualenvwrapper.sh"



python38=`/usr/bin/which python3.8`
VIRTUALENVWRAPPER_PYTHON=${python38}

export WORKON_HOME=/root/.virtualenvs

if [ $? -eq 0 ]; then
        venvwrap=`/usr/bin/which $venvwrap`
        source $venvwrap
fi

workon .xxx
source activate
uwsgi --ini /webroot/xxx/uwsgi.ini &
~                                                                                                                                             
~                                                                                                                                             
~                                                                                                                                             
~          

     要设置 VIRTUALENVWRAPPER_PYTHON          ,否则source ...../virtualenvwrapper.sh会报错,报找不到python

     要export WORKON_HOME    ,否则workon找不到虚拟环境

     activate要以source 参数的形式运行,否则报无权限

      uwsgi  最后添&,表示后台执行

 

 

 

 reboot

不登录,不执行任何指令

打开浏览器,浏览网站

SUCCESS!

 

posted @ 2020-09-11 17:19  吾非无心  阅读(1064)  评论(0编辑  收藏  举报