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

57 天前
 hwhtj

只有一个需求,在 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 中如何开机时自动运行上面提到的脚本

1455 次点击
所在节点    Django
20 条回复
sky92682
57 天前
Ubuntu 下用 systemd 做开机自启吧
Hopetree
57 天前
不对啊,哪有直接使用 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
57 天前
journelctl -u rc.local -f 看一下服务的详细日志
Debin006
57 天前
一般是不会直接这样跑的呀,这样跑即不好管理,不都是起一个 docker 容器的吗?
`docker pull django`
sagaxu
57 天前
多半是环境变量的问题
sky92682
57 天前
@sky92682 打错了 是 journalctl -u rc.local.rc -f
lingeo
57 天前
你用了虚拟环境?
HashV2
57 天前
/you_venv_path/bin/python /home/some/Qexo/manage.py runserver 0.0.0.0:8000 --noreload 、
chenqh
57 天前
你 supervisor 配置怎么写的?
lingeo
57 天前
你安装 Django 使用的应该是普通用户,包都安装在~/.local/lib/python3xx/目录下,而你使用 rc 脚本并没有指定用户,默认使用的是 root 用户,所以找不到 Django 。发个 supervisor 的配置,将 user 改成你的用户名。
julyclyde
57 天前
首先,别用 runserver
其次,别用 supervisord
第三,别用 rc.local

最后,
@lingeo 说的情况应该是这次的主要问题
maocat
57 天前
楼上这么多人都不提一下 uwsgi 吗
xhatt510
57 天前
写个 service 注册一下。不仅能自启动 还能帮你自动重启。不香吗?
awanabe
57 天前
你的 python 环境里面没有装依赖库
你是在非 root 里面的 python 装了 django 切换到了 root 就没有了
所以启动的时候找不到 django
mirrornighth
57 天前
用 systemd 做开机自启
vivisidea
57 天前
docker
hwhtj
57 天前
@lingeo 谢谢你的提醒,确实如你说说,我的 python3.11 装在了~/.local/lib/python311 ,所以才这样。十分感谢
ytmsdy
57 天前
跑个 docker 吧,然后设置成自启动。
chempotato
57 天前
nginx+uwsgi+django
deplives
48 天前
好家伙,直接用 runserver 跑

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1059406

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX