nirokato
Published

Sat 25 April 2015

←Home

A Raspberry Pi powered by Grav

Ghost came to mind when I decided I wanted to put a blog on my Pi, but 256MB of RAM just wasn't enough. So I did some research online (read:faffing about on Google) and eventually stumbled upon some key phraseology "flat file cms" which led me to Grav. Grav is a new, file-based web platform that's incredibly flexible and fast. Since Grav is file-based there is no installation required and no database needed. I'm extremely pleased with how light and flexible it truly is.

  • You can learn all about Grav by checking out their dedicated Learn Grav site.
  • Grav has plugins, themes, as well as other Grav skeleton packages you can download at the Grav Downloads page.

I'm not going to go through the installation and configuration of the OS for the Raspberry Pi. There are so many tutorials already out there, and most are better written than I could ever hope to achieve.

Here are a few of my favorites: RaspberryPi.org/Documentation/installation HowToForge.com - Installing Raspbian on the Raspberry Pi * eLinux.org - RPi Easy SD Card Setup

Grav Requirements

By now you should have a fully functional installation of Raspbian on your Raspberry Pi. Make sure it's updated and upgraded to the latest packages. To update run:

sudo apt-get update && sudo apt-get upgrade

Grav has some basic requirements. You'll need: + A modern webserver like Apache, Nginx, LiteSpeed, IIS, etc. - I chose Nginx for its light weight and thrifty usage of RAM, I only have 256 MB to work with. + PHP version 5.4 or higher with the following additions - PHP5-gd - PHP5-fpm - PHP5-curl - PHP5-cli + git-core for pulling down the latest Grav + That's it!

Install Nginx

Install the latest package available for Nginx. If you haven't used it before I'd highly recommend reading the Raspberry Pi foundation's documentation Setting up Nginx web server on a Raspberry Pi.

sudo apt-get install nginx

Create an nginx group to allow other users to make Nginx config files and be able to edit them.

sudo groupadd nginx 
sudo chown -R root:nginx /etc/nginx
sudo chmod -R g+wr /etc/nginx

Update your user (the default is pi for Raspbian) to have www-data and nginx group membership. We'll be making use of this later.

sudo usermod -a -G www-data,nginx pi

Note: You'll need to close your current terminal session and start a new one to apply your new group membership. You can verify which memberships you have by issuing the groups command.

By default Nginx is configured with the default web root of /usr/share/nginx/www, but I prefer to have my web content in /var/www. So I did the following:

sudo mkdir /var/www
sudo chown www-data:www-data /var/www
sudo chmod +wrx /var/www
sudo chmod g+wr /var/www

We'll make the configuration changes later that will allow us to create a reverse proxy with static content caching as well as a FastCGI cache for PHP. These caches will do the brunt of the work handling requests so php5-fpm doesn't get hammered and cause the system to screech to a halt.

Install PHP

Next we'll need to install PHP and a few extensions needed by Grav and its plugins.

sudo apt-get install php5 php5-gd php5-fpm php5-curl php5-cli

The installation is very straightforward, there isn't anything else you need to configure.

Note: Securing PHP is outside the scope of this post.

Install GIT

If you haven't heard of Git I would recommend going through the Pro Git Book. We want to keep rolling so let's install it!

sudo apt-get install git-core

Clone Grav

Once you have Git you can change directory to /var/www, invoke pi:www-data as your primary group, and clone Grav.

Newgrp allows you to start a new shell instance in your current terminal with whichever group you call. We're going to call www-data as this group has write and read access to /var/www. To end the www-data shell instance, just run exit and you'll be placed back in your original shell with pi:pi membership.

newgrp www-data
cd /var/www

Now we have group write permission to the directory AND the proper group membership will be applied to any files created while newgrp is set to www-data. Now we clone Grav.

git clone https://github.com/getgrav/grav.git

This will pull down Grav into its own folder. We'll need to install the components and 3rd party plugins that Grav relies upon.

cd /var/www/grav
bin/grav install

If you've already installed all of the requirements in Grav Requirements you should have no trouble with the install. If you do, please revisit the requirements section.

Once the install has completed, there's an additional customization that implemented for serving static files from an uploads folder.

mkdir /var/www/grav/uploads

That's it! Now we can move on to updating Nginx's configuration.

Configuring Nginx

Nginx is one of the most flexible and easy to implement web servers I've ever had the pleasure to use. Let's get it configured.

  • Stop the nginx service

    sudo service nginx stop
    
  • Revoke www-data and invoke nginx as your primary group

    • Check your current primary group

      groups

    • It should look similar to this:

      www-data pi adm dialout cdrom sudo audio nginx video plugdev games users netdev input spi gpio

    • Revoke www-data by exiting the shell.

      exit

    • Invoke nginx by running:

      newgrp nginx

  • Remove the symbolic link in sites-enabled

    Tip: _ The default configuration resides in /etc/nginx/sites-available so you can link to it again if you need to._

    rm -f /etc/nginx/sites-enabled/default
    
  • We'll need to modify a line in /etc/nginx/nginx.conf, look for server_names_hash_bucket_size and change the value to 64.

    nano /etc/nginx/nginx.conf
    
    • Press CTRL+W
    • Type: server_names_hash and press ENTER
    • Edit the line so it reads: server_names_hash_bucket_size 64;
    • Press CTRL+X to save and exit.

Note: nginx should still be set as your primary group in order to save grav.conf and local_proxy.conf with pi:nginx ownership.

  • Copy the contents of local_proxy.conf into /etc/nginx/sites-available/local_proxy.conf and save.

    nano /etc/nginx/sites-available/local_proxy.conf
    
    • Past in the text from the local_proxy.conf file you downloaded.
    • Press CTRL+X to save and exit.
  • Copy the contents of grav.conf into /etc/nginx/sites-available/grav.conf and save.

    nano /etc/nginx/sites-available/grav.conf
    
    • Past in the text from the grav.conf file you downloaded.
    • Press CTRL+X to save and exit.
  • Create the symbolic link /etc/nginx/sites-enabled/local_proxy.conf and point it at /etc/nginx/sites-available/local_proxy.conf

    ln -s /etc/nginx/sites-available/local_proxy.conf /etc/nginx/sites-enabled/local_proxy.conf
    
  • Create the symbolic link /etc/nginx/sites-enabled/grav.conf and point it at /etc/nginx/sites-available/grav.conf

    ln -s /etc/nginx/sites-available/grav.conf /etc/nginx/sites-enabled/grav.conf
    
  • We'll need to ensure that php5-fpm is started.

    sudo service php5-fpm start
    
  • Now we test the configurations for Nginx

    sudo nginx -t
    
  • If the above command comes back with the following, you're good to go:

    nginx the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx configuration file /etc/nginx/nginx.conf test is successful
    
  • Now we can start Nginx

    sudo service nginx start
    

Browse to http://localhost or if you edited the server_name field in your local_proxy.conf point your browser to http://yourservername

That's it, an extremely resource efficient setup!

Tip: If you make any changes and they don't show up, try clearing the proxy and FastCGI caches using: sudo rm -rf /tmp/*_cache and the Grav cache via https://learn.getgrav.org/cli-console/grav-cli#clearing-grav-cache

Conclusion

I am honestly impressed with the capabilities of Grav. I didn't think that I would ever get such a responsive website out of such a small software package. Truly remarkable!

Upcoming Article

How to import Grav Skeletons to jump-start your website!

Andy Molenda Posted by Andy Molenda

Go Top