Prepare for deployment
Trước khi triển khai, thì chúng ta cần phải lưu ý một số biến môi trường như suitable
HOST="0.0.0.0"
PORT=8000
MYSQL_HOST="database-host-private-ipv4"
MYSQL_USER="admin"
MYSQL_USER_PASSWORD="letmein12345"
MYSQL_DATABASE="TODOAPP"
Và binary của Node là nằm ở trong /home/ubuntu/.nvm
chứ không nằm trong /usr/bin
hay /usr/local/bin
.

Chúng ta sẽ dùng đường dẫn này để tạo service.
Vào trong todoapp/server
, tiến hành cài đặt các dependencies
npm install

Move source code to OPT Directory
Đầu tiên thì chúng ta sẽ tạo một directory mới ở trong /opt
sudo mkdir -p /opt/todoapp

Sau đó copy cả thư mục với đường dẫn mới
sudo cp -R $HOME/todoapp/server /opt/todoapp

Create service
Giờ thì bắt đầu tạo Service cho NodeJS App.
sudo vi /etc/systemd/system/todoapp.service
Thêm các dòng này vào trong todoapp.service
[Unit]
Description=TodoApp Node.js Server
After=network.target
[Service]
# Replace bin của node ở đây nếu `which node` ra kết quả khác
ExecStart=/home/ubuntu/.nvm/versions/node/v20.18.0/bin/node /opt/todoapp/server/index.js
WorkingDirectory=/opt/todoapp/server
Restart=always
User=ubuntu
Group=ubuntu
Environment=HOST="0.0.0.0"
Environment=PORT=8000
# Thay địa chỉ IPv4 mật của Database Host
Environment=MYSQL_HOST="database-host-private-ipv4"
Environment=MYSQL_USER="admin"
Environment=MYSQL_USER_PASSWORD="letmein12345"
Environment=MYSQL_DATABASE="TODOAPP"
[Install]
WantedBy=multi-user.target

Lưu lại và nạp cấu hình mới
sudo systemctl daemon-reload

Lần lượt chạy và kiểm tra trạng thái của service
sudo systemctl start todoapp.service
sudo systemctl status todoapp.service

Giờ thì bật service, để mỗi khi máy reboot thì ứng dụng sẽ chạy lại theo máy
sudo systemctl enable todoapp.service
