skip to Main Content

i have been trying to deploy my first full MERN stack to an actual server and not Heroku and Netlify ,

I bought a server with a ubuntu OS,

  1. I set up the server,

2.installed nodejs, pm2, Nginx

  1. I used ssh to copy all the files in the react build folder :

    asset-manifest.json  favicon.ico  index.html  logo192.png  logo512.png  manifest.json  robots.txt  static```
    
    
  2. I set up Nginx to serve the folder as the default request for my IP in port 80

    server {
            listen 80;
            listen [::]:80;
    
            root /var/www/qdx/html;
            index index.html index.htm index.nginx-debian.html;
    
            server_name your_domain www.your_domain;
    
            location / {
                    try_files $uri $uri/ =404;
            }
    }```
    
    

so far so good and I can access my site using URL : http://185.97.117.14/

now I want to setup the backend on the same server so they can connect with each other :

5.I copied all the express code I have into the root of the server

```
ubuntu@first:~/server$ ls
config  node_modules  package.json  package-lock.json  README.md  src

```

then I set up pm2 using pm2 start

ubuntu@first:~/server$ pm2 show 0
 Describing process with id 0 - name qdx-server
???????????????????????????????????????????????????????????????????
? status            ? online                                      ?
? name              ? qdx-server                                  ?
? namespace         ? default                                     ?
? version           ? 1.0.0                                       ?
? restarts          ? 0                                           ?
? uptime            ? 7h                                          ?
? script path       ? /home/ubuntu/server/src/index.js            ?
? script args       ? N/A                                         ?
? error log path    ? /home/ubuntu/.pm2/logs/qdx-server-error.log ?
? out log path      ? /home/ubuntu/.pm2/logs/qdx-server-out.log   ?
? pid path          ? /home/ubuntu/.pm2/pids/qdx-server-0.pid     ?
? interpreter       ? node                                        ?
? interpreter args  ? N/A                                         ?
? script id         ? 0                                           ?
? exec cwd          ? /home/ubuntu/server/src                     ?
? exec mode         ? fork_mode                                   ?
? node.js version   ? 16.1.0                                      ?
? node env          ? N/A                                         ?
? watch & reload    ? ?                                           ?
? unstable restarts ? 0                                           ?
? created at        ? 2021-05-10T11:55:04.319Z                    ?
???????????????????????????????????????????????????????????????????
 Actions available
??????????????????????????
? km:heapdump            ?
? km:cpu:profiling:start ?
? km:cpu:profiling:stop  ?
? km:heap:sampling:start ?
? km:heap:sampling:stop  ?
??????????????????????????
 Trigger via: pm2 trigger qdx-server <action_name>

 Code metrics value
??????????????????????????????????????
? Heap Size              ? 29.31 MiB ?
? Heap Usage             ? 95.33 %   ?
? Used Heap Size         ? 27.94 MiB ?
? Active requests        ? 0         ?
? Active handles         ? 4         ?
? Event Loop Latency     ? 0.31 ms   ?
? Event Loop Latency p95 ? 1.07 ms   ?
??????????????????????????????????????
 Divergent env variables from local env
?????????????????????????????????????????
? XDG_SESSION_ID ? 18                   ?
? SSH_CLIENT     ? 5.123.134.1 44336 22 ?
? OLDPWD         ? /home/ubuntu/server  ?
? SSH_TTY        ? /dev/pts/0           ?
? PWD            ? /home/ubuntu/server/ ?
? SSH_CONNECTION ? 5.123.134.1 44336 18 ?
?????????????????????????????????????????

6.although the express app should run in the port 5000
and I have used firewall and gave access to the API , I cant seem to open the express app and get it connected ,

ubuntu@first:/etc/nginx/sites-enabled$ sudo ufd
sudo: unable to resolve host first
sudo: ufd: command not found
ubuntu@first:/etc/nginx/sites-enabled$ sudo fwd
sudo: unable to resolve host first
sudo: fwd: command not found
ubuntu@first:/etc/nginx/sites-enabled$ sudo ufw status
sudo: unable to resolve host first
Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
80                         ALLOW       Anywhere
Nginx HTTP                 ALLOW       Anywhere
5000                       ALLOW       Anywhere
22 (v6)                    ALLOW       Anywhere (v6)
80 (v6)                    ALLOW       Anywhere (v6)
Nginx HTTP (v6)            ALLOW       Anywhere (v6)
5000 (v6)                  ALLOW       Anywhere (v6)

I have been working on this and trying to learn everything for a week now just to learn to deploy ,
any help would be greatly appreciated

2

Answers


  1. Is the server app running? Do you see the port open (netstat -an |grep 5000)?
    The webserver and server app are on same host so you should be able to access via localhost:5000

    I do like Taleodor’s answer that they linked to. Makes things a lot more simple and portable if needed.

    Login or Signup to reply.
  2. If you wan’t to use nginx you can do:

    server {
        listen 80;
        listen [::]:80;
    
        root /var/www/qdx/html;
        index index.html index.htm;
    
        server_name your_domain www.your_domain;
    
        location / {
                proxy_pass http://localhost:5000;
        }
    }
    

    I recommend this source: webdock tutorial
    to learn about how to set things up.

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