There are very useful descriptions out there. I followed their instruction.
You should check them out.
How To Serve Flask Applications with uWSGI and Nginx on Ubuntu 16.04 | DigitalOcean
How To Serve Flask Applications with uWSGI and Nginx on Ubuntu 14.04+ · GitHub
This is my records how I did it.
OS: Rasbian (Jessie-lite)
Python 2.7
1. Install and Configure VirtualEnv and VirtualEnvWrapper
pi@raspberrypi:~ $ sudo apt-get update
pi@raspberrypi:~ $ sudo apt-get install python-pip python-dev
pi@raspberrypi:~ $ sudo pip install virtualenv virtualenvwrapper
pi@raspberrypi:~ $ echo "export WORKON_HOME=~/.virtualenvs" >> ~/.bashrc
pi@raspberrypi:~ $ echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
pi@raspberrypi:~ $ source ~/.bashrc
2. Create the virtual environment (flaskappenv)
pi@raspberrypi:~ $ mkvirtualenv flaskappenv
You are now in the your environment
(flaskappenv)pi@raspberrypi:~ $
3. Install Flask and uWSGI under the flaskappenv
(flaskappenv)pi@raspberrypi:~$ pip install uwsgi flask
4. Set up a Flask Application (flaskapp)
(flaskappenv)pi@raspberrypi:~ $ cd ~
(flaskappenv)pi@raspberrypi:~ $ mkdir flaskapp
(flaskappenv)pi@raspberrypi:~flaskapp $ cd flaskapp/
(flaskappenv)pi@raspberrypi:~flaskapp $ vi flaskapp.py
- flaskapp.py
from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "<h1 style='color:blue'>Hello There!</h1>" if __name__ == "__main__": app.run(host='0.0.0.0')
5. Create the WSGI Entry Point
(flaskappenv)pi@raspberrypi:~ $ vim ~/flaskapp/wsgi.py
- wsgi.py
from flaskapp import app if __name__ == "__main__": app.run()
6. We're now done with our virtual environment, so we can deactivate it:
(flaskappenv)pi@raspberrypi:~ $ deactivate
7. Creating a uWSGI Configuration File under the system's Python environment
pi@raspberrypi:~ $ vim ~/flaskapp/flaskapp.ini
- flaskapp.ini
[uwsgi] module = wsgi:app master = true processes = 5 socket = flaskapp.sock chmod-socket = 660 vacuum = true die-on-term = true
pi@raspberrypi:~ $ sudo vim /etc/systemd/system/flaskapp.service
- flaskapp.service
[Unit] Description=uWSGI instance to serve flaskapp After=network.target [Service] User=pi Group=www-data WorkingDirectory=/home/pi/flaskapp Environment="PATH=/home/pi/.virtualenvs/flaskappenv/bin" ExecStart=/home/pi/.virtualenvs/flaskappenv/bin/uwsgi --ini flaskapp.ini [Install] WantedBy=multi-user.target
pi@raspberrypi:~ $ sudo systemctl start flaskapp
pi@raspberrypi:~ $ sudo systemctl enable flaskapp
8. Install Nginx and Configuring Nginx to Proxy Requests
pi@raspberrypi:~ $ sudo apt-get install python-pip python-dev nginx
pi@raspberrypi:~ $ sudo vim /etc/nginx/sites-available/flaskapp
- /etc/nginx/sites-available/flaskapp
server { listen 80; server_name localhost; location / { include uwsgi_params; uwsgi_pass unix:/home/pi/flaskapp/flaskapp.sock; } }
pi@raspberrypi:~ $ sudo ln -s /etc/nginx/sites-available/flaskapp /etc/nginx/sites-enabled
pi@raspberrypi:~ $ cd /etc/nginx/sites-available
pi@raspberrypi:/etc/nginx/sites-available $ sudo mv default default.back
pi@raspberrypi:~ $ cd ~
pi@raspberrypi:~ $ sudo nginx -t
pi@raspberrypi:~ $ sudo systemctl restart nginx
sudo usermod -aG www-data pi sudo chown -R pi:www-data /home/pi/flaskapp/*
9. You should now be able to go to your server's domain name or IP address in your web browser:
http://server_domain_or_IP
In my case, http://raspberrypi.local
10. check you have the following files in the directory "~/flaskapp" IF http://raspberrypi.local did not show Hello There!
flaskapp.ini flaskapp.py flaskapp.pyc flaskapp.sock wsgi.py wsgi.pyc