8
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

gunicornをsystemdで自動起動する

Last updated at Posted at 2021-06-02

#はじめに
gunicornを自動起動しようとしたところ2時間ハマったのでメモ

#環境

  • CentOS 8.1
  • miniconda 3
  • python 3.6
  • gunicorn 20.1.0

#前提

  • django アプリの場所 /home/kimisyo/helloworld
  • wsgiファイルの場所 /home/kimisyo/helloworld_app
  • guniconコマンドの場所 /home/kimisyo/minicona3/envs/django/bin/gunicorn

#方法

設定ファイル作成

公開ポートは8000としている。

/home/kimisyo/helloworld/gunicorn.conf.py
import multiprocessing

bind = "0.0.0.0:8000"
daemon = True
workers = multiprocessing.cpu_count() * 2 + 1

serviceファイル作成

/etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=kimisyo
Group=kimisyo
WorkingDirectory=/home/kimisyo/helloworld
ExecStart=/home/kimisyo/minicona3/envs/django/bin/gunicorn --config /home/kimisyo/helloworld/gunicorn.conf.py helloworld.wsgi:application
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

自動起動設定

#systemctl enable gunicorn.service
#systemctl start gunicorn
#systemctl status gunicorn

うまくいけば以下のような状態になっているはずだ。

#systemctl status gunicorn
● gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: disabled)
   Active: active (exited) since Thu 2021-06-03 07:34:56 JST; 8min ago
  Process: 798 ExecStart=/home/kimisyo/yes/envs/django/bin/gunicorn --config /home/kimisyo/helloworld/gunicorn.conf.py helloworld.wsgi:application (code=exited, status=0/SUCCESS)
 Main PID: 798 (code=exited, status=0/SUCCESS)
    Tasks: 4 (limit: 11492)
   Memory: 110.4M
   CGroup: /system.slice/gunicorn.service
           ├─1028 /home/kimisyo/yes/envs/django/bin/python /home/kimisyo/yes/envs/django/bin/gunicorn --config /home/kimisyo/helloworld/gunicorn.conf.py helloworld.wsgi:application
           ├─1051 /home/kimisyo/yes/envs/django/bin/python /home/kimisyo/yes/envs/django/bin/gunicorn --config /home/kimisyo/helloworld/gunicorn.conf.py helloworld.wsgi:application
           ├─1054 /home/kimisyo/yes/envs/django/bin/python /home/kimisyo/yes/envs/django/bin/gunicorn --config /home/kimisyo/helloworld/gunicorn.conf.py helloworld.wsgi:application
           └─1058 /home/kimisyo/yes/envs/django/bin/python /home/kimisyo/yes/envs/django/bin/gunicorn --config /home/kimisyo/helloworld/gunicorn.conf.py helloworld.wsgi:application

 6月 03 07:34:56 localhost.localdomain systemd[1]: Started gunicorn daemon.

#最大のポイント
最大のポイントは、gunicorn.service ファイルにRemainAfterExit=yes を入れることである。これがないと起動してもすぐに停止してしまう。

8
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?