Craft is an open-source, flexible and user-friendly content management system with a large community of developers and communities worldwide. It is a very good alternative to WordPress and Drupal and is used for creating customized digital experiences. It has an intuitive and easily accessible control panel that allows you to manage your website via the web browser. It offers hundreds of free and paid plugins that you can use to add additional features.

In this post, we’ll explain how to install Craft CMS with Apache and Let’s Encrypt SSL on Ubuntu 20.04.

Prerequisites

  • A server running Ubuntu 20.04.
  • A valid domain name that points to the IP of your server.
  • A root password is configured on the server.

Install Apache, MariaDB and PHP

Craft CMS runs on the web server, is written in PHP and uses MariaDB as database backend. Therefore, you need to install all these packages on your server.

Run the following command to install all of these packages:

apt-get install apache2 mariadb-server php php-cli libapache2-mod-php php-common php-json php-curl php-gd php-imagick php-json php-mbstring php-mysql php-pgsql php-zip php-intl php-xml -y

After you have installed all the packages, edit the PHP configuration file and change the default settings:

nano /etc/php/7.4/php.ini

Change the following settings:

memory_limit = 512M
post_max_size = 32M
upload_max_filesize = 32M
max_execution_time = 360

Save and close the file and restart the Apache service to apply the changes:

systemctl restart apache2

Create a database for Fork CMS

Next, you need to create a database and a user for Fork CMS. First, log in to the MariaDB shell with the following command:

mysql

Once you are logged in, create a database and a user with the following command:

MariaDB [(none)]> CREATE DATABASE craft;
MariaDB [(none)]> GRANT ALL ON craft.* TO 'craftuser' IDENTIFIED BY 'password';

Next, delete the authorizations and exit the MariaDB shell with the following command:

MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

Now the MariaDB database for the Craft CMS has been created. You can now continue with the next step.

Install Craft CMS

Next, you need to install Composer to download the latest version of Craft CMS. You can install it with the following command:

curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

Once Composer is installed, navigate to the Apache web root directory and create a Craft CMS project using the following command:

cd /var/www/html
composer create-project craftcms/craft craftcms

You will be prompted to enter your database settings, administrator username and password, and website URL (see below):

Which database driver are you using? (mysql or pgsql) [mysql] 
Database server name or IP address: [127.0.0.1] 
Database port: [3306] 
Database username: [root] craftuser
Database password: 
Database name: craft
Database table prefix: 
Testing database credentials ... success!
Saving database credentials to your .env file ... done

Install Craft now? (yes|no) [yes]:yes

Username: [admin] admin
Email: [email protected]
Password: 
Confirm: 
Site name: CraftCMS Site
Site URL: http://craftcms.example.com
Site language: [en-US] 

    > add foreign key fk_rlbmgnhpxsljkaunjwnsezfrnrkhwzpthfsq: {{%widgets}} (userId) references {{%users}} (id) ... done (time: 0.035s)
    > populating the info table ... done
    > saving default site data ... done
    > saving the first user ... done
*** installed Craft successfully (time: 5.449s)

Next, you’ll need to assign the correct permissions and ownership to the Craft CMS directory:

chown -R www-data:www-data /var/www/html/craftcms/
chmod -R 755 /var/www/html/craftcms/

Once you are done with this, you can proceed to the next step.

Create an Apache virtual host for Craft CMS

Next, you need to create a configuration file for an Apache virtual host for Craft CMS. You can create it with the following command:

nano /etc/apache2/sites-available/craftcms.conf

Paste the following lines:

     ServerAdmin [email protected]
     DocumentRoot /var/www/html/craftcms/web
     ServerName craftcms.example.com


     
          Options FollowSymlinks
          AllowOverride All
          Require all granted
     

     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined
    
     
            RewriteEngine on
            RewriteBase /
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*) index.php [PT,L]
    

Save and close the file and activate the Apache virtual host and the rewrite module with the following command:

a2ensite craftcms.conf
a2enmod rewrite

Then restart the Apache service to apply the changes:

systemctl restart apache2

You can also check the Apache status with the following command:

systemctl status apache2

You will receive the following output:

? apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-04-02 08:39:25 UTC; 5s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 17643 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 17647 (apache2)
      Tasks: 6 (limit: 2348)
     Memory: 13.4M
     CGroup: /system.slice/apache2.service
             ??17647 /usr/sbin/apache2 -k start
             ??17648 /usr/sbin/apache2 -k start
             ??17649 /usr/sbin/apache2 -k start
             ??17650 /usr/sbin/apache2 -k start
             ??17651 /usr/sbin/apache2 -k start
             ??17652 /usr/sbin/apache2 -k start

Apr 02 08:39:25 ubuntu2004 systemd[1]: Starting The Apache HTTP Server...

Once your Apache web server is configured, you can proceed to the next step.

Accessing the Craft CMS web interface

Now open your web browser and enter the URL http://craftcms.example.com to access the Craft CMS web interface. You should see the following page:

How to Install Craft CMS on Ubuntu linux ubuntu

Click on the Go to your Control Center button. You will be redirected to the Craft CMS login page:

How to Install Craft CMS on Ubuntu linux ubuntu

Enter your admin username and password and click the Login button. On the following page, you should see the Craft CMS dashboard:

How to Install Craft CMS on Ubuntu linux ubuntu

Secure Craft CMS with Let’s Encrypt SSL

Next, we recommend securing your website with Let’s Encrypt SSL. First, install the Certbot client with the following command:

apt-get install python3-certbot-apache -y

Once installed, run the following command to secure your website with Let’s Encrypt SSL:

certbot --apache -d craftcms.example.com

You will be asked to enter your e-mail address and accept the terms of use (see below):

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator standalone, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [email protected]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Plugins selected: Authenticator apache, Installer apache
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for craftcms.example.com
Enabled Apache rewrite module
Waiting for verification...
Cleaning up challenges
Created an SSL vhost at /etc/apache2/sites-available/craftcms-le-ssl.conf
Enabled Apache socache_shmcb module
Enabled Apache ssl module
Deploying Certificate to VirtualHost /etc/apache2/sites-available/craftcms-le-ssl.conf
Enabling available site: /etc/apache2/sites-available/craftcms-le-ssl.conf

Next, select whether or not to redirect HTTP traffic to HTTPS (see below):

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

Type 2 and press enter to install Let’s Encrypt SSL for your website:

Enabled Apache rewrite module
Redirecting vhost in /etc/apache2/sites-enabled/craftcms.conf to ssl vhost in /etc/apache2/sites-available/craftcms-le-ssl.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://craftcms.example.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=craftcms.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/craftcms.example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/craftcms.example.com/privkey.pem
   Your cert will expire on 2022-07-23. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

Conclusion

Congratulations! You have successfully installed Craft CMS with Apache and Let’s Encrypt SSL on Ubuntu 20.04. You can now start creating your website with Craft CMS. If you have any questions, please feel free to contact me.