简体   繁体   中英

How to deploy Next.js app with Passenger?

I'm totally new to develop and I'm trying to deploy my first application (let's say the name is testing ).
I want to deploy my Next.js React app by using Passenger (this is included and required by Dreamhost, so I didn't install it and I don't have passenger command on the server's terminal) but I have no clue how to do that.

As far as I know, Passenger is looking for app.js to start an application, therefore, on my server, inside my domain folder, there should be an app.js ( testing.com/app.js ).
But Next.js doesn't have an explicit app.js , all I have is an _app.js inside my pages folder that makes some small changes, so I don't know how to connect them.

I'd also like to know once I have my app.js configured, do I need to run node app.js ?
It seems like my website would make the corresponding change if I change the code of testing.com/app.js , just something like the following code:

var http = require("http");

http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}).listen(80);

If I run node app.js with listing to port 3000, it works for http , https will still show something like We're sorry, but something went wrong. .
If I don't run node app.js or I set listen to port 80, all I can get is We're sorry, but something went wrong. .

Any help would be appreciated.

I would highly suggest reading up on how to deploy a node.js app using passenger .

As for the port issue...

Since you haven't shown any error logs I'm assuming the problem is that you don't have root access. Any ports below 1024 require root access to use.

Simply run sudo node app.js and you should be able to listen to ports below 1024.

You do not need to install Passenger or run node app.js . App.js is automatically executed via Passenger. However you will need to install NVM and nodejs on your dreamhost. Full instructions for installing node can be found at: https://help.dreamhost.com/hc/en-us/articles/360029083351-Installing-a-custom-version-of-NVM-and-Node-js

Then ensure you have enabled Passenger & Node.js for your domain: https://help.dreamhost.com/hc/en-us/articles/216635318-Enabling-Passenger-for-Node-js

After the above you can finally get started. Ensure you have the following directory structure for your domain:

application directory
  |
  +-- app.js
  |
  +-- .htaccess
  |
  +-- public/
  |
  +-- tmp/

Your app.js file can look like this

var http = require("http");
http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World. This page is running Node.js version: ");
  response.write(process.version);
  response.end();
}).listen(80);

Your .htaccess file can look like this

#path to nodejs version installed
PassengerNodejs [PUT IN YOUR PATH TO YOUR INSTALLATION OF NODE JS]

#error reporting
PassengerFriendlyErrorPages on

Now and anytime you change your configs you will need to command Passenger to restart your nodejs app. This can be done within your app directory

touch tmp/restart.txt

Your node.js app should how "Hello world" on your domain. If it still does not then you will need to check the logs and then email Dreamhost support. The first time I had this setup, Dreamhost has to fix something their side.

However in regards to Next.Js. This may be more tricky as you can only trigger app.js. Next.js sits in a directory call pages . Unfortunately I am still finding a way around this.

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