How to Setup Python Web Application With Flask Gunicorn and Nginx
Last updated: July 19th 2023
Introduction
In 2023, Python will remain one of the best programming languages to learn. It has applications in back-end web development, machine learning, scientific modeling, system operations, and several enterprise-specific software.
Generally, Python is considered one of the more approachable programming languages because it has dynamically typed, English-like syntax and many libraries.
The server is an integral part of the client-server model that helps make up the internet. When you visit a website, your browser acts as the client who makes a request to the server for the files it needs to operate. The server then responds to the client's request with the necessary files to display the web page.
In this tutorial, I will cover how to set up a web server using Python and Flask on your Webdock server. Let’s go ahead without any further ado!
Prerequisites
- Webdock LEMP server
- A Sudo User
Installing Python
All Webdock’s default stack (LEMP/LAMP) comes with Python pre-installed. If you’re not sure, you can try installing Python. There are multiple ways to install Python on Ubuntu. But installing Python using APT is my favorite:
Firstly, update your system:
$ sudo apt update
Then download and install the latest version of Python:
$ sudo apt install python3
Installing PIP
Pip is the package manager for Python. Like Python, PIP is also pre-installed. You can always reinstall it when in doubt:
$ sudo apt-get install python3-pip
Installing and Setting up Flask
Flask is one of the many Python packages and a simple way to set up your Web Server. To install Flask, run the following as a non-sudo user:
$ pip install flask
And to set up Flask, you may need to create a new folder:
$ mkdir python-server
Then, open your newly made folder:
$ cd python-server
And then, create a new file:
$ nano __init__.py
(That’s two underscores)
And paste the following and save the file:
from flask import Flask, jsonify app = Flask(__name__) app.config['SECRET_KEY'] = "secretkey123" @app.route("/") # this sets the route to this page def home(): return jsonify({'Message': "Working"}) if __name__ == "__main__": app.run()
Setting up NGINX
Webdock’s LEMP stack comes with NGINX pre-installed. You would need to create a configuration file for your Flask application, so it can be accessed via the browser using NGINX as the gateway. To do so, create a new configuration file using the following:
$ sudo nano /etc/nginx/sites-enabled/python-server
(the config file name can be anything but I prefer using the folder name I used "python-server")
And paste the following by replacing the same with your IP address and save the file:
server {
listen 80;
server_name [server-ip];
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Instead of the IP address, you can use your domain name here but make sure you point your domain to your Webdock server's IP address.
Now execute the below command to check for syntax errors.
$ sudo nginx -t
You should see this output. If you see some error, that means a mistake has been made while copy-pasting the code.
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
If all is good, restart the NGINX server:
$ sudo systemctl restart nginx
Installing Gunicorn
Gunicorn acts as a gateway between your NGINX setup and your flask application. To install Gunicorn, you’d need to run the following:
$ sudo apt-get install gunicorn -y
Starting your Web server
Once you have installed and set up all the above modules, you just need to run a line of command to start your Application, but first, you would need to come to the home directory where you created the folder made in step 3. To do so, simply type and enter the following:
$ cd ..
This will take you to the folder above. Now, run the following:
$ gunicorn -w 5 python-server:app
(Replace the python-server with your folder name)
The number after -w indicates the number of cores dedicated to the worker. It's simple to calculate and should be 2x the number of cores on your server. Since I use an SSD bit instance, I have 2.5 cores, hence the number (2.5 x 2) Be sure to adjust the same depending on your profile.
Once you run the above command, your web server will start.
But your web server will close as soon as you close your SSH screen. To avoid this and to run your Flask application in the background, run the following:
$ sudo apt-get install screen -y
Followed by:
$ screen gunicorn -w 5 python-server:app
Hit Ctrl + A then D to detach from screen.
And that’s all on how you can set up a Python Web Application on your Webdock machine!
Conclusion
This article outlined steps on how to host a Python Flask web application with Gunicorn and Nginx.
Aayush Nair has been designing WordPress websites for eight years now. He is also a content writer, writing blogs on technology and related topics and likes helping people in his free time.
Related articles
-
How to install OpenLiteSpeed on Webdock
In this article we show you how you can install the OpenLiteSpeed web server on Ubuntu Jammy.
Last updated: March 29th 2023
-
How to Deploy your First Node.js Application on your Ubuntu Web Server
Deploy a Node.js application on your Ubuntu server and how to issue SSL certificates for your Node application.
Last updated: July 19th 2023
-
How to set up WireGuard on your Webdock Server
This article details how you can quickly and easily install WireGuard on your Webdock Server.
Last updated: July 21st 2024
-
How to set up OpenVPN on your Webdock Server
This article details how you can quickly and easily set up a VPN on your Webdock server.
Last updated: November 10th 2022
-
How to Install and configure aaPanel on Ubuntu
Install aaPanel on Ubuntu as well as building a LEMP stack, pointing your domain to your server and setting SSL certificates with Let's Encrypt.
Last updated: November 10th 2022
-
How to install azuracast on Webdock
This guide shows you how to work around certain issues when installing azuracast on Webdock.
Last updated: November 10th 2022
-
How to set up Runcloud on Webdock
This article details the steps you need to go through in order to install Runcloud on a Webdock server.
Last updated: July 29th 2024
-
How to set up cPanel on Webdock
This article details the steps you need to go through in order to install cPanel on a Webdock server.
Last updated: September 23rd 2024
-
How to set up Gridpane on Webdock
This article details the steps you need to go through in order to install Gridpane on a Webdock server.
Last updated: November 10th 2022
-
How to set up Ploi on Webdock
This article details the steps you need to go through in order to install Ploi on a Webdock server.
Last updated: November 10th 2022
-
How to set up Laravel Forge on Webdock
This article details the steps you need to go through in order to install Laravel Forge on a Webdock server.
Last updated: November 10th 2022
-
How to set up Plesk on Webdock
This article details the steps you need to go through in order to install Plesk on a Webdock server.
Last updated: November 10th 2022
-
How to set up Cyberpanel on Webdock
This article details the steps you need to go through in order to install Cyberpanel on a Webdock server.
Last updated: December 9th 2023
-
How to set up SpinupWP on Webdock
This article details the steps you need to go through in order to installSpinupWP on a Webdock server.
Last updated: March 15th 2023
-
How to set up DirectAdmin on Webdock
This article details the steps you need to go through in order to install DirectAdmin on a Webdock server.
Last updated: September 1st 2023
-
How to set up Hestia on Webdock
This article details the steps you need to go through in order to install Hestia on a Webdock server.
Last updated: November 10th 2022
-
How to set up Virtualmin on Webdock
This article details the steps you need to go through in order to install Virtualmin on a Webdock server.
Last updated: November 10th 2022
-
How to install and create pipelines in Jenkins
This guide describes the step-by-step procedure of installing Jenkins and creating pipelines in Jenkins.
Last updated: November 10th 2022
-
Basic WordPress site setup with aaPanel
In this guide, we will install and setup a basic WordPress site with aaPanel.
Last updated: January 23rd 2023
-
How to use Nginx as reverse proxy and secure connections with SSL certificates
Using Nginx to proxy pass your site with SSL security.
Last updated: July 19th 2023
-
Setting up monitoring with Netdata on your Webdock server
Setting up monitoring on your server to receive alerts and to know real-time resource consumption on your server.
Last updated: July 19th 2023
-
How to Daemonize an Application with Systemd
Using systemd to autostart your application on system startup.
Last updated: July 19th 2023
-
Set-up New Relic Monitoring on Your Webdock Server
This guide provides step-by-step instructions to install New Relic to monitor your VPS.
Last updated: July 29th 2024
-
Getting Started with Ruby on Rails on Webdock
In this guide, we will show you how to get started with Ruby on Rails on your Webdock server
Last updated: July 19th 2023
-
How to Install VaultWarden on Your Webdock Server
This guide provided step-by-step instructions to Vaultwarden, an open-source Password Manager on your Webdock Server.
Last updated: July 19th 2023
-
How to Install the Latest Version of HTOP on Ubuntu Server
Instructions to install latest htop package on your Ubuntu server
Last updated: July 29th 2024
-
How to Install ImageMagick 7 on Ubuntu LEMP/LAMP stacks
Simple instructions to install ImageMagick 7 along with the PHP extension
Last updated: November 1st 2023
-
A Quick Guide to Installing Rust on Ubuntu
Instructions to install Rust
Last updated: December 18th 2023
-
How To Install Proxmox on Your Webdock Server
This article provides instructions on how to install Proxmox on your Webdock server
Last updated: July 29th 2024
-
How To Run Nextcloud on Your Webdock Ubuntu Server
Instructions to Install Nextcloud on your server with Docker
Last updated: February 13th 2024
-
The Ultimate Guide to Setting Up Mastodon server
A detailed guide with instructions to set up Mastodon on your Webdock server
Last updated: February 20th 2024
-
A Guide To Setting Up Mindustry Game Server on Ubuntu
Step-by-step instructions to set up your own Mindustry server
Last updated: November 4th 2024
-
A Guide to Installing ERPNext on Your Webdock Server
Step-by-step instructions to install ERPNext - a resource planning software - on your server
Last updated: May 28th 2024
-
A Beginner's Guide to Installing Apache Kafka on Ubuntu
Detailed instructions to get Kakfa running on Ubuntu!
Last updated: June 20th 2024
-
A Quick Guide To Installing Rocket Chat on Ubuntu
Instructions to install Rocket Chat on your Ubuntu server
Last updated: July 2nd 2024
-
How to Install Ghost CMS on a Webdock Server
Easily install Ghost CMS on your Webdock server with these instructions!
Last updated: September 2nd 2024
-
How to Install cPanel on a Webdock Ubuntu Server
Instructions for setting up cPanel on an Ubuntu server
Last updated: September 23rd 2024