How to Install Grav CMS on CentOS 7

Updated on April 5, 2019
How to Install Grav CMS on CentOS 7 header image

Grav is an open source flat-file CMS written in PHP. Grav source code is publicly hosted on GitHub. This guide will show you how to install Grav CMS on a fresh CentOS 7 Vultr instance.

Requirements

  • Web Server (Apache, Nginx, LiteSpeed, Lightly or IIS). In this guide we will use Nginx.
  • PHP 5.5.9 or higher

Before you begin

Check the CentOS version.

cat /etc/centos-release
# CentOS Linux release 7.5.1804 (Core)

Create a new non-root user account with sudo access and switch to it.

useradd -c "John Doe" johndoe && passwd johndoe
usermod -aG wheel johndoe
su - johndoe

NOTE: Replace johndoe with your username.

Set up the timezone.

timedatectl list-timezones
sudo timedatectl set-timezone 'Region/City'

Ensure that your system is up to date.

sudo yum update -y

Install necessary packages.

sudo yum install -y wget curl vim unzip

For simplicity, disable SELinux and Firewall.

sudo setenforce 0 ; sudo systemctl stop firewalld ; sudo systemctl disable firewalld

Install PHP and required PHP extensions

Setup the Webtatic YUM repo.

sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

Install PHP and PHP extensions.

sudo yum install -y php72w php72w-cli php72w-fpm php72w-common php72w-curl php72w-gd php72w-json php72w-mbstring php72w-xml php72w-zip php72w-opcache php72w-pecl-apcu

Check the version.

php --version

Start and enable PHP-FPM service.

sudo systemctl start php-fpm.service
sudo systemctl enable php-fpm.service

Install and configure Nginx

Install Nginx.

sudo yum install -y nginx

Check the version.

nginx -v

Start and enable Nginx service.

sudo systemctl start nginx.service
sudo systemctl enable nginx.service

Configure Nginx. Run sudo vim /etc/nginx/conf.d/grav.conf and populate the file with the following configuration.

server {
  
  listen 80;

  server_name example.com;
  root /var/www/grav;
  
  index index.html index.php;
  
  location / {
    try_files $uri $uri/ /index.php?$query_string;
  }
  
  location ~* /(\.git|cache|bin|logs|backup|tests)/.*$ { return 403; }
  location ~* /(system|vendor)/.*\.(txt|xml|md|html|yaml|yml|php|pl|py|cgi|twig|sh|bat)$ { return 403; }
  location ~* /user/.*\.(txt|md|yaml|yml|php|pl|py|cgi|twig|sh|bat)$ { return 403; }
  location ~ /(LICENSE\.txt|composer\.lock|composer\.json|nginx\.conf|web\.config|htaccess\.txt|\.htaccess) { return 403; }

  location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
  }

}

Test Nginx configuration.

sudo nginx -t

Reload Nginx.

sudo systemctl reload nginx.service

Install Grav

Create a document root directory.

sudo mkdir -p /var/www/grav

Change ownership of the /var/www/grav directory to johndoe.

sudo chown -R johndoe:johndoe /var/www/grav

Navigate to the document root folder.

cd /var/www/grav

Download and unzip Grav.

wget https://getgrav.org/download/core/grav-admin/1.4.8
unzip 1.4.8
mv grav-admin/* . && mv grav-admin/.* .
rm -rf grav-admin 1.4.8

Change ownership of the /var/www/grav directory to nginx.

sudo chown -R nginx:nginx /var/www/grav

Run sudo vim /etc/php-fpm.d/www.conf and set user and group to nginx. Initially, it will be set to user and group apache.

sudo vim /etc/php-fpm.d/www.conf
# user = nginx
# group = nginx

Restart PHP-FPM service.

sudo systemctl restart php-fpm.service

Create /var/lib/php/session/ directory and change ownership to nginx.

sudo mkdir -p /var/lib/php/session/ && sudo chown -R nginx:nginx /var/lib/php/session/

Open http://example.com in your web browser and follow the on-screen instructions. To access Grav admin append /admin to your URL.