Spaces:
Sleeping
Sleeping
File size: 3,996 Bytes
63de3ab | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | =============================== 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. |