我将我必须的服务器迁移到 Ubuntu 16.04 并systemd替换了upstart. 我有三个开发环境:生产、登台和开发。只有后一个使用本地数据库,其他两个使用远程数据库。因此,为了处理部署和服务,我设法创建以下systemd服务文件:
[Unit]
Description=Gunicorn service for Project
After=network.target
[Service]
User=projuser
WorkingDirectory=/home/projuser/sites/Project/source
ExecStartPre=/usr/bin/env DJANGO_SETTINGS_MODULE=v3.settings_staging
ExecStart=/home/projuser/sites/Project/env/bin/gunicorn --bind 127.0.0.1:8090 --workers 6 --access-logfile ../access.log --error-logfile ../error.log v3.wsgi:application
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
服务启动了,但它似乎忽略了这一行:ExecStartPre=/usr/bin/env DJANGO_SETTINGS_MODULE=v3.settings_staging。我可以在日志中看到以下内容:
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
Run Code Online (Sandbox Code Playgroud)
主要是因为它使用的是我用于开发的默认settings.py文件。
如何确保它加载 staging settings.py 文件?
好吧,作为参数传递gunicorn也有效:
--env DJANGO_SETTINGS_MODULE=myproject.settings
https://gunicorn-docs.readthedocs.io/en/latest/run.html#gunicorn-django
我想要的可以通过以下文件来实现/etc/systemd/system:
[Unit]
Description=Gunicorn service for a Django Project
After=network.target
[Service]
User=a_user
WorkingDirectory=/home/a_user/project/source
Environment="DJANGO_SETTINGS_MODULE=app.settings_staging"
ExecStart=/home/a_user/envs/project_env/bin/gunicorn --bind 127.0.0.1:8090 --workers 6 --access-logfile ../access.log --error-logfile ../error.log v3.wsgi:application
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
请注意以下行:Environment="DJANGO_SETTINGS_MODULE=app.settings_staging"。
https://www.linux.com/learn/understanding-and-using-systemd
| 归档时间: |
|
| 查看次数: |
1899 次 |
| 最近记录: |