AzureVM and Flask APP
2. Create VM with properties below
3. VM’s Operations
Download the Private Key File:
chmod 600 /home/AlexYang/Tools/Pearl_2022.pem
Login:
ssh -i /home/AlexYang/Tools/Pearl_2022.pem yangxb@172.189.57.114
Upload Files:
scp -i /home/AlexYang/Tools/Pearl_2022.pem /home/AlexYang/workspace/online_quiz.zip yangxb@172.189.57.114:/home/yangxb/
4. Python & Flask Enviroments
Flask App Folder:
/home/yangxb/online_quiz
Create New Virtue Enviroments:
yangxb@Alex2026:~/online_quiz$ python3 -m venv quiz_env
yangxb@Alex2026:~/online_quiz$ source quiz_env/bin/activate
Set up requirements:
pip install numpy==1.21.0 –no-cache-dir
pip install -r requirements.txt –no-cache-dir
5. Gunicorn & Nginx Configuration
#!/bin/bash
echo "开始部署Online Quiz应用..."
echo "安装Nginx..." sudo apt update -q sudo apt install nginx -y -q
echo "部署Gunicorn应用..." cd /home/yangxb/online_quiz source quiz_env/bin/activate pip install gevent -q
cat > gunicorn_config.py << 'EOF' bind = "unix:/home/yangxb/online_quiz/online_quiz.sock" workers = 8 worker_class = "gevent" timeout = 120 accesslog = "/home/yangxb/online_quiz/logs/access.log" errorlog = "/home/yangxb/online_quiz/logs/error.log" loglevel = "info" EOF
echo "配置Nginx..." sudo tee /etc/nginx/sites-available/online_quiz << 'EOF' server { listen 80; server_name _; location / { proxy_pass http://unix:/home/yangxb/online_quiz/online_quiz.sock; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } EOF
sudo ln -sf /etc/nginx/sites-available/online_quiz /etc/nginx/sites-enabled/ sudo rm -f /etc/nginx/sites-enabled/default sudo nginx -t
echo "配置systemd服务..." sudo tee /etc/systemd/system/online_quiz.service << 'EOF' [Unit] Description=Online Quiz Flask Application After=network.target
[Service] User=yangxb Group=yangxb WorkingDirectory=/home/yangxb/online_quiz Environment="PATH=/home/yangxb/online_quiz/quiz_env/bin" ExecStart=/home/yangxb/online_quiz/quiz_env/bin/gunicorn -c gunicorn_config.py app:app Restart=always RestartSec=10
[Install] WantedBy=multi-user.target EOF
echo "设置权限..." sudo chown yangxb:yangxb /home/yangxb/online_quiz -R mkdir -p logs chmod 755 logs
echo "启动服务..." sudo systemctl daemon-reload sudo systemctl enable online_quiz sudo systemctl start online_quiz sudo systemctl restart nginx
echo "开放防火墙端口..." sudo ufw allow 80 --yes sudo ufw allow ssh --yes sudo ufw --force enable
SERVER_IP=$(curl -s ifconfig.me)
echo "========================================" echo "✅ 部署完成!" echo "🌐 访问地址:http://$SERVER_IP" echo "========================================" echo "" echo "📋 管理命令:" echo " 查看状态:sudo systemctl status online_quiz" echo " 查看日志:sudo journalctl -u online_quiz -f" echo " 重启服务:sudo systemctl restart online_quiz" echo " 查看Nginx日志:sudo tail -f /var/log/nginx/error.log"
|
chmod +x deploy_all.sh ./deploy_all.sh
|
6. Flask APP Management Commands
Daily Operations
- Start:
sudo systemctl start online_quiz
- Stop:
sudo systemctl stop online_quiz
- Restart:
sudo systemctl restart online_quiz
- Status:
sudo systemctl status online_quiz
Nginx Management
- Restart:
sudo systemctl restart nginx
- Reload:
sudo systemctl reload nginx
Log Inspection
- App logs:
sudo journalctl -u online_quiz -f
- Nginx errors:
sudo tail -f /var/log/nginx/error.log
- Access logs:
sudo tail -f /var/log/nginx/access.log
Quick Checks
- Port listening:
sudo netstat -tulpn | grep :80
- Process status:
ps aux | grep gunicorn
Site URL
http://$(curl -s ifconfig.me)
7. GitHub Deploy
1) 生成 SSH 密钥(推荐)
# 生成 SSH 密钥 ssh-keygen -t ed25519 -C "your_email@example.com" # 或使用 RSA # ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# 查看公钥 cat ~/.ssh/id_ed25519.pub
# 添加到 GitHub # 1. 登录 GitHub → Settings → SSH and GPG keys → New SSH key # 2. 粘贴公钥内容
|
2) 本地 Git 初始化
# 进入项目目录 cd /path/to/your/project
# 初始化 Git 仓库 git init
# 查看状态 git status
|
创建 .gitignore 文件
# 创建 .gitignore 文件(非常重要!) cat > .gitignore << 'EOF' # 系统文件 .DS_Store Thumbs.db *.swp *~
# 编辑器/IDE .vscode/ .idea/ *.sublime-*
# 运行时文件 *.pyc __pycache__/ *.so *.dylib
# 日志文件 *.log logs/
# 依赖目录 node_modules/ venv/ env/ .env .env.local
# 构建输出 dist/ build/ *.egg-info/
# 数据库文件 *.db *.sqlite3
# 临时文件 /tmp/ *.tmp EOF
|
添加文件到仓库
# 添加所有文件(排除 .gitignore 中指定的) git add .
# 提交到本地仓库 git commit -m "Initial commit: 项目初始化"
# 关联远程仓库 git remote add origin git@github.com:用户名/仓库名.git
# 第一次推送 git push -u origin main
|
3) 日常开发工作流
(1). 基本操作
# 查看状态 git status
# 查看提交历史 git log --oneline --graph
# 添加文件 git add file1.py file2.py
# 提交更改 git commit -m "Add feature X"
# 推送更改 git push
# 拉取最新代码 git pull
|
(2). 分支管理
# 创建并切换分支 git checkout -b feature/new-feature
# 查看分支 git branch
# 切换分支 git checkout main
# 合并分支 git merge feature/new-feature
# 删除分支 git branch -d feature/new-feature
|
(3). 撤销操作
# 撤销未提交的修改(危险!) git checkout -- file.py
# 撤销暂存的文件 git reset HEAD file.py
# 修改上次提交 git commit --amend -m "新的提交信息"
# 回退到某个提交 git reset --hard commit_id
|
7. Config Domains
Domain Server: www.namecheap.com
My Domain: yangxb.org