Run a website on the Raspberry Pi using Middleman
Once you have set-up a web server like Apache or nginx running on the Raspberry Pi it is time to create a website. From here there a several options: A CMS that relies on a database, some purely manual crafted pages or a static pages generated by a script. I chose the latter for some reasons.
Static sites have a lot of advantages:
- no database to slow requests down
- offer greater security, as they do not contain dynamic content, so are immune to the most common attacks
- flat, text files, makes them ideal to be used with version control systems, such as Git
- low footprint on the server as serving raw html files
But there also some limitations:
- relying on third party services for dynamic features (e.g. search or comments)
- requires rebuilding the site for frequently changing content
Most static site generators cater towards bloggers. I was looking for a generator that creates a classic website just so I would not have to build everything manually. Luckily I discovered Middleman.
Once you have Ruby and RubyGems up and running, execute the following from the command line:
gem install middleman
This will install Middleman, its dependencies and the command-line tools for using Middleman.
The installation process will add one new command to your environment, with 3 useful features:
- middleman init
- middleman server
- middleman build
Creating the project is just a simple command:
middleman init my_new_project
This creates the skeleton of your page..
Using the command-line, from the project folder, run middleman build:
This will create a static file for each file located in your source folder. Template files will be compiled, static files will be copied and any enabled build-time features (such as compression) will be executed. Middleman will automatically clean out files from the build directory for you that are left over from earlier builds but would no longer be produced.
To deploy the resulting website you use rsync to move the files to your Raspberry Pi:
$ rsync build/ raspberrypi:/var/www/html/
You can now check the website in your browser.
The code for my Raspberry Pi website is on Github: https://github.com/Tafkas/my-raspberry-pi-site