{"id":2797,"date":"2018-01-26T14:30:53","date_gmt":"2018-01-26T11:30:53","guid":{"rendered":"https:\/\/www.howtoforge.com\/tutorial\/how-to-install-and-configure-grav-cms-on-ubuntu-1604\/"},"modified":"2018-01-26T14:30:53","modified_gmt":"2018-01-26T11:30:53","slug":"how-to-install-grav-cms-with-nginx-on-ubuntu-16-04","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/how-to-install-grav-cms-with-nginx-on-ubuntu-16-04\/","title":{"rendered":"How to Install Grav CMS with Nginx on Ubuntu 16.04"},"content":{"rendered":"<p>Grav is a modern web application that requires no database. It&#8217;s a\u00a0file-based web platform that&#8217;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.<\/p>\n<p>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.<\/p>\n<h2 id=\"prerequisites\">Prerequisites<\/h2>\n<ul>\n<li>Ubuntu 16.04<\/li>\n<li>Root privileges<\/li>\n<\/ul>\n<h2 id=\"what-we-will-do\">What we will do:<\/h2>\n<ol>\n<li>Install Nginx<\/li>\n<li>Install and Configure PHP-FPM<\/li>\n<li>Install PHP Composer<\/li>\n<li>Download and Install Grav CMS<\/li>\n<li>Configure Nginx Virtual Host for Grav<\/li>\n<li>Testing<\/li>\n<\/ol>\n<h2 id=\"step-install-nginx-web-server\">Step 1 &#8211; Install Nginx Web Server<\/h2>\n<p>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.<\/p>\n<p class=\"command\">sudo apt update<br \/>sudo apt upgrade<\/p>\n<p>Now install the Nginx web server.<\/p>\n<p class=\"command\">sudo apt install nginx -y<\/p>\n<p>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.<\/p>\n<p class=\"command\">systemctl start nginx<br \/>systemctl enable nginx<\/p>\n<p>The Nginx web server has been installed on Ubuntu 16.04, and it&#8217;s running on the default HTTP port 80. Check it using the netstat command in the following way.<\/p>\n<p class=\"command\">netstat -plntu<\/p>\n<p>And you will get the result as shown below.<\/p>\n<p><a class=\"fancybox\" id=\"img-1\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_grav_cms_on_ubuntu_1604\/big\/1.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-install-grav-cms-with-nginx-on-ubuntu-16-04.png\" alt=\"Install Nginx\" width=\"500\" height=\"198\" title=\"\"><\/a><\/p>\n<h2 id=\"step-install-and-configure-phpfpm\">Step 2 &#8211; Install and Configure PHP-FPM<\/h2>\n<p>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.<\/p>\n<p class=\"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<\/p>\n<p>Now we need to configure PHP-FPM.<\/p>\n<p><strong>&#8211; Install additional extensions<\/strong><\/p>\n<p>This is optional, but it&#8217;s best for Grav performance. We will install some additional extensions manually from the PHP extensions repository using the PECL command.<\/p>\n<p>Install the &#8216;apcu&#8217; extension (for increased cache performance) using the &#8216;pecl&#8217; command below.<\/p>\n<p class=\"command\">sudo pecl install apcu<\/p>\n<p>Now create a new extension configuration &#8216;apcu.ini&#8217; in the &#8216;\/etc\/php\/7.0\/mods-available\/&#8217; directory.<\/p>\n<p class=\"command\">sudo vim \/etc\/php\/7.0\/mods-available\/apcu.ini<\/p>\n<p>Paste the following configuration there.<\/p>\n<pre class=\"system\">extension=apcu.so<\/pre>\n<p>Save and exit.<\/p>\n<p>Next, activate the extension using the commands below.<\/p>\n<p class=\"command\">sudo ln -s \/etc\/php\/7.0\/mods-available\/apcu.ini \/etc\/php\/7.0\/fpm\/conf.d\/20-apcu.ini<br \/>sudo ln -s \/etc\/php\/7.0\/mods-available\/apcu.ini \/etc\/php\/7.0\/cli\/conf.d\/20-apcu.ini<\/p>\n<p><a class=\"fancybox\" id=\"img-2\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_grav_cms_on_ubuntu_1604\/big\/2.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-install-grav-cms-with-nginx-on-ubuntu-16-04-1.png\" alt=\"Enable apcu module in PHP\" width=\"500\" height=\"102\" title=\"\"><\/a><\/p>\n<p>Now, we will install the &#8216;YAML&#8217; extension to further improve Grav performance. But before installing the extension, we need to install some packages using the following apt command.<\/p>\n<p class=\"command\">sudo apt install libyaml-dev unzip -y<\/p>\n<p>Next, install the yaml extensions using the pecl command.<\/p>\n<p class=\"command\">sudo pecl install yaml-2.0.0<\/p>\n<p>After the installation is complete, create a new &#8216;yaml.ini&#8217; configuration file.<\/p>\n<p class=\"command\">sudo vim \/etc\/php\/7.0\/mods-available\/yaml.ini<\/p>\n<p>Paste the following configuration there.<\/p>\n<pre class=\"system\">extension=yaml.so<\/pre>\n<p>Save and exit.<\/p>\n<p>Now activate the YAML extension.<\/p>\n<p class=\"command\">sudo ln -s \/etc\/php\/7.0\/mods-available\/yaml.ini \/etc\/php\/7.0\/fpm\/conf.d\/20-yaml.ini<br \/>sudo ln -s \/etc\/php\/7.0\/mods-available\/yaml.ini \/etc\/php\/7.0\/cli\/conf.d\/20-yaml.ini<\/p>\n<p><a class=\"fancybox\" id=\"img-3\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_grav_cms_on_ubuntu_1604\/big\/3.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-install-grav-cms-with-nginx-on-ubuntu-16-04-2.png\" alt=\"Configure YAML extension\" width=\"500\" height=\"102\" title=\"\"><\/a><\/p>\n<p>New additional PHP extensions for Grav have been added.<\/p>\n<p><strong>&#8211; Configure PHP-FPM Pool for Grav<\/strong><\/p>\n<p>Before creating a new PHP-FPM pool configuration, we need to add a new user named &#8216;grav&#8217; to the system, something which you can do using the useradd command.<\/p>\n<p class=\"command\">useradd -m -s \/bin\/bash grav<br \/>passwd grav<\/p>\n<p>Now go to the &#8216;\/etc\/php\/7.0\/fpm\/pool.d&#8217; directory and backup the default &#8216;www.conf&#8217; configuration.<\/p>\n<p class=\"command\">cd \/etc\/php\/7.0\/fpm\/pool.d\/<br \/>mv www.conf www.conf.bak<\/p>\n<p>Next, create new PHP-FPM pool configuration &#8211; &#8216;grav.conf&#8217; &#8211; for Grav.<\/p>\n<p class=\"command\">vim grav.conf<\/p>\n<p>Paste the following configuration there.<\/p>\n<pre class=\"system\" readability=\"10\">[grav]<p>user = grav<br\/>group = grav<\/p><p>listen = \/run\/php\/php7.0-fpm.sock<\/p><p>listen.owner = www-data<br\/>listen.group = www-data<\/p><p>pm = dynamic<br\/>pm.max_children = 5<br\/>pm.start_servers = 2<br\/>pm.min_spare_servers = 1<br\/>pm.max_spare_servers = 3<\/p><p>chdir = \/<\/p><\/pre>\n<p>Save and exit.<\/p>\n<p><a class=\"fancybox\" id=\"img-4\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_grav_cms_on_ubuntu_1604\/big\/4.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-install-grav-cms-with-nginx-on-ubuntu-16-04-3.png\" alt=\"Configure PHP-FPM Pool\" width=\"500\" height=\"224\" title=\"\"><\/a><\/p>\n<p>Restart the PHP-FPM service and enable it to launch at system boot.<\/p>\n<p class=\"command\">systemctl restart php7.0-fpm<br \/>systemctl enable php7.0-fpm<\/p>\n<p>PHP-FPM is now running under the &#8216;php7.0-fpm.sock&#8217; file &#8211; check it using the netstat command in the following way.<\/p>\n<p class=\"command\">netstat -pl | grep php<\/p>\n<p>And you should get the result as shown below.<\/p>\n<p><a class=\"fancybox\" id=\"img-5\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_grav_cms_on_ubuntu_1604\/big\/5.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-install-grav-cms-with-nginx-on-ubuntu-16-04-4.png\" alt=\"Start PHP-FPM\" width=\"500\" height=\"130\" title=\"\"><\/a><\/p>\n<p><strong>Note:<\/strong><\/p>\n<p>If you want to check the apcu and yaml extensions, you can use the following php commands.<\/p>\n<p class=\"command\">sudo php -m | grep apcu<br \/>sudo php -m | grep yaml<\/p>\n<p><a class=\"fancybox\" id=\"img-6\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_grav_cms_on_ubuntu_1604\/big\/6.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-install-grav-cms-with-nginx-on-ubuntu-16-04-5.png\" alt=\"Check PHP modules\" width=\"500\" height=\"117\" title=\"\"><\/a><\/p>\n<p>Installation and configuration for PHP-FPM has been completed.<\/p>\n<h2 id=\"step-install-php-composer\">Step 3 &#8211; Install PHP Composer<\/h2>\n<p>The composer is an application-level package manager for PHP. It&#8217;s used for managing PHP dependencies.<\/p>\n<p>Install the PHP Composer using the following command.<\/p>\n<p class=\"command\">curl -sS https:\/\/getcomposer.org\/installer | php<br \/>sudo mv composer.phar \/usr\/local\/bin\/composer<\/p>\n<p>After the installation is complete, try to run the composer command in the following way.<\/p>\n<p class=\"command\">composer &#8211;version<\/p>\n<p>The PHP Composer has been installed.<\/p>\n<p><a class=\"fancybox\" id=\"img-7\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_grav_cms_on_ubuntu_1604\/big\/7.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-install-grav-cms-with-nginx-on-ubuntu-16-04-6.png\" alt=\"Install PHP composer\" width=\"500\" height=\"202\" title=\"\"><\/a><\/p>\n<h2 id=\"step-download-and-install-grav\">Step 4 &#8211; Download and Install Grav<\/h2>\n<p>We will install and run the tool as &#8216;grav&#8217; user. The user was created in one of the previous steps.<\/p>\n<p>First, login as &#8216;grav&#8217; user.<\/p>\n<p class=\"command\">su &#8211; grav<\/p>\n<p>Now, create a new &#8216;www&#8217; directory.<\/p>\n<p class=\"command\">mkdir -p ~\/www<br \/>cd ~\/www<\/p>\n<p>Download the latest grav version using wget and add a &#8216;.zip&#8217; extension to the file name.<\/p>\n<p class=\"command\">wget https:\/\/getgrav.org\/download\/core\/grav\/latest<br \/>mv latest latest.zip<\/p>\n<p>Extract &#8216;latest.zip&#8217; file and rename the &#8216;grav&#8217; directory to &#8216;html&#8217;.<\/p>\n<p class=\"command\">unzip latest.zip<br \/>mv grav\/ html\/<\/p>\n<p>Now back to root priveliges and change the group owner to &#8216;www-data&#8217;.<\/p>\n<p class=\"command\">exit<br \/>cd \/home\/grav\/www<br \/>sudo chown -R grav:www-data html\/<\/p>\n<p>Grav is now downloaded and installed by the &#8216;grav&#8217; user in the &#8216;\/home\/grav\/www\/html&#8217; directory.<\/p>\n<h2 id=\"step-configure-grav-virtual-host\">Step 5 &#8211; Configure Grav virtual host<\/h2>\n<p>In this step, we will configure the Nginx virtual host for Grav. We will create a new virtual host file named &#8216;grav&#8217; inside the &#8216;sites-available&#8217; directory,\u00a0 and then activate the Grav virtual host.<\/p>\n<p>Goto the &#8216;\/etc\/nginx&#8217; configuration directory and create a new file &#8216;grav&#8217; using the <a href=\"https:\/\/www.howtoforge.com\/vim-basics\" target=\"_blank\" rel=\"noopener noreferrer\">vim editor<\/a>.<\/p>\n<p class=\"command\">cd \/etc\/nginx<br \/>vim sites-available\/grav<\/p>\n<p>Paste the following Grav Nginx virtual host configuration there.<\/p>\n<pre class=\"system\" readability=\"24\">server {<br\/>\u00a0\u00a0\u00a0 ## Define Index<br\/>\u00a0\u00a0\u00a0 #listen 80;<br\/>\u00a0\u00a0\u00a0 index index.html index.php;<p>\u00a0\u00a0\u00a0 ## Web root and Domain Name<br\/>\u00a0\u00a0\u00a0 root \/home\/grav\/www\/html;<br\/>\u00a0\u00a0\u00a0 server_name grav.hakase-labs.co;<\/p><p>\u00a0\u00a0\u00a0 ## Begin - Index<br\/>\u00a0\u00a0\u00a0 # for subfolders, simply adjust the rewrite:<br\/>\u00a0\u00a0\u00a0 # to use `\/subfolder\/index.php`<br\/>\u00a0\u00a0\u00a0 location \/ {<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 try_files $uri $uri\/ \/index.php?$query_string;<br\/>\u00a0\u00a0\u00a0 }<br\/>\u00a0\u00a0\u00a0 ## End - Index<\/p><p>\u00a0\u00a0\u00a0 ## Begin - PHP-FPM Configuration<br\/>\u00a0\u00a0\u00a0 location ~ \\.php$ {<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Choose either a socket or TCP\/IP address<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 fastcgi_pass unix:\/run\/php\/php7.0-fpm.sock;<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # fastcgi_pass 127.0.0.1:9000;<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 fastcgi_split_path_info ^(.+\\.php)(\/.+)$;<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 fastcgi_index index.php;<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 include fastcgi_params;<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 fastcgi_param SCRIPT_FILENAME $document_root\/$fastcgi_script_name;<br\/>\u00a0\u00a0\u00a0 }<br\/>\u00a0\u00a0\u00a0 ## End - PHP<\/p><p>\u00a0\u00a0\u00a0 ## Begin - Security<br\/>\u00a0\u00a0\u00a0 # deny all direct access for these folders<br\/>\u00a0\u00a0\u00a0 location ~* \/(.git|cache|bin|logs|backups)\/.*$ { return 403; }<br\/>\u00a0\u00a0\u00a0 # deny running scripts inside core system folders<br\/>\u00a0\u00a0\u00a0 location ~* \/(system|vendor)\/.*\\.(txt|xml|md|html|yaml|php|pl|py|cgi|twig|sh|bat)$ { return 403; }<br\/>\u00a0\u00a0\u00a0 # deny running scripts inside user folder<br\/>\u00a0\u00a0\u00a0 location ~* \/user\/.*\\.(txt|md|yaml|php|pl|py|cgi|twig|sh|bat)$ { return 403; }<br\/>\u00a0\u00a0\u00a0 # deny access to specific files in the root folder<br\/>\u00a0\u00a0\u00a0 location ~ \/(LICENSE|composer.lock|composer.json|nginx.conf|web.config|htaccess.txt|\\.htaccess) { return 403; }<br\/>\u00a0\u00a0\u00a0 ## End - Security<br\/>}<\/p><\/pre>\n<p>Save and exit.<\/p>\n<p>Next, activate the virtual host and test the nginx configuration.<\/p>\n<p class=\"command\">ln -s \/etc\/nginx\/sites-available\/grav \/etc\/nginx\/sites-enabled\/<br \/>nginx -t<\/p>\n<p><strong>Note: <\/strong>Make sure you get no Nginx error after testing the configuration.<\/p>\n<p>Now restart the Nginx and PHP-FPM services.<\/p>\n<p class=\"command\">systemctl restart nginx<br \/>systemctl restart php7.0-fpm<\/p>\n<p><a class=\"fancybox\" id=\"img-8\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_grav_cms_on_ubuntu_1604\/big\/8.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-install-grav-cms-with-nginx-on-ubuntu-16-04-7.png\" alt=\"Enable Grav vhost in Nginx\" width=\"500\" height=\"179\" title=\"\"><\/a><\/p>\n<p>Configuration for Nginx Grav virtual host has been completed.<\/p>\n<h2 id=\"step-testing\">Step 6 &#8211; Testing<\/h2>\n<p>Open your web browser and type your Grav site domain name, mine is http:\/\/grav.hakase-labs.co\/<\/p>\n<p>And you will get the default Grav homepage, as shown below.<\/p>\n<p><a class=\"fancybox\" id=\"img-9\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_grav_cms_on_ubuntu_1604\/big\/9.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-install-grav-cms-with-nginx-on-ubuntu-16-04-8.png\" alt=\"Grav CMS successfully installed\" width=\"500\" height=\"288\" title=\"\"><\/a><\/p>\n<p>Grav Flat CMS installation using PHP-FPM and Nginx web server on Ubuntu 16.04 has been completed successfully.<\/p>\n<h2 id=\"reference\">Reference<\/h2>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Grav is a modern web application that requires no database. It&#8217;s a\u00a0file-based web platform that&#8217;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 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-2797","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2797","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=2797"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2797\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=2797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=2797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=2797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}