最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Ubuntu Python Web 开发
时间:2026-07-19 17:47:03 编辑:袖梨 来源:一聚教程网
Ubuntu Python Web Development: A Step-by-Step Guide

Python is a top choice for web development on Ubuntu due to its simplicity and the availability of robust frameworks like Flask, Django, and FastAPI. Below is a structured guide to setting up your environment, choosing a framework, building a basic app, and deploying it with Nginx.
1. Prerequisites: Install Python and pip
Ubuntu typically includes Python 3 pre-installed, but you should verify and update it to the latest version. Run these commands in the terminal:
sudo apt updatesudo apt install python3 python3-pip python3-venvCheck the installation with:
python3 --version# Should return Python 3.x.xpip3 --version # Should return pip x.x.x from Python 32. Set Up a Virtual Environment
Virtual environments isolate project dependencies to avoid conflicts. Create and activate one:
python3 -m venv myenv# Create a virtual environment named "myenv"source myenv/bin/activate# Activate the environment (your terminal prompt will change)Deactivate with deactivate when done.
3. Choose a Web Framework
Select a framework based on your project needs:
- Flask: Lightweight and flexible, ideal for small to medium projects or prototypes.
- Django: Full-stack framework with built-in ORM, authentication, and admin panels—perfect for large, complex applications.
- FastAPI: Modern, high-performance framework for APIs (supports async), great for real-time apps.
Install your chosen framework:
pip3 install Flask# For Flaskpip3 install Django # For Djangopip3 install fastapi uvicorn # For FastAPI (uvicorn is the ASGI server)4. Build a Basic Application
Flask Example
Create a file named app.py and add this code:
from flask import Flaskapp = Flask(__name__)@app.route('/')def hello():return 'Hello, World from Flask!'Run the app:
python3 app.pyVisit http://127.0.0.1:5000 in your browser to see the message.
Django Example
Create a project and app:
django-admin startproject myproject# Create a Django projectcd myprojectpython3 manage.py startapp myapp # Create an app named "myapp"Register the app in myproject/settings.py by adding myapp to the INSTALLED_APPS list. Run the development server:
python3 manage.py runserverVisit http://127.0.0.1:8000 to see Django’s welcome page.
FastAPI Example
Create a file named main.py:
from fastapi import FastAPIapp = FastAPI()@app.get("/")def read_root():return {"message": "Hello, World from FastAPI!"}Run the app with Uvicorn:
uvicorn main:app --reloadVisit http://127.0.0.1:8000 to see the JSON response. Use http://127.0.0.1:8000/docs to view auto-generated Swagger UI docs.
5. Deploy to Production with Nginx
For production, use Nginx as a reverse proxy to handle traffic and serve your app securely.
Install Nginx
sudo apt install nginxConfigure Nginx
Edit the default Nginx configuration file:
sudo nano /etc/nginx/sites-available/defaultReplace the location / block with this (adjust for your app’s port):
location / {proxy_pass http://127.0.0.1:5000;# For Flask (default port)# proxy_pass http://127.0.0.1:8000;# For Django/development serverproxy_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;}Save (Ctrl+O, Enter) and exit (Ctrl+X). Test the configuration:
sudo nginx -tRestart Nginx to apply changes:
sudo systemctl restart nginx6. Optional: Use Gunicorn for Production
For better performance, use Gunicorn (a Python WSGI server) instead of running the app directly. Install Gunicorn:
pip3 install gunicornRun your Flask app with Gunicorn:
gunicorn -w 3 -b 127.0.0.1:5000 app:app# 3 workers, bind to localhost:5000Update the Nginx proxy_pass directive to point to 127.0.0.1:5000 (as shown above).
Key Tips for Success
- Virtual Environments: Always use them to manage dependencies.
- Port Management: Flask/Django run on different ports by default (5000/8000)—adjust as needed.
- Security: In production, disable debug mode (
debug=Falsein Flask/Django) and use HTTPS (via Let’s Encrypt). - Async Support: FastAPI excels at async operations—use it for real-time features (e.g., WebSockets).
This guide covers the basics to get you started with Python web development on Ubuntu. From here, you can explore framework-specific features (e.g., Django’s ORM, Flask extensions) or advanced deployment techniques (e.g., Docker, Kubernetes).
相关文章
- 逆战未来枪械大全 逆战未来枪械稀有度等级与获取方式详解 07-19
- 逆战未来新手攻略 逆战未来萌新入门避坑指南 07-19
- "无醇啤酒"是完全不含酒精吗 蚂蚁庄园1月17日答案早知道 07-19
- "太常寺"是古代掌管什么事务的机构 蚂蚁新村1月16日答案 07-19
- 四川名菜"开水白菜"就是用开水炖白菜吗 蚂蚁庄园1月17日答案早知道 07-19
- 蚂蚁新村今天正确答案1.16 07-19