Learn how to deploy a full MERN stack (MongoDB, Express, React, Node.js) application on a VPS with ubuntu. This guide covers essential steps, including server setup, database configuration, and application deployment to ensure a smooth and scalable production environment. Perfect for developers looking to host their web apps.
30 min
Edited:16-09-2024
Deploying a MERN stack application to a VPS is an essential step for developers aiming to move their projects from local development to a live, production-ready environment. The MERN stack, which combines MongoDB, Express, React, and Node.js, offers a powerful full-stack framework for building modern web applications. This guide walks you through the process of setting up your VPS, configuring your database, and deploying your application to ensure it runs smoothly and securely in a real-world setting.
We will guide you on how to install each one in this guide
Node.js is a powerful, open-source JavaScript runtime built on Chrome's V8 engine, widely used for building scalable network applications. It’s lightweight, efficient, and highly versatile, making it a favorite among developers. This guide will walk you through installing Node.js on an Ubuntu server.
Before starting any installation, it's a good practice to update your system’s package index to ensure you are working with the latest package versions.
1. sudo apt update
2. sudo apt upgrade -y
This command updates the list of available packages and their versions and then upgrades any out-of-date packages on your system.
Ubuntu's default repository often contains Node.js. To check for the version available in the repository, use the following command:
1. sudo apt install nodejs
After installation, verify the version by running:
1. node -v
However, the version in the default repositories may not always be the latest one. If you need the latest version, consider using NodeSource
If you want to install a specific or the latest version of Node.js, you can use the NodeSource repository. First, install the NodeSource PPA:
1. curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
The above command adds the NodeSource repository for the LTS (Long-Term Support) version of Node.js. . For a specific version like by replacing the version like this:
1. deb.nodesource.com/setup_16.x
You can also install the latest version (instead of LTS) by running the command:
1. curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
Once the repository is added, install Node.js:
1. node -v
2. npm -v
If npm is not installed, you can install it with the command:
1. sudo apt install npm
You can run the commands below to remove nodejs from the system, this is useful if the package you installed had issues, or you want to install another version:
1. sudo apt remove nodejs
2. sudo apt purge nodejs
It is always recommended to install the latest versions of packages to get all the features and fixes, but keep in mind that the LTS versions are the versions that are currently have the long term support and usually it is the most used
You’ve now installed Node.js on your Ubuntu server. Whether you used the default repository, NodeSource,your environment is now set up to start developing with Node.js
MongoDB is a popular NoSQL database known for its flexibility and scalability, making it a top choice for modern applications. This guide will walk you through the installation of MongoDB on an Ubuntu server.
Before installing any software, update the package index to ensure your system is working with the latest packages.
1. sudo apt update
2. sudo apt upgrade -y
For the community edition you can follow the steps below, but please keep in mind that the versions may change over time, so it is better to check the official documentation, you can see them here
In this guide the ubuntu version used is Ubuntu 22.04 (Jammy)
1- Import the Public Key
1. sudo apt-get install gnupg curl
To import the MongoDB public GPG key, run the following command:
1. curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \ sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \ --dearmor
2- Create the List File
1. echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
3- Reload the Package Database
1. sudo apt-get update
4- Install MongoDB Community Server
1. sudo apt-get install -y mongodb-org
Now that MongoDB is installed, you need to start the MongoDB service and ensure it starts automatically on boot.
1. sudo systemctl start mongod
Enable MongoDB to start on boot:
1. sudo systemctl enable mongod
Check the status of MongoDB to ensure it is running:
1. sudo systemctl status mongod
You can verify that MongoDB is working correctly by connecting to the database and running some basic commands.
Start the MongoDB shell:
1. mongosh
You’ve successfully installed and configured MongoDB on your Ubuntu server. Whether you’re using it for development or production, MongoDB’s flexibility and scalability make it a powerful choice for your data storage needs. If you're planning to deploy it on production, don’t forget to secure your database with authentication and limit access to trusted IPs.
React is a popular JavaScript library for building user interfaces, particularly single-page applications (SPAs). It's known for its efficiency, flexibility, and robust ecosystem. In this guide, you'll learn how to install React and create a new React project on an Ubuntu system.
1. sudo apt update
2. sudo apt upgrade -y
create-react-app is an officially supported command-line tool that helps you create a new React.js project with a pre-configured build setup.
1. sudo npm install -g create-react-app
This installs create-react-app globally, allowing you to use the command anywhere on your system.
Now that create-react-app is installed, you can initiate a new React project by running the following command:
1. npx create-react-app my-react-app
Replace my-react-app with the name you want for your project. The command will generate all the necessary files and install the required dependencies. This may take a few minutes, depending on your internet speed and system.
Once the project has been created, navigate to your project directory:
1. cd my-react-app
Now that you're in your project folder, you can start the development server by running:
1. npm start
This will start the local development server and automatically open your default web browser, displaying the React app. The app will be accessible at:
1. http://localhost:3000
Whenever you make changes to your React components, the app will automatically reload to reflect the changes, providing a smooth development experience.
Now you can start building the React application by editing the files with your code editor like VScode for example
Once you've finished developing your React app and want to prepare it for production, you can build the application by running:
1. npm run build
This will create a production-ready version of your React application in the build/ folder. You can then deploy the files from the build/ directory to a web server.
Using React directly in production is generally not recommended because it lacks essential features that frameworks like Next.js, Remix, or Gatsby provide out-of-the-box for better performance, scalability, and developer experience.
Congratulations! You've successfully installed React and created a new React project on your Ubuntu system. You now have a fully functional development environment for building React applications. From here, you can start building your React components, routing, and state management to create interactive user interfaces.
Deploying a backend server using Express.js in a MERN (MongoDB, Express, React, Node.js) stack is a key part of getting your full-stack application online. In this guide, we’ll walk through the steps to deploy an Express server, assuming you have an existing MERN stack project.
First, we need to create the directory where we want our ExpressJS server to run, you can use the commands below to create the folder and access it:
1. mkdir myapp
2. cd myapp
Use the npm init command to create a package.json file for your application. For more information on how package.json works,
1. npm init
This command prompts you for a number of things, such as the name and version of your application. For now, you can simply hit Enter to accept the defaults for most of them:
On the npm init procees you will see entry point: (index.js). this means what file the server should mainly run from, Enter app.js, or whatever you want the name of the main file to be. If you want it to be index.js, hit Enter to accept the suggested default file name.
Now, install Express in the myapp directory and save it in the dependencies list. For example:
1. npm install expres
You’ve successfully deployed your Express server on an Ubuntu server, You can now connect your React frontend to the Express backend, ensuring seamless communication between them.
deploying a MERN stack project involves several key steps, from setting up your server and database to configuring your environment for optimal performance. By properly installing MongoDB, Express, React, and Node.js on your VPS, and ensuring your application is secured and optimized, you can smoothly transition from development to production. With everything set up, your MERN stack application is ready to serve real users, providing a dynamic, full-stack web experience. This process not only makes your application accessible but also scalable and maintainable for future updates.
14-10-2024
This article offers a detailed guide on installing and configuring IPTables on an Ubuntu VPS. IPTables is a powerful firewall tool that helps secure your server by controlling inbound and outbound traffic. Learn how to set up rules for traffic filtering, configure basic security policies, and apply custom rules to protect your VPS.
IPtables
security
12 min
This article offers a comprehensive guide on installing and configuring ModSecurity, a powerful web application firewall (WAF), on an Ubuntu VPS. Learn how to secure your server by filtering and monitoring HTTP requests, set up ModSecurity with Nginx or Apache, and apply rules to protect against common web attacks.
Modsecurity
security
10 min
14-10-2024
This article provides a comprehensive guide on installing and configuring PHP-FPM (FastCGI Process Manager) on an Ubuntu VPS. Learn how to optimize PHP performance for your web applications by configuring PHP-FPM with Nginx or Apache, managing pools, and fine-tuning settings for efficient processing of PHP scripts.
PHP-FPM
speed
optimise
12 min