We have a few production servers at work, and we have a central bitbucket repository to store our core code. Once we make a change on our testing server (!), we used to have to commit, push changes to bitbucket, and then ssh into each server, then pull changes, and update each repository. A pain in the backside! Enter Capistrano. A ruby ssh automation tool. In a few simple steps you can create a recipe file that will let you do this all with one command.
Setting up SSH host shortnames
Here’s an example setup to create SSH host shortnames. On you local computer, add the following to ~/.ssh/config:
Host server1
HostName server1.internet.com
User david
Host server2
HostName server2.internet.com
User david
Host *
User davidwinter
Now with this file saved, you can ssh into server2.internet.com with just the following command:
ssh server2
This will save you having to type out:
ssh david@server2.internet.com
And if you have public key authentication setup, it makes the process even smoother. Adding the wildcard host record at the bottom allows you to specify a default username to use for other servers to the ones you’ve not specified above.
SSH and public key authentication
Fed up with having to type your password in each time you log into a server over SSH? Me too. Down with passwords, and in with public key authentication!
Avoid giving 777 permissions
Just a quick post for me to remember the following workflow of commands:
sudo find . -type d -exec chmod 755 {} \;
sudo find . -type f -exec chmod 644 {} \;
sudo chgrp -R www-data .
sudo chmod -R g+w .
Using o2 PAYG mobile broadband
Our internet connection via our landline has been dead since Tuesday afternoon, so I’ve needed an alternative connection in the meantime to give me my twitter fix!
I’d kept an eye on the o2 pay-as-you-go mobile broadband for a while, because it offered the cheapest, noncommittal setup. A one off £20 for the USB dongle, and then as little as £2 for 500MB for a 24 hour period.
So I plug in the dongle and we get a nice little installer to set everything up; installing drivers and the o2 Mobile Connect application. The annoying thing is that if you install going this route, you can only connect if you have this app open. You can’t just use system preferences. Also, turns out this ‘handy’ installer program then blocks you from getting to the installation files again, and the dongle will refuse to do anything unless Mobile Connect is open (you’ll get a red light on the dongle when it’s being evil).
So, what can you do?
Redirecting Apache traffic to a maintenance page
Here’s a simple solution to redirect users to a maintenance page in Apache. This rewrite rule can stay in your config (<VirtualHost> or .htaccess) all the time; all that you need to enable it is to create the file maintenance.html.
The first rewrite condition checks to see if the file exists, and only if it does, will it redirect all traffic to it. The second rewrite condition is there to prevent an infinite loop, by only redirecting traffic to files other than maintenance.html.
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteCond %{REQUEST_FILENAME} !/maintenance.html
RewriteRule ^.*$ /maintenance.html [L]
Install Mercurial on CentOS 4
We’ve finally moved to Mercurial at work (well, we didn’t exactly move from anywhere, but that’s another story…). Our production server is running CentOS 4, which comes installed with Python 2.3.4. Mercurial requires 2.4. No Python updates available in the yum repository. What to do?