![無法啟動gunicorn.service:找不到單位gunicorn.service。烏本托 18.04](https://rvso.com/image/922397/%E7%84%A1%E6%B3%95%E5%95%9F%E5%8B%95gunicorn.service%EF%BC%9A%E6%89%BE%E4%B8%8D%E5%88%B0%E5%96%AE%E4%BD%8Dgunicorn.service%E3%80%82%E7%83%8F%E6%9C%AC%E6%89%98%2018.04.png)
我正在關注這如何在 Ubuntu 18.04 上使用 Postgres、Nginx 和 Gunicorn 設定 Django 指南。
我創建了以下文件 .socket
sudo nano /etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
我建立了以下文件 .service
sudo nano /etc/systemd/system/gunicorn.service
原 RECOMENDED_FORMATTING-s 中指導
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=sammyRECOMENDED_FORMATTING
Group=www-data
WorkingDirectory=/home/sammyRECOMENDED_FORMATTING/myprojectdirRECOMENDED_FORMATTING
ExecStart=/home/sammyRECOMENDED_FORMATTING/myprojectdirRECOMENDED_FORMATTING/myprojectenvRECOMENDED_FORMATTING/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
myprojectRECOMENDED_FORMATTING.wsgi:application
[Install]
WantedBy=multi-user.target
我如何格式化我自己的版本我的虛擬環境位於伺服器上的專案資料夾之外
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=SERVER_USER
Group=www-data
WorkingDirectory=/home/SERVER_USER/MAIN_PROJECT_FOLDER
ExecStart=/home/SERVER_USER/ven/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/home/SERVER_USER/MAIN_PROJECT_FOLDER/MAINAPPLICATION_FOLDER.sock \
MAINAPPLICATION_FOLDER.wsgi:application
[Install]
WantedBy=multi-user.target
我也嘗試過按照最初的建議保留這些
--bind unix:/run/gunicorn.sock \
比我嘗試執行以下程式碼
sudo systemctl start gunicorn
錯誤訊息1
Failed to start gunicorn.service: Unit gunicorn.service not found.
我也試過 2
sudo systemctl start gunicorn.socket
錯誤訊息2
Failed to start gunicorn.socket: Unit gunicorn.socket is not loaded properly: Invalid argument.
See system logs and 'systemctl status gunicorn.socket' for details.
為了解決這個問題,我嘗試過
- https://stackoverflow.com/questions/40711747/failed-to-start-gunicorn-service-unit-gunicorn-service-not-found這與我正在做的指南完全相同,除了舊版的 Linux 之外。
- 這不是相同的代碼並且沒有得到答复單位gunicorn.service無法載入:沒有這樣的檔案或目錄
- 運行:
systemctl status gunicorn.socket
結果:Warning: The unit file, source configuration file or drop-ins of gunicorn.socket changed on disk ● gunicorn.socket - gunicorn daemon Loaded: error (Reason: Invalid argument) Active: inactive (dead)
- 運行:
systemctl is-enabled gunicorn.socket
結果:enabled
- 運行:
systemctl is-enabled gunicorn.service
結果:Failed to get unit file state for gunicorn.service: No such file or directory
答案1
您的服務中有 [Install] 部分,這表示您需要執行命令sudo systemctl enable gunicorn.service
來在系統啟動時啟動服務。
運行該命令後,將在目錄中建立該檔案的符號連結/etc/systemd/system/multi-user.target.wants/
。
請注意,您還需要建立名為的套接字檔案gunicorn.socket
並需要啟用它。啟用後,您可以透過執行命令啟動套接字sudo systemctl start gunicorn.socket
。您不需要執行該服務,因為 systemd 會找出依賴關係。
您可以使用 檢查服務的狀態systemctl status gunicorn.service
。
答案2
就我而言,伺服器沒有創建該gunicorn.socket
文件。所以,我必須自己創建該文件並啟用它。
轉到:/etc/systemd/system
並透過命令檢查所有可用的檔案和目錄ls
。
如果該文件不存在,請嘗試透過以下方式從第一個文件重新建立該文件:sudo nano ~/etc/systemd/system/gunicorn.socket
、 或sudo nano gunicorn.socket
。
然後先通過: 啟用它sudo systemctl enable gunicorn.socket
,然後啟用 do: sudo systemctl start gunicorn.socket
。
如果一切正常,您將看到文件的狀態gunicorn.socket
為活動狀態,運行命令後sudo systemctl status gunicorn.socket
您將看到:
gunicorn.socket - gunicorn socket
Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor prese>
Active: active (listening) since Fri 2020-06-26 17:53:10 UTC; 14s ago
Triggers: ● gunicorn.service
Listen: /run/gunicorn.sock (Stream)
Tasks: 0 (limit: 1137)
Memory: 0B
CGroup: /system.slice/gunicorn.socket