Python Flask with Nginx and uWSGI
requirements.txt
included in that folder. (Hold on !!) We will be going through it in some timePROJECT_FOLDER_HERE
With your Project app PathMY_USERNAME_HERE
With your UserName; You can get your username using command whoami
PROJECT_FOLDER_HERE
With your Project Path0.0.0.0
. You can change it as per your needs.USER_NAME_HERE
with your user-name.PROJECT_FOLDER_HERE
with your project app directoryuwsgi.ini_PATH_HERE
with uwsgi.ini
pathClone this repository.
A Stable Debian based Linux O.S, preferrably Ubuntu with sudo
privileges configured. I have tried it on Ubuntu 18.04 and it worked flawlessly.
We will me utilizing Virtual Env Wrapper for complete Flask Project.
Before proceeding any further, Install the following packages:
sudo apt-get update
sudo apt-get install python3 python3-pip
sudo apt-get install systemd nginx
sudo pip3 install virtualenv
Initializing Project Virtual Environment:
cd PROJECT/APP_FOLDER
virtualenv -p python3 venv
source venv/bin/activate
pip3 install -r requirements.txt
Now, as this project is configured with a simple Hello World Application inside app.py
, we will be using it for deployment. You can also have your complete project inside the app folder with an app.py
file
Now comes the main dreaded and & feared (kiddin!) Installation part. Considering you have installed all the above steps successfully, will start with Nginx.
You will need to place your nginx_config
in /etc/nginx/sites-available/nginx_config
. Then, to enable this nginx configuration, we will have to link it to the nginx sites-enabled directory using this command:
sudo ln -s /etc/nginx/sites-available/nginx_config /etc/nginx/sites-enabled
The above command will create a sym-link for nginx_config
Restart Nginx:
sudo service nginx restart
Before starting up the main service, let make a folder for logs; the configuration for which is defined in uwsgi.ini
sudo mkdir -p /var/log/uwsgi
sudo chown -R thetechfreak:thetechfreak /var/log/uwsgi
Instead of thetechfreak
use your username.
Now, we will have to configure systemd service & for that we place production_flask.service
in /etc/systemd/system/production_flask.service
and then restart and enable the service to auto start after reboot.
sudo systemctl start production_flask.service
sudo systemctl enable production_flask.service
At this point, our service should successfully start and incase of any updates you can just restart the service using:
sudo systemctl restart production_flask.service
After this step, your server should be up and running
In order to check the logs, you can navigate to the logs directors /var/log/uwsgi/
Monitoring the service is also very easy as you justneed to go inside your PROJECT_DIR
and run the following command:
uwsgitop stats.production_flask.sock
OR
If you want to monitor the logs of the application itself, you can make use of journalctl
: Note: These logs are same as the one which are stared in /var/log/uwsgi/
; So, unless you really want to have a real time log on your system, it's not required:
sudo journalctl -u production_flask.service -f
If your application requires static files to be served, you can add the following rule inside nginx_config
file:
location /static {
root APP_DIR;
}
Replace APP_DIR
with your application directory and as a result, all your static files located at APP_DIR/static
will be served by nginx
These are my final thoughts and some notes which are worth noting.
lazy-apps
mode which is responsible for loading the application one time per worker. You can read more about it here
parameters
to be passed, you can make use of CONFIGPARSERS but DO-NOT pass it via command line as uWSGI is the one responsible for invoking the app.Socket permission
is given to everyone. You can adjust it as per your needs, but make sure that nginx and uWSGI can still talk to each otherlog
files. By default, nginx writes error message to the file /var/log/nginx/errors.log
Contributions are very welcome. Do make PULL requests if you want to change anything, or post an ISSUE if you encounter any bugs.