Upgrading Mautic from v3 to v4

In a previous post we showed you how to upgrade Mautic from v2 to v3. In this tutorial, we will go through the steps you need to take to upgrade Mautic from v3 to v4.

If you are running Mautic on a VPS or in a LXC/LXD container, it is highly recommended to take a snapshot before starting the upgrade process. This way you can always roll back to the old version if something goes wrong.

We assume here that you are logged in as root and do not have to use “sudo” every time a command needs to be executed.If you are not yet root, switch now.

# sudo -i

Make sure the system is up-to-date and that all packages are fully upgraded.

# apt update && apt upgrade -y

Check if your server is running php 7.4 which is required by Mautic v4.

# php -v
PHP 7.3.33-8 (cli) (built: Oct 28 2022 18:25:16) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.33, Copyright (c) 1998-2018 Zend Technologies

In our case we need to upgrade to php7.4.

If your distribution does not support php7.4 by default, you must first add Ondřej Surý’s PPA repository.

# add-apt-repository ppa:ondrej/php

And run a package update.

# apt update

Now install the required packages.

# apt install libapache2-mod-php7.4 php7.4 unzip php7.4-xml php7.4-mysql php7.4-imap php7.4-zip php7.4-intl php7.4-curl php7.4-gd php7.4-mbstring php7.4-bcmath ntp php7.4-imap -y

Modify the php.ini file for php7.4 to meet the requirements of Mautic.

vi /etc/php/7.4/apache2/php.ini
file_uploads = On
allow_url_fopen = On
short_open_tag = On
memory_limit = 512M
upload_max_filesize = 256M
max_execution_time = 360
post_max_size = 256M
date.timezone = Europe/Amsterdam # Change to your local timezone

Enable php7.4

# a2enmod php7.4

And disable php7.3

# a2dismod php7.3

Restart Apache for the changes to take effect.

# systemctl restart apache2

Check the php version.

# php -v
PHP 7.4.30 (cli) (built: Aug  1 2022 15:06:03) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.30, Copyright (c), by Zend Technologies

We can now start the upgrade process of Mautic.

If you haven’t already done so, create an alias “mtc” that replaces the long command “sudo -Hu www-data php /var/www/mautic/app/console” so we don’t have to enter it in full each time. If your mautic root directory is not “/var/www/mautic” modify it accordingly.

# vi .bashrc

And add the code at the end of the file.

alias mtc='sudo -Hu www-data php /var/www/mautic/bin/console'

Implement new settings running “exec bash”.

# exec bash

Check if the alias is working.

# mtc --version

Which should give you the current version of Mautic.

Mautic version 3.2.5 - app/prod

Even if you have taken a snapshot, it is always advisable to make a dump of the database before starting the upgrade.

# mysqldump mautic > mautic-$(date +"%m-%d-%y").sql

Time to upgrade to the latest v4 version.

# mtc mautic:update:find
Version 4.4.2 of Mautic is available for download. Please visit https://github.com/mautic/mautic/releases/tag/4.4.2 for more information.
To update, you can run 'php bin/console mautic:update:apply' from the command line.

Apply the update.

# mtc mautic:update:apply
# Are you sure you wish to update Mautic to the latest version?

To continue enter “y” and enter.

Are you sure you wish to update Mautic to the latest version? y

To finish the process run the command again adding “–finish”

# mtc mautic:update:apply --finish

Create or edit the file with a bash script that performs the procedure for assigning proper file and directory permissions and greatly simplifies this procedure.

# vi /usr/local/sbin/mtc_permissions.sh

cd /var/www/mautic
find . -type d -exec chmod 755 {} \;
find . -type f -exec chmod 644 {} \;
chmod -R g+w var/cache/
chmod -R g+w var/logs/
chmod -R g+w app/config/
chmod -R g+w media/files/
chmod -R g+w media/images/
chmod -R g+w translations/
chown -R www-data:www-data .

Make this file executable.

# chmod +x /usr/local/sbin/mtc_permissions.sh

Now run it.

# /usr/local/sbin/mtc_permissions.sh

Delete the cache directory.

# rm -rf /var/www/mautic/var/cache/

Clear cache.

# mtc cache:clear

You should now be running the latest version.

If you are running Mautic on a VPS or LXC/LXD container you might want to take another snapshot.

Similar Posts