You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在同一Ubuntu服务器部署多学生Flask应用,实现用户名路径访问?

Solution for Student Flask Apps with Username-Based Path Access

Server Pre-Configuration (Professor completes once)

  1. Install Required Software

    • Install Nginx and Python tools via apt:
      sudo apt update && sudo apt install nginx python3-venv python3-pip
      
  2. 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>
      
  3. 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
      

Student-Specific Setup (Each student completes independently)

  1. 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).
  2. 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
      
  3. 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:app with your app's entry point (e.g., main:app if your file is named main.py).
      • The --umask=002 flag ensures Nginx can access the socket file.
  4. Verify Access

    • Visit http://myserver.com/<your-username> to confirm your app loads correctly.

Key Notes for Students

  • Use Flask's url_for helper 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 21:25:16