如何设置 gunicorn 以使用 systemd 读取自定义设置
How setup gunicorn to read custom settings with systemd
我迁移了我必须 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
服务启动,但似乎忽略了这一行: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?
主要是因为它使用了我用于开发的默认 settings.py
文件。
我如何确保它加载临时 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
注意以下行:Environment="DJANGO_SETTINGS_MODULE=app.settings_staging"
。
我迁移了我必须 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
服务启动,但似乎忽略了这一行: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?
主要是因为它使用了我用于开发的默认 settings.py
文件。
我如何确保它加载临时 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
注意以下行:Environment="DJANGO_SETTINGS_MODULE=app.settings_staging"
。