Set up Shopware 6 Background Queue Worker (root rights)

This article is limited to the setup and control of Shopware 6 Background Queue Workers with root rights. If you only have limited access to your server, please use the article: Setting up Shopware 6 Background Queue Workers (User Rights)


Shopware 6 uses so-called message queue workers for processing background tasks and recurring tasks. In the standard configuration, these are executed via the logged-in administrator in the browser. As soon as more than one administrator is logged in, these message queue workers are executed multiple times and this can lead to a very high CPU load and problems with the execution of PHP FPM.


To solve this problem permanently, Shopware 6 offers the option of moving the execution of these message queue workers to the background, more precisely as a CLI command.



Deactivation of the Frontend Message Queue Worker

To deactivate the Shopware 6 Message Queue Worker for the logged in administrators, a zz-creoline.yaml config file must be created as a system user in the Shopware directory /config/packages.

To do this, first log in as a system user:

su <SYSTEMUSER>

And now create the config file:

nano config/packages/zz-creoline.yaml


Tip: On our managed Shopware servers, the correct document root directory is located under the absolute path: /var/www/vhosts/my-shop-domain.com/httpdocs/


# /var/www/vhosts/meine-shop-domain.de/httpdocs/config/packages/zz-creoline.yaml

shopware:
    admin_worker:
        enable_admin_worker: false


After the config file has been created, the Shopware cache must be cleared so that the new configuration file is activated:

bin/console cache:clear


bin/console must never be executed with root rights! Please always log in as system user first.


Our Managed Shopware servers have various PHP binaries available for running the CLI console. To clear the cache with the PHP 8.2 CLI, the following command must be executed:

/opt/plesk/php/8.2/bin/php /var/www/vhosts/meine-shop-domain.de/httpdocs/bin/console cache:clear



Setting up the Message Queue Worker as a systemd service

To ensure a permanent execution of the Message Queue Worker, we recommend setting it up as a systemd service. Two service files must be created for this:


From here root rights are required

touch /etc/systemd/system/shopware_consumer.service

touch /etc/systemd/system/shopware_scheduled_tasks.service


The Shopware Message Queue Consumer is then defined as follows:

# /etc/systemd/system/shopware_consumer.service

[Unit]
Description=Shopware Message Queue Consumer
After=mysql.service
StartLimitIntervalSec=0

[Service]
Type=simple
User=ssh_example_user
Restart=always
RestartSec=10
ExecStart=/opt/plesk/php/8.3/bin/php /var/www/vhosts/meine-shop-domain.de/httpdocs/bin/console messenger:consume async failed --time-limit=120 --memory-limit=512M

[Install]
WantedBy=multi-user.target


If you are using an older Shopware version, the default receiver must be added here as an alternative. Example for default receiver:

ExecStart=/opt/plesk/php/8.3/bin/php /var/www/vhosts/meine-shop-domain.de/httpdocs/bin/console messenger:consume failed default --time-limit=120 --memory-limit=512M


If you have registered more than one receiver, you can specify several receivers in succession or define a separate service for each receiver. Example for several receivers:

messenger:consume default receiver2 fasttrack


You can use the pre-installed text editor nano or vi to edit the file. Example for opening and editing the file:

nano /etc/systemd/system/shopware_consumer.service


Then paste the content shown above from the clipboard and save the changes with the keyboard shortcut CTRL + X. Then confirm the file change with the Y and Enter keys.


Important: Make sure that the directories and system users are correct. In this example, the installation is located in the directory /var/www/vhosts/meine-shop-domain.de/. This directory only serves as an example and must be adapted to the correct path of your installation.


In addition to the consumer service, the service for recurring tasks is also required:

# /etc/systemd/system/shopware_scheduled_tasks.service

[Unit]
Description=Shopware Scheduled Tasks Runner
After=mysql.service
StartLimitIntervalSec=0

[Service]
Type=simple
User=ssh_example_user
Restart=always
RestartSec=10
ExecStart=/opt/plesk/php/8.3/bin/php /var/www/vhosts/meine-shop-domain.de/httpdocs/bin/console scheduled-task:run --time-limit=120 --memory-limit=512M

[Install]
WantedBy=multi-user.target


Important: The correct directory and the corresponding system user for your Shopware 6 installation must also be specified for the Scheduled Tasks Service.


After the two services have been set up, they only need to be started:

systemctl start shopware_consumer
systemctl start shopware_scheduled_tasks

systemctl enable shopware_consumer
systemctl enable shopware_scheduled_tasks


Tip: If changes are subsequently made to one or more systemd services, the command systemctl daemon-reload must be executed for the recent changes to take effect.