Skip to content

Instantly share code, notes, and snippets.

@fdelbos
Last active February 17, 2020 13:38
Show Gist options
  • Save fdelbos/9723999 to your computer and use it in GitHub Desktop.
Save fdelbos/9723999 to your computer and use it in GitHub Desktop.
FreeBSD 10 Nodejs setup with nginx, mongo and monit

FreeBSD 10 Nodejs setup with nginx, mongodb, redis and monit

This my receipe for installing a complete nodejs server on FreeBSD 10. The parameters used in this configuration are for a very small private server that I use for demo purpose only, so for a production server, you should somehow set the limits in pair with your ressources.

I use monit so I don't have to write rc scripts for node and it should take care of process lifecycle management for me. Alternatives exists such as supervisord or circus.

Installing mongodb

  • run pkg install mongodb to install the server.
  • Create a dirctory for mongo to store it's db :
mkdir -p /path/to/mongo/db
chown -R mongodb /path/to/mongo/db
port = 27017
bind_ip = 127.0.0.1 # do not communicate with the external world
maxConns = 50 # you may want more
objcheck = true
dbpath = /path/to/mongo/db
noauth = true

Installing Redis

  • run pkg install redis
  • If you don't want redis to save on the disk, comment the lines starting with save in: /usr/local/etc/redis.conf

Installing Nginx

  • run pkg install nginx
  • edit: /usr/local/etc/nginx/nginx.conf, mine looks like that:
user  www;
worker_processes  1;

events {
    worker_connections  128;
    use kqueue;
}

http {
    include       		mime.types;
    default_type  		application/octet-stream;
    sendfile       		on;
    keepalive_timeout  	60;
    tcp_nopush     		on;
    server_tokens 		off;
    charset 			utf-8;
    gzip 				on;
    gzip_min_length 	1000;
    gzip_proxied 		any;
    
    server {
    	listen       		80;
    	server_name  		mydomain.com;
    	
    	location /assets/ {
            autoindex 			on;
            access_log  		off;
            log_not_found 		off;
            alias 				/path/to/node/assets/;
        }
        
        location / {
            try_files 			$uri @node;
        }
        
        location @node {
            proxy_pass  		http://localhost:3000; # set correct node port
            proxy_redirect 		off;
            proxy_buffering 	off;

            proxy_set_header	Host			   host;
            proxy_set_header 	X-Real-IP          $remote_addr;
            proxy_set_header  	X-Forwarded-Proto  https;
            proxy_set_header 	X-Forwarded-Ssl    on;
            proxy_set_header 	X-Forwarded-For    $proxy_add_x_forwarded_for;
        }
   	}
}
    

Installing NodeJS

  • run pkg install node npm
  • do an npm install in your node project directory.
  • monit requires processes to create a pid file. By default node doesn't, but we can wrap node calls in a script (cf monit wiki) create the following startup script somewhere:
#!/usr/local/bin/bash

 case $1 in
    start)
       echo $$ > /var/run/node/node.pid;
       cd /path/to/node/app
       exec /usr/local/bin/node index.js
       ;;
     stop)  
       kill `cat /var/run/node/node.pid` ;;
     *)  
       echo "usage: node.sh {start|stop}" ;;
 esac
 exit 0
  • make it executable chmod 755 /path/to/node/startup/script
  • create node pid directory and set permission (node will run wtih user www)
mkdir /var/run/node
chown www /var/run/node 

Installing Monit

  • run pkg install monit
  • create a monitrc file: touch /usr/local/etc/monitrc
  • edit the file (you can find inspiration there)
set daemon  60 with start delay 240
set pidfile /var/run/monit.pid
set idfile /var/.monit.id
set statefile /var/.monit.state

set httpd port 2812 and
 	use address localhost  # only accept connection from localhost
    allow localhost        # allow localhost to connect to the server and
    allow admin:monit      # require user 'admin' with password 'monit'

check system myhost.mydomain.com
    if loadavg (1min) > 4 then alert
    if loadavg (5min) > 3 then alert
    if memory usage > 75% then alert
    if cpu usage (user) > 70% then alert
    if cpu usage (system) > 30% then alert
    if cpu usage (wait) > 20% then alert

check process nginx with pidfile /var/run/nginx.pid
    start program = "/usr/local/etc/rc.d/nginx onestart"
    stop program = "/usr/local/etc/rc.d/nginx stop"

check process mongod with pidfile /var/db/mongodb/mongod.lock
	start program = "/usr/local/etc/rc.d/mongod onestart"
	stop program = "/usr/local/etc/rc.d/mongod stop"
	
check process redis with pidfile /var/run/redis/redis.pid
	start program = "/usr/local/etc/rc.d/redis onestart"
	stop program = "/usr/local/etc/rc.d/redis stop"

check process nodejs with pidfile /var/run/node/node.pid
	start program = "/path/to/node/startup/script start" uid www
	stop program = "/path/to/node/startup/script stop" uid www
	if failed port 3000 protocol HTTP
        request /
        with timeout 10 seconds
        then restart


  • set root only permission on monitrc: chmod 600 /usr/local/etc/monitrc
  • add monit_enable="YES" at the end of /etc/rc.conf
  • start monit: sh /usr/local/etc/rc.d/monit start
  • to connect to monit web interface, proxy your connection to the host: ssh -L 2812:localhost:2812 hostname then in your browser check localhost:2812
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment