<commit_before>---
layout: blog-post
title: Node.js and Nginx on Ubuntu
category: blog
tags: [Node.js, Ngnix, Ubuntu]
short_url: mig.gs/TxoG
description: Get Node.js and Nginx running on your Ubuntu server.
---
<p>In this tutorial I will show how to install and configure <a href="http://nodejs.org/" target="_blank">Node.js</a> and <a href="http://nginx.org/" target="_blank">Nginx</a> on you <a href="http://www.ubuntu.com/" target="_blank">Ubuntu</a> server.</p>

<h2>Installing Dependencies</h2>

<p>The only dependency we really need is the <code>build-essential</code> package in order to be able to compile the Node.js source code.</p>

{% highlight bash %}
# Make sure to download the latest repos.
sudo apt-get update

# Required to run `make` command.
sudo apt-get install build-essential

# If you need to use https.
sudo apt-get install libssl-dev

# My favorite text editor.
sudo apt-get install vim

# Git to manage your repos.
sudo apt-get install git
{% endhighlight %}

<h2>Installing Node.js</h2>

<p>Head over to the <a href="http://nodejs.org/download/" target="_blank">Node.js Downloads</a> page and copy the link to the source code tarball (node-&lt;version&gt;.tar.gz).</p>

{% highlight bash %}
# Create and change into directory.
sudo mkdir /opt/node/
cd /opt/node/

# Download the Node.js tarball.
sudo wget http://nodejs.org/dist/v0.10.19/node-v0.10.19.tar.gz

# Extract the tarball.
sudo tar xvzf node-v0.10.19.tar.gz

