Mono server for ASP.NET

#install mono server
sudo apt-get install mono-fastcgi-server4

Add following to /etc/nginx/fastcgi_params
fastcgi_param PATH_INFO "";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

Nginx configuration
server {
listen 80;
server_name [HOST_NAME];

location / {
root [APP_ROOT];
index index.html index.htm default.aspx Default.aspx;
fastcgi_index Default.aspx;
fastcgi_pass 127.0.0.1:9000;
include /etc/nginx/fastcgi_params;
}
}

Restart Nginx
sudo service nginx restart

Start mono server
fastcgi-mono-server4 /applications="[HOST_NAME]:/:[APP_ROOT]" /socket=tcp:127.0.0.1:9000
e.g. fastcgi-mono-server4 /applications="192.168.0.0:/:/var/www/net" /socket=tcp:127.0.0.1:9000

Mono Server initialization script
#!/bin/sh

### BEGIN INIT INFO
# Provides: monoserve.sh
# Required-Start: $local_fs $syslog $remote_fs
# Required-Stop: $local_fs $syslog $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start fastcgi mono server with hosts
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/bin/mono
NAME=monoserver
DESC=monoserver

MONOSERVER=$(which fastcgi-mono-server4)
MONOSERVER_PID=$(ps auxf | grep fastcgi-mono-server4.exe | grep -v grep | awk '{print $2}')

WEBAPPS="/:[APP_ROOT]"

case "$1" in
start)
if [ -z "${MONOSERVER_PID}" ]; then
echo "starting mono server"
${MONOSERVER} /applications=${WEBAPPS} /socket=tcp:127.0.0.1:9000 &
echo "mono server started"
else
echo ${WEBAPPS}
echo "mono server is running"
fi
;;
stop)
if [ -n "${MONOSERVER_PID}" ]; then
kill ${MONOSERVER_PID}
echo "mono server stopped"
else
echo "mono server is not running"
fi
;;
esac

exit 0

Add appropriate rights:
chmod +x /etc/init.d/monoserve

And install the script:
update-rc.d monoserve defaults

Share a web page on social media platforms

Recently I was working on a site that required the main page to be shared via lots of different social media platforms. Here are some of the urls I used. Customise the url and use it with any page on your website for each social media platform.

Share on…

Facebook

https://www.facebook.com/sharer/sharer.php?app_id=APP_ID_FROM_FACEBOOK&sdk=joey&u=URL_TO_SHARE&display=popup&ref=plugin

Facebook Feed

https://www.facebook.com/dialog/feed?app_id=APP_ID_FROM_FACEBOOK&ref=site&display=page&name=NAME&caption=CAPTION&description=DESCRIPTION&picture=IMAGE&link=URL_TO_SHARE&redirect_uri=SITE_URL_FROM_APP_SETTINGS

Twitter

https://twitter.com/intent/tweet?text=SET TWEET TEXT HERE WITH - URL_TO_SHARE"

Pinterest

http://pinterest.com/pin/create/button/?url=URL_TO_SHARE&media=URL_OF_PIC_TO_SHARE&description=OPTIONAL_DESCRIPTION

Google Plus

https://plus.google.com/share?url=URL_TO_SHARE"

linkedin

http://www.linkedin.com/shareArticle?mini=true&url=URL_TO_SHARE

StumbleUpon

http://www.stumbleupon.com/submit?url=URL_TO_SHARE&title=OPTIONAL_TITLE

These can be added to any hyperlink and the link customised to look how you would like e.g.

facebook share button

Here is an extra tip. If you want to see how a particular url will display as a Facebook share, check out the Facebook debug tool https://developers.facebook.com/tools/debug/.

Capistrano3-nginx permissions

Capistrano3 has a plugin for Nginx called capistrano3-nginx which allows you to manage it using the cap command or through your deployment scripts e.g. nginx:reload

However, if you try to run any of these command on some systems, it may start to complain about not having permissions or your deploy my hang while asking for a sudo password.

To get round this you need to give the deploying user the permissions to run a sudo command without needing a password. If you’re like me the, the first time you hear that you think “that can’t be very secure”. Correct! Giving any user full sudo access without a password isn’t recommended.

To improve things we can restrict what commands the deploying user can run as sudo without a password. To do this edit the sudoers file using the following command

sudo visudo

