Deploying Next.js to Apache server
Asked Answered
S

4

14

I've been developing a Next.js website locally and now want to set it up on my Apache server (with cPanel). However, I'm very new to Next.js and Node apps and not too sure how to go about it.

Has anyone done this successfully? Can you list the required steps and what files should be on the server?

Also, can this be done on a subdomain?

Thank you!

Seawright answered 28/11, 2019 at 20:11 Comment(3)
You will need to install node on the server (Apache won't serv node apps). and start the server with node dist/index.jsWormeaten
@Wormeaten Could you elaborate a little more? Where do I run node dist/index.js? And will doing so have an impact on whatever else is on the server that doesn't use node?Seawright
Apache is a process on the machine, node can be another process on the same machine.Wormeaten
A
10

To start with some clear terms just so we're on the same page, there are two or three very different things people mean when they say "server":

  • A Server Machine is a computer that is connected to the internet that you intend to use to serve something to people on the internet.

  • A Server Program is some software you run on your Server Machine. The job of the Server Program is to actually calculate the responses to various requests.

  • A Server as a Service is a webapp provided by a company that stores your code and then puts it onto Server Machines with the right Server Program as needed.

While we're here, let's also define:

  • A Programming Language is the language your website is written in. Some sites have no language (and are just raw HTML/CSS files that are meant to be returned directly to the user). Many sites, though, have some code that should be run on the server and then the result of that code should be returned to the user.

In your case, you have a Machine whose condition we don't know other than that it is running the Program Apache (or probably "Apache HTTP Server"). Apache HTTP server is very old and proven and pretty good at serving raw files back to users. It can also run some Programming Languages like PHP and return the result.

However, Next.JS is built on top of the Programming Language Javascript, which Apache does not have the ability to run. Next.JS instead wants its Server Program to be Node.

So the problem here is basically that you have a hammer, but only screws. You can't use the tool you have, Apache, to solve the problem you need solved, running Node code and returning the result. To get around this you have two options:

First, you can find a way to access the Server Machine that is currently running Apache and tell it, instead, to run Node pointed at your Next.JS code whenever it starts up. This might not be possible, depending on who owns this machine and how they've set it up.

Second, and probably easier, is to abandon this Machine and instead use a Server as a Service. Heroku, AWS, and Netlify all support Next.JS and have a free tier. The easiest solution, though, is probably to just deploy it on Vercel, which is a Server as a Service run by the same team that makes Next.JS and which has a very generous free tier for you to get started with.

The good news, though, is that yes next.js does totally support being hosted from a subdomain.

Adin answered 16/6, 2020 at 14:35 Comment(0)
P
7

Next.JS allows you to build fully functional Node Applications, as well as simple statically-generated sites like Jeckyl or Docpad. If your use case is a simple statically generated site look here: https://nextjs.org/docs/advanced-features/static-html-export

In particular the next build && next export command will create all the HTML and assets necessary to host a site directly via an HTTP server like Apache or Ngnix. Contents will be outputed to an out directory that could serve as the server root.

Pay very close attention to what features are not supported via this approach.

Percyperdido answered 15/5, 2022 at 22:45 Comment(0)
E
4

Yes, you can run your NextJS application on a cPanel/Apache machine.

  1. Install node using platform package manager. And to answer your question, no, this package will not interfere with other cPanel software.
  2. Copy your NextJS application into position. I would put it next to the httpdocs folder so that the pages won't accidentally get served through the normal Apache.
  3. Run application as with npm start or whatever (which starts on port 3000).
  4. Update Apache's host domain configuration and add a reverse proxy to pass everything that comes to that virtual domain to localhost:3000. See mod_proxy which is available in all cPanel suites.

There are a few things to bare in mind.

  1. You will have to manage starting and stopping the node programs when the server is rebooted.
  2. If node is down but Apache is up then you will get a Bad Gateway error.
  3. Ensure that if NextJS produces urls then you specify the correct (external) domain.
Everybody answered 22/6, 2023 at 16:58 Comment(2)
Thanks, big help! You can use pm2 and its start and startup commands (follow the instructions of the latter) to automatically restart the node server if the machine is rebooted.Landrum
I didn't know about pm2. I've implemented it on all my servers now, so big thanks :)Everybody
X
0

If you are going to use SSR it is easy you just need to add VirtualHost and handle the port on which you serve your app.

If you are going to publish SSG it is important to setup redirects and so on. You can read more about it in this Article, it is step by step instruction - https://medium.com/@viktor.panasiuk97/how-to-publish-the-next-js-app-on-the-apache-server-60cb64e91853

Xerophilous answered 30/3 at 19:26 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewAntimacassar

© 2022 - 2024 — McMap. All rights reserved.