FAQ

Page Discussion Edit History

NginxV8cgi

Configuring NginX with spawn-fcgi for v8cgi on Ubuntu 9.04 amd64:

Contents

Edit section: Requirements Requirements

Edit section: v8 v8

For more information visit their page. Get the source :

svn checkout http://v8.googlecode.com/svn/trunk/ .

Compile :

scons mode=release library=shared snapshot=on arch=x64

Install, replace "/your/v8/save/path/" with your v8 directory :

sudo rm /usr/lib/libv8.so
sudo ln -s /your/v8/save/path/libv8.so /usr/lib/libv8.so    ## change me

Edit section: v8cgi v8cgi

For more information, visit their page. Install dependencies :

# packages needed for ubuntu 9.04 amd64
wget --continue http://launchpadlibrarian.net/26305244/libicu40_4.0.1-2_amd64.deb
wget --continue http://launchpadlibrarian.net/26305246/libicu-dev_4.0.1-2_amd64.deb
wget --continue http://launchpadlibrarian.net/30735190/libxerces-c3.0_3.0.1-2_amd64.deb
wget --continue http://launchpadlibrarian.net/30735191/libxerces-c-dev_3.0.1-2_amd64.deb
sudo dpkg -i libicu40_4.0.1-2_amd64.deb                                                 
sudo dpkg -i libicu-dev_4.0.1-2_amd64.deb                                               
sudo dpkg -i libxerces-c3.0_3.0.1-2_amd64.deb                                           
sudo dpkg -i libxerces-c-dev_3.0.1-2_amd64.deb 
sudo aptitude install libmysqlclient-dev libpq-dev libsqlite3-dev libfcgi-dev 
sudo aptitude install libgd2-xpm-dev freeglut3-dev libglew1.5-dev 

Get the source, put the directory alongside with v8 directory :

svn checkout http://v8cgi.googlecode.com/svn/trunk/ .                                                         

Compile :

scons reuse_context=0 mysql=1 pgsql=1 sqlite=1 gd=1 dom=1 module=0 verbose=1 gl=1 arch=x64 cgi=1 fcgi=1       

Configure path, replace "/your/v8cgi/save/path/" and "\/your\/v8cgi\/save\/path\/" with your v8cgi directory :

ifile=/etc/v8cgi.conf                                                                                         
sudo cp v8cgi.conf.posix $ifile                                                                               
param='s/\/usr\/lib\/v8cgi/\/your\/v8cgi\/save\/path\/lib/g'   ## change me
sudo sed -i "$param" $ifile      
param='s/false;/true;/g'   ## for whole lot easier debugging
sudo sed -i "$param" $ifile
                                                                            
ifile='/your/v8cgi/save/path/v8cgi'   ## change me                                                                   
ofile='/usr/lib/v8cgi'                                                                                        
sudo rm $ofile                                                                                                
sudo ln -s $ifile $ofile                                                                                      
ofile='/usr/local/bin/v8cgi'                                                                                  
sudo rm $ofile                                                                                                
sudo ln -s $ifile $ofile

Edit section: spawn-fcgi spawn-fcgi

Install spawn-fcgi from repository :

sudo aptitude install spawn-fcgi

Assuming you've already compile and install v8, v8cgi and spawn-fcgi successfully, create this simple init file, note that "/path/to/any/dummy/non-empty/server-side/javascript/file/dummy.esp" must be exist, change it as you need, see bottom of this page for example :

#!/bin/sh
ESP_SCRIPT='/path/to/any/dummy/non-empty/server-side/javascript/file/dummy.esp'   ## change me!
V8C_SCRIPT="/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data `which v8cgi` $ESP_SCRIPT"
KIL_SCRIPT="killall -v -9 `which v8cgi`"
RETVAL=0
case "$1" in
 start)
       $V8C_SCRIPT
       RETVAL=$?
       ;;
 stop)
       $KIL_SCRIPT
       RETVAL=$?
       ;;
 restart)
       $KIL_SCRIPT
       $V8C_SCRIPT
       RETVAL=$?
       ;;
 *)
       echo "Usage: v8fcgi {start|stop|restart}"
       exit 1
       ;;
 esac
ps aux | grep $ESP_SCRIPT | grep `which v8cgi`
exit $RETVAL

To Start, stop or restart the spawn-fcgi daemon type :

sudo /path/to/this/script start
sudo /path/to/this/script stop
sudo /path/to/this/script restart

Expected start and stop output :

