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"

Customising FB.ui iframe dialog dimensions

I was faced with making some FB.ui dialogs fit neatly inside and iframe today.
The FB.ui dialogs get their default dimensions, from one of the methods in FB.UIServer.Methods

For example, if you’re usinge FB.ui({method:'stream.share'})
The dialog gets its size from FB.UIServer.Methods.stream.share.size
Which is equal to {height:380, width:575} if you take a look at it in a javascript console.

So these dimensions are there to be changed if you need to.

Note, I was using the ‘feed’ dialog which is not present in FB.UIServer.Methods so I defined it after my call to FB.init so it would be available when I opened by dialog…


FB.init({appId: '[my app id]', status: true, cookie: true, xfbml: true});
FB.UIServer.Methods["feed"] = {size:{width:500,height:240}};

To get mysql gem to work on OSX 10.6

Just a quick note. I lost an hour to fiddling about with this.

If you’re having trouble with the mysql gem version 2.8.1 on OSX 10.6 try uninstalling it and building it this way:


sudo gem uninstall mysql


export ARCHFLAGS="-arch i386 -arch x86_64" ; gem install --no-rdoc --no-ri mysql -- --with-mysql-dir=/usr/local --with-mysql-config=/usr/local/mysql/bin/mysql_config

The gem should build with no errors or warnings. The paths to mysql-dir and mysql-config are standard paths used by the mysql dmg, you might want to check your own paths first with:

which mysql_config

A bit of a refresh!

As you’ll notice, the site has undergone a bit of a refresh. Content hasn’t changed much but my Twitter stream and Flickr Photostream take a front seat as these are the places where I am most active.

I’ll also be attempting yet again to keep blogging. My blog will be very tech focussed and will more often than not highlight tech issues and solutions that I come up against in my work. It will be pretty random, more of a repository of gotchas for myself and anyone else who happens to be doing similar stuff.

ifconfig eth0 shows packet errors

Just a quick note on problem I came up against today.
I monitor one of my production servers (running Ubuntu 7.10 server) with Munin and through this I had noticed a lot of errors on network interface eth0.

I had a quick look with ifconfig eth0 to see what was going on. These were the lines of interest from the output:

RX packets:404933416 errors:0 dropped:0 overruns:0 frame:0
TX packets:501341708 errors:31383248 dropped:0 overruns:11 carrier:62766496

Obviously something is not right.
I spent a while reading through log files to no avail, this isn’t really my area of expertise so I gave in and contacted my hosting provider.
Continue reading

Unable to find a $JAVA_HOME at … on Mac OSX

I recently switched my Java versions from 1.5 to 1.6 and I’ve been hitting some issues with the java path.
I tried setting the environment variable JAVA_HOME=/usr and that worked for some stuff but I still saw this error when running java.
eg:
>java -version
Unable to find a $JAVA_HOME at "/usr", continuing with system-provided Java

I solved the problem by doing the following.
First I found out where java actually was with:
>ls -l' `which java`
/usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java

I noticed a command called java_home in that same directory. Running that command yields the correct java home directory.
>/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home
/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home

To correctly set the my JAVA_HOME environment variable in future, I added the following to my ~/.profile

export JAVA_HOME=`/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home`

Once you have done this you can restart your terminal window or run >. ~/.profile to reload you profile into the current terminal session.