uWSGI
Important: This page applies only to bare-metal installations (without Docker).
If you use Docker (recommended), see Installing with Docker.
uWSGI is the application server that runs Django, replacing runserver in production.
Installation
uWSGI is installed as part of setting up the site.
If it is missing:
source lcojsite/bin/activate
pip3 install uwsgiConfiguration
The uwsgi.ini file
Create a uwsgi.ini file in the site directory:
[uwsgi]
# Django project
chdir = /home/lcoj/site
module = dmoj.wsgi:application
# Virtual environment
home = /home/lcoj/lcojsite
# Process
master = true
processes = 4
threads = 2
# Socket
socket = /tmp/lcoj-site.sock
chmod-socket = 666
vacuum = true
# Logging
logto = /var/log/uwsgi/lcoj-site.log
# Performance
max-requests = 5000
harakiri = 60Explanation:
chdir: Project directorymodule: WSGI modulehome: Virtual environmentprocesses: Number of worker processesthreads: Number of threads per processsocket: Unix socket that nginx connects tomax-requests: Restart a worker after N requestsharakiri: Timeout (seconds)
Create the log directory
mkdir -p /var/log/uwsgi
chown lcoj:lcoj /var/log/uwsgiRunning with Supervisor
File /etc/supervisor/conf.d/site.conf:
[program:site]
command=/home/lcoj/lcojsite/bin/uwsgi --ini /home/lcoj/site/uwsgi.ini
directory=/home/lcoj/site
user=lcoj
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/uwsgi/site-supervisor.logStart it:
supervisorctl update
supervisorctl start siteNginx configuration
Nginx config file:
upstream lcoj {
server unix:///tmp/lcoj-site.sock;
}
server {
listen 80;
server_name luyencode.net;
location / {
uwsgi_pass lcoj;
include uwsgi_params;
}
location /static/ {
alias /home/lcoj/site/staticfiles/;
}
location /media/ {
alias /home/lcoj/site/media/;
}
}Reload nginx:
service nginx reloadOptimization
Number of processes
Formula: processes = (CPU cores × 2) + 1
Example: 4 cores → 9 processes
processes = 9Threads
Increase threads for I/O-heavy workloads:
threads = 4Buffer size
Increase it if you handle large requests:
buffer-size = 32768Lazy apps
Load the app after forking (saves RAM):
lazy-apps = trueOffload
Offload static files:
offload-threads = 4Monitoring
Stats server
Add to uwsgi.ini:
stats = 127.0.0.1:9191View stats:
uwsgitop 127.0.0.1:9191Install uwsgitop:
pip3 install uwsgitopLog
Follow the log in real time:
tail -f /var/log/uwsgi/lcoj-site.logOr via Supervisor:
supervisorctl tail -f siteTroubleshooting
uWSGI won't start:
- Check the
uwsgi.inisyntax - Check the paths
- View the log:
supervisorctl tail -f site
502 Bad Gateway:
- Check that uWSGI is running
- Check that the socket file exists:
ls -la /tmp/lcoj-site.sock - Check the socket permissions
Slow response:
- Increase
processesandthreads - Check database performance
- Check the
harakiritimeout
Memory leak:
- Lower
max-requests - Check the code for leaks
- Restart periodically
Reload
Graceful reload
No downtime:
supervisorctl restart siteOr:
touch /home/lcoj/site/uwsgi.iniForce reload
killall -9 uwsgi
supervisorctl start siteNotes
- Do not use
runserverin production - Monitor RAM and CPU usage
- Back up your config before changing it
- Test on staging before deploying to production
