• Contact
  • Contact Us
  • Disclamer
  • Home 1
  • Home 2
  • Home 3
  • Privacy Policy
Sunday, July 6, 2025
How VPS - How to use/setup VPS
  • Login
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
How VPS - How to use/setup VPS
No Result
View All Result
Home Operating System Linux

How to Setup Gunicorn to Serve Python Web Applications

How VPS by How VPS
January 1, 2020
in Linux
0
0
SHARES
17
VIEWS
Share on FacebookShare on Twitter

Contents

  1. Getting started
  2. Want to contribute?


In this article, we will setup a VPS to serve Python web applications with Gunicorn and Nginx as a reverse proxy. We use Nginx as a reverse proxy instead of serving with Gunicorn directly to prevent DoS attacks on Gunicorn. Using Nginx also results in better performance (with proxy buffering), and results in more requests being handled.

Getting started

This tutorial assumes that you already have a VM with Ubuntu 14.04 or newer installed, updated, and secured.

  • Our Nginx instance will run on HTTP port 80 (but you can easily upgrade to HTTPS).
  • Our example Python app will listen on port 8080. This port will be closed on iptables to prevent remote access.
  • This tutorial should run on other distros with minimal changes.

Install pip, virtualenv:

apt-get install python-pip python-virtualenv

Close port 8080. Gunicorn will be reachable only by Nginx.

iptables -A INPUT -p tcp --destination-port 8080 -j DROP

Create a new folder and configure our application. We’ll be using the Flask framework.

mkdir /var/www-folder/
cd /var/www-folder/
virtualenv exampleapp
source exampleapp/bin/activate
cd exampleapp
pip install gunicorn
pip install flask

Create the application file.

nano myapplication.py 

Paste the following inside:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, Vultr!'

This would produce a simple page saying “Hello, Vultr!” when someone accesses the page. Now, start Gunicorn. Replace x with 2 times the number of your VM’s CPU cores.

gunicorn -w x myapplication:app &

Install Nginx and configure the reverse proxy.

deactivate
apt-get install nginx
nano /etc/nginx/sites-enabled/default

Search for the location / { line and replace the content inside the brackets with the following content.

proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8000/;

This configuration instructs Nginx to forward some information (HTTP protocol, remote IP address) along with the actual request to the Gunicorn server. Reload Nginx.

service nginx reload

At this point, your Gunicorn application has been properly configured. You can access it using a web browser at http://your-vps-ip/.

Want to contribute?

You could earn up to $300 by adding new articles

Submit your article
Suggest an update
Request an article
Previous Post

Terraria Server with TShock on Linux

Next Post

How to Install and Use ArangoDB on Ubuntu 16.04

Next Post

How to Install and Use ArangoDB on Ubuntu 16.04

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

No Result
View All Result

Recent Post

Install Imagemagick on CentOS
CentOS

Install Imagemagick on CentOS

by How VPS
June 28, 2023
0

This is how I installed Imagemagick on a vanilla CentOS server Start off by installing the prerequisites yum install php-pear...

Read more
how to Check phpinfo

How to Check phpinfo of Hosting or VPS?

June 28, 2023
Failed to download metadata for repo 'appstream' on Centos 8

How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

February 25, 2022
How to Fix MySQL Error "Plugin 'InnoDB' registration as a STORAGE ENGINE failed"?

How to Fix MySQL Error “Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed”?

November 17, 2020
How to optimize Mysql or MariaDB

How to optimize Mysql or MariaDB

November 3, 2020

Recent News

  • Install Imagemagick on CentOS
  • How to Check phpinfo of Hosting or VPS?
  • How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

Category

  • Arch
  • Authentication
  • Backups
  • BSD
  • Centmin Mod
  • CentOS
  • Control Panels
  • CoreOS
  • CWP
  • Debian
  • Directadmin
  • Encryption
  • Fedora
  • Firewalls
  • Hocvps Script
  • Hosting providers
  • Kloxo-MR
  • Linux
  • Mitigations
  • Operating System
  • Plesk
  • Reviews
  • Securing VPS/Servers
  • Security Patches
  • SSL Certificates
  • Uncategorized
  • Upgrading
  • VPS/Servers management guides
  • Vulnerability Detection
  • Web servers software
  • Webhosting Control Panel
  • About
  • Advertise
  • Careers
  • Contact

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In