The MERN stack, consisting of MongoDB, Express, React, and Node, is a powerful combination of technologies for building and deploying full-stack web applications. By leveraging the versatility of JavaScript and JSON, the MERN stack enables developers to create a 3-tier architecture, encompassing the frontend, backend, and database, all within a unified development framework.
In this tutorial, we will guide you through the process of installing the MERN stack on Debian 11, ensuring that you have all the necessary prerequisites in place before proceeding with the installation. We will cover the installation of MongoDB Server, Node.js, React.js, and Express, taking you step by step through each component of the stack.
Prerequisites
Before we begin, make sure you have the following prerequisites:
- A server running Debian 11.
- A root password configured on the server.
Getting Started
To get started, it is recommended to update your system’s package cache to the latest version. Open the terminal and enter the following command:
apt-get update -y
This will update the package cache on your Debian 11 system. Once the update is complete, you can proceed to install the other required dependencies by entering the following command:
apt-get install gnupg2 curl -y
This command will install the necessary dependencies for the MERN stack installation on Debian 11.
Installing MongoDB Server
MongoDB is a NoSQL, object-oriented database technology that is widely used for large data storage. By default, MongoDB is not included in the Debian 11 default repository. Therefore, we need to add the MongoDB repository to APT.
To add the MongoDB GPG key, run the following command:
wget -qO https://www.mongodb.org/static/pgp/server-4.4.asc | apt-key add -
Next, add the MongoDB repository to APT by running the following command:
echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/4.4 main" | tee /etc/apt/sources.list.d/mongodb-org-4.list
After adding the repository, update the repository and install the MongoDB server package by entering the following command:
apt-get update -y apt-get install mongodb-org -y
Once the installation is complete, start the MongoDB service and enable it to start at system reboot by running the following commands:
systemctl start mongod systemctl enable mongod
To verify the MongoDB version, enter the following command:
mongod --version
If MongoDB is installed correctly, you should see output similar to the following:
db version v4.4.8 Build Info: { "version": "4.4.8", "gitVersion": "83b8bb8b6b325d8d8d3dfd2ad9f744bdad7d6ca0", "openSSLVersion": "OpenSSL 1.1.1k 25 Mar 2021", "modules": [], "allocator": "tcmalloc", "environment": { "distmod": "debian10", "distarch": "x86_64", "target_arch": "x86_64" }}
Congratulations! You have successfully installed MongoDB on your Debian 11 system. Now, let’s move on to installing Node.js.
Installing Node.js
Node.js is a runtime environment that allows you to run JavaScript on the server-side, outside of the browser. It utilizes an event-driven model, making it lightweight and scalable for distributed applications.
To install the latest version of Node.js, we will add the NodeSource repository to our system. Enter the following command to download and install the repository:
curl -sL https://deb.nodesource.com/setup_16.x | bash -
Once the repository is installed, run the following command to install Node.js version 16:
apt-get install nodejs -y
To verify the installation, enter the following command:
node --version
If Node.js is installed correctly, you should see output similar to the following:
v16.8.0
Congratulations! You have successfully installed Node.js on your Debian 11 system. Now, let’s proceed with the installation of React.js.
Installing React.js
React.js is an open-source JavaScript library used for developing front-end web applications. It allows developers to create reusable components for building user interfaces.
To install React.js, we will use the create-react-app tool, which can be installed via npm (Node Package Manager). Enter the following command to install create-react-app globally:
npm install -g create-react-app
Once the installation is complete, you can create a new React application by running the following command:
create-react-app reactapp
This will create a new directory called “reactapp” and scaffold a basic React application inside it. You will see the following output:
Success! Created reactapp at /root/reactapp Inside that directory, you can run several commands: npm start Starts the development server. npm run build Bundles the app into static files for production. npm test Starts the test runner. npm run eject Removes this tool and copies build dependencies, configuration files and scripts into the app directory. If you do this, you can’t go back! We suggest that you begin by typing: cd reactapp npm start Happy hacking!
To start the React application, change to the “reactapp” directory by running the following command:
cd reactapp
Then, start the application by running the following command:
npm start
This will start the development server, and you can access your React application in your web browser by visiting http://localhost:3000.
Congratulations! You have successfully installed and set up React.js on your Debian 11 system. Now, let’s move on to installing Express.
Installing Express
Express is a fast and minimalist web application framework for Node.js. It simplifies the process of building web applications by providing a set of modular and flexible features.
To install Express, we will use npm. Enter the following command to install Express globally:
npm install -g express-generator
Once the installation is complete, you can create a new Express application by running the following command:
express mearnapp
This will create a new directory called “mearnapp” and scaffold a basic Express application inside it. You will see the following output:
create : mearnapp/ create : mearnapp/public/ create : mearnapp/public/javascripts/ create : mearnapp/public/images/ create : mearnapp/public/stylesheets/ create : mearnapp/public/stylesheets/style.css create : mearnapp/routes/ create : mearnapp/routes/index.js create : mearnapp/routes/users.js create : mearnapp/views/ create : mearnapp/views/error.jade create : mearnapp/views/index.jade create : mearnapp/views/layout.jade create : mearnapp/app.js create : mearnapp/package.json create : mearnapp/bin/ create : mearnapp/bin/www change directory: $ cd mearnapp install dependencies: $ npm install run the app: $ DEBUG=mearnapp:* npm start
To start the Express application, change to the “mearnapp” directory by running the following command:
cd mearnapp
Then, install all the application dependencies by running the following command:
npm install
Finally, start the application by running the following command:
npm start
This will start the Express server, and you can access your Express application in your web browser by visiting http://localhost:3000.
Congratulations! You have successfully installed and set up Express on your Debian 11 system. You now have the complete MERN stack installed and ready to use.
Conclusion
In this tutorial, we have covered the installation of the MERN stack on Debian 11. By following the step-by-step instructions, you have successfully installed MongoDB, Node.js, React.js, and Express, enabling you to develop and deploy JavaScript-based web applications.
The MERN stack offers a powerful and efficient framework for building full-stack applications, combining the strengths of MongoDB, Express, React, and Node. With this stack, you have the flexibility to develop scalable and feature-rich applications, all within a unified JavaScript ecosystem.
We hope this guide has been helpful in getting you started with the MERN stack on Debian 11. If you have any further questions or need assistance, feel free to reach out to our support team at Shape.host. Shape.host offers reliable and secure cloud hosting solutions, including Cloud VPS, to help businesses thrive in the digital landscape.