0

I'm running Ubuntu 18.04 LTS and I wanted to setup my machine as a web hosting server to host a Node.js application, I already have the app done and it's running on localhost port 3000 just fine, and I bought a domain this morning, so IDK what's the best approach to take on this, I searched online for a while but couldn't find anything good cause IDK exactly what I need to start, cause it's my first time doing this and it's my first time installing linux without a VM (still a linux beginner), so it would be great if you just can guide me on how to start this or you can suggest a good article to get me started.

Thanks in advance.

New contributor
Ali Mohamed is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
0

You have some choices, some better than others and some more secure than others. What I would do to host a node.js app is run nginx (pronounced engine-x) as a reverse proxy to the app.

$> sudo apt install nginx

Once it is installed, you need to check that it is running so you know that it's working. To do this you will need the ip address of your host.

$> ip address

The output will have a section with the network interface and the under, it's IP address. For my computer I get the following:

$> ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:61:3d:43 brd ff:ff:ff:ff:ff:ff
    inet 10.0.20.11/23 brd 10.0.21.255 scope global dynamic ens3
       valid_lft 5987sec preferred_lft 5987sec
    inet6 fe80::5054:ff:fe61:3d43/64 scope link
       valid_lft forever preferred_lft forever

So the IP address of my server is 10.0.20.11 (the /23 part means the subnet is 255.255.254.0)

If you have a GUI open a browser window and go to http://localhost. If you are running ubuntu server with no GUI, from another machine go to http://10.0.20.11 (replace this with the IP of your own system). You should see the default Welcome to nginx! page telling you the server works.

So your webserver is up and running, now you need to configure the reverse proxy:

First remove the default settings:

$> sudo unlink /etc/nginx/sites-enabled/default

Now create a new configuration:

$> sudo vi /etc/nginx/sites-available/reverse

Of course, you can use whatever editor you like, just replace vi with nano or gedit (replace sudo with gksudo if you use gedit). Paste the following:

server {
    listen 80;
    server_name myserver.networkname.lan 10.0.20.11;

    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         http://127.0.0.1:3000;
    }
}
}

The server_name definition will need to be edited, replace "myserver.networkname.lan" with your server's fqdn and IP address with the one you found earlier. I have configured my network to have home.lan as the name of the network so it would be server.home.lan in my case. If you haven't configure DNS for your network you can leave it out and just put in your IP address. Save the file

Last step is to link your virtual host configuration to sites-enabled

$> sudo ln -s /etc/nginx/sites-available/reverse /etc/nginx/sites-enabled/reverse

Test the config has correct syntax

$> sudo nginx -t

Now restart nginx

 $> sudo systemctl restart nginx

And reload the browser window from before.

**Firewall configuration:

$> sudo ufw allow 'Nginx HTTP'

If you have an internet facing web server, get an ssl cert from letsencrypt. There's plenty of guides on how to do this on the web. This will require editing the 'reverse' file so port 443 is being used by nginx and were your cert is located. If you haven't configured DNS, you should consider doing so - it makes hosting a web app. Also, if this is a computer at your home, you can forward port 80 (& 443) to the host so it is accessible from the internet. Then you can use dynamicDNS to have anyone in the world go to http(s)://your.dynamicdns.tld and use your app. The best part is this is all free all you need is a computer.

You can search 'nginx proxy nodejs' for more information on all of the above and a bunch of guides to walk you through everything.

0

Every web server must have an environment that runs it and handles all layer 7 sessions with your app. This happens in every OS and every machine. In windows server (and some other windows copies) you even have a propitiatory feature embedded called IIS.

Ubuntu integrates very well with open source web servers such as apache and nginx, both have pros and cons.

As one of the answers here suggested and attached the nginx option and guide, I'll attach the apache approach with node.js and guide, and the choice is purely yours. The guide was written for version 16.04, but the same steps are used in 18.04.

Note: you didn't mention in your question, but I assume you are using ubuntu server OS for your project.

Set Up a Node.js App for a Website With Apache on Ubuntu 16.04

Your Answer

Ali Mohamed is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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