- Notifications
You must be signed in to change notification settings - Fork13
Python Flask with Nginx and uWSGI
License
source-nerd/Python-flask-with-uwsgi-and-nginx
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
- app Folder
- uwsgi.ini
- nginx_config
- production_flask.service
- app Folder - It contains you complete app and directories. Do initialize it as a virtual environment and install
requirements.txtincluded in that folder. (Hold on !!) We will be going through it in some time - uwsgi.ini - UWSGI server configuration. Edit/Replace the following places:
- my_app_folder: Replace
PROJECT_FOLDER_HEREWith your Project app Path - my_user: Replace
MY_USERNAME_HEREWith your UserName; You can get your username using commandwhoami
- my_app_folder: Replace
- nginx_config - Contains the main nginx configuration. Edit/Replace the following to make it work properly:
- uwsgi_pass: Replace
PROJECT_FOLDER_HEREWith your Project Path - [OPTIONAL] listen: Port on which you want nginx to Listen
- [OPTIONAL] server_name: Currently its
0.0.0.0. You can change it as per your needs.
- uwsgi_pass: Replace
- production_flask.service - A Systemd service file to ensure that the server restarts on failure and can be set to auto-start in case of server restart. Edit/Replace the following terms:
- Replace
USER_NAME_HEREwith your user-name. - Replace
PROJECT_FOLDER_HEREwith your project app directory - Replace
uwsgi.ini_PATH_HEREwithuwsgi.inipath
- Replace
Clone this repository.
A Stable Debian based Linux O.S, preferrably Ubuntu with
sudoprivileges 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:
sudoapt-getupdatesudoapt-getinstallpython3python3-pipsudoapt-getinstallsystemdnginxsudopip3installvirtualenv
Initializing Project Virtual Environment:
cdPROJECT/APP_FOLDERvirtualenv-ppython3venvsourcevenv/bin/activatepip3install-rrequirements.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 anapp.pyfile
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_configin/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_configRestart 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.inisudo mkdir -p /var/log/uwsgisudo chown -R thetechfreak:thetechfreak /var/log/uwsgi
Instead of
thetechfreakuse your username.Now, we will have to configure systemd service & for that we place
production_flask.servicein/etc/systemd/system/production_flask.serviceand then restart and enable the service to auto start after reboot.sudo systemctl start production_flask.servicesudo systemctlenable production_flask.serviceAt 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
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 yourPROJECT_DIR and run the following command:
uwsgitopstats.production_flask.sock
OR
If you want to monitor the logs of the application itself, you can make use ofjournalctl: 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:
sudojournalctl-uproduction_flask.service-f
If your application requires static files to be served, you can add the following rule insidenginx_config file:
location /static { root APP_DIR;}ReplaceAPP_DIR with your application directory and as a result, all your static files located atAPP_DIR/static will be served by nginx
These are my final thoughts and some notes which are worth noting.
- UWSGI is configured in
lazy-appsmode which is responsible for loading the application one time per worker. You can read more about ithere - Only Basic configuration of nginx is used; however it is sufficient for most of the use cases. But still, if you want to tune it further you can read uphere
- If your app requires some
parametersto be passed, you can make use ofCONFIGPARSERS but DO-NOT pass it via command line as uWSGI is the one responsible for invoking the app. - In this app,
Socket permissionis given to everyone. You can adjust it as per your needs, but make sure that nginx and uWSGI can still talk to each other - If something goes wrong, the first place to check is the
logfiles. 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.
About
Python Flask with Nginx and uWSGI
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.