How to Deploy the Next.js App to Cpanel?

17 minutes read

To deploy a Next.js app to cPanel, you can follow these steps:

  1. Build the Next.js app: Open your terminal or command prompt, navigate to the root directory of your Next.js app, and run the command npm run build. This will generate an optimized build of your application.
  2. Zip the build folder: Once the build is complete, locate the newly created /.next folder and compress it into a zip file. You can use the built-in compression utility of your operating system or a third-party tool.
  3. Access cPanel: Log in to your cPanel account provided by your hosting provider. This will grant you access to the cPanel dashboard.
  4. Locate the File Manager: In the cPanel dashboard, locate and click on the "File Manager" icon. This will open the file management tool for your hosting account.
  5. Upload the zip file: In the File Manager, navigate to the public_html directory or the directory where you want to host your Next.js app. Click the "Upload" button in the toolbar and select the zip file you created in step 2. Wait for the upload to complete.
  6. Extract the zip file: Once the zip file is uploaded, select it in the File Manager and click the "Extract" button in the toolbar. This will unzip the contents of the file into the current directory.
  7. Set up deployment configurations (optional): If required, configure your deployment settings, such as custom server routes or environment variables, according to your Next.js app's requirements.
  8. Configure the entry point: By default, cPanel assumes that your app's entry point file is index.html. If your Next.js app uses a different entry point file, such as index.js or server.js, you will need to rename your main Next.js file (ex: index.js) to index.html.
  9. Verify the deployment: Once the extraction is complete, visit your website's URL through a web browser. Your Next.js app should be up and running, fully deployed on cPanel.


It's worth mentioning that deploying a Next.js app to cPanel requires access to the server and certain configurations that may vary depending on the hosting provider or server setup.

Best Next.js Hosting Providers in 2024

1
DigitalOcean

Rating is 5 out of 5

DigitalOcean

2
AWS

Rating is 4.9 out of 5

AWS

3
Vultr

Rating is 4.8 out of 5

Vultr

4
Cloudways

Rating is 4.7 out of 5

Cloudways


What are the recommended settings for deploying Next.js on cPanel?

Deploying a Next.js application on cPanel requires specific settings to ensure optimal performance. Here are the recommended settings:

  1. Node.js: Ensure that a compatible version of Node.js is installed on your cPanel server. You can usually find this option in the "Software" or "Setup Node.js App" section of cPanel.
  2. Package Installation: Next.js requires several dependencies. Install the required packages by navigating to the project directory using SSH or the terminal, and running the command npm install.
  3. Building the Application: Next.js applications need to be built before deployment. Use the command npm run build to build your application. This will generate an optimized and production-ready version of your application in the .next directory.
  4. Node.js Application Setup: In cPanel, locate the "Setup Node.js App" section and create an application. Set the application mode to "Production" and choose the Node.js version that matches the one installed on the server.
  5. Application Entry Point: In the Node.js application setup, set the "Entry Point" to ./node_modules/next/dist/server/index.js. This tells cPanel to use the Next.js server as the entry point for your application.
  6. Application Startup File: Specify the startup file by entering ./node_modules/next/dist/server/index.js in the "Application Startup File" field.
  7. Environment Variables: If your Next.js application uses environment variables, add them in the "Environment Variables" section of the Node.js application setup.
  8. Start the Application: After saving the Node.js application setup, go back to the "Setup Node.js App" page and click on the "Run NPM Install" button. This will install the required dependencies.
  9. Access your Website: You can now access your Next.js website by visiting the assigned domain or subdomain.


Note: It's worth mentioning that cPanel might not be the most ideal environment for hosting a Next.js application due to its shared hosting nature. However, if cPanel is your only option, the above settings should help you deploy your Next.js application successfully.


How to configure caching for a Next.js app on cPanel?

To configure caching for a Next.js app on cPanel, you can follow these steps:

  1. Login to your cPanel account and go to the "Software" section.
  2. Click on "Select PHP Version" or "MultiPHP Manager" (depending on your cPanel version).
  3. Make sure you have selected the appropriate PHP version for your Next.js app.
  4. Go to the "Optimize Website" section.
  5. Select your domain from the drop-down menu.
  6. Choose the caching level you want to configure (e.g., "Disabled", "Standard", or "Aggressive").
  7. Click on the "Update Settings" button.


Next.js apps typically generate static HTML files, which can benefit from browser caching. To enable browser caching for static assets, such as JavaScript and CSS files, you can add caching rules in the .htaccess file. Here's an example:

  1. Connect to your cPanel account using FTP or File Manager.
  2. Locate the public folder of your Next.js app.
  3. Look for the .htaccess file in the public folder. If it doesn't exist, create a new file and name it ".htaccess".
  4. Open the .htaccess file and add the following lines:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Enable caching for static assets
