There is a new version of this tutorial available for Debian 12 (Bookworm).

How to Install Craft CMS with Nginx on Debian 11

Craft CMS is a free and open-source content management system written in PHP and based on the Yii framework. It is a secure and scalable CMS that offers many plugins that help you customize your website easily. It allows you to create your content and structure your application data with control over everything, including all your own HTML. It is a simple, lightweight, and easy-to-use CMS platform that allows website owners to collaborate and automate engaging experiences with users across multiple devices.

This post will show you how to install Craft CMS with Nginx and Let's Encrypt SSL on Debian 11.

Prerequisites

  • A server running Debian 11.
  • A valid domain name pointed with server IP.
  • A root password is configured on the server.

Install Nginx, MariaDB and PHP

First, let's install the Apache web server, MariaDB server, PHP and other required PHP extensions:

apt-get install nginx mariadb-server php php-cli php-fpm php-common php-curl php-gd php-imagick php-json php-mbstring php-mysql php-pgsql php-zip php-intl php-xml sudo curl -y

Once all the packages are installed, edit the php.ini file with the following command:

nano /etc/php/7.4/fpm/php.ini

Change the following settings:

memory_limit = 512M
post_max_size = 32M
upload_max_filesize = 32M
date.timezone = Asia/Kolkata

Save and close the file then restart the PHP-FPM service to apply the changes:

systemctl restart php7.4-fpm

Create a Database for Craft CMS

Next, you will need to create a database and user for Craft CMS. To do so, log in to MySQL shell with the following command:

mysql

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

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

Next, flush the privileges and exit from the MariaDB shell with the following command:

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

Once you are finished, you can proceed to the next step.

Install Composer

Composer is a dependency manager for PHP used to install dependencies required for your PHP project. You can install the Composer using the following command:

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

You should see the following output:

All settings correct for using Composer
Downloading...

Composer (version 2.1.8) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

Once the installation is completed, verify the COmposer version using the following command:

composer --version

You should see the following output:

Composer version 2.1.8 2021-09-15 13:55:14

Install Craft CMS

Next, you will need to install the Craft CMS using Composer. First, change the ownership of the Nginx default root directory to www-data:

chown -R www-data:www-data /var/www/html

Next, navigate to /var/www/html directory and install the Craft CMS using the following command:

cd /var/www/html
sudo -u www-data composer create-project craftcms/craft craftcms

You will be asked to provide your database credentials, admin username, password, and website URL as shown below:

> @php craft setup/welcome

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


Generating an application ID ... done (CraftCMS--286d40e5-ec80-49e0-b64b-13bb7870375e)
Generating a security key ... done (5k8ygP059NnCLeTZvxRily-R4Wect6tY)

Welcome to Craft CMS!

Are you ready to begin the setup? (yes|no) [no]:yes
Generating an application ID ... done (CraftCMS--37ec6cd5-fde9-4b09-b2e8-1ad29edd4142)

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

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

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

Once the Craft CMS has been installed, set proper ownership to the craftcms directory:

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

Configure Nginx for Craft CMS

Next, you will need to create an Nginx virtual host configuration file to serve Craft CMS. You can create it using the following command:

nano /etc/nginx/conf.d/craftcms.conf

Add the following lines:

server {
listen 80;
 server_name craftcms.example.com;
 root /var/www/html/craftcms/web;
 index index.php;
 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.4-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 "";
 }
}

Save and close the file then verify the Nginx for any syntax error using the following command:

nginx -t

You should see the following output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Next, reload the Nginx service to apply the changes:

systemctl reload nginx

To check the Nginx status, run the following command:

systemctl status nginx

You should see the following output:

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-09-26 02:24:40 UTC; 6s ago
       Docs: man:nginx(8)
    Process: 370207 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 370208 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 370209 (nginx)
      Tasks: 5 (limit: 9510)
     Memory: 4.7M
        CPU: 53ms
     CGroup: /system.slice/nginx.service
             ??370209 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ??370210 nginx: worker process
             ??370211 nginx: worker process
             ??370212 nginx: worker process
             ??370213 nginx: worker process

Sep 26 02:24:40 debian11 systemd[1]: Starting A high performance web server and a reverse proxy server...
Sep 26 02:24:40 debian11 systemd[1]: Started A high performance web server and a reverse proxy server.

Secure Craft CMS with Let's Encrypt SSL

Next, you will need to install the Certbot client package to install the manage the Let's Encrypt SSL.

First, install the Certbot with the following command:

apt-get install python3-certbot-nginx -y

Once the installation is finished, run the following command to install the Let's Encrypt SSL on your website:

certbot --nginx -d craftcms.example.com

You will be asked to provide a valid email address and accept the term of service as shown below:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
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
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for craftcms.example.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/craftcms.conf

Next, choose whether or not to redirect HTTP traffic to HTTPS as shown bellow:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 hit Enter to finish the installation. You should see the following output:

Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/craftcms.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-05-26. 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"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - 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

 - We were unable to subscribe you the EFF mailing list because your
   e-mail address appears to be invalid. You can try again later by
   visiting https://act.eff.org.

Access Craft CMS

Now, open your web browser and access the Craft CMS using the URL https://craftcms.example.com. You should see the following screen:

Click on Go to your control panel. You should see the Craft CMS login page:

Provide your login credential and click on the Login button. You should see the Craft CMS dashboard on the following screen:

Conclusion

Congratulations! you have successfully installed CraftCMS with Nginx and Let's Encrypt SSL on Debian 11. You can now build your website easily from the Craft CMS control panel. Feel free to ask me if you have any questions.

Share this page:

0 Comment(s)