Hello, I'm Arthur. Building a Python application is one thing, but making it accessible online is another.
A VPS gives you a remote server where you can install your application and run it independently of your personal computer.
In this tutorial, we'll create a small Flask application and prepare it for deployment on a Linux VPS.
1. Create a Flask Application
First, install Flask:
pip install flask
Create a file called app.py:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello from my Flask application!"
if __name__ == "__main__":
app.run()
Run the application:
python app.py
Your Flask app will start locally. Open the address shown in your terminal to test it.
2. Prepare Your VPS
For this tutorial, you'll need a Linux VPS with SSH access.
Connect to your server:
ssh username@your_server_ip
Install Python and the required tools using your distribution's package manager.
For example, on Ubuntu:
sudo apt update
sudo apt install python3 python3-venv python3-pip
A VPS is useful when you want your application to run on a remote machine instead of relying on your personal computer.
If you're comparing hosting options, Seimaxim's VPS hosting is one place to explore server configurations for Python projects.
3. Upload Your Application
Create a project directory on the server:
mkdir my-flask-app
cd my-flask-app
Upload your app.py file using SCP or another secure file transfer method.
Create a virtual environment:
python3 -m venv venv
source venv/bin/activate
Install Flask:
pip install flask
Keeping dependencies in a virtual environment helps prevent conflicts between different Python projects.
4. Run the Application
For a quick test, you can run the Flask development server:
python app.py
However, Flask's built-in development server is not intended for production use.
For a real deployment, use a production WSGI server such as Gunicorn.
Install it:
pip install gunicorn
Then run:
gunicorn --bind 127.0.0.1:8000 app:app
This starts the application on the local server interface at port 8000.
A reverse proxy such as Nginx can then forward public web requests to Gunicorn.
5. Configure Your Domain and HTTPS
If you want visitors to access your application through a domain name, configure the domain's DNS records to point to your server.
You can then configure Nginx to forward requests to your Flask application.
For public websites, HTTPS is essential for protecting information exchanged between the browser and your server.
You can use a trusted certificate authority such as Let's Encrypt to obtain a certificate.
Final Thoughts
Deploying a Flask application on a VPS is a useful skill for Python developers.
It helps you understand Linux, SSH, Python environments, application servers, and basic web hosting.
Start with a small application, test it carefully, and add production features as your project grows.
When you're ready to host your own application, you can also explore Seimaxim for VPS hosting options.
Top comments (0)