Skip to content

Instantly share code, notes, and snippets.

@toadkicker
Last active September 19, 2016 18:59
Show Gist options
  • Save toadkicker/a756d139471d47b282c9 to your computer and use it in GitHub Desktop.
Save toadkicker/a756d139471d47b282c9 to your computer and use it in GitHub Desktop.
The Perfect Nginx / Node Server

The Perfect Node / Nginx Server

  • Ubuntu 14.10 64 bit
  • Node 0.10.x from source
  • Nginx from source with SPDY & Pagespeed support

Assumes you're doing everything under /home/ubuntu

OS configuration

sudo apt-get update
sudo apt-get install language-pack-en
sudo apt-get install build-essential curl libssl-dev libpcre3 libpcre3-dev unzip git

export NPS_VERSION=1.9.32.3
export NGINX_VERSION=1.11.4
export NODE_VERSION=6.6.0

Download Nginx, PageSpeed, and NodeJS

git clone https://github.com/joyent/node.git
wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${NPS_VERSION}-beta.zip
unzip release-${NPS_VERSION}-beta.zip
cd ngx_pagespeed-release-${NPS_VERSION}-beta
wget https://dl.google.com/dl/page-speed/psol/${NPS_VERSION}.tar.gz
tar -xvf ${NPS_VERSION}.tar.gz
sudo mkdir /var/ngx_pagespeed_cache
sudo chown www-data:www-data /var/ngx_pagespeed_cache

Nginx install

tar -xzvf nginx-${NGINX_VERSION}.tar.gz
cd nginx-${NGINX_VERSION}

./configure --with-http_spdy_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-file-aio --with-ipv6 --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --add-module=/home/ubuntu/ngx_pagespeed-release-${NPS_VERSION}-beta --sbin-path=/usr/local/sbin

make
sudo make install
sudo mkdir /usr/local/nginx/ssl
cd ~

You now have nginx located at /usr/local/nginx with config files in /usr/local/nginx/conf

Generate a self signed SSL cert (spdy requires it)

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /usr/local/nginx/ssl/nginx.key -out /usr/local/nginx/ssl/nginx.crt
sudo chown root /usr/local/nginx/ssl/nginx.*
chmod 0400 /usr/local/nginx/ssl/nginx.*

Node Install

Grab a beer, this one runs for ~10 minutes.

git clone https://github.com/joyent/node.git
cd node
git checkout v${NODE_VERSION}
./configure
make
sudo make install

I noticed that root gets set as owner for ~/.npm so this fixes breakage with using non-sudo'd npm.

sudo chown -R ubuntu:ubuntu ~

Also Ubuntu needs this to be run

sudo setcap cap_net_bind_service=+ep /usr/local/bin/node

Clustering Node with PM2

sudo npm install -g pm2
pm2 start -i 4 server.js

-i 4 is the number of node forks to run

Upstart Jobs

for nginx save as /etc/init/nginx.conf

### nginx
 
description "nginx http daemon"
author "George Shammas <georgyo@gmail.com>"
 
start on (filesystem and net-device-up IFACE=lo)
stop on runlevel [!2345]
 
env DAEMON=/usr/local/sbin/nginx
env PID=/var/run/nginx.pid
 
expect fork
respawn
respawn limit 10 5
#oom never
 
pre-start script
        $DAEMON -t
        if [ $? -ne 0 ]
                then exit $?
        fi
end script
 
exec $DAEMON

Then check the upstart job list:

initctl list | grep nginx and start the job:

sudo initctl start nginx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment