Deploy a MERN Project on Windows VPS server

Deploy a MERN Project on Windows VPS server

Deploying a MERN (MongoDB, Express.js, React, Node.js) app on a Windows VPS involves several steps. Here's a general guide to get you started:

  1. Set Up Your Windows VPS:

    • Connect to your Windows VPS server via Remote Desktop (search in your PC remote desktop connection and connect to your server through this)

    • Update Windows and install any necessary software.

  2. Install Node.js:

    • Download the installer from the official Node.js website.

    • Run the installer and follow the prompts. This will also install npm, which you'll use to manage Node packages.

  3. Install MongoDB:

    • Download the MongoDB installer for Windows from the official MongoDB website.

    • Install MongoDB by following the installation guide. Ensure you set MongoDB to run as a service so it starts automatically with the server.

    • Consider setting up authentication for MongoDB for added security.

  4. Clone Your App:

    • If your app is stored on a Git repository, install Git for Windows.

    • Clone your MERN app to a directory of your choice on the VPS.

  5. Install Dependencies:

    • Navigate to the directory where your app is stored.

    • Run npm install to install all Node dependencies for your backend.

    • If you have a separate frontend directory for your React app, navigate there and run npm install again.

  6. Environment Variables & Configuration:

    • Ensure that you set any required environment variables for your app. You can set permanent environment variables through the Windows System Properties or you can send your config file from various file transfer protocols like Filezilla

    • Update any database connection strings to point to the MongoDB instance on your VPS.

  7. Start Your App:

    • If you have separate backend and frontend directories:

      • Navigate to your backend directory and run node server.js (or whatever your entry file is named).

      • Navigate to your frontend directory and run npm start or build your React app using npm run build and serve it using a static server or reverse proxy.

    • If it's a unified app, navigate to your app directory and run node server.js.

  8. Set Up a Reverse Proxy (Optional but Recommended):

    • For production deployments, it's a good idea to use a reverse proxy like NGINX or Apache to serve your app.

    • This allows you to easily use SSL, load balancing, and provides additional security.

  9. Install PM2 (Optional):

    • PM2 is a production process manager for Node.js apps. It allows your app to auto-start on boot and restart if it crashes.

    • Install PM2 globally with npm install pm2 -g.

    • Start your app with PM2 using pm2 start server.js.

  10. Set Up Security:

    • Make sure to open only necessary ports in the Windows Firewall.

    • Always use HTTPS for production deployments. You can get free SSL certificates from Let's Encrypt.

    • Consider using tools like Helmet.js for Express to add extra security headers and protections.

  11. Backup:

    • Regularly backup your MongoDB database and app files to prevent data loss.