Node, MySQL on EC2
This article summarizes the instructions for configuring an EC2 instance to run Node.js webapp with MariaDB backend, proxied via Nginx. Our first prefence is to use packages available in amazon-linux-extras. Both nginx and mariadb are available in extras. For Node.js we compile from source, install nodejs to yum repolist, perform yum install for nodejs. For production mode setup and to provide resiliency against instance restarts, we manually create systemd service for nodejs.
Outline of steps:
- Launch EC2 instance
- Install mariadb
- Install nodejs
- Install nginx
Install mariadb
amazon-linux-extras list | grep maria
sudo amazon-linux-extras install mariadb10.5
sudo systemctl enable mariadb
sudo systemctl start mariadb
sudo systemctl status mariadb
sudo mysql_secure_installation
n #for unix_socket authentication
Y #for change root password
mysql -u root -p
show engines;
show variables like '%innodb%';
cd /etc/my.cnf.d
sudo vi mariadb-server.cnf
Install nodejs
#install nodejs into yum repolist
sudo yum install -y gcc-c++ make
curl -sL https://rpm.nodesource.com/setup_16.x | sudo -E bash -
sudo yum install -y nodejs
node -e "console.log('Running Node.js ' + process.version)"
#config syslog for nodejs service
sudo touch /var/log/nodejs.log
sudo chmod 600 nodejs.log
sudo vi /etc/rsyslog.d/nodejs.conf
sudo systemctl restart rsyslog
#config systemd service for nodejs
sudo vi /etc/systemd/system/nodejs.service
sudo systemctl daemon-reload
sudo systemctl enable nodejs
sudo systemctl start nodejs
Install nginx
amazon-linux-extras list
sudo amazon-linux-extras install nginx1
sudo systemctl enable nginx
sudo mv ~/my_nginx.conf /etc/nginx/nginx.conf
sudo systemctl start nginx
sudo systemctl status nginx
Nodejs systemd config
# file: /etc/systemd/system/nodejs.service
[Unit]
Description=nodejs service for my-app
After=network.target
After=mariadb.service
[Service]
User=ec2-user
Group=ec2-user
WorkingDirectory=/home/ec2-user/my-app-dir
ExecStart=/usr/bin/node server.js
Restart=on-failure
RestartSec=10s
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodejs
[Install]
WantedBy=multi-user.target
Nodejs rsyslog config
# file: /etc/rsyslog.d/nodejs.conf
if $programname == 'nodejs' then /var/log/nodejs.log
& stop
Checking logs
sudo tail -f /var/log/messages
sudo tail -f /var/log/nodejs.log
sudo tail -f /var/log/nginx/access.log
Posted on
Dec 11, 2022 at 07:40