Head in the clouds: Amazon s3 and CloudFront with Joomla! and PHP

I’m managing a LAMP server on a virtual dedicated setup and have recently run into some performance issues. During periods of higher user visits the CPU usage shoots up This could be partly because of the platform running on it, but I’m not sure that the PHP scripting is the root cause.

To alleviate some of my concerns, I’ve moved most of the static images, css, and javascript over to Amazon s3 and set up a CDN for that data with CloudFront. It’s fast. Really fast compared to what we’re used to. It has drastically helped load times and from what I can tell it is improving the performance of the site altogether by reducing the load on the server.

Caching is out in full force for the main joomla site while the content being served from amazon s3 is gzipped, minified, and speedy. I’m still working through YSlow to get things even more streamlined, but I’m more satisfied now than before.

I was using a minify component to grab scripts on-the-fly and combine them but that’s just a nightmare for performance so I’m going to revamp and get the remainder of those files out to the CDN and compressed.

For those who are interested, I’m serving gzipped js and css but using some varied methods also depending on if the connection is secure or not. Since S3 does not function over https using cnames, I had to set up a check to see which URL to use for my CDN files.

Something like this did the trick for my Joomla! template (placed in the template’s index.php):

//check to see if the browser supports gzip content
$gz = strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false ? 'gz' : '';
if ($_SERVER['HTTPS'] != "on") {
//insert your code to execute over http here
$document->addStyleSheet('https://yourbucketname.s3.amazonaws.com/templates/css/style.'.$gz.'css');
}
else{
//enter your https code here
$document->addStyleSheet('http://cdn.yoursite.com/templates/css/style.'.$gz.'css');
}

I’ve got this loading the CNAME urls such as cdn.yourdomain.com/css/style.gzcss for the non-secure connection but using the secure URLS that amazon provides for the secure connection.

And no, style.gzcss is not a typo. There were issues with how the browser was grabbing files with the regular style.gz.css extension, even when I had set the content-encoding to gzip and the content-type to text/css. This has worked for me, but there may be a more elegant solution floating around. Cloudberry explorer has been a great asset in doing this quickly and efficiently.

Integrating VirtueMart with Constant Contact

This week I’ve been tackling a project that has been in the back of my mind for a few months now. I was able to leverage the Constant Contact API to integrate Constant Contact user records with VirtueMart/Joomla user profiles. Now when a user is viewed from within the VirtueMart user manager you will also be able to see/edit their related constant contact record and list subscriptions.

New contacts can be created at the same time as Joomla users and it will auto fill information into the constant contact record based on the VirtueMart information. If an existing user profile that does not have a constant contact record is viewed, then when the profile is saved a new constant contact record will be created for that user. I’ll eventually be making more use of the API to allow users to select from various newsletters when registering so that they will be able to have more targeted communications.

The only issue I ran into was when I uploaded the changes to the live web server with PHP 5.2.11. It seems that on my local server with PHP 5.2.9 everything worked well but the comments in the constant contact class file were causing parse errors. I was able to get around the errors by deleting some of the comments.

Ordering VirtueMart Child Items

Edit: This is easily my most popular post and solicits a lot of questions. This method sorts children in a table on the parent product page. It DOES NOT sort children in a drop down list. 1/10/2011

UPDATE: New methods here: I am updating with another post that will try to address how to universally sort attribute/child drop down lists.

I’ve been pulling my hair out the past couple of hours trying to find a built-in way to order child items in VirtueMart. There are plenty of ways to sort built-in to the category view of multiple products, but there is no built in function to sort child products (items). The guidance on the VirtueMart forums was lacking and would produce errors when including external child ids to be included, so I had to do some digging.

Around line 488 in ps_product_attribute I was able to add the following code after the if($child_ids){} clause:

elseif(!$child_ids){
$q .=” ORDER BY product_id”;
}

Now my child items are sorted by ID. You could easily product_id to be any other field, but for my purposes this worked great.