Introduction
Deploying a Node.js application on DomainIndia's cPanel hosting is made simple with the built-in Node.js application management feature available in the cPanel Software section. For users requiring a powerful, scalable database solution, MongoDB Atlas offers a secure, cloud-based service that seamlessly integrates with your Node.js app.
This guide will provide step-by-step instructions for deploying a Node.js application through cPanel, connecting it to MongoDB Atlas, and managing firewall configurations using CSF to ensure secure connectivity.
Prerequisites
Before you begin, ensure you have the following:
- A hosting account with DomainIndia.com that includes cPanel access.
- Node.js application option available in the Software section of cPanel.
- A MongoDB Atlas account with a database cluster.
- Basic understanding of SSH (optional) for advanced management.
Step-by-Step Guide: Deploying a Node.js Application in cPanel
A. Setting Up Node.js on cPanel
-
Access Node.js in the Software Section: Log in to your DomainIndia cPanel account and navigate to the Software section. You will see an option labeled Node.js Web Applications.
-
Create a Node.js Application:
- Click on Create Application.
- Select the desired Node.js version.
- Define your application root, application URL, and start-up file (usually
app.js
). - Once the application is created, the Node.js environment will be set up automatically, and your app will be ready to deploy.
-
Managing Node.js via cPanel:
- You can start, stop, or restart the Node.js application directly from the cPanel interface.
- If needed, you can use SSH to access your Node.js environment for advanced control.
B. Creating a MongoDB Atlas Cluster
MongoDB Atlas is a cloud-hosted version of MongoDB, offering features like automated backups, security, and scalability. Since DomainIndia’s cPanel hosting doesn’t natively support MongoDB, MongoDB Atlas provides an excellent external database solution.
-
Create an Account on MongoDB Atlas:
- Go to the MongoDB Atlas website and sign up.
- Set up a free-tier or paid cluster depending on your needs.
-
Create a Cluster:
- Once logged in, click "Build a Cluster" and follow the prompts to configure your MongoDB Atlas cluster.
- The default settings are usually sufficient for most small to medium-sized projects.
-
Database and User Configuration:
- Create a new database user with appropriate privileges.
- Ensure you note the MongoDB Connection URI for connecting to your app.
Example MongoDB connection string:
mongodb+srv://username:password@cluster0.mongodb.net/myDatabase?retryWrites=true&w=majority
C.Connecting Your Node.js App to MongoDB Atlas
-
Install the MongoDB Driver:
- In your cPanel Node.js environment, you can install required Node.js packages. Navigate to the Node.js Web Applications section and use the Terminal or SSH to install the MongoDB driver (
Mongoose
):npm install mongoose
Configure the
.env
File:- Add a
.env
file in the root of your Node.js project to securely store your MongoDB connection string.
Example
.env
file:MONGO_URI=mongodb+srv://username:password@cluster0.mongodb.net/myDatabase?retryWrites=true&w=majority PORT=3000
- Add a
- In your cPanel Node.js environment, you can install required Node.js packages. Navigate to the Node.js Web Applications section and use the Terminal or SSH to install the MongoDB driver (
-
Update Your Application to Use the
.env
File:- Modify your
app.js
ordatabase.js
file to connect to MongoDB using the connection string from the.env
file:const mongoose = require('mongoose'); require('dotenv').config(); const mongoURI = process.env.MONGO_URI; mongoose.connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true }) .then(() => console.log('MongoDB connected successfully')) .catch((err) => console.error('MongoDB connection error:', err));
3.Deploying and Running the Node.js Application
-
Upload the Application to cPanel:
- Use File Manager in cPanel to upload your Node.js app files, or use FTP to transfer the files to your server.
-
Run the Node.js Application:
- After uploading, you can start or restart the Node.js application via the Node.js Web Applications interface in cPanel.
-
Configure Reverse Proxy via Apache:
- To allow external access to your Node.js application, use Apache’s
ProxyPass
to route traffic to the app.
Example Apache configuration (or use
.htaccess
):ProxyPass / http://localhost:3000/ ProxyPassReverse / http://localhost:3000/
- To allow external access to your Node.js application, use Apache’s
-
Setting Up Environment Variables in cPanel
You can manage environment variables directly in cPanel’s Node.js Web Applications section or by using a
.env
file. Store sensitive data like the MongoDB URI and port number here for security.What is MongoDB Atlas Cluster?
A MongoDB Atlas cluster is a cloud-hosted database cluster managed by MongoDB Atlas. It enables users to deploy scalable and secure MongoDB databases without managing physical infrastructure.
Key Benefits:
- Scalability: Automatically adjusts to your data and performance needs.
- Security: Requires IP whitelisting, ensuring that only authorized servers can connect.
- Availability: Replicates your data across multiple nodes, ensuring high availability.
MongoDB Atlas is especially useful for applications hosted on cPanel, where MongoDB isn’t natively supported.
Handling MongoDB Atlas Security: Whitelisting IP Addresses
MongoDB Atlas uses IP whitelisting to restrict database access to trusted servers. To connect your Node.js app to MongoDB Atlas, you need to whitelist the IP address of your DomainIndia hosting server.
Steps to Whitelist IP:
- Log in to your MongoDB Atlas dashboard.
- Navigate to the Network Access section.
- Click Add IP Address and input the IP address of your hosting server (e.g., 123.45.67.89).
- Apply the changes to allow your server to connect to MongoDB Atlas.
Working with CSF (ConfigServer Security & Firewall)
CSF (ConfigServer Security & Firewall) is used to manage incoming and outgoing network traffic. If MongoDB uses a specific port (like 27017), it may need to be whitelisted in CSF for external connectivity.
If you require MongoDB-related ports to be opened in CSF, DomainIndia can assist you in whitelisting specific ports.
Submit a support ticket if you need help whitelisting the MongoDB port.
Troubleshooting Common Issues
-
Node.js App Fails to Start:
- Check error logs via cPanel’s Node.js management interface.
- Ensure all dependencies are correctly installed using
npm install
.
-
Cannot Connect to MongoDB Atlas:
- Ensure the server’s IP address is whitelisted in MongoDB Atlas.
- Use
telnet
to verify the connection to MongoDB Atlas:telnet cluster0.mongodb.net 27017
Firewall Issues:
- Ensure MongoDB’s port (
27017
) is open in the firewall (CSF). - Contact DomainIndia support for port whitelisting.
- Ensure MongoDB’s port (
-
Performance Considerations
For larger applications or heavy database usage, consider upgrading to a VPS or dedicated server for better performance and control. Implement caching strategies (e.g., Redis) to reduce MongoDB queries and improve performance.
Additional Resources
Conclusion
With the built-in Node.js Web Applications feature in DomainIndia cPanel and the flexibility of MongoDB Atlas, deploying a scalable and secure Node.js app is easier than ever. For advanced configurations like CSF port whitelisting, feel free to contact DomainIndia support.
-
- Modify your