<IfModule mod_expires.c>
  ExpiresActive on
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType text/javascript "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/gif "access plus 1 year"
</IfModule>


  1. Save the .htaccess file.


This configuration enables browser caching for CSS, JavaScript, and image files, with different expiration times specified. You can modify the expiration times according to your needs.


Remember to clear the cache of your browser or use a different browser to see the changes taking effect.


What are the common deployment issues for Next.js on cPanel?

There are a few common deployment issues that Next.js developers may encounter when deploying their application on cPanel:

  1. Environment configuration: Next.js relies on environment variables for configuration. cPanel may not provide a straightforward way to set environment variables, requiring developers to find alternate methods like using .htaccess files or modifying their server's configuration directly.
  2. Routing issues: Next.js uses a file-based routing system. cPanel might require additional configuration to ensure that requests are properly handled by Next.js's routing system rather than being directly served as static files.
  3. Server-side rendering (SSR) compatibility: Next.js is known for its SSR capabilities. However, cPanel may not be optimized for SSR out of the box, requiring manual configuration adjustments to ensure that the application is correctly rendered on the server side.
  4. Proxy configuration: cPanel may require proxy configuration to redirect requests to the Next.js server. This entails configuring Apache or NGINX to properly forward requests to the application's server.
  5. Build process and deployment flow: Next.js applications typically require a build step to generate optimized assets that can be served by a production server. cPanel may not have built-in support for running custom build scripts, so developers may need to manually configure their build process and update their deployment flow.


Overall, deploying Next.js applications on cPanel may require additional configuration and adjustments compared to a standard Node.js server setup.

Best Next.js Books to Read in 2024

1
JavaScript: The Definitive Guide: Master the World's Most-Used Programming Language

Rating is 5 out of 5

JavaScript: The Definitive Guide: Master the World's Most-Used Programming Language

2
Real-World Next.js: Build scalable, high-performance, and modern web applications using Next.js, the React framework for production

Rating is 4.9 out of 5

Real-World Next.js: Build scalable, high-performance, and modern web applications using Next.js, the React framework for production

3
Next.js Cookbook: Learn how to build scalable and high-performance apps from scratch (English Edition)

Rating is 4.8 out of 5

Next.js Cookbook: Learn how to build scalable and high-performance apps from scratch (English Edition)

4
Next.js, The Full Stack React JS Framework, Web Development Crash Course: Enterprise Web Development, and Full Stack JavaScript Development with ReactJS and NodeJS Server Side JavaScript

Rating is 4.7 out of 5

Next.js, The Full Stack React JS Framework, Web Development Crash Course: Enterprise Web Development, and Full Stack JavaScript Development with ReactJS and NodeJS Server Side JavaScript

5
React Key Concepts: Consolidate your knowledge of React's core features

Rating is 4.6 out of 5

React Key Concepts: Consolidate your knowledge of React's core features

6
Learning React: Modern Patterns for Developing React Apps

Rating is 4.5 out of 5

Learning React: Modern Patterns for Developing React Apps


How to manage file uploads in a Next.js app on cPanel?

To manage file uploads in a Next.js app on cPanel, you can follow these steps:

  1. Set up a file upload endpoint in your Next.js app. This requires creating an API route that handles file uploads. You can use the multer package or any other file handling library to handle the file uploads. Here's an example of how you can set up a file upload endpoint in a Next.js app: import multer from 'multer'; const upload = multer({ dest: 'uploads/' }); export default function handler(req, res) { upload.single('file')(req, res, (err) => { if (err) { return res.status(400).json({ error: err.message }); } // Process the uploaded file here // ... res.status(200).json({ success: true }); }); }
  2. Deploy your Next.js app on cPanel. There are multiple ways to deploy a Next.js app on cPanel. You can use the cPanel File Manager to manually upload your app's files, or you can use a deployment tool like Bitbucket Pipelines or GitHub Actions to automatically deploy your app.
  3. Configure cPanel to support file uploads. By default, cPanel limits the maximum file upload size, so you may need to adjust the settings to allow larger file uploads. You can do this by editing the php.ini configuration file or by using the cPanel interface to modify the PHP settings.
  4. Test the file upload functionality. Once your app is deployed and the file upload settings are configured, you can test the file upload functionality by making a POST request to your file upload endpoint. You can use tools like Postman or curl to send the requests and check if the files are successfully uploaded.


