hwhtj
V2EX  ›  Django

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

  •  
  •   hwhtj ·
    sometech · Jul 23, 2024 · 6318 views
    This topic created in 832 days ago, the information mentioned may be changed or developed.

    只有一个需求,在 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 replies  •  2024-08-01 12:34:20 +08:00
    sky92682
    sky92682
    Jul 23, 2024 via iPhone
    1
    Ubuntu 下用 systemd 做开机自启吧
    Hopetree
    Hopetree
    Jul 23, 2024
    2
    不对啊,哪有直接使用 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
    sky92682
    Jul 23, 2024 via iPhone
    3
    journelctl -u rc.local -f 看一下服务的详细日志
    Debin006
    Debin006
    Jul 23, 2024
    4
    一般是不会直接这样跑的呀,这样跑即不好管理,不都是起一个 docker 容器的吗?
    `docker pull django`
    sagaxu
    sagaxu
    Jul 23, 2024
    5
    多半是环境变量的问题
    sky92682
    sky92682
    Jul 23, 2024 via iPhone
    6
    @sky92682 打错了 是 journalctl -u rc.local.rc -f
    lingeo
    lingeo
    Jul 23, 2024
    7
    你用了虚拟环境?
    HashV2
    HashV2
    Jul 23, 2024
    8
    /you_venv_path/bin/python /home/some/Qexo/manage.py runserver 0.0.0.0:8000 --noreload 、
    simple2025
    simple2025
    Jul 23, 2024
    9
    你 supervisor 配置怎么写的?
    lingeo
    lingeo
    Jul 23, 2024❤️1
    10
    你安装 Django 使用的应该是普通用户,包都安装在~/.local/lib/python3xx/目录下,而你使用 rc 脚本并没有指定用户,默认使用的是 root 用户,所以找不到 Django 。发个 supervisor 的配置,将 user 改成你的用户名。
    julyclyde
    julyclyde
    Jul 23, 2024
    11
    首先,别用 runserver
    其次,别用 supervisord
    第三,别用 rc.local

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