Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have full access to a cloud server (running Ubuntu 14.04) via SSH.

I already have installed Node.JS from PPA:

sudo apt-get install -y python-software-properties python g++ make
sudo add-apt-repository ppa:chris-lea/node.js -y
sudo apt-get update
sudo apt-get install nodejs -y

Now, I want to set up my first Node.JS application on my cloud server. It will send a Hello World message from server to client.

require("http").createServer(function (req, res) {
   res.end("Hello World!");
}).listen(3000);

Then:

$ node my-script.js

What are the steps to allow users to see this hello world message at a custom domain (e.g. example.com)?

share|improve this question

closed as off-topic by SvW, mdpc, Ward, Rex, Falcon Momot Jun 8 at 23:48

This question appears to be off-topic. The users who voted to close gave these specific reasons:

  • "Questions must be relevant to professional system administration. Server Fault is dedicated to professional system and network administrators. End user and enthusiast questions are off-topic (contact your system administrator or hire a professional to help you out). Please see the Help Center for more information." – mdpc, Ward, Falcon Momot
  • "Questions must demonstrate a minimal understanding of the problem being solved. Try including attempted solutions, why they didn't work, and the expected results. See How can I ask better questions on Server Fault? for further guidance." – SvW, Rex
If this question can be reworded to fit the rules in the help center, please edit the question.

    
What do you mean, "send it to a custom domain"? Do you mean that when a user asks for http://example.com:3000 they'll see your message? –  Andrew Schulman Jun 8 at 10:18
    
@AndrewSchulman When somebody will open in browser production.example.com will get Hello World! response. –  Ionică Bizău Jun 8 at 10:21
    
OK, then why is your server listening on port 3000? Don't you want 80? –  Andrew Schulman Jun 8 at 10:23
    
@AndrewSchulman Probably. It's the first time when I set up NodeJS on a cloud server. I am getting EACCES error for listen(80). –  Ionică Bizău Jun 8 at 10:25
    
If I start the process with sudo, then it works fine! But, how can I avoid running the process with sudo? –  Ionică Bizău Jun 8 at 10:28

1 Answer 1

up vote 0 down vote accepted

Solution

  1. Linked my domain with the server ip adding A record in DNS management:

    Sr No  | Name                   | Destination IP Address | Status    
    ------------------------------------------------------------------
    ...
    3      | production.example.com | xxx.xxx.xx.xxx         | Active
    

    Where Destination IP Address is the ip address used when connecting via ssh:

    $ ssh ubuntu@xxx.xxx.xx.xxx -A
    
  2. Redirect 80 port to 3000

    $ sudo iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3000

  3. Created a script named index.js containing:

    require("http").createServer(function (req, res) {
       console.log(req.url);
       res.end("Hello World!");
    }).listen(3000);
    
  4. Run it using:

    $ node index.js

Then, the response of production.example.com is Hello World!.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.