Craft is an open source CMS written in PHP. Craft allows you to easily create and manage great-looking content, and to design and build exactly what you need. It's secure and scalable CMS with big plugin ecosystem and the Craft source code is hosted on GitHub. This tutorial will walk you through the Craft CMS installation procedure on a fresh CentOS system using Nginx as the web server and we will secure the website with a Let's encrypt SSL certificate.
Requirements
Craft requires the following:
- PHP version 7.0 or greater with the following PHP extensions: (ctype, cURL, GD or ImageMagick, iconv, JSON, Multibyte String, OpenSSL, PCRE, PDO MySQL Driver or PDO PostgreSQL Driver, PDO, Reflection, SPL, Zip, Intl, DOM)
- MySQL 5.5+ with InnoDB, MariaDB 5.5+, or PostgreSQL 9.5+
- At least 256MB of memory allocated to PHP
- At least 200MB of free disk space
Prerequisites
- An operating system running CentOS 7.
- A non-root user with sudo privileges.
Initial steps
Check your CentOS version:
cat /etc/centos-release
# CentOS Linux release 7.6.1810 (Core)
Set up the timezone:
timedatectl list-timezones
sudo timedatectl set-timezone 'Region/City'
Update your operating system packages (software). This is an important first step because it ensures you have the latest updates and security fixes for your operating system's default software packages:
sudo yum update -y
Install some essential packages that are necessary for basic administration of CentOS operating system:
sudo yum install -y curl wget vim git unzip socat
Step 1 - 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, as well as the necessary PHP extensions:
sudo yum install -y php72w php72w-cli php72w-fpm php72w-common php72w-curl php72w-gd php72w-pecl-imagick php72w-json php72w-mbstring php72w-mysql php72w-pgsql php72w-zip php72w-intl php72w-xml php72w-pdo
Check PHP version:
php --version
# PHP 7.2.13 (cli) (built: Dec 6 2018 23:18:37) ( NTS )
# Copyright (c) 1997-2018 The PHP Group
# Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
Start and enable PHP-FPM service:
sudo systemctl start php-fpm.service
sudo systemctl enable php-fpm.service
Run sudo vim /etc/php.ini
and set memory_limit
to at least 256MB
:
memory_limit = 256M
We can move on to the next step, which is the database installation and setup.
Step 2- Install MariaDB and create a database
Craft CMS supports MySQL/MariaDB and PostgreSQL databases. In this tutorial, we will use MariaDB as database server.
Install MariaDB database server:
sudo yum install -y mariadb-server
Check MariaDB version:
mysql --version
# mysql Ver 15.1 Distrib 5.5.60-MariaDB, for Linux (x86_64) using readline 5.1
Start and enable MariaDB service:
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service
Run mysql_secure installation
script to improve MariaDB security and set the password for MariaDB root
user:
sudo mysql_secure_installation
Answer each of the questions:
New password: your_secure_password
Re-enter new password: your_secure_password
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y
Connect to MariaDB shell as the root user:
sudo mysql -u root -p
# Enter password
Create an empty MariaDB database and user for Craft CMS and remember the credentials:
mariadb> CREATE DATABASE dbname;
mariadb> GRANT ALL ON dbname.* TO 'username' IDENTIFIED BY 'password';
mariadb> FLUSH PRIVILEGES;
Exit from MariaDB:
mariadb> exit
Replace dbname
, username
and password
with your own names.
Step 3 - Install acme.sh
client and obtain Let's Encrypt certificate (optional)
Securing your website with HTTPS is not necessary, but it is a good practice to secure your site traffic. In order to obtain TLS certificate from Let's Encrypt we will use Acme.sh client. Acme.sh is a pure unix shell software for obtaining TLS certificates from Let's Encrypt with zero dependencies.
Download and install Acme.sh:
sudo mkdir /etc/letsencrypt
git clone https://github.com/Neilpang/acme.sh.git
cd acme.sh
sudo ./acme.sh --install --home /etc/letsencrypt --accountemail your_email@example.com
cd ~
Check Acme.sh version:
/etc/letsencrypt/acme.sh --version
# v2.8.0
Obtain RSA and ECC/ECDSA certificates for your domain/hostname:
# RSA 2048
sudo /etc/letsencrypt/acme.sh --issue --standalone --home /etc/letsencrypt -d example.com --keylength 2048
# ECDSA
sudo /etc/letsencrypt/acme.sh --issue --standalone --home /etc/letsencrypt -d example.com --keylength ec-256
After running the above commands, your certificates and keys will be in:
- For RSA:
/etc/letsencrypt/example.com
directory. - For ECC/ECDSA:
/etc/letsencrypt/example.com_ecc
directory.
Step 4 - Install and Configure Nginx
Craft CMS can work fine with many popular web server softwares. In this tutorial, we selected Nginx.
Download and install latest mainline release of Nginx from the official Nginx repository.
In order to check a signature it is necessary to download Nginx signing key and import it to the rpm
program’s keyring:
wget https://nginx.org/keys/nginx_signing.key
sudo rpm --import nginx_signing.key
To set up the yum repository for CentOS, create the file named /etc/yum.repos.d/nginx_mainline.repo
with the following contents:
[nginx] name=nginx repo baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/ gpgcheck=1 enabled=1
Install the latest mainline Nginx packages:
sudo yum install -y nginx nginx-module-geoip nginx-module-image-filter nginx-module-njs nginx-module-perl nginx-module-xslt
Start and enable Nginx service:
sudo systemctl start nginx.service
sudo systemctl enable nginx.service
Check the Nginx version:
nginx -v
# nginx version: nginx/1.15.8
Configure Nginx for Craft CMS by running:
sudo vim /etc/nginx/conf.d/craft.conf
And populate the file with the following configuration:
server {
listen 80;
listen [::]:80;
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com www.example.com;
root /var/www/craft/web;
index index.php;
ssl_certificate /etc/letsencrypt/example.com/fullchain.cer;
ssl_certificate_key /etc/letsencrypt/example.com/example.com.key;
ssl_certificate /etc/letsencrypt/example.com_ecc/fullchain.cer;
ssl_certificate_key /etc/letsencrypt/example.com_ecc/example.com.key;
location / {
try_files $uri/index.html $uri $uri/ /index.php?$query_string;
}
location ~ [^/]\.php(/|$) {
try_files $uri $uri/ /index.php?$query_string;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param HTTP_PROXY "";
}
}
Replace example.com with your own domain name in the above file.
NOTE: For complete and production ready Nginx config for Craft visit https://github.com/nystudio107/nginx-craft. The config is intentionally simplified to keep config dense and simple.
Test Nginx configuration:
sudo nginx -t
Reload Nginx:
sudo systemctl reload nginx.service
Step 5 - Install Composer
Install Composer, the PHP dependency manager globally:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer
Check the Composer version:
composer --version
# Composer version 1.8.0 2018-12-03 10:31:16
NOTE: Composer installation commands will change in the future, so check https://getcomposer.org/download/ for the most up to date commands if the above commands doesn't work.
Step 6 - Install Craft CMS
Craft CMS can be downloaded with Composer or by manually downloading a .zip
or tar.gz
archive. In this tutorial we will use Composer to download Craft CMS.
Create a document root directory where Craft CMS should reside in:
sudo mkdir -p /var/www/craft
Navigate to the document root directory:
cd /var/www/craft
Change ownership of the /var/www/craft
directory to {your_username}.
sudo chown -R {your_username}:{your_username} /var/www/craft
To create a new Craft project, run this command:
composer create-project craftcms/craft .
Change ownership of the /var/www/craft
directory to nginx:
sudo chown -R nginx:nginx /var/www/craft
Run sudo vim /etc/php-fpm.d/www.conf
and set the user and group to nginx
. Initially, they will be set to apache:
sudo vim /etc/php-fpm.d/www.conf
# user = nginx
# group = nginx
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
In your web browser, go to http://<Hostname>/index.php?p=admin/install
(substituting <Hostname>
with your web server’s host name). If you’ve done everything right so far, you should be greeted by Craft’s Setup Wizard.
Step 7 - Complete the Craft CMS setup
After opening your installation URL the page like below should appear. Click "Install Craft" button. Accept the license by clicking on the "Got it" button.
Fill in database details and click on the "Next" button. Create Craft account and click on the "Next" button. Now, Setup your site and click on the "Finish up" button. After this you should be redirected to the Craft admin page.