Closed juliosaraiva closed 4 years ago
Cenary:
the web client <-> the web server <-> the socket <-> uWSGI <-> Python
sudo apt install nginx
systemctl enable nginx
systemctl start nginx
Configure Nginx for your site You will need the uwsgi_params file, which is available in the Nginx directory of the uWSGI distribution, or from https://github.com/nginx/nginx/blob/master/conf/uwsgi_params
Copy it into your project directory. In a moment we will tell Nginx to refer to it.
This file is available in contrib directory in this project.
I will set up the application to use Unix sockets because there’s less overhead than TCP port socket. So, I need to specify it on Nginx conf bellow.
# /etc/nginx/sites-available/mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:///path/to/your/mysite/mysite.sock; # Unix Socket
# server 127.0.0.1:8001; # TCP Port Socket
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name example.com; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /path/to/your/mysite/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
# Here that we need to point to our uwsgi_params file that we downloaded from nginx repository in our project
# https://github.com/nginx/nginx/blob/master/conf/uwsgi_params
location / {
uwsgi_pass django;
include /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
ln -s /etc/nginx/sites-available/mysite_nginx.conf /etc/nginx/sites-enabled/
systemctl restart nginx.service
We can put the same options that we used with uWSGI into a file, and then ask uWSGI to run with that file. It makes it easier to manage configurations.
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /path/to/your/project
# Django's wsgi file
module = project.wsgi
# the virtualenv (full path)
home = /path/to/virtualenv
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /path/to/your/project/mysite.sock
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
And run uwsgi using this file:
uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file
uWSGI can run in ‘emperor’ mode. In this mode it keeps an eye on a directory of uWSGI config files, and will spawn instances (‘vassals’) for each one it finds.
Whenever a config file is amended, the emperor will automatically restart the vassal.
# create a directory for the vassals
sudo mkdir -p /etc/uwsgi/vassals
# symlink from the default config directory to your config file
sudo ln -s /path/to/your/mysite/mysite_uwsgi.ini /etc/uwsgi/vassals/
# run the emperor
uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data
You may need to run uWSGI with sudo:
sudo uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data
The options mean:
emperor: where to look for vassals (config files) uid: the user id of the process once it’s started gid: the group id of the process once it’s started
Check the site; it should be running.
One approach to integrate uWSGI apps with your init system is using the Emperor.
Your init system will talk only with the Emperor that will rule all of the apps itself.
Create a systemd service file (you can save it as /etc/systemd/system/emperor.uwsgi.service
)
/etc/systemd/system/emperor.uwsgi.service
[Unit]
Description=uWSGI Emperor
After=syslog.target
[Service]
ExecStart=/root/uwsgi/uwsgi --ini /etc/uwsgi/emperor.ini
# Requires systemd version 211 or newer
RuntimeDirectory=uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
Then run it
systemctl daemon-reload
systemctl enable emperor.uwsgi.service
systemctl start emperor.uwsgi.service
Setting up Django and your web server with uWSGI and nginx.
Reference: https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html