Remember to handle file size limits, file type validation, and any other specific requirements you may have for file uploads in your Next.js app.


What is the recommended backup strategy for a Next.js app on cPanel?

There are a few recommended backup strategies for a Next.js app hosted on cPanel:

  1. cPanel Backup: Utilize the built-in backup functionality provided by cPanel. You can configure regular backups through the "Backup" section in your cPanel account. This allows you to back up your entire account, including your Next.js app, files, databases, and settings.
  2. Manual File Backup: Periodically make a manual backup of your Next.js app files and directories. This can be done by connecting to your cPanel account using an FTP client or cPanel's File Manager, and downloading the relevant directories to your local machine. This ensures that you have a separate copy of your app's code and assets.
  3. Database Backup: If your Next.js app relies on a database (e.g., MySQL), it's important to regularly back up the database separately from the files. You can use cPanel's "phpMyAdmin" or command-line tools like mysqldump to export your database data and schema, and store it in a safe location.
  4. Version Control: Consider using a version control system like Git to track changes in your Next.js app's codebase. By committing and pushing changes to a remote repository (e.g., GitHub), you not only have a backup of your code but also the ability to rollback to previous versions if needed.
  5. Offsite Backups: It's wise to keep backups in an offsite location, separate from your hosting server. You can manually upload backups to cloud storage services like Dropbox, Google Drive, or Amazon S3. Alternatively, you can use cPanel's "Backup" feature to configure remote destinations like FTP or SCP to automatically transfer backups to another server.


It's recommended to combine multiple backup strategies for redundancy. Regularly test your backups by restoring them in a local development environment to ensure they are complete and functional.


What are the dependencies needed for a Next.js app on cPanel?

To run a Next.js app on cPanel, the following dependencies are required:

  1. Node.js: Next.js is built on top of Node.js, so it needs to be installed on the server. The recommended version is v12.0.0 or later.
  2. Package Manager: npm or yarn is required to install and manage dependencies.
  3. Git: Next.js projects are often stored in a Git repository, so Git is needed to clone the repository and manage version control.
  4. Web Server: Next.js can be run using various web servers, but the most common one is node-http-server, which should be installed as a dependency.
  5. SSL Certificate: It is recommended to have an SSL certificate installed on the server for secure communication (HTTPS protocol).


It's important to note that cPanel is primarily designed for hosting websites built with server-side scripting languages like PHP, so some configuration may be required to deploy a Next.js app. Additionally, some features of Next.js (such as server-side rendering) may have limited support on cPanel due to its shared hosting environment.


How to configure the server for a Next.js app on cPanel?

To configure the server for a Next.js app on cPanel, you need to follow these steps:

  1. Log in to cPanel and go to the "Software" section.
  2. Click on "Setup Node.js App" or "Node.js Version Manager" (depending on your cPanel version).
  3. Choose the domain or subdomain where you want to configure the Next.js app.
  4. Select the Node.js version you want to use in the "Application mode" section. Ensure that the selected version is compatible with Next.js.
  5. Enter a startup file name, typically "server.js" for Next.js apps.
  6. Provide the application root. This should be the root directory of your Next.js project.
  7. Set the Application URL path to "/". This is the default path where Next.js applications are served.
  8. Click on the "Create" button to create the Node.js app.
  9. Once the app is created, you will see the Node.js app configuration details.
  10. Under the "Application Mode" section, click on the "Setup Vhosts" link. This will open a new tab.
  11. In the new tab, click on the "Setup" button to generate the Apache virtual host configuration for your Next.js app.
  12. Once the setup is complete, go back to the original cPanel tab.
  13. Scroll down to the "Application Details" section and note down the "Application URL". This is the URL where your Next.js app will be accessible.
  14. You may need to install required dependencies and build your Next.js app before it can be served. To do this, you can use SSH or cPanel's "Terminal" feature to navigate to your application root and run commands like npm install and npm run build.


That's it! Your server should now be configured for a Next.js app on cPanel. You can access your app by visiting the Application URL noted in step 13.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To deploy a Next.js app to AWS, you can follow these general steps:Build your Next.js app: Before deploying, make sure to create a production-ready build of your Next.js app. You can do this using the next build command. This will generate an optimized and bun...
To deploy a Next.js app on Netlify, you can follow these steps:Create a new project or navigate to an existing Next.js app directory using your terminal/command prompt.Initialize the project as a git repository, if it is not already one, using the command: git...
To deploy a React.js app to a hosting platform, follow these steps:Build the React App: Before deploying your app, it&#39;s essential to create a production-ready build. Run the command npm run build in your project folder. This will generate an optimized and ...