If the cPanel hosting you are using has the Terminal or SSH feature and the Node.js feature, then it is very likely that you can upload Next.JS to cPanel.
However, if you are using hosting, especially shared hosting, you might encounter various errors or issues when installing Next.JS and will need to use a specific method so that Next.JS can run on cPanel.
But if you still want to upload the Next.JS project you have created to cPanel hosting, you can follow the steps in this tutorial.
How to Upload Next.JS to cPanel
As a starting point, in this tutorial we will use the Next.JS application Next js Blog Boilerplate with Tailwind CSS which can be obtained from this page: https://github.com/ixartz/Next-js-Blog-Boilerplate. And because this is just a Boilerplate or basic application, the result when successful will look like this:
So let us get straight to the steps for uploading Next.JS to cPanel so the explanation does not get any longer.
1. Upload Files to Hosting
First, you obviously need to prepare the files to be uploaded to the hosting by compressing the files into a zip or you can also upload directly using FTP.
If inside the next.js folder you created on localhost there is a folder node_modules, make sure not to upload that folder. Also ensure that if there are any files or folders starting with a dot (.), you upload those folders or files as well.
If you use the compression method, you can upload files into any folder within cPanel, it does not have to be inside the public_html folder. For example, here the files are uploaded to the folder nextjs-wpgan which is located outside of public_html
Once uploaded, you need to extract the zip file by right-clicking on the zip file, then selecting the extract option
2. Create a Startup File
Before deploying Next.js, you need to create a startup file first so that the Next.JS application can run on cPanel later. In the Next.JS installation folder, create a file named main.js
Inside main.js, you need to add this line of code
// startup file next.js - wpgan.comconst { createServer } = require('http')const { parse } = require('url')const next = require('next')
const dev = process.env.NODE_ENV !== 'production'const hostname = 'localhost'const port = process.env.port || 3000// when using middleware `hostname` and `port` must be provided belowconst app = next({ dev, hostname, port })const handle = app.getRequestHandler()
app.prepare().then(() => { createServer(async (req, res) => { try { // Be sure to pass `true` as the second argument to `url.parse`. // This tells it to parse the query portion of the URL. const parsedUrl = parse(req.url, true) const { pathname, query } = parsedUrl
if (pathname === '/a') { await app.render(req, res, '/a', query) } else if (pathname === '/b') { await app.render(req, res, '/b', query) } else { await handle(req, res, parsedUrl) } } catch (err) { console.error('Error occurred handling', req.url, err) res.statusCode = 500 res.end('internal server error') } }).listen(port, (err) => { if (err) throw err console.log(`> Ready on http://${hostname}:${port}`) })})After adding that code, you also need to edit the file package.json. Inside this file, look for the line “start”, then change the line to look like this
"start": "NODE_ENV=production node main.js",Once updated, the display in package.json will look more or less like this
3. Setup Node.JS App
Now that all files are ready, you can deploy or setup Node.JS in cPanel. To do this, search for Setup Node.JS App in the search bar
Once you are on the Node.JS page, click the button Create application
Now you will be asked to fill in the details of the application you are going to deploy
- Node.js version: adjust this to the Node.JS version you already have running on localhost to minimize errors
- Application mode: You can choose the application scope as development or production. It is recommended to build Next.JS on localhost until it is finished, so that when it is uploaded to hosting it is already in production. if when using the production method there is a command that does not work, for example command not found, try using the development option.
- Application root: fill this with the folder where you uploaded the files earlier. If the folder is outside public_html, for example nextjs-wpgan, then you can fill it with nextjs-wpgan. But if it is inside the public_html folder, then you can fill it with public_html/nextjs-wpgan
- Application URL: The website link that you will access once the application has been successfully deployed
- Application startup file: fill this with main.js according to what was created in step number 2.
Configuration in this tutorial, the result will be like the image below and you can click the button Create
4. Run Next.JS
When Node.js has been successfully created, you will get path or command information that you need to use to run Node.js in the terminal, just copy that command
Now open the Terminal or SSH in your cPanel hosting account to start running Next.js, then once you are on the Terminal page, you can run the command you copied from the Node.js page
Now you just need to run the command npm install to start adding modules to the Next.js application you uploaded
When the npm install process is finished, you can run the Next.js application with the command npm run start. If successful, the display will look more or less like this
5. Access the Website
Because the installation process or the process of uploading next.js to cpanel is finished, you can try accessing your website according to the Application URL you specified, and the result will be like this
That is the easy way to upload Next.JS to cPanel. However, it is less recommended if you upload Next.JS or Node.JS to cpanel shared hosting because when installing and running the application it will require a lot of resources such as RAM, CPU, Number of Processes, and I/O which might end up with your website showing a 503 error when accessed.
For example, with this Boilerplate application alone, it immediately hit the resource limit when the process finished.
COMMENTS
Loading...