Static site generators combine the best of both worlds: a local server lets you use templates to easily manage the design and content on your site but ultimately all you deploy is simple static content files (HTML, CSS, Javascript) which can be hosted for pennies a month on Amazon S3 or the Content Delivery Network (CDN) of your choice.

With a static site generator, there’s no need to run a production server and worry about its associated cost, security, and maintenance. Caching is much easier and overall performance goes up. A true win-win.

Basically unless your website requires a database, use a static site generator.

Why Cactus

Ok, so which one to use? Over at Full Stack Python there is a comprehensive list of Python-powered options.

I prefer Cactus which uses Django’s templating system and makes deployment to S3 super simple. Here is a quick guide to get you started.

Cactus installation

I assume you are using virtualenvwrapper since we always want to use a virtual environment in our Python projects.

To get started we will setup a new virtual environment called “mysite,” install Cactus, create a new Cactus site called “mysite,” change directories into it, and then run the local server so we can view the results in our browser. Ready?

In Terminal type the following:

mkvirtualenv mysite
pip install cactus
cactus create mysite
cd mysite
cactus serve

Now visit http://127.0.0.1:8000/ to see the standard Cactus welcome page.

Deploy Cactus

Deploying with S3 is even easier. If you don’t already have an Amazon S3 account, go create one now. Make sure to note your Amazon Access Key ID which we will use shortly.

Next in Terminal type the following:

cactus deploy

You will be asked for the bucket name for your site. In general it is a good idea for the bucket name to match your site name, however please note that S3 bucket names must be unique so you will not be able to use mysite.com if someone else has used it before. Therefore create something distinct to use instead of “mysite” for your S3 bucket.

Enter the bucket name (e.g.: www.example.com) > mysite.com

Next it will ask for your Amazon Access Key Id. The “(will not be echoed)” part means that what you type will not appear on the screen for security reasons (think, someone looking over your shoulder). So type your Access Key ID and hit Return. It will work.

Enter your Amazon Access Key ID (will not be echoed)

Ok the site is now successfully deployed to S3. Log into your S3 account and you will see a new bucket named “mysite.com” or whatever your bucket name is.

Website permissions

To make our website code visible we have to enable Static Website Hosting within S3. Fortunately this is simple.

Click on Permissions next to your “mysite.com” bucket and then click on “Static Website Hosting.” Now click on “Enable website hosting” (the default is “Do not enable website hosting”). We need to specify an Index Document, our homepage, and an Error document. Enter “index.html” for the Index Document and “error.html” for the Error Document. Remember to click the blue Save button below.

Now if you click on the link for your S3 bucket’s Endpoint, which you can see within Static Website Hosting, your code is live!

Extra goodies: clean URLs

With Cactus we can enable clean URLs with only one line of code. A clean URL replaces mysite.com/about.html with mysite.com/about/. The latter just looks better, is easier to remember, and is how it would be displayed in a database-driven app, like Django. So let’s set this up.

Open your Cactus site code with the text editor of your choice and open the “config.json” file. By default this look like the following with different actual values:

{
"aws-access-key": "XXXX",
"aws-bucket-name": "mysite.com",
"aws-bucket-website": "mysite.com.s3-website-us-east-1.amazonaws.com"
}

We need to add a setting for “‘prettify’: true” here. Add it after “aws-bucket-website” and remember to add the comma at the end.

{
"aws-access-key": "XXXX",
"aws-bucket-name": "mysite.com",
"aws-bucket-website": "mysite.com.s3-website-us-east-1.amazonaws.com",
"prettify": true
}

Now to generate the new files type “cactus serve” in Terminal, head to our local homepage at http://127.0.0.1:8000/ and click on the “About” link in the header which will redirect to http://127.0.0.1:8000/about/ instead of “/about.html”.

Finally deploy the site:

cactus deploy

And confirm the code changes happened on the live site.

Looking for a way to go from beginner to intermediate Django developer? Check out my book Django for Beginners.