[メモ] Nginx + Gunicorn の設定 (rhel)


前置き

自分のメモ
usキーボードの日本語切り替えが面倒くさいの自分英語で書きます。
気が向いたら日本語に直します。

Nginx install latest version

Installation

Note: http://nginx.org/en/linux_packages.html#RHEL-CentOS
Install yum-utils

yum install yum-utils

Set up repository (/etc/yum.repos.d/nginx.repo)

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

To install nginx

yum install nginx

Configuration

.conf file

/etc/nginx/conf.d/foo.conf
server {
    listen 80;
    server_name foobar;
    
    # Read static files by nginx
    location /static {
        alias /usr/share/nginx/html/static;
    }

    location / {
        proxy_pass http://127.0.0.1:8000/;
    }
}

service command

systemctl start nginx

Python

someday

Gunicorn

Installation

pip install gunicorn

Configuration

/etc/foo/settings.py
wsgi_app = config.wsgi:application
chdir = '/etc/foo/bar'
daemon = False
bind = '127.0.0.1:8000'
workers = 3
timeout = 300
# worker_class = 'gevent'
capture_output = True
accesslog = '/var/log/gunicorn/access.log'
errorlog = '/var/log/gunicorn/error.log'
gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=root
WorkingDirectory=/etc/foo/bar
ExecStart=/etc/foo/env/bin/gunicorn \
                --config /etc/foo/setting.py

[Install]
WantedBy=multi-user.target

service command

systemctl start gunicorn