How to Install Grav CMS with Nginx on Ubuntu 16.04
Grav is a modern web application that requires no database. It's a file-based web platform that's simple, fast, and requires zero installation. Grav uses modern technologies including Twig Templating for templating, Markdown for content creation, YAML for simple configuration, Parsedown, and Doctrine cache for performance layer etc. The tool is written in PHP and comes with the powerful Package Management System that allows you to manage the Grav system, including install and upgrade Grav itself, as well as themes and plugins.
In this tutorial, I will show you step by step how to install and configure Grav CMS on Ubuntu 16.04. As Grav is PHP web application-based, we need to install PHP on the system, and to get more performance, we will be using PHP-FPM with Nginx web browser.
Prerequisites
- Ubuntu 16.04
- Root privileges
What we will do:
- Install Nginx
- Install and Configure PHP-FPM
- Install PHP Composer
- Download and Install Grav CMS
- Configure Nginx Virtual Host for Grav
- Testing
Step 1 - Install Nginx Web Server
In this first step, we will install the Nginx web server from the Ubuntu repository. Before we do that, we need update all packages and repository using the following apt commands.
sudo apt update
sudo apt upgrade
Now install the Nginx web server.
sudo apt install nginx -y
After the installation is complete, start the service and enable it to launch at system boot, something which you can do using the following systemctl commands.
systemctl start nginx
systemctl enable nginx
The Nginx web server has been installed on Ubuntu 16.04, and it's running on the default HTTP port 80. Check it using the netstat command in the following way.
netstat -plntu
And you will get the result as shown below.
Step 2 - Install and Configure PHP-FPM
Now we will install and configure PHP-FPM for our Grav installation. Install PHP and PHP-FPM packages (including all required extensions) using the following command.
sudo apt install -y php7.0 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 php7.0-zip
Now we need to configure PHP-FPM.
- Install additional extensions
This is optional, but it's best for Grav performance. We will install some additional extensions manually from the PHP extensions repository using the PECL command.
Install the 'apcu' extension (for increased cache performance) using the 'pecl' command below.
sudo pecl install apcu
Now create a new extension configuration 'apcu.ini' in the '/etc/php/7.0/mods-available/' directory.
sudo vim /etc/php/7.0/mods-available/apcu.ini
Paste the following configuration there.
extension=apcu.so
Save and exit.
Next, activate the extension using the commands below.
sudo ln -s /etc/php/7.0/mods-available/apcu.ini /etc/php/7.0/fpm/conf.d/20-apcu.ini
sudo ln -s /etc/php/7.0/mods-available/apcu.ini /etc/php/7.0/cli/conf.d/20-apcu.ini
Now, we will install the 'YAML' extension to further improve Grav performance. But before installing the extension, we need to install some packages using the following apt command.
sudo apt install libyaml-dev unzip -y
Next, install the yaml extensions using the pecl command.
sudo pecl install yaml-2.0.0
After the installation is complete, create a new 'yaml.ini' configuration file.
sudo vim /etc/php/7.0/mods-available/yaml.ini
Paste the following configuration there.
extension=yaml.so
Save and exit.
Now activate the YAML extension.
sudo ln -s /etc/php/7.0/mods-available/yaml.ini /etc/php/7.0/fpm/conf.d/20-yaml.ini
sudo ln -s /etc/php/7.0/mods-available/yaml.ini /etc/php/7.0/cli/conf.d/20-yaml.ini
New additional PHP extensions for Grav have been added.
- Configure PHP-FPM Pool for Grav
Before creating a new PHP-FPM pool configuration, we need to add a new user named 'grav' to the system, something which you can do using the useradd command.
useradd -m -s /bin/bash grav
passwd grav
Now go to the '/etc/php/7.0/fpm/pool.d' directory and backup the default 'www.conf' configuration.
cd /etc/php/7.0/fpm/pool.d/
mv www.conf www.conf.bak
Next, create new PHP-FPM pool configuration - 'grav.conf' - for Grav.
vim grav.conf
Paste the following configuration there.
[grav]
user = grav
group = grav
listen = /run/php/php7.0-fpm.sock
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /
Save and exit.
Restart the PHP-FPM service and enable it to launch at system boot.
systemctl restart php7.0-fpm
systemctl enable php7.0-fpm
PHP-FPM is now running under the 'php7.0-fpm.sock' file - check it using the netstat command in the following way.
netstat -pl | grep php
And you should get the result as shown below.
Note:
If you want to check the apcu and yaml extensions, you can use the following php commands.
sudo php -m | grep apcu
sudo php -m | grep yaml
Installation and configuration for PHP-FPM has been completed.
Step 3 - Install PHP Composer
The composer is an application-level package manager for PHP. It's used for managing PHP dependencies.
Install the PHP Composer using the following command.
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
After the installation is complete, try to run the composer command in the following way.
composer --version
The PHP Composer has been installed.
Step 4 - Download and Install Grav
We will install and run the tool as 'grav' user. The user was created in one of the previous steps.
First, login as 'grav' user.
su - grav
Now, create a new 'www' directory.
mkdir -p ~/www
cd ~/www
Download the latest grav version using wget and add a '.zip' extension to the file name.
wget https://getgrav.org/download/core/grav/latest
mv latest latest.zip
Extract 'latest.zip' file and rename the 'grav' directory to 'html'.
unzip latest.zip
mv grav/ html/
Now back to root priveliges and change the group owner to 'www-data'.
exit
cd /home/grav/www
sudo chown -R grav:www-data html/
Grav is now downloaded and installed by the 'grav' user in the '/home/grav/www/html' directory.
Step 5 - Configure Grav virtual host
In this step, we will configure the Nginx virtual host for Grav. We will create a new virtual host file named 'grav' inside the 'sites-available' directory, and then activate the Grav virtual host.
Goto the '/etc/nginx' configuration directory and create a new file 'grav' using the vim editor.
cd /etc/nginx
vim sites-available/grav
Paste the following Grav Nginx virtual host configuration there.
server {
## Define Index
#listen 80;
index index.html index.php;
## Web root and Domain Name
root /home/grav/www/html;
server_name grav.hakase-labs.co;
## Begin - Index
# for subfolders, simply adjust the rewrite:
# to use `/subfolder/index.php`
location / {
try_files $uri $uri/ /index.php?$query_string;
}
## End - Index
## Begin - PHP-FPM Configuration
location ~ \.php$ {
# Choose either a socket or TCP/IP address
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
# 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;
}
## End - PHP
## Begin - Security
# deny all direct access for these folders
location ~* /(.git|cache|bin|logs|backups)/.*$ { return 403; }
# deny running scripts inside core system folders
location ~* /(system|vendor)/.*\.(txt|xml|md|html|yaml|php|pl|py|cgi|twig|sh|bat)$ { return 403; }
# deny running scripts inside user folder
location ~* /user/.*\.(txt|md|yaml|php|pl|py|cgi|twig|sh|bat)$ { return 403; }
# deny access to specific files in the root folder
location ~ /(LICENSE|composer.lock|composer.json|nginx.conf|web.config|htaccess.txt|\.htaccess) { return 403; }
## End - Security
}
Save and exit.
Next, activate the virtual host and test the nginx configuration.
ln -s /etc/nginx/sites-available/grav /etc/nginx/sites-enabled/
nginx -t
Note: Make sure you get no Nginx error after testing the configuration.
Now restart the Nginx and PHP-FPM services.
systemctl restart nginx
systemctl restart php7.0-fpm
Configuration for Nginx Grav virtual host has been completed.
Step 6 - Testing
Open your web browser and type your Grav site domain name, mine is http://grav.hakase-labs.co/
And you will get the default Grav homepage, as shown below.
Grav Flat CMS installation using PHP-FPM and Nginx web server on Ubuntu 16.04 has been completed successfully.