Spaces:
Sleeping
Sleeping
| =============================== | |
| EC2 FastAPI Deployment Notes | |
| =============================== | |
| PROJECT: Blog Writing Agent | |
| STACK: FastAPI + uv + systemd + GitHub Actions CI/CD | |
| -------------------------------------------------- | |
| 1οΈβ£ Create systemd Service File | |
| -------------------------------------------------- | |
| Command: | |
| sudo nano /etc/systemd/system/blog-agent.service | |
| Why? | |
| - /etc/systemd/system/ is system-level directory | |
| - Normal user cannot create files here | |
| - sudo required for admin access | |
| - This file defines how our app runs as a service | |
| -------------------------------------------------- | |
| 2οΈβ£ Service File Content | |
| -------------------------------------------------- | |
| [Unit] | |
| Description=Blog Agent FastAPI App | |
| After=network.target | |
| [Service] | |
| User=ubuntu | |
| WorkingDirectory=/var/www/blog-agent | |
| ExecStart=/home/ubuntu/.local/bin/uv run uvicorn Application.app:app --host 0.0.0.0 --port 8000 | |
| Restart=always | |
| [Install] | |
| WantedBy=multi-user.target | |
| Why each line? | |
| User=ubuntu | |
| β Runs app as ubuntu user (safer than root) | |
| WorkingDirectory | |
| β Tells system where project exists | |
| ExecStart | |
| β Exact command to start FastAPI app | |
| Restart=always | |
| β If app crashes, systemd restarts it automatically | |
| WantedBy=multi-user.target | |
| β Makes service start when system boots | |
| -------------------------------------------------- | |
| 3οΈβ£ Reload systemd | |
| -------------------------------------------------- | |
| Command: | |
| sudo systemctl daemon-reload | |
| Why? | |
| - After creating new service file | |
| - systemd must reload configuration | |
| - Otherwise it wonβt detect new service | |
| -------------------------------------------------- | |
| 4οΈβ£ Enable Service (Auto Start on Reboot) | |
| -------------------------------------------------- | |
| Command: | |
| sudo systemctl enable blog-agent | |
| Why? | |
| - Makes app start automatically when server reboots | |
| - Without this, service wonβt auto start | |
| -------------------------------------------------- | |
| 5οΈβ£ Start Service | |
| -------------------------------------------------- | |
| Command: | |
| sudo systemctl start blog-agent | |
| Why? | |
| - Immediately start app without reboot | |
| -------------------------------------------------- | |
| 6οΈβ£ Check Status | |
| -------------------------------------------------- | |
| Command: | |
| sudo systemctl status blog-agent | |
| Why? | |
| - Verify if service is running | |
| - Shows logs and errors if any | |
| -------------------------------------------------- | |
| 7οΈβ£ Restart During Deployment (Used in CD) | |
| -------------------------------------------------- | |
| Command: | |
| sudo systemctl restart blog-agent | |
| Why? | |
| - After git pull & uv sync | |
| - Restart loads latest code | |
| - Clean exit code (no 143 error) | |
| - GitHub Actions shows GREEN β | |
| -------------------------------------------------- | |
| π Why We Stopped Using nohup & ? | |
| -------------------------------------------------- | |
| Problem: | |
| - SSH session close β background process killed | |
| - GitHub showed "Process exited with status 143" | |
| - Deployment looked failed even though server updated | |
| Solution: | |
| - Use systemd service | |
| - App runs independently of SSH | |
| - Clean process management | |
| - Production ready setup | |
| -------------------------------------------------- | |
| π― Final Deployment Flow | |
| -------------------------------------------------- | |
| GitHub Push | |
| β | |
| CI Run (Tests) | |
| β | |
| CD SSH into EC2 | |
| β | |
| git pull origin main | |
| uv sync | |
| sudo systemctl restart blog-agent | |
| β | |
| App Updated Successfully | |
| -------------------------------------------------- | |
| π₯ Important Concepts Learned | |
| -------------------------------------------------- | |
| β Difference between normal user & root | |
| β What sudo does | |
| β What systemd is | |
| β Why production apps use services | |
| β Why background (&, nohup) is not production safe | |
| β Why exit code 143 happens | |
| β Proper CI/CD deployment architecture | |
| -------------------------------------------------- | |
| π Result | |
| -------------------------------------------------- | |
| App: | |
| - Auto restarts if crashed | |
| - Auto starts on server reboot | |
| - Independent of SSH | |
| - Clean GitHub deployment (Green tick) | |
| Production-level deployment achieved. |