Setting up Craft CMS on a DigitalOcean server on Ubuntu 18

First, ssh into the VM as root and set up a non-root user to go through the rest.

~$ adduser martin
~$ usermod -aG sudo {username}

Then log in as that user. Obvisouly, you should pick your own {username}.

Update and upgrade

Run the standard update and upgrade routines. Running this on a brand new VM should ideally not do a lot.

~$ sudo apt-get update
~$ sudo apt-get upgrade
~$ sudo apt-get dist-upgrade

Install PHP

Then follow https://phoenixnap.com/kb/how-to-install-composer-ubuntu-18-04 to install PHP

~$ sudo apt-get install php libapache2-mod-php

~$ php --version
PHP 7.2.24-0ubuntu0.18.04.6 (cli) (built: May 26 2020 13:09:11) ( NTS )
[…]

Version 7.2 is current as of now.

This is followed by an Apache restart and required dependencies.

~$ sudo systemctl restart apache2

~$ sudo apt-get install curl php-cli php-mbstring git unzip php7.2-curl
~$ sudo apt install imagemagick php-imagick zip unzip php-zip
~$ sudo apt-get install php-intl
~$ sudo apt-get install php-xml
~$ sudo apt-get install php7.2-mysql

Install Composer

The preferred way to install Craft seems to be Composer. Therefore, follow https://www.digitalocean.com/community/tutorials/how-to-install-and-use-composer-on-ubuntu-18-04 to install that.

~$ curl -sS https://getcomposer.org/installer -o composer-setup.php
~$ sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
~$ composer --version
Composer version 1.10.7 2020-06-03 10:03:56

~$ rm composer-setup.php 

Install Maria DB

Then follow https://www.digitalocean.com/community/tutorials/how-to-install-mariadb-on-ubuntu-18-04 to install the database server and client.

Also, run the mysql_secure_installation script to clean up the installation, and set some sensible default.

~$ sudo apt-get install mariadb-server mariadb-client

sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

sudo mysql_secure_installation

Then create the Craft CMS database. For that you need to choose the database name (e.g. craftdb) and database user name (e.g. craftuser) and a password for that user (which I am not going to document here for obvious reasons)

~$ sudo mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 38
Server version: 10.1.44-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> 
MariaDB [(none)]> CREATE DATABASE craftdb;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> CREATE USER 'craftuser'@'localhost' IDENTIFIED BY '**************';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> GRANT ALL ON craftdb.* TO 'craftuser'@'localhost' IDENTIFIED BY '3H$A"/DWmCS*;7XK' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> EXIT;

Firewall

Let’s see how far we’ve got

Follow https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 to set up the firewall rules to allow traffic to and from that VM over HTTP(s) and SSH.

~$ sudo ufw allow 'Apache Full'
~$ sudo ufw allow 'OpenSSH'
~$ sudo ufw enable
~$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
Apache                     ALLOW       Anywhere                  
Apache Full                ALLOW       Anywhere                  
OpenSSH                    ALLOW       Anywhere                  
Apache (v6)                ALLOW       Anywhere (v6)             
Apache Full (v6)           ALLOW       Anywhere (v6)             
OpenSSH (v6)               ALLOW       Anywhere (v6)     

Create website in Apache

Here are a handful commands to check the status of Apache, to stop it, to start it, to restart it, and reload the configuration without restarting it.

~$ sudo systemctl status apache2
~$ sudo systemctl stop apache2
~$ sudo systemctl start apache2
~$ sudo systemctl restart apache2
~$ sudo systemctl reload apache2

Then run

