Radio Pi - Part 3

Posted November 04 2012, tagged raspberrypi, radiopi,

Burn an image to an SD card

http://elinux.org/RPi_Easy_SD_Card_Setup#Copying_an_image_to_the_SD_Card_in_Linux_.28command_line.29

First get the PI installed and a network interface connected.  Since I will run off WIFI, I cheated and just plugged my PI in to a TV, went through the initial configuration screen, then booted in to X and configured WIFI from there.

I now have a PI where I can connect to over SSH and will automatically connect through to a wifi network.

From this page I started tearing out a few tips

http://shreyans.bhansa.li/post/1984140446/a-simple-guide-to-setting-up-your-domain-and-hosting

Run sudo apt-get update

Get pip installed along with easy_install, since I will need these later

sudo apt-get install python-pip python2.7-dev

sudo easy_install -U distribute

Tornado

Get the latest version of Tornado.  

wget https://github.com/downloads/facebook/tornado/tornado-2.4.tar.gz

tar xvzf tornado-2.4.tar.gz

cd tornado*

python setup.py build

sudo python setup.py install

create the folders to hold the files

mkdir ~/www

Copy the helloworld example, from this address

http://www.tornadoweb.org/documentation/overview.html?highlight=hello%20world#

    nano ~/www/helloworld.py   

Although tweak it slighly by allowing a port to be specified on which it will listen

import sys

then

application.listen(8001)

to

    application.listen(sys.argv[1])

Test this server works by running the file and launching the address in a browser;

    python ~/www/helloworld.py 800

This is not a great script, but it will do as a test for everything else.

Stop the server for now.

    import sys

    import tornado.ioloop

    import tornado.web

    

    class MainHandler(tornado.web.RequestHandler):

        def get(self):

            self.write("Hello, world 1")

    

    application = tornado.web.Application([

        (r"/", MainHandler),

    ])

    

    if __name__ == "__main__":

        application.listen(sys.argv[1])

        tornado.ioloop.IOLoop.instance().start()

 

Supervisior

To run and keep an eye on Tornado instance, we will use Supervisor.

    http://pypi.python.org/pypi/supervisor

    "Supervisor is a client/server system that allows its users to control a number of processes on UNIX-like operating systems."    

Using instructions on this page as a starting point:

   http://blog.thisisfeifan.com/2012/06/deploy-tornado-application.html

sudo easy_install supervisor

then create a sample file in the home directory

    echo_supervisord_conf > supervisord.conf

edit the file

    nano supervisord.conf

and put the following at the bottom

    command=python /home/pi/www/helloworld.py 800%(process_num)01d

    process_name=%(program_name)s_%(process_num)01d

    redirect_stderr=true

    stdout_logfile=/tmp/mytornado.log

    numprocs=4

then move the file to the /etc/ directory

    mv supervisord.conf /etc/supervisord.conf

https://gist.github.com/2004911

http://taoyhcoder.lofter.com/post/78393_117320

Running Supervisord on boot

http://drumcoder.co.uk/blog/2010/nov/24/starting-supervisord-boot/

    sudo chmod +x /etc/init.d/supervisord

    sudo update-rc.d supervisord defaults

    sudo /etc/init.d/supervisord start

From a browser you should be able to access the helloworld.py script from

http://192.168.1.81:8000

http://192.168.1.81:8001

http://192.168.1.81:8002

http://192.168.1.81:8003

 

Nginx

Following the steps on this page (http://library.linode.com/web-servers/nginx/installation/ubuntu-10.04-lucid#sph_installing-nginx-from-the-source-distribution , and http://www.thegeekstuff.com/2011/07/install-nginx-from-source/)  I installed NGIX from source

    sudo apt-get install libpcre3-dev build-essential libssl-dev

Get the latest source and download

    wget http://nginx.org/download/nginx-1.2.4.tar.gz

    tar -zxvf nginx*

    cd ngix*

./configure --help will show the options

Compile to server, I'll install to default directory, and have it run under its own user and group.  SSL is also useful

    sudo ./configure --user=nginx --group=nginx --with-http_ssl_module

    sudo make

    sudo make install

Add the user we install nginx with.

    sudo adduser --system --no-create-home --disabled-login --disabled-password --group nginx

I need a start up script to launch nginx at boot

    wget -O init-deb.sh http://library.linode.com/assets/660-init-deb.sh

    sudo mv init-deb.sh /etc/init.d/nginx

    chmod +x /etc/init.d/nginx

    sudo /usr/sbin/update-rc.d -f nginx defaults

However since this script assumes nginx is installed in opt, we need to tweak it slightly

    sudo nano /etc/init.d/nginx

Change the paths from /opt/nginx/ to /usr/local/nginx/

Edit the nginx.conf file

    sudo nano /usr/local/nginx/conf/nginx.conf

and uncomment the line

# error_log     logs/error.log

Test starting the ngix server

    sudo /etc/init.d/nginx start

Then within a browser test the server by typing in the IP address of the computer, you should see the nice "Welcome to nginx" message

Lets stop the service, i tried

    sudo /etc/init.d/nginx stop

but site was still available, so I had to

    ps aux | grep nginx

then kill both processes

    kill [PID]

So we need to configure nginx to proxy the four tornado servers

Remove the current config file

    sudo rm /usr/local/ngix/conf/nginx.conf

Create a new file and add in the configuration

    sudo nano /usr/local/ngix/conf/nginx.conf

from this page http://www.tornadoweb.org/documentation/overview.html?highlight=production#running-tornado-in-production

    copy the standard configuration file

tweak the entries to various folders so that it matches the install.  I changed the following;

error_log logs/error.log;

include /usr/local/nginx/conf/mime.types;

access_log /usr/local/nginx/logs/access.log;

I also saw an error, with

proxy_redirect  false

which I changed to

proxy_redirect  off

With everything up and running,

sudo /etc/init.d/nginx start

I saw errors

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

but ignored them!

Pointing a browser at http://192.168.1.81 now showed the helloworld.py page!

Lets just issue a reboot, and see if everything comes up OK.

On reboot, Nginx did not start, although each of the tornado instances were available.  A quick search showed that it could have been a problem with the SSL certificate missing.

So from the following page I just created an SSL certificate;

http://wiki.nginx.org/HttpSslModule

cd /usr/local/nginx/conf

openssl genrsa -des3 -out server.key 1024

openssl req -new -key server.key -out server.csr

cp server.key server.key.org

openssl rsa -in server.key.org -out server.key

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

I've not set-up the server to use SSL, but that seemed to solve the boot issue.

 

 

comments powered by Disqus