PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 1, 2022

[FIXED] How do I set my VPS Webmin/Virtualmin server to show data from MongoDB in the hosted website?

 August 01, 2022     mern, mongodb, virtualmin, vps, webmin     No comments   

Issue

This is my first question I hope I do it right. So :

I developed a MERN website, on which I have perfect connection with a MongoDB db as well as an Amazon S3 one.

I am currently trying to host it on a Hostinger VPS with Virtualmin and Webmin. The data is in thanks to FTP working, so the website design shows but the mongoDB data is missing.

So far :

  • DNS set properly,
  • SSH all good,
  • mongo shell installed inside of server through the console, I can see my db and the data
  • new user created successfully with mongo method db.createUser(), attached to my db

So my question is : what are the following steps to make the way to data, through the server, to the website ?

I'm new to this and I've searched for several days now everywhere without any success, and the hosting support is lost on the matter...

Thanks!


Solution

In case it helps anyone I did succeed in setting up the server, it's quite a bit of work. Here's my config :

  • I set Nginx to listen to the https requests from the front and send them to the back. The config file is called "default", in the folder sites-available, and has the following content :
server {
  listen  80;
  listen 443 ssl;

  root /the/frontend/root/folder;
  server_name _;
  
  ssl on;
  ssl_certificate /the/ssl/.crt/file;
  ssl_certificate_key /the/ssl/.key/file;

  # react app & front-end files
  location / {
    try_files $uri /index.html;
  }

  # node api reverse proxy
  location /api/ {
    proxy_pass http://localhost:portlistenedbybackend/api/;
  }
}
  • The React frontend comes with a .env file that is integrated in the build. In it I set the url to where the frontend sends requests (these are then caught by Nginx). Be careful to set this url to the domain of your website when deploying, so in my case : https://example.com/api

  • The production process manager pm2 is useful to keep alive the backend at all times so I installed it and used it for the Node backend. The command to add the backend main server file (in my case server.js) to pm2 from the console : sudo pm2 start your/serverfile/address

  • Here's a couple of links that proved very useful to understand how to configure the server :

    • Applicable to Amazon server but a lot applicable here too : https://jasonwatmore.com/post/2019/11/18/react-nodejs-on-aws-how-to-deploy-a-mern-stack-app-to-amazon-ec2

    • A guide to log journal in console for debugging : https://www.thegeekdiary.com/beginners-guide-to-journalctl-how-to-use-journalctl-to-view-and-manipulate-systemd-logs/

    • For setting up Webmin server : https://www.serverpronto.com/kb/cpage.php?id=Webmin

At first I discarded Webmin and Virtualmin since all I could find (from support included) was tutorials to setup the server via console. So bits by bits I set it up. Then, finally, I got from the support a tuto to setup the server from Webmin. But to this day I can't say wether that made a difference in the structure. But at least it's clean.

  • Last but not least, a couple of console commands I found very useful :

systemctl status theserviceyouwanttosee

systemctl start theserviceyouwanttostart

systemctl stop theserviceyouwanttostop

systemctl restart theserviceyouwanttorestart

Examples of services : nginx, mongod...

Test if Nginx is setup properly : sudo nginx -t

reload nginx config file after any modification : sudo nginx -s reload

See the last errors logged by nginx : sudo tail -f /var/log/nginx/error.log

Save current pm2 settings : pm2 save

Backend execution logs : sudo pm2 logs

Identify the processes still running with mongo (useful if mongod won't restart properly) : pgrep mongo

Kill the mongo process to be able to start fresh : kill <process>

Show all services used by server : sudo systemctl

See all processes in execution and stats of the CPU amongst other things : top

I'm still new to all of this despite my couple of weeks on the subject so this description is most probably improvable. don't hesitate to suggest any improvement, mistake, or ask questions, I'll do my best to answer them.

Cheers!



Answered By - ebrun
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing