wsgi_hosting - dwilson2547/wiki_demo GitHub Wiki
Absolutely, Daniel! Hosting a WSGI application involves several steps, from writing your app to deploying it on a server. Here’s a detailed, step-by-step guide to help you through the process:
- 1. Write Your WSGI Application
- 2. Choose a WSGI Server
- 3. Install the WSGI Server
- 4. Test Your Application Locally
- 5. Prepare for Production Deployment
- 6. Deploy Your Application
- 7. Run Your Application with a Process Manager
- 8. Set Up a Reverse Proxy (Nginx)
- 9. Secure Your App with SSL (HTTPS)
- 10. Monitor and Maintain
- Summary Table
First, ensure you have a WSGI-compliant application. Here’s a simple example using Flask (a popular WSGI framework):
# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello, WSGI World!"
if __name__ == '__main__':
app.run()
- This is a basic Flask app, but the same principles apply to Django, Pyramid, or even raw WSGI apps.
You need a WSGI server to run your application in production. Popular choices include:
- Gunicorn (recommended for beginners)
- uWSGI (highly configurable)
- Waitress (pure-Python, Windows-friendly)
- mod_wsgi (for Apache)
For this guide, we’ll use Gunicorn.
Install Gunicorn using pip:
pip install gunicorn
Run your app with Gunicorn to ensure it works:
gunicorn -w 4 -b 0.0.0.0:8000 app:app
-
-w 4
: Use 4 worker processes. -
-b 0.0.0.0:8000
: Bind to all network interfaces on port 8000. -
app:app
: The firstapp
is the module name (app.py
), and the secondapp
is the Flask application object.
Visit http://localhost:8000
in your browser. You should see your app running.
You can host your WSGI app on:
- VPS (Virtual Private Server): DigitalOcean, Linode, AWS EC2, etc.
- Platform-as-a-Service (PaaS): Heroku, Render, PythonAnywhere.
- Shared Hosting: Some providers support WSGI (e.g., A2 Hosting).
For this guide, we’ll use a VPS (e.g., DigitalOcean).
- Create a VPS instance and SSH into it.
-
Update the system:
sudo apt update && sudo apt upgrade -y
-
Install Python, pip, and dependencies:
sudo apt install python3 python3-pip python3-venv -y
Use git
, scp
, or rsync
to upload your code to the server. For example:
scp -r /path/to/your/app user@your-server-ip:/path/to/deploy
python3 -m venv /path/to/venv
source /path/to/venv/bin/activate
pip install -r requirements.txt
sudo pip install gunicorn
Use a process manager like systemd or Supervisor to keep your app running. Here’s how to set up a systemd service:
-
Create a service file:
sudo nano /etc/systemd/system/myapp.service
-
Add the following (adjust paths as needed):
[Unit] Description=Gunicorn instance for my WSGI app After=network.target [Service] User=your-user Group=www-data WorkingDirectory=/path/to/your/app Environment="PATH=/path/to/venv/bin" ExecStart=/path/to/venv/bin/gunicorn -w 4 -b 0.0.0.0:8000 app:app [Install] WantedBy=multi-user.target
-
Enable and start the service:
sudo systemctl daemon-reload sudo systemctl start myapp sudo systemctl enable myapp
A reverse proxy (like Nginx) handles static files, SSL, and forwards requests to your WSGI app.
-
Install Nginx:
sudo apt install nginx -y
-
Configure Nginx:
sudo nano /etc/nginx/sites-available/myapp
Add:
server { listen 80; server_name your-domain.com; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
-
Enable the site:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled sudo nginx -t sudo systemctl restart nginx
Use Let’s Encrypt to add SSL:
-
Install Certbot:
sudo apt install certbot python3-certbot-nginx -y
-
Obtain a certificate:
sudo certbot --nginx -d your-domain.com
-
Logs: Check logs for errors:
sudo journalctl -u myapp -f sudo tail -f /var/log/nginx/error.log
- Updates: Regularly update your server and dependencies.
Step | Action |
---|---|
Write App | Create a WSGI-compliant app (e.g., Flask, Django). |
Install WSGI Server | pip install gunicorn |
Test Locally | gunicorn -w 4 -b 0.0.0.0:8000 app:app |
Deploy Code | Upload to server (e.g., scp , git ). |
Virtual Environment | Create and activate a venv, install dependencies. |
Process Manager | Set up systemd/Supervisor to manage your app. |
Reverse Proxy | Configure Nginx to forward requests to your WSGI app. |
SSL | Secure with Let’s Encrypt. |
Monitor | Check logs and update regularly. |