skip to Main Content

I am trying to deploy a node js application on GoDaddy VPS hosting.

I have uploaded all the files to the server and started the server using pm2
following This Tutorial

My server is running on port 3021 which I want to run on port 80
for a particular domain.

I have completed steps till pm2 in the tutorial but from Nginx part, I cannot understand what to do next.

I have installed Nginx then running this command sudo vi /etc/nginx/sites-available/default

and adding configurations and when I am saving it an error shows [ Error writing /etc/nginx/sites-available/default: No such file or directory ]

I am running apache in the server. Is it possible to do the same using apache??

Edit

Here is my nginx config::

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;


include /usr/share/nginx/modules/*.conf;

events {
    worker_connections  1024;
}


http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;


    include /etc/nginx/conf.d/*.conf;
}

2

Answers


  1. Try to use /etc/nginx/conf.d/default.conf instead of /etc/nginx/sites-available/default to add your configuration. According to your config it should be the correct path to configure your server.

    If you want to use /etc/nginx/sites-available/defaultyou should add include /etc/nginx/sites-available/*; to your nginx.conf.

    http {
        ...
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-available/*;
    }
    
    Login or Signup to reply.
  2. So if you have pm2 running then you are half way through 🙂 About nginx conf. This command: sudo vi /etc/nginx/sites-available/default probably does not work because the sites-available directory does not exist. Keep in mind that sites-available and sites-enabled are not required by nginx. It’s just a good practice to have each server config in a separate file. But nginx will work without them too. If you are not planning to add any more servers, just use the /etc/nginx/nginx.conf file to configure your server. What you need to do is to create a new server block with desired configuration, e.x:

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  yourapp.com www.yourapp.com;
        root         /var/www/html; # <- for you it can be any other dir
        index        index.html index.htm # <- or any other file which is your main file
    
        # You want to proxy_pass the traffic on any route to your internal app
        location ~ /(.*) { # ~ means that you are using regex
            proxy_pass http://localhost:3021/$1$is_args$args; # <- proxy_pass to your internal app
            proxy_redirect off;
            proxy_read_timeout 60s;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
        }
    }
    

    Just place the above config into /etc/nginx/nginx.conf inside http block.

    Now check your nginx config:

    nginx -t
    

    If it’s successful, your nginx config is done.

    It’s not over yet. One more thing to do- open port 80 in the firewall, like so:

    firewall-cmd --zone=public --add-port=80/tcp --permanent
    firewall-cmd --reload
    

    You can check if it’s open:

    firewall-cmd --list-ports
    

    Open your browser at http://yourapp.com/ and see if it works.

    Remember to keep an eye on nginx and your app’s logs:

    tail -f /var/logs/nginx/access.log # or error.log to see nginx access/error log
    pm2 logs # to see your app logs
    

    Hope it helps. Do not hesitate to share more info if you encounter any more problems

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search