How to Deploy a Nextjs App on cPanel efficiently - 2023 Update
- Published on
Introduction:
Deploying a Next.js application on cPanel can provide a convenient and reliable hosting solution for your web application. In this article, we will guide you through the step-by-step process of efficiently deploying a Next.js app on cPanel, ensuring smooth operation and optimal performance.
Prerequisites:
Before proceeding with the deployment, make sure you have the following:
- A cPanel hosting account with SSH access enabled.
- Node.js and npm installed on your local development environment.
- Git installed on your local machine.
Step 1: Set Up Your Next.js Application Locally
- Open your terminal and navigate to the directory where you want to create your Next.js app.
- Use the following command to create a new Next.js app:
npx create-next-app your-app-name
- Change into the app directory:
cd your-app-name
- Start the local development server:
npm run dev
Verify that your app is running correctly at http://localhost:3000 before proceeding.
Step 2: Prepare Your Next.js App for Deployment
- Open the package.json file of your Next.js app and ensure the "scripts" section contains the following:
"scripts": {
"build": "next build",
"start": "next start"
}
- Run the following command to create a production build of your Next.js app:
npm run build
This will generate an optimized build of your app in the .next
directory.
Step 3: Set Up Your cPanel Environment
- Log in to your cPanel account and navigate to the "File Manager".
- Create a new directory for your app inside the "public_html" folder, e.g.,
your-app
. - Upload the contents of the
.next
directory from your local machine to theyour-app
directory in cPanel using the file manager or an FTP client.
Step 4: Configure Your cPanel Node.js App
- Go back to the cPanel dashboard and navigate to the "Setup Node.js App" section.
- Click on the "Create Application" button and fill in the necessary details:
- Application root: Select the
your-app
directory you created earlier. - Application URL: Enter the URL path for your app, e.g.,
/your-app
. - Application startup file: Leave it as the default value (
server.js
). - Application mode: Select "Production".
- Environment variables: Add any required environment variables, if applicable.
- Application root: Select the
- Click on the "Create" button to create your Node.js application on cPanel.
Step 5: Start Your Next.js App on cPanel
- Once the application is created, go back to the "Setup Node.js App" section.
- Under "Applications", locate your app and click on the "Run" button to start your Next.js app.
- Wait for the application to start, and you will see the "Running" status.
- Visit your app by navigating to the specified URL path, e.g.,
https://your-domain.com/your-app
.
Conclusion:
By following this step-by-step guide, you can efficiently deploy your Next.js app on cPanel. This process ensures that your application is set up correctly, optimized for production, and ready to deliver a seamless user experience. Take advantage of the power and convenience of cPanel hosting while leveraging the capabilities of Next.js to build modern and performant web applications.