~$ sudo systemctl status apache2
apache2.service - The Apache HTTP Server
   Loaded: loaded
        (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: active (running) since Thu 2020-06-11 08:37:28 UTC; 16min ago
 Main PID: 8178 (apache2)

and make sure you a line in there saying active (running).

The create a new folder {foldername} for a new website with a simple HMTL file.

~$ sudo mkdir /var/www/{foldername}
~$ sudo chown -R $USER:$USER /var/www/{foldername}
~$ sudo chmod -R 755 /var/www/{foldername}

Create a simple html file

~$ nano /var/www/{foldername}/index.html

Copy&paste this content

------------ 8< ------------ 8< ------------
    <html>
        <head>
            <title>Welcome to foq.me!</title>
        </head>
        <body>
            <h1>Success!  The your_domain virtual host is working!</h1>
        </body>
    </html>
------------ 8< ------------ 8< ------------

Then create a.conf file for your website

$ sudo nano /etc/apache2/sites-available/{your website}.conf

Copy&paste this content

------------ 8< ------------ 8< ------------
    <VirtualHost *:80>
        ServerAdmin {your email}
        ServerName {your domain}
        ServerAlias www.{your domain}
        DocumentRoot /var/{foldername}/foq_me
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
------------ 8< ------------ 8< ------------

Then add this site to the Apache configuration, remove the original (default) configuration, run a config test to make sure this Ok and then restart Apache.

~$ sudo a2ensite foq_me.conf
~$ sudo a2dissite 000-default.conf
~$ sudo apache2ctl configtest
~$ sudo systemctl restart apache2

Let’s encrypt

Follow https://www.digitalocean.com/community/tutorials/how-to-secure-apache-with-let-s-encrypt-on-ubuntu-18-04 to create a certificate for your server and set up the https binding.

~$ sudo add-apt-repository ppa:certbot/certbot (You’ll need to press ENTER to accept.)
~$ sudo apt install python-certbot-apache

~$ sudo certbot --apache -d {your domain} -d {your domain}

[…]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://{your domain} and
https://www.{your domain}

Testing

All prerequisites should now be in place and the server should be online now, and the dummy HTML shown above should now appear on the internet.

Screenshot 2020 06 11 at 15 30 30

Craft CMS installation

First double-check that all the prerequisite are indeed in place by following https://github.com/craftcms/server-check

~$ wget https://github.com/craftcms/server-check/archive/master.zip
~$ unzip master.zip 
~$ cd server-check-master/server/
~$ php checkit.php 

[…]
------------------------------------------
Errors: 0   Warnings: 0   Total checks: 26

All errors and warnings I got at this stage have been fed back into the steps above. If anything else comes up it needs to be resolved before continuing.

Then remove our dummy html files from above and follow https://docs.craftcms.com/v3/installation.html

~$ rm /var/www/{foldername}/index.html
~$ composer create-project craftcms/craft /var/www/{foldername}

[…]
   ______ .______          ___       _______ .___________.
  /      ||   _  \        /   \     |   ____||           |
 |  ,----'|  |_)  |      /  ^  \    |  |__   `---|  |----`
 |  |     |      /      /  /_\  \   |   __|      |  |
 |  `----.|  |\  \----./  _____  \  |  |         |  |
  \______|| _| `._____/__/     \__\ |__|         |__|

     A       N   E   W       I   N   S   T   A   L   L
               ______ .___  ___.      _______.
              /      ||   \/   |     /       |
             |  ,----'|  \  /  |    |   (----`
             |  |     |  |\/|  |     \   \
             |  `----.|  |  |  | .----)   |
              \______||__|  |__| |_______/



Generating a security key ... done (9uhECHoK0uEeA6qc8kOqrJOGTQ57Zart)

Welcome to Craft CMS! Run the following command if you want to setup Craft from your terminal:

    /var/www/{foldername}/craft setup

Then run

/var/www/{foldername}/craft setup

(and copy & paste db details from above)

Fixes

Here are a list of things I need to “fix” to get everything up and running.

And what I needed to add the […]/web suffix to the path to the DocumentRoot line in those two files:

~$ sudo nano /etc/apache2/sites-available/foq_me.conf 
~$ sudo nano /etc/apache2/sites-available/foq_me-le-ssl.conf 

~$ sudo apache2ctl configtest
~$ sudo systemctl restart apache2

Also, follow https://unix.stackexchange.com/questions/35711/giving-php-permission-to-write-to-files-and-folders to give PHP access to the ./config directory.

~$ ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\  -f1
www-data

~$ sudo chown -R www-data:www-data /var/www/{foldername}

Also, change the AllowOverride setting (otherwise you might not be able to load the /admin pages.)

~$ sudo nano /etc/apache2/apache2.conf

and change

    <Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>

to

    <Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

and reload the Apache config.

~$ sudo systemctl reload apache2

Finally, to resolve a few warnings, update the php.ini

Craft requires a minimum PHP memory limit of 256M. The memory_limit directive in php.ini is currently set to 128M.
Craft requires a minimum PHP max execution time of 120 seconds. The max_execution_time directive in php.ini is currently set to 30.

These are fairly easy to spot settings in /etc/php/7.2/apache2/php.ini

~$ sudo cp /etc/php/7.2/apache2/php.ini /etc/php/7.2/apache2/php.ini.bak
~$ sudo nano /etc/php/7.2/apache2/php.ini

And reload the configuration.

~$ sudo systemctl reload apache2
Screenshot 2020 06 11 at 16 22 44

That’s it.

(as of June 2020)

And when you got to this point it is probably worth doing a VM backup or snapshot.

Updates 26-June-2020

I moved out of dev: in /var/www/{foldername} do

$ sudo cp .env .env.bak

$ sudo nano .env

and change the line that says

ENVIRONMENT="dev"

to

ENVIRONMENT="production"

0 comment(s)

Leave a comment?