Skip to content

How To Deploy a Flask Application on an Ubuntu VPS

Cloud Infrastructure Expert
How To Deploy a Flask Application on an Ubuntu VPS

 

 

Flask is a lightweight WSGI web application framework in Python. This guide will walk you through deploying a Flask application on an Ubuntu VPS.

1. Update Your System

Ensure your system is up-to-date:

sudo apt-get update
sudo apt-get upgrade

2. Install Required Packages

Install Python, pip, and virtualenv:

sudo apt-get install python3 python3-pip python3-venv

3. Create a Project Directory

Create a directory for your Flask project:

mkdir ~/myflaskapp
cd ~/myflaskapp

4. Set Up a Virtual Environment

Create and activate a virtual environment:

python3 -m venv venv
source venv/bin/activate

5. Install Flask

Install Flask and any other dependencies within the virtual environment:

pip install Flask

6. Create a Flask Application

Create a simple Flask application. Create a file named app.py:

nano app.py

Add the following content to app.py:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return 'Hello, Flask!'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

Save and close the file (press Ctrl+X, then Y, and Enter).

7. Test the Flask Application

Run your Flask application to make sure it works:

python app.py

Open a web browser and navigate to http://your_server_ip:5000. You should see “Hello, Flask!” displayed.

8. Set Up Gunicorn

Install Gunicorn, a WSGI HTTP server for Python web applications:

pip install gunicorn

Run your Flask application with Gunicorn:

gunicorn --bind 0.0.0.0:8000 app:app

9. Set Up Nginx

Install Nginx to act as a reverse proxy for Gunicorn:

sudo apt-get install nginx

Create a new Nginx configuration file for your Flask app:

sudo nano /etc/nginx/sites-available/myflaskapp

Add the following configuration:

server {
    listen 80;
    server_name your_server_ip;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Enable the configuration and restart Nginx:

sudo ln -s /etc/nginx/sites-available/myflaskapp /etc/nginx/sites-enabled/
sudo service nginx restart

10. Set Up a Systemd Service

Create a systemd service file to manage your Flask application:

sudo nano /etc/systemd/system/myflaskapp.service

Add the following content:

[Unit]
Description=Gunicorn instance to serve myflaskapp
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/myflaskapp
Environment="PATH=/home/ubuntu/myflaskapp/venv/bin"
ExecStart=/home/ubuntu/myflaskapp/venv/bin/gunicorn --workers 3 --bind 0.0.0.0:8000 app:app

[Install]
WantedBy=multi-user.target

Start and enable the service:

sudo systemctl start myflaskapp
sudo systemctl enable myflaskapp

11. Troubleshooting

If you encounter issues, check the logs for Gunicorn, Nginx, and your systemd service:

sudo journalctl -u myflaskapp
sudo tail -f /var/log/nginx/error.log

12. Further Configuration

For additional configuration and optimization, refer to the Flask and Gunicorn documentation, as well as Nginx and systemd manuals.

Windows VPS Deutschland

Windows VPS España

Windows VPS Nederland

Windows VPS Italia

Windows VPS Portugal

VPS Windows Italia

Windows VPS

Windows VPS

Windows VPS Sverige

Windows VPS Norge

Windows VPS

Windows VPS Türkiye

Windows RDS (Remote Desktop Services)

Windows VPS

Explore more

More on this topic

Netcloud24
Netcloud24
Cloud Infrastructure Expert · NetCloud24

Comments are closed.