# Do some cleaning up.
sudo mv node-v0.10.19/* .
sudo rm node-v0.10.19.tar.gz
sudo rm -r node-v0.10.19/

# Run `make` and `make install`.
# Be patient. Compiling from source takes a few minutes.
sudo make
sudo make install

# Running `make test` is a good idea.
sudo make test

# Check the version to see if it was properly installed.
node -v
{% endhighlight %}

<p>If you get a version number then you're good to go.</p>

<h2>Setting up a Node.js webserver</h2>

<p></p>

{% highlight bash %}
# Create the directory to hold the sites.
sudo mkdir /var/www/
cd /var/www/

# Create our Hello World site.
sudo mkdir helloworld/
cd helloworld/

# Create a server file.
vim server.js
{% endhighlight %}

<p>Paste in the simple code into server.js</p>

{% highlight javascript %}
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(3000);
console.log('Server running at http://<your server ip address>:3000/');
{% endhighlight %}

<p>Hit the <key>ESC</key> key then type in <code>:wq</code> to save and exit. Then run the webserver with:</p>

{% highlight bash %}
# Run webserver.
node server.js
{% endhighlight %}

<p>To always run in the background, check out the <a href="https://github.com/nodejitsu/forever" target="_blank">Forever</a> node module.</p>

{% highlight bash %}
# Install Forever module globally.
sudo npm install -g forever

# Start webserver.
# To view options, run: forever --help
forever start -l hello-world.log server.js

# View log file.
tail -f ~/.forever/hello-world.log 
{% endhighlight %}

<p>Open up a browser window and go to:</p>

{% highlight bash %}
http://<your server ip address>:3000/
{% endhighlight %}

</p>You should see the "Hello World" message.</p>

<p>If for whatever reason you don't know your server's IP address, run:</p>

{% highlight bash %}
# Find server IP address
ifconfig eth0 | grep inet | awk '{ print $2 }'
{% endhighlight %}

<h2>Installing Nginx</h2>

<p>Nginx is good for serving static files, as well as a being a <em>load balancer</em>. And you can route multiple domains to your Node.js webserver. Let's install it now.</p>

{% highlight bash %}
# Installing Nginx is a breeze.
sudo apt-get install nginx
{% endhighlight %}

<p>Here are some useful Ngnix commands.</p>

{% highlight bash %}
# Self explanatory.
sudo service nginx {stop|start|restart|reload}

# Alternatively, you also do:
/etc/init.d/nginx {stop|start|restart|reload}
{% endhighlight %}

<p>The default nginx configuation file is located under:</p>

{% highlight bash %}
# Default nginx config file.
/etc/nginx/nginx.conf 
{% endhighlight %}

<p>Take note of the default log file locations, which you can view by using the <code>tail</code> command.</p>

{% highlight bash %}
# View access logs.
tail -f /var/log/nginx/access.log

# View error logs.
tail -f /var/log/nginx/error.log
{% endhighlight %}

<h2>Configuring Nginx server</h2>

<p>To proxy the Node.js server to Nginx, a new webserver config needs to be created.</p>

{% highlight bash %}
# Backup default config.
mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak

# Create new config.
vim /etc/nginx/sites-available/default
{% endhighlight %}

<p>In the empty config file add the following settings (<a href="" target="_blank">gist</a>):</p>

<script src="https://gist.github.com/miguelmota/6912559.js"></script>

<p>After you have saved the file and exited, reload the Nginx config.</p>

{% highlight bash %}
sudo service nginx reload
{% endhighlight %}

<p>Nginx will let you know if you have any errors in your config. Fix them and reload once again.</p>

<p>If you're trying to reference multiple domains and got the following error:</p>

{% highlight bash %}
Reloading nginx configuration: nginx: [emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32
{% endhighlight %}

<p>Uncomment this line in <code>/etc/nginx/nginx.conf</code>.</p>

{% highlight bash %}
server_names_hash_bucket_size 64; 
{% endhighlight %}

<p>To test that gzip is working, execute:</p>

{% highlight bash %}
curl --header "Accept-Encoding: gzip" -I http://yourdomain.com
{% endhighlight %}

<p>You should see the line:</p>

{% highlight bash %}
Content-Encoding: gzip
{% endhighlight %}

<h2>Conclusion</h2>
<commit_msg>update nginx post<commit_after>---
layout: blog-post
title: Node.js and Nginx on Ubuntu
category: blog
tags: [Node.js, Ngnix, Ubuntu]
short_url: mig.gs/TxoG
description: Get Node.js and Nginx running on your Ubuntu server.
---
<p>In this tutorial I will show how to install and configure <a href="http://nodejs.org/" target="_blank">Node.js</a> and <a href="http://nginx.org/" target="_blank">Nginx</a> on you <a href="http://www.ubuntu.com/" target="_blank">Ubuntu</a> server.</p>

<h2>Installing Dependencies</h2>

<p>The only dependency we really need is the <code>build-essential</code> package in order to be able to compile the Node.js source code.</p>

{% highlight bash %}
# Make sure to download the latest repos.
sudo apt-get update

# Required to run `make` command.
sudo apt-get install build-essential

# If you need to use https.
sudo apt-get install libssl-dev

# My favorite text editor.
sudo apt-get install vim

# Git to manage your repos.
sudo apt-get install git
{% endhighlight %}

<h2>Installing Node.js</h2>

<p>Head over to the <a href="http://nodejs.org/download/" target="_blank">Node.js Downloads</a> page and copy the link to the source code tarball (node-&lt;version&gt;.tar.gz).</p>

{% highlight bash %}
# Create and change into directory.
sudo mkdir /opt/node/
cd /opt/node/

# Download the Node.js tarball.
sudo wget http://nodejs.org/dist/v0.10.19/node-v0.10.19.tar.gz

# Extract the tarball.
sudo tar xvzf node-v0.10.19.tar.gz

# Do some cleaning up.
sudo mv node-v0.10.19/* .
sudo rm node-v0.10.19.tar.gz
sudo rm -r node-v0.10.19/

# Run `make` and `make install`.
# Be patient. Compiling from source takes a few minutes.
sudo make
sudo make install

# Running `make test` is a good idea.
sudo make test

# Check the version to see if it was properly installed.
node -v
{% endhighlight %}

<p>If you get a version number then you're good to go.</p>

<h2>Setting up a Node.js webserver</h2>

<p></p>

{% highlight bash %}
# Create the directory to hold the sites.
sudo mkdir /var/www/
cd /var/www/

# Create our Hello World site.
sudo mkdir helloworld/
cd helloworld/

# Create a server file.
vim server.js
{% endhighlight %}

<p>Paste in the simple code into server.js</p>

{% highlight javascript %}
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(3000);
console.log('Server running at http://<your server ip address>:3000/');
{% endhighlight %}

<p>Hit the <key>ESC</key> key then type in <code>:wq</code> to save and exit. Then run the webserver with:</p>

{% highlight bash %}
# Run webserver.
node server.js
{% endhighlight %}

<p>To always run in the background, check out the <a href="https://github.com/nodejitsu/forever" target="_blank">Forever</a> node module.</p>

{% highlight bash %}
# Install Forever module globally.
sudo npm install -g forever

# Start webserver.
# To view options, run: forever --help
forever start -l hello-world.log server.js

# View log file.
tail -f ~/.forever/hello-world.log 
{% endhighlight %}

<p>Open up a browser window and go to:</p>

{% highlight bash %}
http://<your server ip address>:3000/
{% endhighlight %}

</p>You should see the "Hello World" message.</p>

<p>If for whatever reason you don't know your server's IP address, run:</p>

{% highlight bash %}
# Find server IP address
ifconfig eth0 | grep inet | awk '{ print $2 }'
{% endhighlight %}

<h2>Installing Nginx</h2>

<p>Nginx is good for serving static files, as well as a being a <em>load balancer</em>. And you can route multiple domains to your Node.js webserver. Let's install it now.</p>

{% highlight bash %}
# Installing Nginx is a breeze.
sudo apt-get install nginx
{% endhighlight %}

<p>Here are some useful Ngnix commands.</p>

{% highlight bash %}
# Self explanatory.
sudo service nginx {stop|start|restart|reload}

# Alternatively, you also do:
/etc/init.d/nginx {stop|start|restart|reload}
{% endhighlight %}

<p>The default nginx configuation file is located under:</p>

{% highlight bash %}
# Default nginx config file.
/etc/nginx/nginx.conf 
{% endhighlight %}

<p>Take note of the default log file locations, which you can view by using the <code>tail</code> command.</p>

{% highlight bash %}
# View access logs.
tail -f /var/log/nginx/access.log

# View error logs.
tail -f /var/log/nginx/error.log
{% endhighlight %}

<h2>Configuring Nginx server</h2>

<p>To proxy the Node.js server to Nginx, a new webserver config needs to be created.</p>

{% highlight bash %}
# Backup default config.
mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak

# Create new config.
vim /etc/nginx/sites-available/default
{% endhighlight %}

<p>In the empty config file add the following settings (<a href="" target="_blank">gist</a>):</p>

<script src="https://gist.github.com/miguelmota/6912559.js"></script>

<p>After you have saved the file and exited, test for any Nginx errors and then reload the config.</p>

{% highlight bash %}
nginx -t

sudo service nginx reload
{% endhighlight %}

<p>Nginx will let you know if you have any errors in your config. Fix them and reload once again.</p>

<p>If you're trying to reference multiple domains and got the following error:</p>

{% highlight bash %}
Reloading nginx configuration: nginx: [emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32
{% endhighlight %}

<p>Uncomment this line in <code>/etc/nginx/nginx.conf</code>.</p>

{% highlight bash %}
server_names_hash_bucket_size 64; 
{% endhighlight %}

<p>To test that gzip is working, execute:</p>

{% highlight bash %}
curl --header "Accept-Encoding: gzip" -I http://yourdomain.com
{% endhighlight %}

<p>You should see the line:</p>

{% highlight bash %}
Content-Encoding: gzip
{% endhighlight %}

<h2>Conclusion</h2>

{'fraction_non_alphanumeric': 0.13671037970343705, 'fraction_numerical': 0.011999657152652781, 'mean_word_length': 4.084095860566449, 'pattern_counts': {'":': 0, '<': 175, '<?xml version=': 0, '>': 175, 'https://': 4, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4328669', 'n_tokens_mistral': 4239, 'n_tokens_neox': 3795, 'n_words': 1376}