How to Install Magento on Debian 12

how to install magento on debian 12

Magento 2 is a popular open-source e-commerce platform that provides a flexible and feature-rich solution for creating online stores and managing digital commerce.

It is the second major version of the Magento platform and offers enhanced performance, scalability, and improved user experience compared to its predecessor, Magento 1. And on this tutorial, we’ll show you how to install Magento on Debian 12.

Prerequisites

– A server running Debian 12 with a minimum of 2GB RAM.
– A non-root user with privileges.
– A domain name to use on the server. In our case, it will be example.com

Pre-installation

Before proceeding, we need to be sure that our system is up to date and the needed packages are installed so we can run the following:

$ apt update -y
$ apt upgrade -y
$ apt install wget curl nano ufw software-properties-common dirmngr apt-transport-https gnupg2 ca-certificates lsb-release debian-archive-keyring unzip -y

1. Installing PHP and extensions

By default, Debian 12 already has PHP 8.2 built-in that can be installed with the following commands

$ apt install php-fpm php-cli php-mysql php-mbstring php-xml php-gd php-bcmath php-zip php-curl php-tidy php-intl php-soap php-xsl libsodium-dev libsodium23 libssl-dev libcurl4-openssl-dev

After it’s installed, you can check the installation with:

$ php --version

2. Installing composer

Composer is needed since it’s a management tool for PHP, and Magento is installed through it.

The installation can be done by running the following:

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php composer-setup.php --2.2
$ php -r "unlink('composer-setup.php');"

Then, after it’s finished, you move the composer file to the bin directory:

$ mv composer.phar /usr/local/bin/composer

To check the installed version, use this command:

$ composer --version

3. Installing MariaDB

Since Debian 12 doesn’t have MySQL by default, but it’s MariaDB, we’ll proceed with that one. You can run the following command to install MariaDB:

$ apt install mariadb-server

After installing, rung the secure install script with:

$ mysql_secure_installation

You will be asked for the root password. Since we haven’t set any password, just press Enter.

After, some questions will be asked, such as switching to unix_socket and changing the root password of your MySQL. For both answers, you can type “n” and press enter. For the rest, you can use “y” by default.

4. Creating the database

Log in to the MariaDB

$ mysql

Create a database using

mysql> CREATE DATABASE magento;

Create a new user

mysql> CREATE USER 'magentouser'@'localhost' IDENTIFIED BY 'Your_password2';

To grant all the privileges on the database with

mysql> GRANT ALL PRIVILEGES ON magento.* TO 'magento'@'localhost';

Flush the privileges and exit the shell

mysql> FLUSH PRIVILEGES;
mysql> exit

5. Installing NGINX

Debian 12, by default, uses an older version of Nginx. We’ll proceed with the installation of the newer one.

To do that, we need to import the Nginx key:

$ curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

Then, add the repository of the Nginx stable version:

$ echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/debian `lsb_release -cs` nginx" | tee /etc/apt/sources.list.d/nginx.list

After the repository is added, you can install nginx with

$ apt update -y
$ apt install nginx

Then you can start the service with

$ systemctl start nginx

6. Installing Certbot

Once our Nginx is up and running, we need to install certbot so that we can generate a valid SSL certificate for our domain.

Before proceeding, we need to install snapd with

$ apt install snapd

After snapd is installed, we’ll run the following to ensure our installation is up to date:

$ snap install core
$ snap refresh core

To finally install certbot, you can run:

$ snap install certbot

When your installation is finished, to use the certbot command, you need to create an alias for the file

$ ln -s /snap/bin/certbot /bin/certbot

To generate an SSL certificate for your domain using the nginx module, you run:

$ certbot certonly --nginx -d example.com

The command will generate an SSL certificate on your /etc/letsencrypt/live/example.com directory. Of course, the name will be different; in your case, it will show your domain/subdomain name.

When finished, we need to generate a group certificate, and this can be accomplished by running:

$ openssl dhparam -dsaparam -out /etc/ssl/certs/dhparam.pem 4096

7. Downloading Magento

First of all, to download our Magento, we need to create a directory for it which can be accomplished with the following mkdir command as nginx user. So you can log in as nginx and run it as shown in the following sentence:

$ su - nginx -s /bin/bash
$ mkdir /var/www/magento -p

Before moving forward, you need the authentication keys for your Magento repository so composer can download the Magento base code. Generate the base code, then go to Sign in with Adobe ID.

Once you are successfully logged in, you need to go to the accessKeys page. There you’ll have a link to click on called “Access Keys”, where you can generate a pair of keys.

Once you copied the Public and Private keys, you need to create an auth.json file so your keys are stored and can be used by the composer.

$ nano ~/.config/composer/auth.json

The following content should be there on the auth.json

{
    "http-basic": {
        "repo.magento.com": {
            "username": "PUBLIC_KEY",
            "password": "PRIVATE_KEY"
        }
    }
}

Where PUBLIC_KEY and PRIVATE_KEY are your own keys that you got on Magento’s official website.

After that, you can log in to your Magento directory and create your project with the following commands:

$ cd /var/www/magento
$ composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition .

You might face a problem with the sample Nginx configuration file by Adobe, which can be quickly fixed with the following command:

$ sed -i 's/php-fpm:9000/fastcgi_backend/g' /var/www/magento/nginx.conf.sample

Then, you can log out from nginx user and run the following commands:

$ chown -R nginx: /var/ww/magento
$ chmod u+x bin/magento
$ find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
$ find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +

8. Running Magento Installation

