How to Install Mautic and Mailgun on Ubuntu 18.04

9 November 2018 / DevOps

Mautic is an open source marketing automation softwares and an excellent alternative to cloud marketing platforms such as Marketo. This tutorial will explain how to install Mautic on Ubuntu 18.04 and configure Postfix to use multiple domains in Mailgun.

Prerequisites

This tutorial assumes that you are running Ubuntu 14.04, 16.04, or 18.04, and that you have a domain in a domain name registrar such as GoDaddy or Namecheap.

Email Service: Mailgun

If you do not already use a transactional email service, I recommend using Mailgun, which allows you to send emails up to 10,000 free emails per month. You are free to use other services such as SendGrid.

Install Postfix

Postfix is responsible for relaying emails to Mailgun. Install it by issuing:

sudo apt-get install -y postfix

When prompted to select the mail server configuration type, select satellite system. System mail name can be left as the default value, since we will not be using it. In SMTP relay host, type [smtp.mailgun.org:587].

We are now going to create a text file called sasl_passwd that will store our Mailgun SMTP credentials. Create a file under /etc/postfix/sasl_passwd and add the following text:

@email.yourdomain.com   postmaster@email.yourdomain.com:password
# default
[smtp.mailgun.org]:587  @email.yourdomain.com   postmaster@email.yourdomain.com:password

The first line instructs Postfix to use the credentials postmaster@email.yourdomain.com:password when the sender is @email.yourdomain.com. You can repeat this line for all your domains/subdomains.

The final line is the credential for our default relay host (Mailgun).

After saving the file, issue the following commands:

chmod 600 /etc/postfix/sasl_passwd
postmap /etc/postfix/sasl_passwd

Let's now create the file that will instruct Postfix to use the correct SMTP address for each domain. Edit the text file /etc/postfix/relayhost_map:

@email.yourdomain.com    [smtp.mailgun.org]:587

You must add a line for every domain you have. If you are using a different email service, change the domain and port accordingly.

Once you're done editing, run:

postmap /etc/postfix/relayhost_map

Edit your Postifx configuration at /etc/postfix/main.cf and add the following lines above at the bottom of the file:

smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_auth_enable = yes
sender_dependent_relayhost_maps = hash:/etc/postfix/relayhost_map
smtp_sender_dependent_authentication = yes
smtp_sasl_security_options = noanonymous

These lines will instruct Postfix to use our sasl_passwd and relayhost_map files. Save and close. Restart Postfix:

systemctl restart postfix

Postfix is now ready to send emails! In the next section we discuss how to install and configure Mautic.

Install Mautic

Before we install Mautic, make sure PHP has the following extensions:

sudo apt-get install php7.0-zip php7.0-xml php7.0-mcrypt php7.0-imap php7.0-intl -y

Head over to Mautic Downloads to download the latest version of Mautic (as of this writing, the latest version is 2.13.1).

Download and unzip the archive to your web server. Make sure that the web server has permission to write to the files. If you are using nginx, add the following lines to your virtual host file:

# Support Clean (aka Search Engine Friendly) URLs
location / {
    try_files $uri $uri/ /index.php?$args;
}

Create a database for Mautic in MySQL or MariaDB:

CREATE DATABASE mautic;

Open your browser and enter the address to your Mautic installation. The installation wizard will ask you for database and admin credentials. When prompted to select a mailer transport, select Sendmail. Select Queue instead of Sent Immediately.

You are almost done! The last step is to set cron jobs. Mautic is a CPU and disk intensive application, so it is best to use cronjobs for heavy workloads like importing CSVs or sending emails. The following is a comprehensive list of cronjobs that works well in production. You must edit the path to fit your environment. Make sure to edit a crontab that has permission to your mautic files:

*/10 * * * * /usr/bin/php /PATH/TO/MAUTIC/app/console mautic:segments:update --batch-limit=50000
15 * * * * /usr/bin/php /PATH/TO/MAUTIC/app/console mautic:campaigns:rebuild
30 * * * * /usr/bin/php /PATH/TO/MAUTIC/app/console mautic:campaigns:trigger
* * * * * /usr/bin/php /PATH/TO/MAUTIC/app/console mautic:emails:send
* * * * * /usr/bin/php /PATH/TO/MAUTIC/app/console mautic:import
* * * * * /usr/bin/php /PATH/TO/MAUTIC/app/console mautic:emails:fetch 
15 * * * * /usr/bin/php /PATH/TO/MAUTIC/app/console mautic:social:monitoring
0 0 * * * /usr/bin/php /PATH/TO/MAUTIC/app/console mautic:iplookup:download

That's it! Your Mautic installation is now ready for email campaigns! If you have any questions, feel free to drop me a message.

Manuel Figueroa

Full Stack Developer

Thank you for reading my blog. Please feel free to contact me or read my other content.

Manuel Figueroa