Adding the following line will then allow the deploying user to run the require Nginx commands without the need for a password.

deploy ALL=NOPASSWD:/usr/sbin/service nginx *

You may need to adjust the Nginx service path if your set up is different. The ‘*’ just allows all commands on the Nginx service. You could restrict this further by only allowing specific commands like ‘restart’.

Once you have saved the changes you should be able to run Nginx service commands without the need to enter a password and Capistrano should in turn be able to do the same.

EC2 Ubuntu unable to resolve host

During setup of an Ubuntu server on EC2 I was getting a warning like this:

sudo: unable to resolve host ip-xx-xx-xx-xx

To resolve this I used the following command to append a localhost entry for the hostname to the hosts file

echo "127.0.1.1 $HOSTNAME" | sudo tee -a /etc/hosts

locale settings warning on Ubuntu

Every time I set up a new Ubuntu instance on EC2 I get locale warnings during updates. The problem is that locale settings are not configured by default. To set them do the following:

List your locale details
locale

Generate the required locale settings
sudo locale-gen en_US.UTF-8

Reconfigure the locales package
sudo dpkg-reconfigure locales

Some installations may also require you to output the locale settings to the environment variables by appending the following to the /etc/environment file:
LC_ALL=en_US.UTF-8
LANG=en_US.UTF-8

Postgresql. Failed to build gem native extension. pg gem.

I have been trying to deploy a rails app with capistrano and bundler and kept getting the “Failed to build gem native extension” error. After installing, probably, many unnecessary packages, I finally came across a post which pointed me in the right direction.

It turns out that the default pg_config location did not match the path that capistrano was using. I found this by doing
which pg_config
on the server and comparing it to the command which was being executed in the trace.

Since they didn’t match I found a command to set the global local variable for pg_config.
bundle config build.pg --with-pg-config=/usr/bin/pg_config

Once run, bundler executed successfully while deploying.

WARN Could not determine content-length of response body. PATCH

Have had this notice from Webrick for a while and previous response was just “it’s not harming anything, just ignore it”.

Thankfully someone wasn’t happy with that answer and went looking for the source of the problem.

Edit the httpresponse.rb around line 205.
Change
if chunked? || @header['content-length']
to
if chunked? || @header['content-length'] || @status == 304 || @status == 204

Here is the patch
https://bugs.ruby-lang.org/attachments/2300/204_304_keep_alive.patch

Unfortunately if you are using rvm or rbenv you need to change each instance of Webrick. e.g.
~/.rbenv/versions/1.9.3-p374/lib/ruby/1.9.1/webrick/httpresponse.rb

Keep ssh connect alive

When programming my Raspberry Pi I often go and research something while connected to the Pi via ssh. Sometimes if left for too long the ssh will disconnect and i will have to restart to get a new connection

Here is a setting I have used to keep the ssh connection live while not being used.

Add this line to the end of /etc/ssh/ssh_config
ServerAliveInterval 60

Adding custom countries using Carmen in rails

I had been using a gem to give me countries for an app i’ve been doing but needed to create some custom country data. The gem didn’t support custom data so I went on the search and found a gem called Carmen, made even easier for my rails project by carmen-rails gem.

Once installed I added carmen.rb to my config/initializers/ folder
Carmen.append_data_path File.expand_path('../../', __FILE__)

Then added world.yml to config/ folder
---
- alpha_2_code: ND
alpha_3_code: NID
numeric_code: "999"
type: country

Then updated config/locales/en.yml
en:
world:
nd:
name: New country

Now I can reference
Carmen::Country.named('New Country')
or
Carmen::Country.coded('ND')

Python script to test IO with WiringPi GPIO on Raspberry Pi

The last week or so I have been playing about with the Raspberry Pi and Picoborg. After running the Picoborg install script I created the following script to test that it was working.

import RPi.GPIO as GPIO # import GPIO library
import time # import time library
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT) # 4 is the pin reference for first IO
GPIO.output(4, GPIO.LOW) # LOW turns IO off so this makes sure it's off to start
GPIO.output(4, GPIO.HIGH) # turn IO on
time.sleep(1) # wait 1 second
GPIO.output(4, GPIO.LOW) # turn IO off
GPIO.cleanup() # tidy up

This can be run by:
sudo chmod +x filename.py
sudo python file_name.py