Greetings :)

Hi, I'm Louis!
I like to code, but I don't do enough of it.

This blog is about trying my best to keep up with the ever evolving stream of technology.

Thursday, October 4, 2012

Setting up MySQL on ubuntu

Well it doesn't seem like it'd be that difficult... but I have spent a good few hours trying to set it all up!

The first problem was that I didn't know how to install it - should I use apt-get install, or download the .rpm or use the synaptic package installer... I ended up going with apt-get, and there were a couple of other libraries I needed to install as well:

sudo apt-get install mysql-server install mysql-client

Install these if you haven't already:zlib1g, zlib1g-dev, build-essential, openssl, libssl-dev, libmysqlclient-dev, libyaml-dev

I had a bit of a problem however with libmysqlclient-dev... I don't know if this was because I was playing around or what... but it wouldn't install because it said:

Unpacking libmysqlclient-dev (from .../libmysqlclient-dev_5.5.24-0ubuntu0.12.04.1_i386.deb) ... dpkg: error processing /var/cache/apt/archives/libmysqlclient-dev_5.5.24-0ubuntu0.12.04.1_i386.deb (--unpack): trying to overwrite '/usr/bin/mysql_config', which is also in package mysql-devel 5.5.28-2

I got rid of this problem by removing the mysql-devel package:
dpkg -r mysql-devel

I also learnt a cool command to see all mysql related installs:
dpkg --get-selections | grep mysql

I had to say where the mysql socket is for rails to make a connection to mysql. The location I used is /var/run/mysqld/mysqld.sock -which seems to be working fine.

Finally, it comes time to run mysql - which it looks like you can also do in various ways! I've been running it by doing:

service mysql start

There is a blog which probably describes this all in more detail:
http://cicolink.blogspot.co.uk/2011/06/how-to-install-ruby-on-rails-3-with.html





Monday, August 13, 2012

Setting up ssl on heroku

I'm setting up SSL on heroku, and it took a while to work things out, so here's my guide (based on heroku's) on how to setup SSL.

Overview

I'm assuming you're already on heroku and you want https traffic for your own domain name.

You want to add SSL. When you add SSL heroku will give you a new app address e.g. soaringeagle223.herokuapp.com for your https traffic. What you will have to do is point a subdomain to this new app. If you configure your certificate for the naked domain i.e. mycoolapp.com then you will get a warning when you visit https://mycoolapp.com... SO make sure you point a subdomain to the secure app name, or you'll end up buying a new certificate! (more info on why this is a good idea is here).

The other thing to note is that you will have to upload YOUR certificate, and also an INTERMEDIATE authority certificate. Basically there are a few trusted certificate issuing authorities on the internet, and some intermediate issuing authorities. You need to link your certificate to a trusted Root Authority, and you do this via intermediate certificates.

Once you have your heroku app configured and certificate on the server, then you should be able to visit https://mysecuresubdomain.mycoolapp.com - to enforce that everyone ends up on https and not http however, you will need to redirect http traffic to https from within your app code!

Let's get started

Get Certified

  1. Go to www.namecheap.com, and buy an ssl cetificate - I choose RapidSSL single domain (e.g. mysubdomain.myapp.com), as I'm cheap and it works for me ;)
  2. Now in namecheap, click on "My Account" -> "Your Domains/Products" -> "SSL Certificates"
  3. You should have a certificate with a status of "Activate Now" - CLICK to activate and you should be taken to a page where you can enter your 'CSR' key
  4. Now you have to generate a 'Certificate Signing Request' (CSR). There are some instructions on how to do this on heroku. Choose Apache 2 as the server type.

    Assuming you have openssl installed, here's the basics on a linux box (more details at heroku):
    1. openssl genrsa -des3 -out server.orig.key 2048
      openssl rsa -in server.orig.key -out server.key
      
      openssl req -new -key server.key -out server.csr
      
  5. Copy and paste your CSR code into namecheap, leaving out the BEGIN and END bits.
  6. Fill in the rest of the details with namecheap - they will send a verfication email to your admin email address.
  7. Finally, copy the certificates that you receive in your email into a text file (one after the other with the begin/end certificate bits), and save it as 'server.crt'. Ensure you have a new line at the end of the file.

Configure Heroku

  1. heroku addons:add ssl:endpoint --app myapp (this costs money)
  2. heroku certs:add server.crt server.key --app myapp (server.crt, and server.key were created in the previous steps) Heroku will let you know if this was successful or not
  3. heroku certs --app myappname (This will show you your certificate and ssl endpoint info)

Configure DNS

The next step is to point your subdomain to your heroku ssl endpoint.
  1. heroku certs --app myappname (This will show you your certificate and ssl endpoint info)
  2. Now create a CNAME record that points from your subdomain to the ssl endpoint (i.e. set it up via GoDaddy or whoever you bought your domain name from) 

Finally

If you browse to https://mysubdomain.mydomainname.com then you should go there, and see https:// in the address bar. If you get a warning message, then something isn't setup quite right...
You will still be able to access the http version too though... If you want to force users to use https rather than http, then you'll need something in your code to do this redirection.

Here's an example for sinatra based apps:
require 'sinatra'

before '*' do
  if(!request.ssl? && request.host != 'localhost')
    request_url = request.env['REQUEST_URI']
    request_url['http'] ='https' 
    redirect request_url
  end
end

If you found this useful let me know :) also, please let me know if I missed something or if there's an error etc.

