简体   繁体   中英

Deploying Next.js to Apache server

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!

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM