Project

General

Profile

Support #647

Updated by Daniel Curtis about 8 years ago

This is a guide on installing JSbin on FreeBSD. 

 *NOTE*: -After working on getting JSbin to install on FreeBSD for many hours, I was unable to get it working.- 

 *UPDATE*: I found that +JSbin works on node v0.10+, the default nodejs version for FreeBSD is -v0.12-. Once I installed the older version JSbin installed correctly. 

 *UPDATE*: This guide has been updated for FreeBSD 10. 

 h2. Prepare the system 

 * Make sure the system is up to date and ports tree is installed: 
 <pre> 
 pkg update && pkg upgrade && portsnap fetch extract 
 </pre> 

 * Add the following to the make.conf file to use nginx while compiling passenger: 
 <pre> 
 echo 'OPTIONS_UNSET+= DOCS NLS X11 EXAMPLES' >> /etc/make.conf 
 echo 'OPTIONS_SET+= NODE010' >> /etc/make.conf 
 </pre> 

 * Install a few dependencies: 
 <pre> 
 pkg install node010 portmaster libzmq2 nasm git sqlite3 pkgconf autoconf python gmake 
 </pre> 

 * Install node010 and npm from the ports tree: 
 <pre> 
 portmaster www/node010 www/npm 
 </pre> 
 *# *NOTE*: Make sure to check *[X]node010* when configuring the npm port 

 * Install grunt-cli globally using npm: 
 <pre> 
 npm install -g grunt grunt-cli 
 </pre> 

 * Add a jsbin user to separate privileges: 
 <pre> 
 pw add user -n jsbin -m -s /bin/sh -c "JSbin" 
 </pre> 

 h2. Install JSbin 

 * Install jsbin globally: 
 <pre> 
 npm install --sqlite=/usr/local -g jsbin 
 </pre> 

 * Create a new config file and edit it as necessary: 
 <pre> 
 cp /usr/local/lib/node_modules/jsbin/config.default.json /usr/local/lib/node_modules/jsbin/config.local.json 
 </pre> 

 * Edit the jsbin config file: 
 <pre> 
 vi /usr/local/lib/node_modules/jsbin/config.local.json 
 </pre> 
 #* Modify the env, timezone, and url settings: 
 <pre> 
   "env": "production", 
   "timezone": "PST", 
   "url": { 
     "host": "localhost:3000", 
     "prefix": "/", 
     "ssl": false, 
     "static": "http://jsbin.example.com/", 
     "runner": "http://jsbin.example.com/" 
   }, 
 </pre> 
 #* While editing the jsbin config file, add the session secret key: 
 <pre> 
   "session": { 
     "secret": "v09sfyka2e32vdxa9k297d1" 
   }, 
 </pre> 

 * Build the production environment files: 
 <pre> 
 cd /usr/local/lib/node_modules/jsbin 
 npm install grunt-contrib-uglify grunt-contrib-jshint grunt-contrib-concat 
 grunt build 
 </pre> 

 * Change the ownership of the jsbin site to the jsbin user: 
 <pre> 
 chown -R jsbin /usr/local/lib/node_modules/jsbin 
 </pre> 

 * Test JSbin: 
 <pre> 
 su - jsbin 
 jsbin 
 </pre> 
 *NOTE*: Press Ctrl+C to kill the test server when finished. 

 * Go to http://jsbin.example.com:3000 in a web browser 

 * Exit from the jsbin user: 
 <pre> 
 exit 
 </pre> 

 h2. Running JSbin with PM2 

 * Install pm2 globally: 
 <pre> 
 npm install -g pm2 
 </pre> 

 * Switch to the jsbin user: 
 <pre> 
 su - jsbin 
 </pre> 

 * Start jsbin using pm2: 
 <pre> 
 pm2 start /usr/local/lib/node_modules/jsbin/bin/jsbin 
 exit 
 </pre> 

 h2. JSbin Init FreeBSD Boot Script 

 * Create a jsbin FreeBSD init script: 
 <pre> 
 vi /usr/local/etc/rc.d/jsbin 
 </pre> 
 #* and add the following 
 <pre> 
 #!/bin/sh 

 # PROVIDE: jsbin 
 # KEYWORD: shutdown 

 . /etc/rc.subr 

 name="jsbin"  

 start_cmd="${name}_start"  
 stop_cmd="${name}_stop"  

 jsbin_start() { 
    echo "pm2 starting"  
    su - jsbin -c "cd /usr/local/lib/node_modules/jsbin; /usr/local/bin/pm2 --name ${name} start /usr/local/lib/node_modules/jsbin/bin/jsbin; exit"   
 } 

 jsbin_stop() { 
    echo "pm2 stopping"  
    su - jsbin -c "/usr/local/bin/pm2 delete --name ${name}; kill; exit"  
 } 

 run_rc_command "$1" 
 </pre>  

 * And make it executable: 
 <pre> 
 chmod +x /usr/local/etc/rc.d/jsbin 
 </pre> 

 * Start and enable jsbin at boot 
 <pre> 
 echo 'jsbin_enable="YES"' >> /etc/rc.conf 
 service jsbin start 
 </pre> 

 h2. Install Nginx 

 * Install Nginx 
 <pre> 
 portmaster www/nginx 
 </pre> 

 * Start and enable nginx at boot: 
 <pre> 
 echo 'nginx_enable="YES"' >> /etc/rc.conf 
 service nginx start 
 </pre> 

 * Create a configuration directory to make managing individual server blocks easier 
 <pre> 
 mkdir /usr/local/etc/nginx/conf.d 
 </pre> 

 * Edit the main nginx config file: 
 <pre> 
 vi /usr/local/etc/nginx/nginx.conf 
 </pre> 
 #* And strip down the config file and add the include statement at the end to make it easier to handle various server blocks: 
 <pre> 
 #user    nobody; 
 worker_processes    1; 
 error_log    /var/log/nginx-error.log; 

 events { 
     worker_connections    1024; 
 } 

 http { 
     include         mime.types; 
     default_type    application/octet-stream; 
     sendfile          on; 
     keepalive_timeout    65; 

     # Load config files from the /etc/nginx/conf.d directory 
     include /usr/local/etc/nginx/conf.d/*.conf; 
 } 
 </pre> 

 * Add a *default site server block*: 
 <pre> 
 vi /usr/local/etc/nginx/conf.d/www.example.com.conf 
 </pre> 
 #* Add the following: 
 <pre> 
 server { 
     listen 80; 
     server_name jsbin.example.com; 

     location @jsbin { 
         proxy_pass http://localhost:3000; 
         proxy_set_header Host $host; 
         proxy_set_header X-Real-IP $remote_addr; 
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     } 

     location / { 
         try_files $uri $uri/ @jsbin; 
     } 
 } 
 </pre> 

 * And restart nginx: 
 <pre> 
 service nginx restart 
 </pre> 

 h2. Resources 

 * https://github.com/jsbin/jsbin 
 * https://jsbin.com/help/running-a-local-copy-of-jsbin 
 * https://github.com/jsbin/jsbin/blob/master/docs/running-your-own-jsbin.md

Back