I was actually pretty disappointed when I noticed that there aren’t really any good tutorials for how to install and run Node on a Linux machine. So I thought that I would help people out here by writing something for all you facing this issue right now. So here we go.
What do you need before you can do this?
1) Some form of an SSH client, I’m using OpenSSH for Windows. If you’re using a Mac there is already one by default.
2) ip Address of the remote Linux Instance. I’m using an Amazon EC2 instance running Linux. So I get my ip address from the EC2 Management Console.
3) Your .pem file for the Linux instance.
1) SSH onto your Linux instance.
2) Run the following commands
sudo mkdir node cd node sudo wget http://nodejs.org/dist/v0.6.6/node-v0.6.6.tar.gz sudo tar -xzf node-v0.6.6.tar.gz
3) Now you need to install the Development Tools needed to compile Node.
yum groupinstall "Development Tools"
Now install openssl-devel:
yum install openssl-devel
4) Now cd into the node folder you created ‘/node/node-vx.x.x’ and run these commands to configure and compile node.
sudo ./configure sudo make sudo make install
Now there’s a chance that when you uploaded your folder containing your node server and website to your Linux instance, that the folder was set to only allow super user access. If that’s the case, you’ll see an error saying that access to your node file is denied. Actually you won’t even be able to cd into the folder. If that happens, simply enter in this command.
chmod -R 777 dir_name/
5) You should now be able to run node and have access to the folder and files necessary to run your app. Navigate to the directory that contains your node file, and run this command.
You should now have your server up and running. The only thing though is that the server can only be reached on the local machine. What we’re going to do now is make it so that you can reach your nodejs server from anywhere. This gets a tad bit more complicated.
Allow Remote Access From Browser to Web Server
So here’s what we need to do:
1) Open the right ports using the Amazon EC2 Management Console. To do this, go to your EC2 Management Console, click on Security Groups, and select the Security Group you’re using for your EC2 instance. Now you need to add a rule. Open up port 80 (http) to allow connections from all ip addresses.
1) Good. Now, you’re app is most likely listening on port 8080, right? What we now need to do is set up port forwarding, from port 80 to 8080. If you’re running on another port, then simply make a note of that in the following commands asking for the port to route to. So first of all, run this command to see if you have port forwarding enabled.
2) It will most likely return 0. If it does, that means that port forwarding is not enabled. To enable it, enter the following command.
sudo nano /etc/sysctl.conf
If you want to you can use vim to edit this file, but that’s up to you.
3) Now change this line from
net.ipv4.ip_forward = 0
net.ipv4.ip_forward = 1
4) Now we need to enable these changes. Enter this command to do that.
sudo sysctl -p /etc/sysctl.conf
5) Now check and make sure that port forwarding is enabled.
This should now return a 1.
6) Now lets set up forwarding from port 80 to port 8080. Enter in the following command.
sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
7) Next, let’s open the Linux firewall to allow connections on port 80:
sudo iptables -A INPUT -p tcp -m tcp --sport 80 -j ACCEPT
sudo iptables -A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPT
8) Now try and enter the public ip address of your EC2 instance in your web browser, and you should see that your server is up and running!