Skip to content

Instantly share code, notes, and snippets.

@asif633
Created August 28, 2019 04:38
Show Gist options
  • Save asif633/a115cca0e47b3ca804d0542c9aa695d5 to your computer and use it in GitHub Desktop.
Save asif633/a115cca0e47b3ca804d0542c9aa695d5 to your computer and use it in GitHub Desktop.
Nodejs, nginx server setup

Initial Setup server

  1. ssh root@your_server_ip
  2. adduser username
  3. usermod -aG sudo username
  4. ufw app list
  5. ufw allow OpenSSH
  6. ufw enable
  7. ufw status
  8. ssh username@your_server_ip

DNS to server public IP

  1. Set A, domain.com record to public_ip
  2. Set A, www.domain.com record to public_ip
  3. Set CNAME, if want sub.domain.com to public_ip

NGINX Setup

  1. sudo apt update
  2. sudo apt install nginx
  3. sudo ufw app list
  4. sudo ufw allow 'Nginx HTTP'
  5. sudo ufw status
  6. sudo systemctl status nginx
  7. sudo systemctl stop nginx
  8. sudo systemctl start nginx
  9. sudo systemctl restart nginx
  10. sudo systemctl reload nginx
  11. sudo systemctl disable nginx
  12. sudo systemctl enable nginx

Secure NGINX

  1. sudo add-apt-repository ppa:certbot/certbot
  2. sudo apt install python-certbot-nginx
  3. sudo nano /etc/nginx/sites-available/default
  4. server_name domain.com www.domain.com;
  5. sudo nginx -t
  6. sudo systemctl reload nginx
  7. sudo ufw status
  8. sudo ufw allow 'Nginx Full'
  9. sudo ufw delete allow 'Nginx HTTP'
  10. sudo ufw status
  11. sudo certbot --nginx -d domain.com -d www.domain.com
  12. Let’s Encrypt’s certificates are only valid for ninety days. This is to encourage users to automate their certificate renewal process. The certbot package we installed takes care of this for us by adding a renew script to /etc/cron.d.
  13. sudo certbot renew --dry-run

Multiple subfolders running different applications

  1. sudo nano /etc/nginx/sites-available/default

  2. ` index index.html server_name domain.com www.domain.com location / { try_files $uri $uri/ = 404; } location /hello/ { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; }

    `

  3. sudo nginx -t

  4. sudo systemctl restart nginx

Multiple domains as server blocks

  1. sudo mkdir -p /var/www/example.com/html
  2. sudo chown -R $USER:$USER /var/www/example.com/html
  3. sudo chmod -R 755 /var/www/example.com
  4. nano /var/www/example.com/html/index.html
<title>Welcome to Example.com!</title>

Success! The example.com server block is working!

6.sudo nano /etc/nginx/sites-available/example.com 7. server { listen 80; listen [::]:80;

    root /var/www/example.com/html;
    index index.html index.htm index.nginx-debian.html;

    server_name example.com www.example.com;

    location / {
            try_files $uri $uri/ =404;
    }

} 8.sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ 9. sudo nano /etc/nginx/nginx.conf 10. server_names_hash_bucket_size 64; 11. sudo nginx -t 12. sudo systemctl restart nginx

Install NODE and set up server

  1. curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
  2. sudo apt-get install nodejs
  3. sudo npm install pm2@latest -g
  4. pm2 start hello.js
  5. pm2 startup systemd
  6. pm2 save
  7. pm2 stop app_name_or_id
  8. pm2 restart app_name_or_id
  9. pm2 list
@asif633
Copy link
Author

asif633 commented Oct 4, 2019

Nodejs Install
sudo apt-get install nodejs; not needed
sudo npm install n -g
sudo n stable
logout then login again

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