Automatically creating new virtual hosts with Nginx (Bash script)

NOTE: This script has been superseded by this one: Bash Script to Create new virtual hosts on Nginx each under a different user

Setting up virtual hosts on any sort of web server normally takes at least a few minutes and several commands (if you’re not running a control panel of some variety, and even then it normally takes a good number of clicks and checking boxes to get what you want). All of this can be quite annoying when you have to set up several of these a day.

So I put together a simple bash script to quickly provision the hosting for a new static site running on Nginx. The script was originally build for use on the Amazon AMI running on AWS. The script uses the sudo command as the password is not required for the default user on the AWS AMI, however if you are running as a non root user with access to the sudo command then you should be prompted for your password when running the script.

What does the script do:

  • Creates a new vhosts entry for nginx using a basic template
  • Creates a new directory for the new vhost and sets nginx as the owner
  • Adds a simple index.html file to the new directory to show the site is working.
  • Reloads Nginx to allow the new vhost to be picked up

Scheduling Individual Backups of all MySQL Databases on Linux

Expanding on from an older post covering a simple usage of the the mysqldump program I finally found some time to write a bash script to control this process and allow all MySQL databases to be backed up separately with one command.

This script uses the MySQL client to pull in a list of all the databases on the server and then loop round and back each one up into its own compressed file (using the gzip program). Once a backup file for all the databases has been created the script then bundles them all into a single tar archive, removing the original individual files. This then allows you to easily keep track of all your backups, by just having one file containing all the data you need to restore all DBs (or just one) to a point in time. A script such as my one could be used to limit the number of backups stored at any one time (to save on disk space).

Continue reading

Bash script to reduce file count to a given number removing the oldest files first FIFO

I needed a simple automated way to remove any number of old backup tar files from a directory leaving only the newest 30 files. As the only thing in this directory was compressed backup files, the script didn’t need to worry about file names or file types, it could just delete the oldest files in the directory (as all the files were effectively the same but just for a different period in time).

So I wrote this bash script (remove_old_files.sh) to handle it for me, it takes two arguments. The first is the number of files to leave (in my case 30), and the second the absolute file path to the directory you want to keep in check.

Continue reading

zRSSFeed unable to load feeds with GET Parameters

zRSSFeed is a nice little RSS Reader for jQuery which uses Google’s Feeds API to parse the RSS Feed.

However if your RSS URL contains required GET variables you’ll find zRSSFeed will just display: “Feed could not be loaded.” – Or similar (if you’ve changed the default error message). There is however a really easy fix, although you will probably want to make the change in the non-minified version of the source and then re-minify. Change line 46 from:

var api = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=" + url;

To:

var api = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=" + encodeURIComponent(url);

This encodes the URL to allow it to be embedded as a GET parameter. (See MDN for more info…)

Edit:

Another thing I noticed with zRSSFeed is it won’t load the feed from Google over SSL if your site is using https. However this is easily fixed just modify the same line as above, from:

var api = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=" + encodeURIComponent(url);

To:

var api = location.protocol + "//ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=" + encodeURIComponent(url);

Using a non-integer primary key with Symfony’s Admin Generator

When Symfony creates an Admin module automatically its default routing setup only allows it to work with an integer based primary key. This means you can’t use a unique varchar column as your primary key, for example. There is however an easy way to solve this and tell Symfony to match other characters in its routing module. All you need to do is update your routing.yml file adding the requirements parameter like so:

user_languages:
 class: sfPropelRouteCollection    
 options:
   model:                user_languages
   module:               user_languages
   prefix_path:          /languages
   column:               iso
   with_wildcard_routes: true
 requirements:  { id: "[a-zA-Z]+" }

The above pattern will match any number of characters, and won’t worry about the case. However you could make this pattern match anything you wanted, the string must be a valid regular expression pattern the same as you would use for preg_match and similar.

Update to sdInteractiveChartPlugin for Symfony PHP

sdInteractiveChartPlugin has been updated to version to 0.3 to fix two little issues:

  • Symfonys sfOutputEscaperArrayDecorator objects can now be passed directly to the charting plugin and it will correctly format the values from the object.
  • Added support for automatically loading the Javascript files over https if the page request is over SSL.

More info and the download for the plugin can be found here:

http://www.symfony-project.org/plugins/sdInteractiveChartPlugin/0_3_0

Upgrading to Ubuntu 10.10 – Not booting, freezing on loading screen (Nvidia Driver issue) – FIX

After restarting my newly upgraded Ubuntu machine (from 10.04 to 10.10) was greeted with my computer freezing on the loading screen (whilst booting).

After rebooting and getting access to the console I discovered it appeared to be an issue with the Nvidia drivers, which were causing the X server to not start:

Failed to load /usr/lib/xorg/extra-modules/nvidia_drv.so
[    23.651] (II) UnloadModule: "nvidia"
[    23.651] (EE) Failed to load module "nvidia" (loader failed, 7)
[    23.651] (EE) No drivers available.
Failed to load /usr/lib/xorg/extra-modules/nvidia_drv.so[    23.651] (II) UnloadModule: "nvidia"[    23.651] (EE) Failed to load module "nvidia" (loader failed, 7)[    23.651] (EE) No drivers available.

So after trying a number of different ways I finally found out how to fix this issue:

run the nvidia-xconfig program:

cd /usr/lib/nvidia-96/bin/
sudo ./nvidia-xconfig

Modify the xorg.conf file, and replace the driver name nvidia with nv.

sudo vi /etc/X11/xorg.conf

Replace:

Driver         "nvidia"

With:

Driver         "nv"
Then just try and start the Xserver again:
startx

Hopefully this will help  others fix this quicker than I managed.

A Symfony Plugin for Generating Charts using Googles Visualization API (sdInteractiveChartPlugin)

Last week I released the first (beta) version of my interactive charting plugin for the Symfony PHP framework. It is currently only version 0.1.2 so it’s a bit rusty round the edges, but over the next couple of weeks I will be improving it and adding extra functionality to it. I also intend to package up another version (which I will host on my blog) for anyone not using Symfony to use in there PHP projects. Below are just a few details on the plugin:

UPDATE: The plugin has been updated to version 0.2.0 (Available on the Symfony website), more of the APIs options have been implemented along with an improved callback function and more streamlined code.

Overview:

The plugin allows you to insert Javascript powered charts into any page of your site without writing any Javascript. It is especially useful when the data for the chart and possibly even the type of chart is based upon an authenticated users preferences (i.e. creating different charts for each user). The plugin handles the loading of the required libraries and creation of the correct chart object as well loading the data in via AJAX if you want. It also helps to keep your HTML page code as neat and tidy as possible by only writing exactly what’s needed into the dynamic page and storing all the rest in a general JS file which is the same for all users.

Chart Types:

It can create a number of different charts:

  • Area Charts
  • Bar Charts
  • Column Charts
  • Gauges
  • Line Graphs
  • Pie Charts

Usage:

Head to: http://www.symfony-project.org/plugins/sdInteractiveChartPlugin