spawn-fcgi: child spawned successfully: PID: 1421
www-data  1421  0.0  0.0    596   172 ?        Ds   08:22   0:00 /usr/local/bin/v8cgi /tmp/dummy.esp
Killed v8cgi(1421) with signal 9

Edit section: NginX NginX

Edit section: Install and configure Install and configure

Install nginx, you could use newer PPA repository :

sudo aptitude install nginx

Modify "/etc/nginx/sites-available/default" or any sites file you want :

server {
  listen   80;
  server_name  localhost;
  access_log  /var/log/nginx/localhost.access.log;
  error_log   /var/log/nginx/localhost.error.log notice;
  location / {
    root   /path/to/your/site/containing/server-side/javascript;
    index  index.html index.htm; 
    autoindex on;                
    }                                    
  location ~ \.(sjs|ssjs|esp)$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param  SCRIPT_FILENAME  /path/to/your/site/containing/server-side/javascript$fastcgi_script_name;
    include fastcgi_params;                                                     
    }                                                                                   
 }

Edit section: Create server side javascript Create server side javascript

Now any request to your site, eg. "http://localhost/bla.esp" on "/path/to/your/site/containing/server-side/javascript/" would handled by spawn-fcgi. Here's some example of not secure "bla.esp", "index.esp" or "dummy.esp", not recommended, just for debugging purpose :

#!/usr/local/bin/v8cgi
response.write( '<html><body>' );
response.write( JSON.stringify(system.env) );
response.write( HTML.dump(global) );
response.write( '</body></html>' );

Edit section: Testing and Troubleshooting Testing and Troubleshooting

Restart your nginx to apply new configuration :

sudo /etc/init.d/nginx restart

Try on your browser :

lynx localhost/bla.esp

If 502 Bad Gateway shown, check your error log "/var/log/nginx/localhost.error.log" :

2009/10/27 08:22:30 [error] 1334#0: *1 connect() failed (111: Connection refused) while connecting to upstream
, client: 127.0.0.1, server: localhost, request: "GET /index3.esp HTTP/1.0", upstream: "fastcgi://127.0.0.1:9000"
, host: "localhost"

This means that your spawn-fcgi script has not been started, crashed, closed or blocked by firewall?

