You are here

unix

Using .htaccess to fix the Facebook like IE fb_xd_fragment bug

If you notice any hits to URLs on your website with a querystring variable that look like:

http://mywebsite.com/somepage?fb_xd_fragment

Then chances are you have the Facebook like button installed and you are experiencing the notorious fb_xd_fragment bug caused by Internet Explorer. There are lots of solutions but if you use a *nix host and have access to your .htaccess file, the easiest thing to do is strip querystring variables matching fb_xd fragment.

Add the following lines and it should fix the problem. (Make sure you replace example.com with your actual URL.):


# Fix facebook bug
RewriteCond %{QUERY_STRING} ^fb_xd_fragment
RewriteRule ^(.*)$ http://example.com/$1? [R=301,L]

Make sure you test on your website before just assuming this works!

Run persistent jobs over SSH using screen

I have a bunch of scripts that I run periodically from the command line by SSHing into a server and running the command. The problem is that sometimes these scripts can take a while and I might lose connectivity, want to reboot my machine, whatever. Yes, you can run these in the background or use nohup, but I want to run these interactively in the foreground and watch the output. Enter screen.

Screen is a windows manager for your command line sessions and is extremely powerful. I am not going to get into all that it can do but am going to simply focus on how easy it is to solve this particular task.

To start, you will need screen installed on the remote unix machine. On Ubunutu 10.04, this should already be installed. Then you simply run screen, run your job and then you can detach and re-attach to your screen session as desired:


ssh remote_machine
apt-get install screen
screen
run your_script

To detach from screen session:
screen -d

To re-attach screen session:
screen -r

If you have multiple screen sessions, you will have to specify which one when using the -r option, but this is pretty straight forward.

Now you can run your scripts without having them terminated (unless they actually blowup!)

Managing linux EC2 instances (or any remote linux machines)

Its been a while since I posted anything so I thought I would talk about how I've setup my servers using Amazon's EC2 cloud services for easy administration. I have several instances running mostly Ubuntu 10.04.

Some of this stuff may be pretty simple to you depending on your level of expertise but it really simplified administration for me so I wanted to share.

I put all my keys in a folder in my home directory and then I have a file named config in my ~/.ssh folder that looks something like this:


Host server_alias
User ubuntu
Hostname public_address_to_server
IdentityFile ~/.keys/key_file.pem
Localforward 6033 localhost:3306

I have an entry like what is shown above for each server. Once you have a host setup in your config file, you can ssh to your server with one very simple command:

ssh server_alias

Wow, that was easy. There's also a bunch of other cool things you can do once you have this setup.

You'll notice I have a local forward setup on port 6033. This allows me to connect to my remote MySQL server (running on its default port of 3306) once I have established an SSH connection using the port 6033. I can use MySQL GUI tools or simply connect with the MySQL command line utility like so:

mysql -h 127.0.0.1 -P 6033 -p

I am not sure why but for whatever reason, it doesn't work if I enter localhost but if I use 127.0.0.1 it does. Weird.

File sharing over ssh is also super simple. Simply use the sshfs command line tool:

sshfs server_alias:/home/whatever_dir ~/server_alias

Booyah, now I can manipulate folders and files on my remote server locally. If you want to kill the share, simply use:

fusermount -uz server_alias

Props to @netwonk for helping me get a lot of this stuff going. Happy server administrating!