Skip to content

Instantly share code, notes, and snippets.

@trandaison
Created January 13, 2022 12:03
Show Gist options
  • Save trandaison/49c9c0df15a156fb7d905948ee70c136 to your computer and use it in GitHub Desktop.
Save trandaison/49c9c0df15a156fb7d905948ee70c136 to your computer and use it in GitHub Desktop.
Setup New FE Server on EC2 (PM2)
module.exports = {
apps: [
{
exec_mode: 'cluster',
script: './server.js',
args: 'start',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'development',
},
env_production: {
NODE_ENV: 'production',
},
},
],
deploy: {
dev: {
user: 'dev',
host: '18.222.247.184',
repo: '[email protected]:trandaison/trandaison.github.io.git',
ref: 'origin/develop',
path: '/home/dev/trandaison.github.io',
'post-deploy': 'yarn && yarn build && pm2 start',
},
},
};
server {
listen 80;
server_name example.me;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

Reference: https://devcoops.com/install-nginx-on-aws-ec2-amazon-linux/

❌ Install nginx as root user, switch to root user before installing

sudo su -

Enable and Install EPEL repo on AWS EC2 Amazon Linux 2 instance

Step 1. Firstly, check if the EPEL repository is enabled:

sudo amazon-linux-extras list | grep epel

If you get an output like:

24  epel      available    [ =7.11  =stable ]

Then it means that you should enable it before installing Nginx.

Step 2. To enable the EPEL repo on Amazon Linux 2 instance run:

sudo amazon-linux-extras enable epel

Step 3. Now you should be able to install the EPEL repo:

sudo yum install epel-release

Install Nginx on AWS EC2 Amazon Linux 2 instance

Step 1. Once the EPEL repo is installed you can install the Nginx latest version as well:

sudo yum install nginx

Step 2. Verify the installation:

nginx -v

Output:

nginx version: nginx/1.20.1

References: https://www.cyberciti.biz/faq/nginx-restart-ubuntu-linux-command/

Common Commands

sudo systemctl start nginx 
sudo systemctl stop nginx 
sudo systemctl restart nginx

sudo service nginx start
sudo service nginx stop
sudo service nginx restart

sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx restart

nginx -t # Check syntax if OK before reloading
nginx -T # Check syntax and print the output before reloading
nginx -s reload  # Reload

❌ Exit root user before install nvm

Install NVM

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Install Node

nvm install v14.17.3

Verify node version

node -v

Install pm2

npm install pm2 -g

Make pm2 auto-boot at server restart

pm2 startup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment