As you know uWSGI is the most popular to deploy a Python Application. And Nginx is a powerful web server to run website in production. So this post will show a simple way to run your application (uses Flask Framework) with WSGI and Nginx

Peace. It does not mean to be in a place where there is no noise, trouble or hard work. It mean to be in the midst of those things and still be calm in your heart
Create wsgi.py in your root directory of the application:
1 2 3 4 |
from app import app as application if __name__ == "__main__": application.run() |
(Please make note that you understand the meaning of the source code above. So you can modify it to match your situation)
And a config file for WSGI – wsgi.ini:
1 2 3 4 5 6 7 8 9 10 11 |
[uwsgi] module = wsgi master = true processes = 5 socket = [your_app_name_here].sock chmod-socket = 660 vacuum = true die-on-term = true |
Install uwsgi with PIP:
1 |
pip install uwsgi |
Test your uWSGI config with command below:
1 |
uwsgi --ini wsgi.ini |
Make sure that WSGI works with your application properly. Now, you can config NGINX to run over socket file(your_app_name_here.sock). Please create nginx config file with content below
1 2 3 4 5 6 7 8 |
server { listen 80; server_name example.com; location / { include uwsgi_params; uwsgi_pass unix:/[path_to_your_app]/your_app_name_here.sock; } } |
Enable your site in Nginx:
1 |
ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com |
And restart Nginx to apply your changes:
1 |
sudo /etc/init.d/nginx restart |
Test your site now!!!
Create upstart script:
1 |
vi /etc/init/myproject.conf |
With content:
1 2 3 4 5 6 7 8 9 10 11 |
description "uWSGI server instance configured to serve accounts service" start on runlevel [2345] stop on runlevel [!2345] setuid root setgid www-data env PATH=/path_to_project/env/bin chdir /path_to_project exec uwsgi --ini wsgi.ini |