Install Grav CMS on Ubuntu

Install Grav CMS on Ubuntu

In this article, we will explain how to install Grav on an Ubuntu 14.04 VPS with PHP-FPM and Nginx. Grav is a fast, simple, and flexible file-based content management system (CMS) application written in PHP and uses Twig as a template engine. This guide should work on other Linux VPS systems as well but was tested and written for an Ubuntu 14.04 VPS.

1. Login to your VPS via SSH

ssh user@vps_IP

2. Update the system and install necessary packages

[user]$ sudo apt-get update && sudo apt-get -y upgrade
[user]$ sudo apt-get install software-properties-common git nano curl build-essential libyaml-dev

3. Install and configure PHP, composer and required PHP modules

To install the latest stable version of PHP version 7 and all necessary modules, run:

[user]$ sudo add-apt-repository ppa:ondrej/php
[user]$ sudo apt-get update
[user]$ sudo apt-get -y install php7.0-fpm php7.0-cli php7.0-gd php7.0-mbstring php-pear \
            php7.0-curl php7.0-dev php7.0-opcache php7.0-xml

The following PHP extensions are optional but recommended:

APCu is the official replacement for the outdated APC extension. To install it, run the following commands:

[user]$ sudo pecl install apcu
[user]$ sudo sh -c "echo extension=apcu.so > /etc/php/7.0/mods-available/apcu.ini"
[user]$ sudo ln -s /etc/php/7.0/mods-available/apcu.ini /etc/php/7.0/fpm/conf.d/20-apcu.ini
[user]$ sudo ln -s /etc/php/7.0/mods-available/apcu.ini /etc/php/7.0/cli/conf.d/20-apcu.ini
[user]$ sudo service php7.0-fpm restart

The Yaml PHP Extension provides a wrapper to the LibYAML library and can dramatically increase performance. To install it, run the following commands:

[user]$ sudo pecl install yaml-beta
[user]$ sudo sh -c "echo extension=yaml.so > /etc/php/7.0/mods-available/yaml.ini"
[user]$ sudo ln -s /etc/php/7.0/mods-available/yaml.ini /etc/php/7.0/fpm/conf.d/20-yaml.ini
[user]$ sudo ln -s /etc/php/7.0/mods-available/yaml.ini /etc/php/7.0/cli/conf.d/20-yaml.ini
[user]$ sudo service php7.0-fpm restart

Composer is a dependency manager for PHP with which you can install packages. Composer will pull in all the required libraries and dependencies you need for your project.

[user]$ curl -sS https://getcomposer.org/installer | php
[user]$ sudo mv composer.phar /usr/local/bin/composer

Create a new PHP-FPM pool for your user:

[user]$ sudo nano /etc/php/7.0/fpm/pool.d/yourUser.conf
[yourUser]
user = yourUser
group = yourUser
listen = /var/run/php-fpm-yourUser.sock
listen.owner = yourUser
listen.group = yourUser
listen.mode = 0666
pm = ondemand
pm.max_children = 5
pm.process_idle_timeout = 10s
pm.max_requests = 200
chdir = /

Do not forget to change yourUser with your username.

Restart PHP-FPM:

[user]$ sudo service php7.0-fpm restart

4. Install Grav CMS on Ubuntu

Installing Grav is very easy, first create a root directory for your Grav CMS using the following command:

[user]$ mkdir -p ~/myGrav.me

Clone the project repository from GitHub:

[user]$ git clone https://github.com/getgrav/grav.git ~/myGrav.me
[user]$ cd  ~/myGrav.me

Install all dependencies:

[user]$ composer install --no-dev -o

To install the plugin and theme dependencies, run the following:

[user]$ bin/grav install

5. Install and configure Nginx

Ubuntu 14.04 comes with Nginx version 1.4, to install the latest stable version of Nginx version 1.8, run:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
[user]$ sudo add-apt-repository -y ppa:nginx/stable
[user]$ sudo apt-get update
[user]$ sudo apt-get -y install nginx

Next, create a new Nginx server block:

[user]$ sudo nano /etc/nginx/sites-available/myGrav.me
server {

    listen      80;
    server_name myGrav.me;
    root /home/yourUser/myGrav.me;
    index index.php;

    access_log  /var/log/nginx/myGrav.me.access.log;
    error_log   /var/log/nginx/myGrav.me.error.log;

    location / {
      try_files $uri $uri/ /index.php$args;
    }

    location ~* /(.git|cache|bin|logs|backups)/.*$ { 
        return 403; 
    }

    location ~* /(system|vendor)/.*\.(txt|xml|md|html|yaml|php|pl|py|cgi|twig|sh|bat)$ { 
        return 403; 
    }

    location ~* /user/.*\.(txt|md|yaml|php|pl|py|cgi|twig|sh|bat)$ { 
        return 403; 
    }

    location ~ /(LICENSE|composer.lock|composer.json|nginx.conf|web.config|htaccess.txt|\.htaccess) { 
        return 403; 
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php-fpm-yourUser.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }
}

Do not forget to change yourUser with your username.

Activate the server block by creating a symbolic link :

[user]$ sudo ln -s /etc/nginx/sites-available/myGrav.me /etc/nginx/sites-enabled/myGrav.me

Test the Nginx configuration and restart nginx:

[user]$ sudo nginx -t
[user]$ sudo service nginx restart

That’s it. You have successfully installed Grav on your Ubuntu 14.04 VPS. For more information about how to manage your Grav installation, please refer to the official Grav documentation.


Of course you don’t have to Install Grav CMS on Ubuntu, if you use one of our Managed CMS Hosting services, in which case you can simply ask our expert Linux admins to setup this for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post, on how to Install Grav CMS on Ubuntu,  please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Leave a Comment