django – 运行两个gunicorn

试图在gunicorn上运行两个站点来编辑服务

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/webapps/kenyabuzz

ExecStart=/home/ubuntu/webapps/djangoenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/kenyabuzz/kb.sock kb.wsgi:application

[Install]
WantedBy=multi-user.target

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/webapps/kenyabuzz
WorkingDirectory=/home/ubuntu/webapps/uganda_buzz

ExecStart=/home/ubuntu/webapps/djangoenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/kenyabuzz/kb.sock kb.wsgi:application
ExecStart=/home/ubuntu/webapps/djangoenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/uganda_buzz/ug.sock kb.wsgi:application

[Install]
WantedBy=multi-user.target

日志显示ExecStart不能重复

ubuntu@ip-172-31-17-122:~$sudo systemctl status gunicorn
● gunicorn.service - gunicorn daemon
   Loaded: error (Reason: Invalid argument)
   Active: active (running) since Tue 2017-02-14 09:36:52 EAT; 35s ago
 Main PID: 26150 (gunicorn)
   CGroup: /system.slice/gunicorn.service
           ├─26150 /home/ubuntu/webapps/djangoenv/bin/python /home/ubuntu/webapps/djangoenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/kenyabuzz/kb.sock kb.wsgi:application
           ├─26156 /home/ubuntu/webapps/djangoenv/bin/python /home/ubuntu/webapps/djangoenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/kenyabuzz/kb.sock kb.wsgi:application
           ├─26157 /home/ubuntu/webapps/djangoenv/bin/python /home/ubuntu/webapps/djangoenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/kenyabuzz/kb.sock kb.wsgi:application
           └─26160 /home/ubuntu/webapps/djangoenv/bin/python /home/ubuntu/webapps/djangoenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/kenyabuzz/kb.sock kb.wsgi:application

Feb 14 09:36:52 ip-172-31-17-122 systemd[1]: Started gunicorn daemon.
Feb 14 09:36:52 ip-172-31-17-122 gunicorn[26150]: [2017-02-14 09:36:52 +0000] [26150] [INFO] Starting gunicorn 19.6.0
Feb 14 09:36:52 ip-172-31-17-122 gunicorn[26150]: [2017-02-14 09:36:52 +0000] [26150] [INFO] Listening at: unix:/home/ubuntu/webapps/kenyabuzz/kb.sock (26150)
Feb 14 09:36:52 ip-172-31-17-122 gunicorn[26150]: [2017-02-14 09:36:52 +0000] [26150] [INFO] Using worker: sync
Feb 14 09:36:52 ip-172-31-17-122 gunicorn[26150]: [2017-02-14 09:36:52 +0000] [26156] [INFO] Booting worker with pid: 26156
Feb 14 09:36:52 ip-172-31-17-122 gunicorn[26150]: [2017-02-14 09:36:52 +0000] [26157] [INFO] Booting worker with pid: 26157
Feb 14 09:36:52 ip-172-31-17-122 gunicorn[26150]: [2017-02-14 09:36:52 +0000] [26160] [INFO] Booting worker with pid: 26160
Feb 14 09:37:15 ip-172-31-17-122 systemd[1]: gunicorn.service: Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.

在这种情况下,使用可用配置运行两个站点的最佳方法是什么.在空格和逗号分隔的同一行上尝试每个都会带来错误.

最佳答案
systemd对这种情况有很大的支持.我假设你在应用程序前面运行像Nginx这样的Web服务器作为反向代理,使用它们唯一的套接字名称将流量路由到每个Gunicorn实例的右端点.在这里,我将重点介绍systemd配置.

希望能够停止并启动一个独立的gunicorn实例,并且还希望将它们视为可以一起停止或启动的单个服务.

systemd解决方案涉及创建“目标”,该目标将用于将两个实例视为单个服务.然后,将使用单个“模板单元”允许您向“目标”添加多个gunicorns.

首先,/ etc / systemd / systemd / gunicorn.target:

# See ReADME.md for more about this file
[Unit]
Description=Gunicorn
Documentation=https://example.com/path/to/your/docs

[Install]
WantedBy=multi-user.target

而已.您已经创建了一个目标,当“启用”将在启动时启动.现在/etc/systemd/system/gunicorn@.service,模板文件:

[Unit]
Description=gunicorn daemon
After=network.target
PartOf=gunicorn.target
# Since systemd 235 reloading target can pass through
ReloadPropagatedFrom=gunicorn.target


[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/webapps/%i

ExecStart=/home/ubuntu/webapps/djangoenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/%i/kb.sock kb.wsgi:application

[Install]
WantedBy=gunicorn.target

请注意我对您的文件所做的更改:

>每个实例现在是PartOf =目标.
>由于更新的WantedBy =,启用此服务将使其作为目标的依赖项启动
> WorkingDirectory =和套接字路径现在使用变量,因为该文件现在是模板.

要立即启动或停止所有Gunicorn实例,我们可以简单地:

systemctl start gunicorn
systemctl stop gunicorn

我们可以使用启用和禁用来动态添加和删除新的Gunicorn实例:

systemctl enable  gunicorn@kenyabuzz
systemctl disable gunicorn@ugandabuzz

我们也可以自行停止并启动特定服务:

systemctl start gunicorn@kenyabuzz
systemctl stop gunicorn@kenyabuzz
systemctl restart gunicorn@kenyabuzz

要了解有关所使用的任何系统指令的更多信息,您可以查看man systemd.directives,它将列出记录每个指令的特定手册页.

转载注明原文:django – 运行两个gunicorn - 代码日志