How to Deploy Mern Stack on Digitalocean?

5 minutes read

To deploy a MERN stack application on DigitalOcean, you first need to create a Droplet on DigitalOcean with the necessary resources based on your application requirements. You will then need to SSH into your Droplet and clone your MERN stack application repository.


Next, you will need to install Node.js, MongoDB, and any other dependencies required for your application. Make sure to configure your MongoDB database properly and set up environment variables for your application.


After setting up the environment, build your MERN stack application using npm run build or any other build script specified in your package.json file. You can then start your application using npm start or any other start script specified in your package.json file.


Finally, you will need to configure Nginx or any other web server to serve your MERN stack application. Update your Nginx configuration file to redirect incoming requests to your MERN stack application running on a specific port.


Once everything is set up and configured correctly, your MERN stack application should be successfully deployed and accessible on DigitalOcean. Make sure to monitor and manage your Droplet and application regularly to ensure optimal performance and security.


How to push code to a GitHub repository?

To push code to a GitHub repository, follow these steps:

  1. Ensure that you have Git installed on your computer. If not, download and install Git from the official website.
  2. Create a GitHub account and login to your account.
  3. Create a new repository on GitHub by clicking on the "New Repository" button on the top right corner of the GitHub homepage.
  4. Give your repository a name, description, and choose whether it should be public or private. Click on the "Create Repository" button.
  5. Copy the URL of the repository that appears on the screen. It should look something like: https://github.com/username/repository-name.git
  6. Open your terminal or command prompt on your computer.
  7. Change to the directory where your code is located by using the cd command.
  8. Initialize a new Git repository by running the command git init.
  9. Add your files to the staging area by running the command git add .
  10. Commit your changes by running the command git commit -m "Your commit message here"
  11. Link your local repository to your GitHub repository by running the command git remote add origin (replace with the URL you copied in step 5).
  12. Push your code to the GitHub repository by running the command git push -u origin master
  13. Enter your GitHub username and password when prompted.


Your code should now be pushed to your GitHub repository successfully. You can go back to the GitHub website and refresh the page to see your code files.


How to configure Nginx for a MERN stack app?

To configure Nginx for a MERN stack app, you will need to set up a reverse proxy to direct traffic to the appropriate servers. Here are the steps to configure Nginx for a MERN stack app:

  1. Install Nginx on your server:
1
2
sudo apt-get update
sudo apt-get install nginx


  1. Create a new server block configuration file for your MERN app:
1
sudo nano /etc/nginx/sites-available/your-app-name


Inside this file, add the following configuration:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
server {
    listen 80;

    server_name your-domain-name.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /api {
        proxy_pass http://localhost:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}


  1. Enable the server block configuration and test the Nginx configuration:
1
2
3
sudo ln -s /etc/nginx/sites-available/your-app-name /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx


  1. Make sure that your MERN stack app is running on the specified ports (3000 for the front end and 5000 for the backend) and test your app by accessing your domain in a web browser.


By following these steps, you should now have Nginx configured to serve your MERN stack app.


How to install Node.js on a server?

To install Node.js on a server, you can follow these steps:

  1. Update your package manager: Before installing Node.js, make sure your package manager is up to date. For example, if you are using apt on Ubuntu, run the following command: sudo apt update
  2. Install Node.js: Use your package manager to install Node.js. For example, on Ubuntu, you can install Node.js using apt by running the following command: sudo apt install nodejs
  3. Install npm: npm is a package manager for Node.js. You can install it along with Node.js by running the following command: sudo apt install npm
  4. Verify the installation: After the installation is complete, you can verify that Node.js and npm are installed by checking their versions using the following commands: node -v npm -v
  5. Install a process manager (optional): If you are planning to run Node.js applications in production, it is recommended to use a process manager such as PM2. You can install PM2 using npm by running the following command: sudo npm install -g pm2
  6. Start your Node.js application: You can now start your Node.js application using the following command: node app.js
  7. (Optional) Use PM2 to manage your application: If you installed PM2, you can use it to manage your Node.js application. For example, you can start your application using PM2 by running the following command: pm2 start app.js


That's it! You have successfully installed Node.js on your server.


What is Git?

Git is a decentralized version control system that is widely used for tracking changes in source code during software development. It allows multiple developers to collaborate on a project simultaneously and keeps track of all the changes made to the codebase over time. Git also provides features for branching, merging, and resolving conflicts, making it a powerful tool for managing code revisions and ensuring the integrity of a project's codebase.


What is SSH?

SSH, which stands for Secure Shell, is a network protocol that allows for secure and encrypted communication between two devices over an insecure network. It is commonly used for remote login or command execution on a remote computer. SSH provides confidentiality and integrity of data transferred between two systems by encrypting the data during transmission. It also provides authentication using public-key cryptography to ensure the security of the connection.

Facebook Twitter LinkedIn Telegram

Related Posts:

To upload images from the web to DigitalOcean Space, you can use the DigitalOcean Control Panel or a command line tool such as the AWS Command Line Interface (CLI).To upload images using the DigitalOcean Control Panel, first log in to your DigitalOcean account...
To delete files from DigitalOcean via Flutter, you can use the DigitalOcean Spaces API and the dio package in Flutter. First, you will need to make an HTTP request to the DigitalOcean Spaces API endpoint for deleting a specific file. You will need to include t...
To get the DigitalOcean environment variable, you can use the DigitalOcean Metadata API. This API allows you to access information about your droplet, including its IP address, region, and other metadata. By making an HTTP request to the metadata endpoint http...
To upload a folder to DigitalOcean Spaces, you will first need to access your Spaces dashboard on the DigitalOcean website. Once in the dashboard, select the specific Space you would like to upload the folder to. Next, click on the "Upload" button and ...
To deploy a Nest.js app on DigitalOcean, you can follow these general steps:Set up a Droplet on DigitalOcean and choose the appropriate server size and location.SSH into your server and install Node.js and NPM.Clone your Nest.js app repository onto the server ...