Saturday, May 26, 2012

Errors installing mysql2 on ubuntu

Upon installing the mysql2 gem I got the following error:

ERROR: Failed to build gem native extension.
I fixed it with:
sudo apt-get install libmysql-ruby libmysqlclient-dev

Thanks to this post here

Sunday, May 20, 2012

Ruby - dynamically create an object :)

Thought this was really cool :)

def create_object(class_name, *args)
  the_class = Object.const_get(class_name)
  the_object = the_class.send :new, *args if !args.empty?
  the_object = the_class.send :new if args.empty?
  
  return the_object
end

Friday, May 18, 2012

Running ruby 1.9.3 on Heroku

I had a few problems trying to get ruby 1.9.3 running on Heroku.

Here's the error I had:
 Heroku receiving push
-----> Removing .DS_Store files
-----> Ruby/Sinatra app detected
-----> Gemfile detected, running Bundler version 1.0.7
       Unresolved dependencies detected; Installing...
       Using --without development:test
       /tmp/build_jpq0amd2bp92/Gemfile:5:in `evaluate': undefined method `ruby' for # (NoMethodError)


Here's what I did to fix it:

     Add the following to Gemfile: 
        ruby '1.9.3'
        gem 'bundler', '1.2.0.pre'

git remote rm heroku
heroku create --stack cedar
gem install bundler --pre
bundle install
git commit -am "fixed heroku problem"
git push heroku master

And some notes to myself for other stuff I need:
heroku addons:add mongohq:free
heroku config:add TWITTER_KEY=`echo $TWITTER_KEY`
heroku config:add TWITTER_SECRET_KEY=`echo $TWITTER_SECRET_KEY`


Tuesday, May 8, 2012

How to make Ethernet work on ubuntu!

So my computer has an Ethernet card which doesn't work out of the box with Ubuntu 11 or 12.04 - which means that I couldn't connect to the internet!


When I ran:


lspci | grep Ethernet 


It showed my card as having a RTL8111 Ethernet controller.


Long story short, this post helped me get the card going properly (for the second time now...)

Saturday, March 10, 2012

Setting up postgres on ubuntu, and getting it going on heroku

links: https://help.ubuntu.com/community/PostgreSQL,

sudo apt-get install postgresql
sudo apt-get install postgresql-server-dev-all

#Create a user account for postgresql, and a database with the name of your user account
sudo -u postgres createuser --superuser myUbuntuUsername
sudo -u postgres psql
postgres=# \password myUbuntuUsername
\q
createdb myUbuntuUsername

#Check that your postgres useraccount was setup properly
psql
\du 

Use require
'dm-core'
instead of
require 'datamapper'
http://devcenter.heroku.com/articles/database

Keep test and production gems separate:
http://yehudakatz.com/2010/05/09/the-how-and-why-of-bundler-groups/