# 1. Gunicorn 설치
pip install gunicorn
# 2. Gunicorn 설정 파일 생성
# gunicorn.conf.py (프로젝트 루트에 생성)
cat > gunicorn.conf.py << 'EOF'
import multiprocessing
# Server socket
bind = "127.0.0.1:8000"
backlog = 2048
# Worker processes
workers = multiprocessing.cpu_count() * 2 + 1
worker_class = "sync"
worker_connections = 1000
timeout = 30
keepalive = 2
# Restart workers after this many requests, to help prevent memory leaks
max_requests = 1000
max_requests_jitter = 100
# Logging
accesslog = "/var/log/gunicorn/access.log"
errorlog = "/var/log/gunicorn/error.log"
loglevel = "info"
# Process naming
proc_name = 'django_app'
# Daemonize the Gunicorn process (detach & enter background)
daemon = False
# User and group to run as
user = "www-data"
group = "www-data"
# Server mechanics
preload_app = True
EOF
# 3. 로그 디렉토리 생성
sudo mkdir -p /var/log/gunicorn
sudo mkdir -p /var/log/django
sudo chown -R www-data:www-data /var/log/gunicorn
sudo chown -R www-data:www-data /var/log/django
# 4. Systemd 서비스 파일 생성
sudo tee /etc/systemd/system/gunicorn.service > /dev/null << 'EOF'
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
Type=notify
User=www-data
Group=www-data
RuntimeDirectory=gunicorn
WorkingDirectory=/path/to/your/django/project
ExecStart=/path/to/your/venv/bin/gunicorn --config gunicorn.conf.py myproject.wsgi:application
ExecReload=/bin/kill -s HUP $MAINPID
Restart=on-failure
RestartSec=5
KillMode=mixed
TimeoutStopSec=5
[Install]
WantedBy=multi-user.target
EOF
# 5. Systemd 소켓 파일 생성
sudo tee /etc/systemd/system/gunicorn.socket > /dev/null << 'EOF'
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
SocketUser=www-data
SocketGroup=www-data
[Install]
WantedBy=sockets.target
EOF
# 6. 서비스 활성화 및 시작
sudo systemctl daemon-reload
sudo systemctl enable gunicorn.socket
sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn
sudo systemctl start gunicorn
# 7. 상태 확인
sudo systemctl status gunicorn
sudo systemctl status gunicorn.socket
'코딩' 카테고리의 다른 글
| OAuth 2.0 (0) | 2025.09.03 |
|---|---|
| django HTTP Security; Headers, SSL, Session, Cookie (0) | 2025.08.26 |
| nginx 설치 및 설정 (0) | 2025.08.24 |
| Set up a Cloudflare Tunnel between local network and Cloudflare (0) | 2025.08.24 |
| 데이터 전처리 및 파이프라인 (0) | 2025.08.22 |