Running a Static Site For Free on Heroku

Every now and then we need to run a static site on the cheap. This is easily doable these days, assuming you don’t expect a whole lot of traffic. One easy way to do it is with Heroku, since they give you a very small server instance for free with paid options to upgrade it in the future if you need to. For the purpose of our small static site, the free one will be just fine though.

Step 1: Create a folder for your site. It doesn’t matter what you call it. For purposes of this post, we will call it myapp.

Step 2: Create a Sinatra app to wrap it. Don’t worry, this is really easy to do even if you don’t know Ruby. We just need to create two files, and you probably won’t even have to modify them.

The first file will be called config.ru. Create this file in your myapp folder and put this inside it:

require 'app'
run Sinatra::Application

The second file will be called app.rb. Create this file in your myapp folder with this content:

require 'rubygems'
require 'sinatra'

get '/' do
redirect '/index.html'
end

Step 3: Copy all your static content in. Create a folder under myapp called ‘public’. Inside here, you can just copy in your whole static site. Just make sure that the root path goes to a file called index.html. If you used a different name, then change the redirect line in the app.rb to reference your root file.

Step 4: Create an account with Heroku. Now that the application is all set up, we need to create an account with Heroku and push the app out there. Go to http://www.heroku.com and sign up for an account right now. Once you click the confirmation link in the email, they will provide you with instructions to get you through the rest. You can either follow their instructions or mine. Either way they are largely the same. I’ll wait here while you get your account ready...

...OK, great. Now that you have an account you can create your application. Open up Terminal and cd to your myapp folder. From there, run the following commands:

Create your local git repo:
git init

Add all your stuff to it:
git add .
git commit -m ‘Initial commit’

Install the heroku gem so you can create your server:
gem install heroku

Create your application on Heroku:
heroku create

Push your application to the server:
git push heroku master

View your site:
heroku open

Now you should be looking at your static site in all its glory. You can go to heroku.com and edit the settings for your application if you want to add any premium features or put a custom domain on it. You can also change the subdomain of the app so you can more easily remember it.

Resources for this post:

http://www.sinatrarb.com/
http://www.heroku.com/