Skip to content

Instantly share code, notes, and snippets.

@junaidulqayyumqureshi
Created May 22, 2024 09:40
Show Gist options
  • Save junaidulqayyumqureshi/0b19a236dc630ef9d3b2e69d33118de2 to your computer and use it in GitHub Desktop.
Save junaidulqayyumqureshi/0b19a236dc630ef9d3b2e69d33118de2 to your computer and use it in GitHub Desktop.
Server management commands
Server management commands
@junaidulqayyumqureshi
Copy link
Author

junaidulqayyumqureshi commented May 22, 2024

NGINX Configuration for Nodejs API

  1. To deploy a nodejs api, go to /etc/nginx/sites-available, and create a new file. Add following contents to it:
upstream mynodeapp {
    server 127.0.0.1:3003;
}
server {
    #server_name _; #Uncomment this if you want to keep it on ip
    #server_name domain.com www.domain.com; #Uncomment this if you want to deploy on a domain
    location / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_pass         http://mynodeapp;
    }
}
  • Make sure that the port you provide is the one you want to host your application on. In this case, port is 3003

  • This application will host directly on ip with port, and not on any subdomain
    -To host this application on a domain, instructions are given in the above snippet

  • After setting the site file, enable it by:
    sudo ln -s /etc/nginx/sites-available/[filename] /etc/nginx/sites-enabled/

  • Test nginx configuration:
    sudo nginx -t
    If there is an error, this command will show the error

  • Restart nginx:
    sudo systemctl restart nginx

@junaidulqayyumqureshi
Copy link
Author

junaidulqayyumqureshi commented May 22, 2024

NGINX Configuration for Web Portal

  1. To deploy a web portal, go to /etc/nginx/sites-available, and create a new file. Add following contents to it:
server {
    listen 81;

    #server_name _; #Uncomment this if you want to keep it on ip
    #server_name domain.com www.domain.com; #Uncomment this if you want to deploy on a domain

    root /var/www/stg.admin/build; #Path of the webportal
    index index.html;
    location / {
       try_files $uri $uri/ /index.html;
    }

}

If you DO NOT provide a port number, it will host on port 80 and if there is already a website hosted on same port, nginx will present error

  • Make sure that the port you provide is the one you want to host your application on. In this case, port is 81

  • This application will host directly on ip with port, and not on any subdomain
    -To host this application on a domain, instructions are given in the above snippet

  • After setting the site file, enable it by:
    sudo ln -s /etc/nginx/sites-available/[filename] /etc/nginx/sites-enabled/

  • Test nginx configuration:
    sudo nginx -t
    If there is an error, this command will show the error

  • Restart nginx:
    sudo systemctl restart nginx

@junaidulqayyumqureshi
Copy link
Author

Set folder permissions

sudo chown -R www-data:www-data /var/www/path;
sudo chmod 600 /var/www/path/.env;

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