بِسْمِ اللهِ الرَّحْمٰنِ الرَّحِيْمِ
Background Story
Di kantor sedang membangun aplikasi dengan stack python, flask, dan socketio. Hnaah pertanyaannya adalah bagaimana cara deploy aplikasi flask socketio dengan environment production?
Kebutuhan
- Python3
- Flask
- uwsgi
- nginx
- virtual environment
Kita asumsikan sudah selesai dengan virtual environment python, jika belum saya akan buatkan ditutorial lain.
Config file uwsgi.ini
[uwsgi]
module = app:app
master = true
processes = 1
socket = /home/batutah/apps/livechat/socket/livechat.sock
gevent = 500
http-websockets = true
virtualenv = /home/batutah/.venvs/livechat
logto = /home/batutah/apps/livechat/logs/livechat.log
enable-threads = true
buffer-size = 32768
uid = batutah
gid = batutah
chdir = /home/batutah/apps/livechat/current
lazy-apps = true
vacuum = true
chmod-socket = 660
chown-socket = batutah:www-data
die-on-term = true
emptiness = true
harakiri = 30
thunder-lock = true
Nginx Config
server {
listen 80;
server_name livechat.batutah.id;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/batutah/apps/livechat/socket/livechat.sock;
}
location /static {
alias /home/batutah/apps/livechat/current/static;
expires 30d;
}
location /socket.io {
include uwsgi_params;
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
uwsgi_pass unix:/home/batutah/apps/livechat/socket/livechat.sock;
}
}
Systemd Service
sudo vim /etc/systemd/system/batutah-livechat.service
Isinya begini:
[Unit]
Description=Batutah Livechat
Requires=network.target
After=network.target
[Service]
TimeoutStartSec=0
RestartSec=10
Restart=always
WorkingDirectory=/home/batutah/apps/livechat/current
User=batutah
Group=www-data
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all
Environment="PATH=/home/batutah/.venvs/livechat/bin"
ExecStart=/home/batutah/.venvs/livechat/bin/uwsgi --ini /home/batutah/apps/livechat/config/uwsgi.ini
[Install]
WantedBy=multi-user.target
Jalankan Service Systemd
sudo systemctl daemon-reload
sudo systemctl enable --now batutah-livechat.service
Jalankan Nginx
sudo systemctl restart nginx
Sumber :
07 Januari 2022
15:16 WIB
Pogung Baru, Sleman
Yogyakarta