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.