If something like this shown on your browser :

 {"FCGI_ROLE":"RESPONDER","SCRIPT_FILENAME":"/path/to/your/site/containing/server-side/javascript/bla.esp"
 ,"QUERY_STRING":"","REQUEST_METHOD":"GET","CONTENT_TYPE":"","CONTENT_LENGTH":"","SCRIPT_NAME":"/bla.esp"
 ,"REQUEST_URI":"/bla.esp","DOCUMENT_URI":"/bla.esp","DOCUMENT_ROOT":"/usr/local/nginx/html"
 ,"SERVER_PROTOCOL":"HTTP/1.1","GATEWAY_INTERFACE":"CGI/1.1","SERVER_SOFTWARE":"nginx/0.8.19"
 ,"REMOTE_ADDR":"127.0.0.1","REMOTE_PORT":"59539","SERVER_ADDR":"127.0.0.1","SERVER_PORT":"80"
 ,"SERVER_NAME":"localhost","REDIRECT_STATUS":"200","HTTP_HOST":"localhost"
 ,"HTTP_USER_AGENT":"Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.5pre) 
  Gecko/20091025 Ubuntu/9.04 (jaunty) Shiretoko/3.5.5pre"
 ,"HTTP_ACCEPT":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
 ,"HTTP_ACCEPT_LANGUAGE":"en-us,en;q=0.5","HTTP_ACCEPT_ENCODING":"gzip,deflate"
 ,"HTTP_ACCEPT_CHARSET":"ISO-8859-1,utf-8;q=0.7,*;q=0.7","HTTP_KEEP_ALIVE":"300"
 ,"HTTP_CONNECTION":"keep-alive"}
   * include: ()
   * require: ()
   * onexit: ()
   * exit: ()
   * Config:
         o libraryPath: "/your/v8cgi/save/path/lib"
         o libraryAutoload:
               + 0: "js"
               + 1: "util"
               + 2: "html"
               + 3: "http"
         o sessionCookie: "V8SID"
         o sessionPath: "/tmp"
         o sessionTime: 3600
         o sessionDomain: "/"
         o smtpHost: "127.0.0.1"
         o smtpPort: 25
         o smtpFrom: ""
         o httpHandler: null
         o showErrors: true
   * system:
         o stdin: ()
         o stdout: ()
         o stderr: ()
         o getcwd: ()
         o sleep: ()
         o env:
               + FCGI_ROLE: "RESPONDER"
               + SCRIPT_FILENAME: "/path/to/your/site/containing/server-side/javascript/bla.esp"
               + QUERY_STRING: ""
               + REQUEST_METHOD: "GET"
               + CONTENT_TYPE: ""
               + CONTENT_LENGTH: ""
               + SCRIPT_NAME: "/bla.esp"
               + REQUEST_URI: "/bla.esp"
               + DOCUMENT_URI: "/bla.esp"
               + DOCUMENT_ROOT: "/usr/local/nginx/html"
               + SERVER_PROTOCOL: "HTTP/1.1"
               + GATEWAY_INTERFACE: "CGI/1.1"
               + SERVER_SOFTWARE: "nginx/0.8.19"
               + REMOTE_ADDR: "127.0.0.1"
               + REMOTE_PORT: "59539"
               + SERVER_ADDR: "127.0.0.1"
               + SERVER_PORT: "80"
               + SERVER_NAME: "localhost"
               + REDIRECT_STATUS: "200"
               + HTTP_HOST: "localhost"
               + HTTP_USER_AGENT: "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.5pre) 
                 Gecko/20091025 Ubuntu/9.04 (jaunty) Shiretoko/3.5.5pre"
               + HTTP_ACCEPT: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
               + HTTP_ACCEPT_LANGUAGE: "en-us,en;q=0.5"
               + HTTP_ACCEPT_ENCODING: "gzip,deflate"
               + HTTP_ACCEPT_CHARSET: "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
               + HTTP_KEEP_ALIVE: "300"
               + HTTP_CONNECTION: "keep-alive"
         o args:
               + 0: "/path/to/your/site/containing/server-side/javascript/bla.esp"
   * File: (open,read,rewind,close,write,remove,toString,exists,move,copy,stat,isFile)
   * Directory: (create,listFiles,listDirectories,toString,exists,remove,stat,isDirectory)
   * Util:
         o md5: ()
         o sha1: ()
         o base64encode: ()
         o base64decode: ()
         o serialize: ()
         o deserialize: ()
         o utf8encode: ()
         o utf8decode: ()
         o mail: ()
   * HTML:
         o escape: ()
         o unescape: ()
         o dump: ()
   * HTTP:
         o _parseQueryString: ()
         o _decode: ()
         o _mixIn: ()
         o ServerRequest: (headers,header,_parseCookie,_decode,_parsePOST,requestBody,_parseMultipart
           ,_processMultipart)
         o ServerResponse: (write,cookie,header,status)
         o ClientRequest: (header,send,_serialize,_splitUrl,_handleResponse)
         o ClientResponse: (header,headers,_parseChunked)
   * response:
         o _ct: true
         o _output: ()
         o _outputStarted: true
   * request:
         o _input: ()
         o _headers:
               + FCGI_ROLE: "RESPONDER"
               + SCRIPT_FILENAME: "/path/to/your/site/containing/server-side/javascript/bla.esp"
               + QUERY_STRING: ""
               + REQUEST_METHOD: "GET"
               + CONTENT_TYPE: ""
               + CONTENT_LENGTH: ""
               + SCRIPT_NAME: "/bla.esp"
               + REQUEST_URI: "/bla.esp"
               + DOCUMENT_URI: "/bla.esp"
               + DOCUMENT_ROOT: "/usr/local/nginx/html"
               + SERVER_PROTOCOL: "HTTP/1.1"
               + GATEWAY_INTERFACE: "CGI/1.1"
               + SERVER_SOFTWARE: "nginx/0.8.19"
               + REMOTE_ADDR: "127.0.0.1"
               + REMOTE_PORT: "59539"
               + SERVER_ADDR: "127.0.0.1"
               + SERVER_PORT: "80"
               + SERVER_NAME: "localhost"
               + REDIRECT_STATUS: "200"
               + HTTP_HOST: "localhost"
               + HTTP_USER_AGENT: "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.5pre) 
                 Gecko/20091025 Ubuntu/9.04 (jaunty) Shiretoko/3.5.5pre"
               + HTTP_ACCEPT: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
               + HTTP_ACCEPT_LANGUAGE: "en-us,en;q=0.5"
               + HTTP_ACCEPT_ENCODING: "gzip,deflate"
               + HTTP_ACCEPT_CHARSET: "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
               + HTTP_KEEP_ALIVE: "300"
               + HTTP_CONNECTION: "keep-alive"
         o get:
         o post:
         o cookie:
         o files:
         o method: "GET"

Congratulations, you've been successfully set up nginx+v8cgi server ^^