The Rasp Pi serves up a tempting web server

Website Efficiency

Now you can build your website just as you would on any other host. You can access the site with FTP, SFTP, and SSH to build and modify files.

Back in the day, with modems as slow as 14.4Kbps, lean text websites delivered a decent viewer experience. Nowadays, users connect under all sorts of conditions, from super high-speed connections with supercomputers to connections with shaky WiFi and older, slower laptops or PCs. Your home server upload speeds could be slow – generally slower than a decent hosted server designed to be built for speed – so you might want to start thinking about efficiency.

If you are thinking along the lines of an open source CMS with various plugins or fancy templating, or running a shopping cart like Magento, your home server might be in over its head. However, do not be discouraged. You can achieve a level of success if you try to be effective using the hardware and connection you have, with the potential to create a 5- to 10-page website that can deliver the same content faster than other commercially hosted environments.

Building for efficiency will be affected by the website files and other variables, like giving Apache a simple tune-up or caching. Enabling mod_expires and creating a custom .htaccess file or using the Varnish Cache web application accelerator [5] can take care of image and file caching.

Meanwhile, if you build your website without slow-loading JavaScript (or use no JavaScript at all), shrink your images to keep file sizes small, "minify" CSS [6], and minimize HTTP requests by using minimal CSS, JavaScript, and images per page, your site will load at a relatively decent speed. You can find many quick-loading CSS templates online [7].

Another option for style and performance is to buy a beautiful, responsive HTML template (about US$ 10), then strip out slideshows and JavaScript files and replace them with lean images.

To enable mod_expires, you use the a2enmod command follow by the module name, which in this case is expires:

# a2enmod expires
# /etc/init.d/apache2 restart

After you enable the module, reboot the server again. An example .htaccess code snippet that can be used as your template is shown in Listing 3. The # (hash) tag indicates a comment, so any text written after it on the same line is ignored.

Listing 3

.htaccess Snippet

# HTACCESS BEGIN
RewriteEngine On
# mod_expires
ExpiresActive On
<IfModule mod_expires.c>
    ExpiresDefault "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType text/html "access plus 600 seconds"
</IfModule>
<Files ~ "\.inc$">
    Order allow,deny
    Deny from all
</Files>
FileETag none
# Redirect 301 / http://example.com/
deny from ipaddressonetoblock
deny from ipaddresstwotoblock
# HTACCESS END

RewriteEngine allows you to rewrite URLs; for now, none are being rewritten. ExpiresActive On allows caching and sets expiry times for file extensions. The IfModule block allows you to make rules if the mod_expires module is enabled. For example, .jpeg file extensions can remain in cache for one month.

Files and FilesMatch (not shown) allow you to make rules for particular file extensions. Generally, Files is good for a single extension, whereas FilesMatch is good for a group of extensions. In the code sample, the Files block prevents access to files with the .inc extension, so if you make a .inc file and try to open it, you will receive an error.

Finally, FileETag none keeps ETags from being sent. The redirect is commented out, but it is a fast and easy way to redirect all files to a new domain. In this case, if you uncommented the line, it would redirect pages from the domain name (i.e., example.org) to example.com. The deny from lines are used to block the specified IP addresses from visiting your site. More details about mod_expires can be found online [8], as can information about MIME types [9].

Testing Performance

A fairly good tool to help you make your website perform better is the YSlow plugin [10] for Firefox or Chrome. With Firefox, you use Firebug and YSlow together.

After you install and enable YSlow, you can run a test that explains what is strong and what needs work on your website. As you fix the weaknesses, your grade goes up. In Figure 4, you can see the YSlow test for the default Apache server page.

Figure 4: Testing a website with YSlow.

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content