To proceed with the Magento installation, we need to edit the XML file from the installer to correct the MariaDB version. To do that, we need to edit the following file:

$ nano /var/www/magento/app/etc/di.xml

There, we’ll search for this line

<item name="MariaDB-(10.2-10.6)" xsi:type="string">^10\.[2-6]\.</item>

And change it to

<item name="MariaDB-(10.2-10.11)" xsi:type="string">^10\.([2-9]|10|11)\.</item>

Then, save the file, and you should be good to go. Then log in as an nginx user and access your Magento directory:

$ su - nginx -s /bin/bash
$ cd /var/www/magento

And finally, run:

$ bin/magento setup:install \
--use-secure=1 \
--use-secure-admin=1 \
--db-host=localhost \
--db-name=magento \
--base-url=http://example.com \
--base-url-secure=https://example.com \
--db-user=magento \
--db-password=Your_password2 \
--admin-firstname=YourName \
--admin-lastname=LastName \
--admin-email=admin@example.com \
--admin-user=admin \
--admin-password=admin_password \
--language=en_US \
--currency=USD \
--timezone=America/Chicago \
--use-rewrites=1 \

Once the installer runs and it’s finished, you’ll see the output with your personal admin URL. Keep a note on that since it’s the URL you’ll manage your store.

You need now to create your Magento cron jobs, which can be accomplished with:

$ php bin/magento cron:install

9. Configuring PHP-FPM for Magento

We are almost there; now you need to configure the PHP-FPM service to serve the PHP files. To do that, open the file the following file:

$ nano /etc/php/8.2/fpm/pool.d/www.conf

You’ll see there are two directives, which refer to the user and group of the files from your Magento instance. You should use nginx for both. As shown here:

user = nginx
group = nginx

The same should be done to the listen.owner and listen.group; both should use nginx.

Now, we need to do some tweaks on the php.ini files for Magento to run properly. You can open the php.ini files and change the below values, or just paste and copy those sed commands:

$ sed -i 's/max_execution_time = 30/max_execution_time = 180/' /etc/php/8.2/fpm/php.ini
$ sed -i 's/max_execution_time = 30/max_execution_time = 180/' /etc/php/8.2/cli/php.ini
$ sed -i 's/memory_limit = 128M/memory_limit = 256M/' /etc/php/8.2/fpm/php.ini
$ sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 25M/g' /etc/php/8.2/fpm/php.ini
$ sed -i 's/post_max_size = 8M/post_max_size = 25M/g' /etc/php/8.2/fpm/php.ini
$ sed -i 's/zlib.output_compression = Off/zlib.output_compression = On/g' /etc/php/8.2/fpm/php.ini

To apply the settings, we need to restart the php-fpm service:

$ systemctl restart php8.2-fpm

10. Configuring Nginx with PHP-FPM

First of all, before creating the vhost for our domain/subdomain, we need to increase the server_names_hash_bucket_size from our nginx. To do that, add the following line before the line that contains “include /etc/nginx/conf.d/*.conf;”:

server_names_hash_bucket_size  64;

This should be done on the file /etc/nginx/nginx.conf.

After that, you can finally create the Magento configuration vhost file with:

$ nano /etc/nginx/conf.d/magento.conf

And the content:

#-- Start of file
upstream fastcgi_backend {
  server  unix:/run/php/php8.2-fpm.sock;
}
server {
  listen 443 ssl http2;
  server_name example.com;
  set $MAGE_ROOT /var/www/magento;
  include /var/www/magento/nginx.conf.sample;
  client_max_body_size 25m;
  access_log /var/log/nginx/magento.access.log;
  error_log  /var/log/nginx/magento.error.log;
  ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
  ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
  ssl_protocols TLSv1.2 TLSv1.3;
  ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384';
  ssl_prefer_server_ciphers on;
  ssl_session_cache shared:SSL:50m;
  ssl_session_timeout 1d;
  ssl_stapling on;
  ssl_stapling_verify on;
  ssl_dhparam /etc/ssl/certs/dhparam.pem;
}
server {
  listen 80;
  server_name example.com;
  return 301 https://$host$request_uri;
}
# -- End of File
# Where example.com is your actual domain!!

Before restarting nginx to apply the settings, it’s a good practice to run a nginx test before, so you avoid restarting the service with any errors. This can be done with

$ nginx -t

If you see the “syntax is ok” output, you can finally restart it with:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
$ systemctl restart nginx

Then, if your domain is already pointed to your server, you can see your front page. And, for some reason, you are not able to see some static content or CSS/JS, you can run:

$ su - nginx -s /bin/bash
$ cd /var/www/magento
$ php bin/magento setup:static-content:deploy -f
$ php bin/magento indexer:reindex

To access our admin page, we need to disable the Two Factor Authentication that is enabled by default on Magento. So, as nginx user, you can run:

Be sure to be logged in as nginx, so if you are not:

$ su - nginx -s /bin/bash

Then

$ php /var/www/magento/bin/magento module:disable Magento_AdminAdobeImsTwoFactorAuth
$ php /var/www/magento/bin/magento module:disable Magento_TwoFactorAuth

After you disable it, you need to recreate the classes and clear the cache with:

$ php /var/www/magento/bin/magento setup:di:compile
$ php /var/www/magento/bin/magento cache:clear

If you haven’t copied the URL from your administrative page, you can retrieve it with:

$ php /var/www/magento/bin/magento info:adminuri

And that’s it. You successfull installed Magento on your Debian 12 System. Please note that if you have a VPS with us, you don’t need to do it alone; you can count on our expert admins to do it for you, as it’s included in our Fully Managed Support.

Leave a Comment