Jump toUpdate content
Installing Mailtrain on an Ubuntu Instance
- compute
- newsletter
- Mailtrain
- Ubuntu
Mailtrain is a self hosted open-source newsletter application built in Node.js and using MariaDB as datastore.
- You have an account and are logged into the Scaleway console
- You have configured your SSH key
- You have created an Instance running Ubuntu Bionic or later
- You have sudo privileges or access to the root user
- You have unlocked the SMTP ports
Preparing the Server
Connect to your server via SSH.
Install sudo on the server:
apt install sudo
Create a regular user account with sudo access and switch into it. Replace my_new_user with your user name.
adduser my_new_user --gecos "My New User"
usermod -aG sudo my_new_user
su - my_new_userUpdate the already installed software on the system to the latest version:
sudo apt update && apt upgrade -y
Install the required tools:
sudo apt install -y build-essential unzip
Install Node.js and NPM:
sudo curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt install -y nodejs
Installing Postfix
To send Mail from your server, a SMTP server is required.
Install Postfix via apt:
sudo apt install -y postfix
When asked choose Internet Site in the list of options:
Enter the mail name for your instance and confirm. The mail name should be a fully qualified domain name (FQDN) resolving to your Instances IP address:
Installing MariaDB
Download the MariaDB repositories to make sure you have the latest version of the database engine available:
sudo curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash
Install MariaDB, an open source MySQL fork:
sudo apt install -y mariadb-server
Initialize the MariaDB server:
sudo mysql_secure_installation
Connect to the database server as root user with the password you have set previously:
mysql -u root -p
Create an empty database for Mailtrain:
CREATE DATABASE mailtrain;
Create a database user for Mailtrain and grant all privileges of the database to the user. Then create another user with read-only access to the database:
GRANT ALL PRIVILEGES ON mailtrain.* TO mailtrain_user@localhost IDENTIFIED BY 'user_password';
GRANT SELECT ON mailtrain.* TO mt_readonly@localhost IDENTIFIED BY 'readonly_password';Note:Replace the demo user and password in the commands above with your preferred username and password.
Flush the new privileges and leave the database prompt:
FLUSH PRIVILEGES;
EXIT;
Installing Nginx
Install nginx via apt:
sudo apt install -y nginx
Create three configuration files for nginx:
sudo nano /etc/nginx/sites-available/mailtrain.example.com.conf
sudo nano /etc/nginx/sites-available/sbox-mailtrain.example.com.conf
sudo nano /etc/nginx/sites-available/newsletter.example.com.confand put the following content into it:
mailtrain.example.com.conf
server {
listen [::]:80;
listen 80;
server_name mailtrain.example.com;
charset utf-8;
client_max_body_size 50M;
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_next_upstream error timeout http_502 http_503 http_504;
}
}sbox-mailtrain.example.com.conf
server {
listen 80;
server_name sbox-mailtrain.example.com;
access_log /var/log/nginx/sbox-mailtrain.access;
error_log /var/log/nginx/sbox-mailtrain.error;
location / {
proxy_pass http://127.0.0.1:3003;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}newsletter.example.com.conf*
server {
listen 80;
server_name newsletter.example.com;
access_log /var/log/nginx/newsletter.access;
error_log /var/log/nginx/newsletter.error;
location / {
proxy_pass http://127.0.0.1:3004;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Activate the configuration by linking the files in the sites-enabled directory:
sudo ln -s /etc/nginx/sites-available/mailtrain.example.com.conf /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/sbox-mailtrain.example.com.conf /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/newsletter.example.com.conf /etc/nginx/sites-enabled/Reload the nginx configuration to enable the new virtual host:
sudo systemctl reload nginx.service
Remember to replace mailtrain.example.com
with the domain name you want to use for your Mailtrain installation. Optionally, you can encrypt the HTTP traffic by using a free SSL certificate from Let’s Encrypt.
Installing Mailtrain
Enter the
www
directory:cd /var/www/
Download and unzip Mailtrain:
wget https://github.com/Mailtrain-org/mailtrain/archive/refs/tags/v2.20210609.0.zip
unzip v2.20210609.0.zip
rm v2.20210609.0.zip
mv mailtrain-2.20210609.0 mailtrainChange the ownership of the directory to your Mailtrain user:
chown -R my_new_user:my_new_user /var/www/mailtrain
Enter the Mailtrain directory:
cd mailtrain
Import the database scheme into the Mailtrain database using the following command. Remember to replace
mailtrain_user
with your database user.mysql -u mailtrain_user -p mailtrain < ./server/setup/sql/mailtrain.sql
Create a
production.yaml
file by opening it in a text editor. This file allows you to overwrite the default configuration values in the file./server/config/default.yaml
.nano ./server/config/production.yaml
Copy the following configuration into the file and edit it towards your requirements. Then save the file and exit the text editor:
user: my_new_user
group: my_new_user
roUser: nobody
roGroup: nobody
www:
host: 127.0.0.1
proxy: true
secret: "Enter secret and random text here"
trustedUrlBase: http://mailtrain.example.com
sandboxUrlBase: http://sbox-mailtrain.example.com
publicUrlBase: http://newsletter.example.com
mysql:
user: mailtrain_user
password: user_password
database: mailtrain
redis:
enabled: true
log:
level: info
builtinZoneMTA:
enabled: false
queue:
processes: 5Note:The Mailtrain configuration requires you to specify the following URL endpoints.
mailtrain.example.com
: The configuration interface for logged-in users.sbox-mailtrain.example.com
: This is used to host template editors. This URL is not shown to any user.newsletter.example.com
: The public URL as seen by visitors.
Create a configuration file for the report worker by opening it in a text editor like nano.
sudo nano ./server/services/workers/reports/config/production.yaml
Copy the following configuration in the editor, save the file and exit.
log:
level: warn
mysql:
user: mailtrain_user
password: user_password
database: mailtrainInstall the required Node packages by running the following command:
for idx in client shared server mvis/client mvis/server mvis/test-embed mvis/ivis-core/client mvis/ivis-core/server mvis/ivis-core/shared mvis/ivis-core/embedding; do
(cd $idx && npm install)
doneTip:Do not use
sudo
when running the command above.
Building Mailtrain
Change into the
client
directory from themailtrain
main directory:cd client
Run the following command to build the application:
npm run build
Set the permissions of the
config
directory:sudo chmod o-rwx /var/www/mailtrain/server/config/
Create a
systemd
service file for Mailtrain by opening it in a text editor like nano:sudo nano /etc/systemd/system/mailtrain.service
Copy the following configuration into the file, save it and exit the text editor:
[Unit]
Description=Mailtrain server
After=syslog.target network.target mariadb.service redis-server.service
[Service]
Environment="NODE_ENV=production"
WorkingDirectory=/var/www/mailtrain/server
ExecStart=/usr/bin/node index.js
Type=simple
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.targetEnable and start the Mailtrain service:
sudo systemctl enable --now mailtrain.service
Accessing the Mailtrain Webinterface
Open your web browser and type the domain name of your Mailtrain instance.
Click Login in the top bar to connect to Mailtrain.
Login with the username
admin
and passwordtest
. Don’t forget to change the password after your first login.To send mails from Mailtrain, you have to specify the SMTP server configuration like following:
You have now specified other parameters like your company name, service URL etc. before starting your first e-marketing campaign. For more information on how to plan your campaigns, refer to the official documentation.
Encrypt connections to your Mailtrain, by enabling HTTPS and by using a Let’s Encrypt TLS certificate.