SoFunction
Updated on 2025-04-08

Common methods and comparisons of publishing web applications in Python

The following are common methods and detailed steps for Python to publish web applications, covering the entire process from local development to production environment deployment:

1. Basic preparation: Developing Web applications

1. Select the framework (taking Flask as an example)

# 
from flask import Flask
app = Flask(__name__)

@('/')
def home():
    return "Hello, World!"

if __name__ == '__main__':
    (debug=True)

2. Installation dependencies

pip install flask

2. Local test run

python 
# Visit http://localhost:5000

3. Production environment deployment method

Method 1: Traditional Server Deployment (Nginx + Gunicorn)

1. Install the necessary components

# Ubuntu/Debian
sudo apt update
sudo apt install python3-pip nginx
pip install gunicorn

2. Configure Gunicorn to start the application

# Start command (bind to Unix socket)gunicorn --bind unix: app:app

# or bind to port (for debugging)gunicorn --bind 0.0.0.0:8000 app:app

3. Configure Nginx reverse proxy

Create configuration file /etc/nginx/sites-available/myapp:

server {
    listen 80;
    server_name your_domain.com;

    location / {
        proxy_pass http://unix:/path/to/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    # Static file processing (optional)    location /static {
        alias /path/to/your/static/files;
    }
}

Enable configuration:

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo systemctl restart nginx

4. Use Systemd to manage processes

[Unit]
Description=Gunicorn instance for myapp
After=

[Service]
User=ubuntu
WorkingDirectory=/path/to/app
ExecStart=/usr/local/bin/gunicorn --workers 3 --bind unix: app:app
Restart=always

[Install]
WantedBy=

Start the service:

sudo systemctl start myapp
sudo systemctl enable myapp

Method 2: One-click deployment of cloud platform (taking Heroku as an example)

1. Install the Heroku CLI

curl / | sh
heroku login

2. Prepare the deployment file

Procfile (no extension):

web: gunicorn app:app

flask
gunicorn

3. Deploy to Heroku

heroku create
git push heroku master
heroku open

Method 3: Docker containerized deployment

1. Write Dockerfile

FROM python:3.9-slim
WORKDIR /app
COPY  .
RUN pip install --no-cache-dir -r 
COPY . .
CMD ["gunicorn", "--bind", "0.0.0.0:80", "app:app"]

2. Build and run the image

docker build -t myapp .
docker run -d -p 80:80 --name myapp myapp

3. Publish to Docker Hub

docker tag myapp yourusername/myapp
docker push yourusername/myapp

Method 4: Serverless deployment (AWS Lambda + API Gateway)

1. Use Zappa (for Flask)

pip install zappa
zappa init  # Follow the prompts to configurezappa deploy dev

2. Access the generated API endpoint

4. Key Optimization and Security

Configuration

Use Let’s Encrypt Free Certificate:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d your_domain.com

2. Firewall settings

sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable

3. Static file cache

Add in Nginx configuration:

location /static {
    expires 30d;
    add_header Cache-Control "public";
}

V. Verification and deployment

Visit test: The browser opens the URL provided by http://your_domain.com or the cloud platform

Log check:

journalctl -u  -f  # View Gunicorn logstail -f /var/log/nginx/

6. Comparison of deployment methods

method Applicable scenarios advantage shortcoming
Nginx + Gunicorn Self-built server/VPS Fully controlled, high performance High maintenance costs
Heroku Rapid prototypes, small projects Simple and free, no operation and maintenance required Resource limitations, costs increase with scale
Docker Cross-environmental deployment, microservice architecture Environmental isolation, easy to expand Need to learn Docker technology stack
Serverless Event-driven, low-frequency access applications On-demand billing, automatic expansion Cold start delay, architecture limitations

Choose the most suitable program based on project size, team skills and budget.

This is the article about the common methods and comparisons of publishing web applications in Python. For more related content on publishing web applications in Python, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!