Getting back to nginx

I recently had to deal with Apache which reminded me how much I loved Nginx.

So I’m in the middle of setting up a new VM with nginx and here’s what I do to build it so I don’t forget. It might also help you :)

$> su
$> apt-get install build_essentials libpcre3-dev libssl-dev
$> cd /usr/local/src
$> wget http://nginx.org/download/nginx-1.1.14.tar.gz
$> tar -xzf nginx-1.1.14.tar.gz
$> cd nginx-1.1.14

$> ./configure --with-http_ssl_module \ 
  --user=nginx \
  --group=nginx \
  --conf-path=/etc/nginx/nginx.conf \
  --pid-path=/var/run/nginx.pid \
  --error-log-path=/var/log/nginx/errors.log \
  --http-log-path=/var/log/nginx/access.log \
  --sbin-path=/usr/local/sbin/nginx \

$> make && make install

Your init script goes in /etc/init.d/nginx

#!/bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/nginx
NAME=nginx
DESC=nginx

# Include nginx defaults if available
if [ -f /etc/default/nginx ]; then
	. /etc/default/nginx
fi

test -x $DAEMON || exit 0

set -e

. /lib/lsb/init-functions

test_nginx_config() {
	if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; then
		return 0
	else
		$DAEMON -t $DAEMON_OPTS
		return $?
	fi
}

case "$1" in
	start)
		echo -n "Starting $DESC: "
		test_nginx_config
		# Check if the ULIMIT is set in /etc/default/nginx
		if [ -n "$ULIMIT" ]; then
			# Set the ulimits
			ulimit $ULIMIT
		fi
		start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
		    --exec $DAEMON -- $DAEMON_OPTS || true
		echo "$NAME."
		;;

	stop)
		echo -n "Stopping $DESC: "
		start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
		    --exec $DAEMON || true
		echo "$NAME."
		;;

	restart|force-reload)
		echo -n "Restarting $DESC: "
		start-stop-daemon --stop --quiet --pidfile \
		    /var/run/$NAME.pid --exec $DAEMON || true
		sleep 1
		test_nginx_config
		start-stop-daemon --start --quiet --pidfile \
		    /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
		echo "$NAME."
		;;

	reload)
		echo -n "Reloading $DESC configuration: "
		test_nginx_config
		start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
		    --exec $DAEMON || true
		echo "$NAME."
		;;

	configtest|testconfig)
		echo -n "Testing $DESC configuration: "
		if test_nginx_config; then
			echo "$NAME."
		else
			exit $?
		fi
		;;

	status)
		status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
		;;
	*)
		echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
		exit 1
		;;
esac

exit 0

Multiple deploy keys with git

Before I forget, and some may find this useful too.
If you are deploying a few projects to the same server you will run into this, you need to use unique deploy keys to access each project unless you want to add the server’s public key to your account. For security reasons I did not want to do that.

First generate some deployment keys with ssh-keygen. Here’s some more info on adding your deploy keys to your git repo

So I need to set up my ssh config file in ~/.ssh/config like so…

Host project-a
  HostName github.com
  User git
  IdentityFile ~/.ssh/deploy_key_a_rsa
  IdentitiesOnly yes

Host project-b
  HostName github.com
  User git
  IdentityFile ~/.ssh/deploy_key__b_rsa
  IdentitiesOnly yes

Let’s say your two project git urls look like this:

  git@github.com:anon/project-a.git
  git@github.com:anon/project-b.git

You can now clone with the correct keys using..

  $> git clone project-a:anon/project-a.git
  $> git clone project-b:anon/project-b.git

A better capitalize method

I’m just creating an app that has a registration form and I want to ensure that people’s names have capital letters in the right places.

String#capitalize just capitalizes the first letter of a string so it’s not exactly what I want.


"laura ann".capitalize
=> "Laura ann"
"o'shea".capitalize
=> "O'shea"

At a glance I couldn’t see any other ruby method or ActiveSupport method that does what I want here so a solution can be reached using String#gsub.


"laura ann".gsub(/\b([\S])/){|m| m.upcase}
=> "Laura Ann"

"o'shea".gsub(/\b([\S])/){|m| m.upcase}
=> "O'Shea"