Deploy an Express API with MySQL database onto AWS EC2
Update OS && Install NodeJS
sudo apt update && sudo apt upgradecurl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -sudo apt install nodejs -y
Install MySQL database
sudo apt-cache search mysql-serversudo apt info -a mysql-server-8.0sudo apt install mysql-server-8.0sudo mysqlALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';create database dojo_blogexit
Rsync the code base into the server
rsync -avz --exclude 'node_modules' . ubuntu@54.95.88.8:~/dojo-apinpm installnpm startYou should be able to open the website with http://54.95.88.8:5000 outside server now
Create a service with systemd
sudo nano /etc/dojo-api.env
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=root
MYSQL_DATABASE=dojo_blog
sudo chmod 600 /etc/dojo-api.env && sudo chown ubuntu:ubuntu /etc/dojo-api.envNot really needed if it's already what it should besudo nano /etc/systemd/system/dojo-api.service
[Unit]
Description=Node.js App
After=network.target multi-user.target
[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/dojo-api
ExecStart=/usr/bin/npm start
Restart=always
Environment=NODE_ENV=production
EnvironmentFile=/etc/dojo-api.env
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=dojo-api
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reloadsudo systemctl enable dojo-api.servicesudo systemctl start dojo-api.serviceYou should be able to open the website with http://54.95.88.8:5000 outside server now again
Set up Caddy reverse proxy (A lightweight Nginx alternative)
- Create a record for your_domain.com under Route 53 Hosted zones, assuming that you have set up custom DNS for your registered domain name. (e. g. pointing api.peng.works to IP address 54.95.88.8)
- Cady Installation
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
- Caddy Configuration
sudo nano /etc/caddy/Caddyfile
api.peng.works {
reverse_proxy localhost:5000
}
- Restart Caddy
sudo systemctl restart caddy
sudo systemctl status caddy
# Make sure the port 443 is open and it should work now. Don't forget to close port 5000 after it works.
Bonus: How to Install NodeJS on Amazon Linux 2023
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bashsource ~/.bashrcnvm install --ltsnode --version && npm --version && which npm- Due to unknown reasons, the dojo-api.service can't successfually start on Amazon Linux 2023. Guessing it's something wrong with environment file handling, I simply forget the /etc/dojo-api.env file and reconfig the
/etc/systemd/system/dojo-api.serviceas below to make it work.
[Unit]
Description=Node.js App
After=network.target multi-user.target
[Service]
User=ec2-user
WorkingDirectory=/home/ec2-user/dojo-api
ExecStart=/home/ec2-user/.nvm/versions/node/v20.12.2/bin/node /home/ec2-user/dojo-api/app.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=dojo-api
[Install]
WantedBy=multi-user.target