Gunicornをsystemdで起動する

Ubuntu Published at Feb. 15, 2025, 4:57 p.m. by admin@senrigan.org

Gunicornをsystemdから起動しようとしたときにハマったこと

Ubuntu 24.04

Deploying Gunicornに説明があるのでやってみたけどうまく起動しない。

$ journalctl -xeu gunicorn

× gunicorn.service - gunicorn daemon
     Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Sun 2025-02-16 17:40:46 JST; 3min 18s ago
TriggeredBy: × gunicorn.socket
    Process: 888 ExecStart=/home/webadmin/www/senrigan/.venv/bin/python /home/webadmin/www/senrigan/.venv/bin/gunicorn --bind unix://run/gunicorn.sock app.wsgi (c>
   Main PID: 888 (code=exited, status=200/CHDIR)
        CPU: 4ms

Feb 16 17:40:46 ip-172-31-1-177 systemd[1]: Starting gunicorn.service - gunicorn daemon...
Feb 16 17:40:46 ip-172-31-1-177 (python)[888]: gunicorn.service: Changing to the requested working directory failed: Permission denied
Feb 16 17:40:46 ip-172-31-1-177 systemd[1]: gunicorn.service: Main process exited, code=exited, status=200/CHDIR
Feb 16 17:40:46 ip-172-31-1-177 systemd[1]: gunicorn.service: Failed with result 'exit-code'.
Feb 16 17:40:46 ip-172-31-1-177 systemd[1]: Failed to start gunicorn.service - gunicorn daemon.
Feb 16 17:40:46 ip-172-31-1-177 systemd[1]: gunicorn.service: Start request repeated too quickly.
Feb 16 17:40:46 ip-172-31-1-177 systemd[1]: gunicorn.service: Failed with result 'exit-code'.
Feb 16 17:40:46 ip-172-31-1-177 systemd[1]: Failed to start gunicorn.service - gunicorn daemon.

UserとGroupはなんとなくwww-dataで設定してたけど、なんとなくだとやっぱり動かない。wwwadminに変更する。でもまだ起動しない。

Feb 16 19:28:46 ip-172-31-1-177 python[2412]:     mod = importlib.import_module(module)
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:   File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:     return _bootstrap._gcd_import(name[level:], package, level)
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:   File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:   File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:   File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:   File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:   File "<frozen importlib._bootstrap_external>", line 995, in exec_module
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:   File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:   File "/home/webadmin/www/senrigan/app/wsgi.py", line 16, in <module>
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:     application = get_wsgi_application()
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:                   ^^^^^^^^^^^^^^^^^^^^^^
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:   File "/home/webadmin/www/senrigan/.venv/lib/python3.12/site-packages/django/core/wsgi.py", line 12, in get_wsgi_ap>
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:     django.setup(set_prefix=False)
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:   File "/home/webadmin/www/senrigan/.venv/lib/python3.12/site-packages/django/__init__.py", line 19, in setup
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:                       ^^^^^^^^^^^^^^^^^^^^^^^
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:   File "/home/webadmin/www/senrigan/.venv/lib/python3.12/site-packages/django/conf/__init__.py", line 81, in __getat>
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:     self._setup(name)
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:   File "/home/webadmin/www/senrigan/.venv/lib/python3.12/site-packages/django/conf/__init__.py", line 68, in _setup
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:     self._wrapped = Settings(settings_module)
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:                     ^^^^^^^^^^^^^^^^^^^^^^^^^
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:   File "/home/webadmin/www/senrigan/.venv/lib/python3.12/site-packages/django/conf/__init__.py", line 166, in __init>
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:     mod = importlib.import_module(self.SETTINGS_MODULE)
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:   File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:     return _bootstrap._gcd_import(name[level:], package, level)
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:   File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:   File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
Feb 16 19:28:46 ip-172-31-1-177 python[2412]:   File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked
Feb 16 19:28:46 ip-172-31-1-177 python[2412]: ModuleNotFoundError: No module named 'app.settings'
Feb 16 19:28:46 ip-172-31-1-177 python[2412]: [2025-02-16 19:28:46 +0900] [2412] [INFO] Worker exiting (pid: 2412)
Feb 16 19:28:46 ip-172-31-1-177 python[2411]: [2025-02-16 19:28:46 +0900] [2411] [ERROR] Worker (pid:2412) exited with code 3
Feb 16 19:28:46 ip-172-31-1-177 python[2411]: [2025-02-16 19:28:46 +0900] [2411] [ERROR] Shutting down: Master
Feb 16 19:28:46 ip-172-31-1-177 python[2411]: [2025-02-16 19:28:46 +0900] [2411] [ERROR] Reason: Worker failed to boot.

モジュールapp.settingsが見つからないという。ターミナルからGunicornは起動できるので環境変数がちゃんと見えてないのか?と思って

man 5 systemd.exec

でEnvironmentの設定のしかたを調べて起動できた。

/etc/systemd/system/gunocorn.service

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

[Service]
User=webadmin
Group=webadmin
WorkingDirectory=/home/webadmin/www/senrigan
ExecStart=/home/webadmin/www/senrigan/.venv/bin/python /home/webadmin/www/senrigan/.venv/bin/gunicorn --bind 127.0.0.1:8000 app.wsgi
Restart=always
RestartSec=3
Environment="DJANGO_SETTINGS_MODULE=app.config.production"

[Install]
WantedBy=multi-user.target