Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

node.js - Where do I put my Node JS app so it is accessible via the main website?

I've recently installed a nodejs app (keystone) app in my home/myusername/myappname directory.

When I visit www.mydomain.com, nothing displays - even after turning on my nodejs app.

Where should these files be?

I am running ubuntu 16.04.

In the past I have worked with a var/www folder, but I am not using apache - do I need to manually create this folder?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For your app to be visible it has to be running (obviously) and accessible on port 80 (if you want it to be available without adding a port number to the URL).

It doesn't matter where it is on the disk as long as it's running.

You don't need Apache or nginx or any other server. Your Node app may listen on port 80. But alternatively it can listen on some other port and your other server (Apache, nginx, etc.) can proxy the requests to that port.

But if your app is listening on, e.g. port 3000 then you should be able to access it as http://www.example.com:3000/.

Also, make sure that your domain is configured correctly. It's A record for IPv4 (or AAAA for IPv6) of the www subdomain should be equal to the publicly accessible IP address of your server.

And make sure that the port you use is not blocked by the firewall.

Update

To see how you can set the port with Keystone, see:

It can be either changed in the config or you can run your app with:

PORT=80 node yourApp.js

instead of:

node yourApp.js

but keep in mind that to use the port number below 1024 you will usually need the program to run as root (or add a special privilege which is more complicated).

It will also mean that this will be the only application that you can run on this server, even if you have more domain names.

If you don't want to run as root or you want to host more application, it is easiest to install nginx and proxy the requests. Such a configuration is called a "reverse proxy" - it's good to search for info and tutorials using that phrase.

The simplest nginx config would be something like this:

server {
    listen 80;
    server_name www.example.com;
    location / {
        proxy_pass http://localhost:3000;
    }
}

You can set it in:

  • /etc/nginx/sites-available/default

or in a different file as e.g.:

  • /etc/nginx/sites-available/example

and then symlinked as /etc/nginx/sites-enabled/example

You need to restart nginx after changing the config.

You can find more options on configuring reverse proxies here:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...