如何在同一Ubuntu服务器部署多学生Flask应用,实现用户名路径访问?
Solution for Student Flask Apps with Username-Based Path Access
Server Pre-Configuration (Professor completes once)
Install Required Software
- Install Nginx and Python tools via apt:
sudo apt update && sudo apt install nginx python3-venv python3-pip
- Install Nginx and Python tools via apt:
Set Up Central Student Directory
- Create a shared directory for all student apps and set permissions:
sudo mkdir -p /var/www/students sudo chown www-data:www-data /var/www/students sudo chmod 775 /var/www/students - For each student, create their personal subdirectory (or let students create it with your oversight):
sudo mkdir /var/www/students/<student-username> sudo chown <student-username>:www-data /var/www/students/<student-username> sudo chmod 750 /var/www/students/<student-username>
- Create a shared directory for all student apps and set permissions:
Configure Nginx Reverse Proxy
- Edit the default Nginx server block (e.g.,
/etc/nginx/sites-available/default) with this configuration:server { listen 80; server_name myserver.com; location ~ ^/([a-zA-Z0-9_-]+) { set $student $1; set $socket_path /var/www/students/$student/app.sock; # Return 404 if the student's app socket doesn't exist if (!-f $socket_path) { return 404; } # Proxy requests to the student's Unix socket proxy_pass http://unix:$socket_path; 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; # Pass subpath info to Flask proxy_set_header SCRIPT_NAME /$student; } } - Validate the config and reload Nginx:
sudo nginx -t sudo systemctl reload nginx
- Edit the default Nginx server block (e.g.,
Student-Specific Setup (Each student completes independently)
Upload Flask App
- SSH into the server and navigate to your assigned directory:
cd /var/www/students/<your-username> - Upload your Flask app files (use
git clone,scp, or other file transfer tools).
- SSH into the server and navigate to your assigned directory:
Set Up Virtual Environment
- Create and activate a Python virtual environment:
python3 -m venv venv source venv/bin/activate - Install Flask and Gunicorn:
pip install flask gunicorn
- Create and activate a Python virtual environment:
Launch the App
- Start your app using Gunicorn, binding to a Unix socket with proper permissions:
gunicorn --workers=2 --bind=unix:./app.sock --daemon --umask=002 app:app- Replace
app:appwith your app's entry point (e.g.,main:appif your file is namedmain.py). - The
--umask=002flag ensures Nginx can access the socket file.
- Replace
- Start your app using Gunicorn, binding to a Unix socket with proper permissions:
Verify Access
- Visit
http://myserver.com/<your-username>to confirm your app loads correctly.
- Visit
Key Notes for Students
- Use Flask's
url_forhelper in templates to handle static files (CSS/JS) and routes—this automatically accounts for the subpath. - To stop your app, find the Gunicorn process ID and terminate it:
ps aux | grep gunicorn kill <process-id> - For persistent runs (after server restarts), you can create a custom systemd service file (ask the professor for guidance if needed).
内容的提问来源于stack exchange,提问作者Erel Segal-Halevi
相关产品推荐
相关产品推荐

