V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
hwhtj
V2EX  ›  Django

请教在 ubuntu23 中如何 让 Django runserver 开机自启动

  •  
  •   hwhtj · 55 天前 · 1442 次点击
    这是一个创建于 55 天前的主题,其中的信息可能已经有所发展或是发生改变。

    只有一个需求,在 ubuntu23 开机后自动运行以下脚本

    /usr/bin/python3 /home/some/Qexo/manage.py runserver 0.0.0.0:8000 --noreload
    

    以上脚本,直接在终端是可以运行的,服务也起来了。 但要开机运行,试过了以下三种方法,都不行 1 、https://blog.csdn.net/yuezhilangniao/article/details/113772277 2 、https://www.cnblogs.com/jingzaixin/p/15920472.html 出现的错误是

    rc-local.service - /etc/rc.local Compatibility
        Loaded: loaded (/etc/systemd/system/rc-local.service; enabled; preset: enabled)
       Drop-In: /usr/lib/systemd/system/rc-local.service.d
                └─debian.conf
        Active: failed (Result: exit-code) since Tue 2024-07-23 09:22:23 CST; 15min ago
      Duration: 236ms
       Process: 3617 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
      Main PID: 3621 (code=exited, status=1/FAILURE)
           CPU: 344ms
    
    7 月 23 09:22:23 some-Ubuntu23 systemd[1]: Starting rc-local.service - /etc/rc.local Compatibility...
    7 月 23 09:22:23 some-Ubuntu23 systemd[1]: Started rc-local.service - /etc/rc.local Compatibility.
    7 月 23 09:22:23 some-Ubuntu23 systemd[1]: rc-local.service: Main process exited, code=exited, status=1/FAILURE
    7 月 23 09:22:23 some-Ubuntu23 systemd[1]: rc-local.service: Failed with result 'exit-code'.
    

    3 、使用 supervisor,出现的错误是:

    ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
    ......
    

    求助,请教,在 ubuntu23 中如何开机时自动运行上面提到的脚本

    20 条回复    2024-08-01 12:34:20 +08:00
    sky92682
        1
    sky92682  
       55 天前 via iPhone
    Ubuntu 下用 systemd 做开机自启吧
    Hopetree
        2
    Hopetree  
       55 天前
    不对啊,哪有直接使用 Python 跑 Django 的,不是要用类似 gunicorn 这种去启动运行吗,比如 supervisor 启动配置

    ```
    [program:gunicorn-django]
    command=gunicorn izone.wsgi -b 0.0.0.0:8000
    directory=/opt/cloud/izone
    stdout_logfile=/var/log/supervisor/gunicorn.django.log
    stderr_logfile=/var/log/supervisor/gunicorn.django.log
    autostart=true
    autorestart=true
    startsecs=2
    stopwaitsecs=2
    priority=100

    ```
    sky92682
        3
    sky92682  
       55 天前 via iPhone
    journelctl -u rc.local -f 看一下服务的详细日志
    Debin006
        4
    Debin006  
       55 天前
    一般是不会直接这样跑的呀,这样跑即不好管理,不都是起一个 docker 容器的吗?
    `docker pull django`
    sagaxu
        5
    sagaxu  
       55 天前
    多半是环境变量的问题
    sky92682
        6
    sky92682  
       55 天前 via iPhone
    @sky92682 打错了 是 journalctl -u rc.local.rc -f
    lingeo
        7
    lingeo  
       55 天前
    你用了虚拟环境?
    HashV2
        8
    HashV2  
       55 天前
    /you_venv_path/bin/python /home/some/Qexo/manage.py runserver 0.0.0.0:8000 --noreload 、
    chenqh
        9
    chenqh  
       55 天前
    你 supervisor 配置怎么写的?
    lingeo
        10
    lingeo  
       55 天前   ❤️ 1
    你安装 Django 使用的应该是普通用户,包都安装在~/.local/lib/python3xx/目录下,而你使用 rc 脚本并没有指定用户,默认使用的是 root 用户,所以找不到 Django 。发个 supervisor 的配置,将 user 改成你的用户名。
    julyclyde
        11
    julyclyde  
       55 天前
    首先,别用 runserver
    其次,别用 supervisord
    第三,别用 rc.local

    最后,
    @lingeo 说的情况应该是这次的主要问题
    maocat
        12
    maocat  
       55 天前 via iPhone
    楼上这么多人都不提一下 uwsgi 吗
    xhatt510
        13
    xhatt510  
       55 天前
    写个 service 注册一下。不仅能自启动 还能帮你自动重启。不香吗?
    awanabe
        14
    awanabe  
       55 天前
    你的 python 环境里面没有装依赖库
    你是在非 root 里面的 python 装了 django 切换到了 root 就没有了
    所以启动的时候找不到 django
    mirrornighth
        15
    mirrornighth  
       55 天前
    用 systemd 做开机自启
    vivisidea
        16
    vivisidea  
       55 天前
    docker
    hwhtj
        17
    hwhtj  
    OP
       55 天前
    @lingeo 谢谢你的提醒,确实如你说说,我的 python3.11 装在了~/.local/lib/python311 ,所以才这样。十分感谢
    ytmsdy
        18
    ytmsdy  
       55 天前
    跑个 docker 吧,然后设置成自启动。
    chempotato
        19
    chempotato  
       55 天前 via Android
    nginx+uwsgi+django
    deplives
        20
    deplives  
       46 天前
    好家伙,直接用 runserver 跑
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1006 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 19:11 · PVG 03:11 · LAX 12:11 · JFK 15:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.