uwsgi django 在启动时无法在没有 postgresql 的情况下启动

uwsgi django at boot fail to start without postgresql

Debian 服务器,引导时的 uwsgi 由 crontab @reboot 启动。

return 这个在 uwsgi.log:

ile "/usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql/base.py", line 175, in get_new_connection connection = Database.connect(**conn_params) File "/usr/lib/python2.7/dist-packages/psycopg2/init.py", line 164, in connect conn = _connect(dsn, connection_factory=connection_factory, async=async) django.db.utils.OperationalError: could not connect to server: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?

Thu Mar 24 05:19:02 2016 - unable to load app 0 (mountpoint='') (callable not found or import error) Thu Mar 24 05:19:02 2016 - * no app loaded. going in full dynamic mode Thu Mar 24 05:19:02 2016 - uWSGI is running in multiple interpreter mode *

如果我等待 postgresql 启动并重新启动 uwsgi 一切正常。

有没有办法告诉 uwsgi 等待 postgresql?

每天使用 systemd 而不是 cron 来启动 uWSGI。

创建文件 /etc/systemd/system/uwsgi.service,内容为:

[Unit]
Description=uWSGI
After=syslog.target
After=postgresql@9.4-main.service
Wants=postgresql@9.4-main.service

[Service]
ExecStart=/usr/local/bin/uwsgi --ini /etc/uwsgi/uwsgi.ini
Restart=always
KillSignal=SIGTERM
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

根据需要更改课程开始命令。

如果你想启动一个以上的uWSGI服务器(可能不止一个用户),考虑使用uWSGI Emperor。你甚至可以创建一些共享目录,每个人都可以在其中创建和管理自己的文件(通过在目录上设置粘性位)并将 emperor 设置为暴君模式,这样每个 vassal 都只会从拥有指定文件的用户帐户启动,如果你想赋予任何人创建自己的 uWSGI 实例的能力。