Deploying a Node.js application using cPanel is a must-have skill for all web developers. Developers seeking to host their applications in a Node.js hosting environment greatly benefit from learning how to deploy a Node.js application. This comprehensive guide covers all the aspects of the deployment process, from preparing your local development environment to maintaining your production application.
Before You Begin
Before beginning the deployment process, here are some checklists to take care of:
- Have a Node.js application/project ready for deployment.
- Have your domain name set up.
- A code editor Visual Studio Code
- A FTP client like FileZilla (only if you decide to upload your zip file using FTP)
- Basic knowledge of Node.js and npm (Node Package Manager)
Step 1: Create Your Node.js Project
When you create a Node.js project, the system automatically generates a `package.json` file. This package.json file has all your app’s dependencies. In case you want to create the package.json file manually, then.
-
- Go to your project directory.
- Run
npm init
As you get your package.json file, let’s move to the next step.
Step 2: Open Your Project in a Code Editor
Open your Node.js project using your code editor (e.g., Visual Studio Code).
Step 3: Build Your Application
Go to your terminal and
Run npm run build
This prepares your app for the deployment stage.
Step 4: Create a ZIP File of Your Application
You’ll need to create a zip file with your project/Application. Be sure to include these:
- What to Include:
- The build folder.
- package.json File.
- Startup file (e.g., app.js, server.js) [Note: Include the same startup file as in your Local Host]
- Database config file (in case you use backend/database in your project).
- .env File (for environment variables).
- What Not to Include: node_modules (we’ll install them on the server). If you see existing node_modules, you can delete them.
Step 5: Log into Your cPanel Account
Log in to your cPanel Account using your cPanel URL and login credentials. If you do not have these, contact your hosting provider.
Step 6: Upload Your ZIP File
Navigate to the File Manage Section, under which move to the public_html folder. You have two options for file uploading.
- Option 1: Using cPanel File Manager
- Go to File Manager > public_html folder.
- Click Upload, select your ZIP file, and upload.
- Option 2: Using FileZilla (FTP)
- Connect to your hosting server using FileZilla.
- Navigate to public_html and upload the ZIP file.
Tips: Use cPanel File Manager for a smooth, hassle-free file upload experience.
Step 7: Set Up Node.js in cPanel
Now, after you upload the File, navigate to the Setup Node.js application in the cPanel.
- Go to Setup Node.js in cPanel and click Create Application.
- Enter:
- Application Path: Choose the directory for your app.
- Domain: Enter your domain name.
- Startup File: Enter the name of your startup file correctly (e.g., app.js, server.js).
- Click Create to save.
Step 8: Extract the ZIP File
- In File Manager, navigate to your public_html directory.
- Locate the uploaded ZIP file project, right-click it, and extract it inside the application root folder, i.e., the Application path.
- Delete the ZIP file after extraction.
Step 9: Remove Existing node_modules Folder
Look for the existing node_modules folder and delete it.
Step 10: Install Dependencies
You have two options for installing dependencies.
- Option 1: Using cPanel Interface
- Go to Node.js > Manage Application/ Edit Section> Install Dependencies via
Run npm install
- Option 2: Using cPanel Terminal
- Open Terminal in cPanel and stop the app if it’s running.
- Copy the virtual environment code and paste it into the terminal.
Run npm install
to install all required packages from package.json.
Step 11: Start the Application
- Go to Node.js in cPanel and click Start Application.
- Wait until the status changes to Running.
Step 12: Visit Your Application
- Click the hostname or URL provided by cPanel.
You’ll now be redirected to your app’s landing page.
Step 13: Verify Everything is Working
- Test all your app’s key features and ensure everything works.
- Check for any errors in the Console or your Application Logs.
Step 14: Finalize Your Process
- Monitor your app’s performance regularly and back up important files like your database and .env file.
- If your Application doesn’t start:
- Check the stderr.log/application log in cPanel.
- Verify file paths and environment variable settings.
- Ensure the correct Node.js version is selected.
- Check your database connection settings.
- If you see errors:
- Look for missing dependencies in your package.json.
- Check file permissions and ensure proper configurations.
- Ensure port settings and syntax are correct.
Backup Regularly
Always back up:
- Database content
- Configuration files
- Environment variables
Routine Maintenance
- Regularly monitor error logs.
- Update dependencies every month.
- Back up your data often.
- Be sure to check your app’s performance and disk space usage.
Security Measures
- Use an SSL certificate for a secure HTTPS communication
- Keep dependencies updated.
- Implement proper error handling.
- Set up firewalls and use secure environment variables.
- Document your deployment process for future reference.
- Keep track of configuration changes.
- Maintain backup copies of essential files.
- Regularly monitor your Application’s performance.
- Set up alerts for any potential issues.
Following the steps mentioned in this guide, you’ll have a smooth Node.js deployment on cPanel. Best regards on your project!
Learn how to choose the best Node.js hosting here.