How to Install Craft CMS on Ubuntu 18.04 LTS

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 Ubuntu 18.04 LTS server 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 Ubuntu 18.04
  • A non-root user with sudo privileges.

Initial steps

Check your Ubuntu version:

lsb_release -ds
# Ubuntu 18.04.1 LTS

Set up the timezone:

sudo dpkg-reconfigure tzdata

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 apt update && sudo apt upgrade -y

Install some essential packages that are necessary for basic administration of Ubuntu operating system:

sudo apt install -y curl wget vim git unzip socat

Step 1 - Install PHP and required PHP extensions

Install PHP, as well as the necessary PHP extensions:

sudo apt install -y php7.2 php7.2-cli php7.2-fpm php7.2-common php7.2-curl php7.2-gd php-imagick php7.2-json php7.2-mbstring php7.2-mysql php7.2-pgsql php7.2-zip php7.2-intl php7.2-xml

Check PHP version:

php --version

# PHP 7.2.10-0ubuntu0.18.04.1 (cli) (built: Sep 13 2018 13:45:02) ( NTS )
# Copyright (c) 1997-2018 The PHP Group
# Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
# with Zend OPcache v7.2.10-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies

Run sudo vim /etc/php/7.2/fpm/php.ini and set memory_limit to at least 256MB:

memory_limit = 256M

PHP-FPM service is automatically started and enabled on reboot on Ubuntu 18.04 system, so there is no need to start and enable it manually. We can move on to the next step, which is database installation and setup.

Step 2- Install MySQL/MariaDB and create a database

Craft CMS supports MySQL/MariaDB and PostgreSQL databases. In this tutorial, we will use MySQL as database server.

Install MySQL database server:

sudo apt install -y mysql-server

Check MySQL version:

mysql --version
# mysql  Ver 14.14 Distrib 5.7.24, for Linux (x86_64) using  EditLine wrapper

Run mysql_secure installation script to improve MySQL security and set the password for MySQL root user:

sudo mysql_secure_installation

Answer each of the questions:

Would you like to setup VALIDATE PASSWORD plugin? N
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 MySQL shell as the root user:

sudo mysql -u root -p
# Enter password

Create an empty MySQL database and user for Craft CMS and remember the credentials:

mysql> CREATE DATABASE dbname;
mysql> GRANT ALL ON dbname.* TO 'username' IDENTIFIED BY 'password';
mysql> FLUSH PRIVILEGES;

Exit from MySQL:

mysql> 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 [email protected]
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.

Start by download Nginx repository PGP key:

wget https://nginx.org/keys/nginx_signing.key

Add key to apt program keyring:

sudo apt-key add nginx_signing.key

Add and set up Nginx mainline APT repository:

sudo -s
printf "deb https://nginx.org/packages/mainline/ubuntu/ $(lsb_release -sc) nginx\ndeb-src https://nginx.org/packages/mainline/ubuntu/ $(lsb_release -sc) nginx\n" >> /etc/apt/sources.list.d/nginx_mainline.list
exit

Update the repository list:

sudo apt update

Install the latest mainline Nginx packages:

sudo apt 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:

sudo 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 unix:/var/run/php/php7.2-fpm.sock;
    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 "";
  }

}

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.

Open nginx.conf file and set user to www-data.

sudo vim /etc/nginx/nginx.conf
user www-data www-data;

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 johndoe.

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 www-data:

sudo chown -R www-data:www-data /var/www/craft

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:

Start craft installation

Accept the license by clicking on the "Got it" button:

Accept the license

Fill in database details and click on the "Next" button:

Connect to database

Create Craft account and click on the "Next" button:

Create a user

Setup your site and click on the "Finish up" button:

Add site information

After this you should be redirected to the Craft admin page:

Craft CMS Dashboard

Share this page:

Suggested articles

0 Comment(s)

Add comment