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:

  1. Launch EC2 instance
  2. Install mariadb
  3. Install nodejs
  4. 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 | 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
Description=nodejs service for my-app

ExecStart=/usr/bin/node server.js



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