Credence Analytics - Mattermost Installation on EC2
By Vijay Simha on May 28, 2022
IntermediateInstall Ubuntu Server 20.04
- To install Ubuntu Server 20.04, see the Ubuntu Installation Guide.
- After the system is installed, make sure that it’s up to date with the most recent security patches. Open a terminal window and issue the following commands:
sudo apt update
sudo apt upgrade
Now that the system is up to date, you can start installing the components that make up a Mattermost system.
Install MySQL database server
Install and set up the database for use by the Mattermost server. You can install either MySQL or PostgreSQL.
Install MySQL on Ubuntu Server 20.04
- Log into the server that will host the database, and open a terminal window.
- Install MySQL.
sudo apt install mysql-server
- Run
sudo mysql_secure_installation
and follow the instructions.
This does not run. As root password has to be changed. run
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'mynewpassword';
And then you can run mysql_secure_installation
- Log in to MySQL as root.
sudo mysql
- Create the Mattermost user mmuser.
mysql> create user 'mmuser'@'%' identified by 'mmuser-password';
Note
- Use a password that is more secure than ‘mmuser-password’.
- The ‘%’ means that mmuser can connect from any machine on the network. However, it’s more secure to use the IP address of the machine that hosts Mattermost. For example, if you install Mattermost on the machine with IP address 10.10.10.2, then use the following command:
mysql> create user 'mmuser'@'10.10.10.2' identified by 'mmuser-password';
- Create the Mattermost database.
mysql> create database mattermost;
- Grant access privileges to the user mmuser.
mysql> grant all privileges on mattermost.* to 'mmuser'@'%';
Note
This query grants the MySQL user we just created all privileges on the database for convenience. If you need more security you can use this query to grant the user only the privileges necessary to run Mattermost.
mysql> GRANT ALTER, CREATE, DELETE, DROP, INDEX, INSERT, SELECT, UPDATE, REFERENCES ON mattermost.* TO 'mmuser'@'%';
- Log out of MySQL.
mysql> exit
With the database installed and the initial setup complete, you can now install the Mattermost server.
Install Mattermost Server
Install Mattermost Server on a 64-bit machine.
Assume that the IP address of this server is 10.10.10.2.
Install Mattermost Server on Ubuntu
- Log in to the server that will host Mattermost Server and open a terminal window.
- Download the latest version of the Mattermost Server. In the following command, replace
X.X.X
with the version that you want to download:
wget https://releases.mattermost.com/X.X.X/mattermost-X.X.X-linux-amd64.tar.gz
- Extract the Mattermost Server files.
tar -xvzf mattermost*.gz
- Move the extracted file to the
/opt
directory.
sudo mv mattermost /opt
- Create the storage directory for files.
sudo mkdir /opt/mattermost/data
Note
The storage directory will contain all the files and images that your users post to Mattermost, so you need to make sure that the drive is large enough to hold the anticipated number of uploaded files and images.
- Set up a system user and group called
mattermost
that will run this service, and set the ownership and permissions. - Create the Mattermost user and group:
sudo useradd --system --user-group mattermost
- Set the user and group mattermost as the owner of the Mattermost files:
sudo chown -R mattermost:mattermost /opt/mattermost
- Give write permissions to the mattermost group:
sudo chmod -R g+w /opt/mattermost
- Set up the database driver in the file
/opt/mattermost/config/config.json
. Open the file in a text editor and make the following changes: - If you are using PostgreSQL:
- Set
"DriverName"
to"postgres"
- Set
"DataSource"
to the following value, replacing<mmuser-password>
and<host-name-or-IP>
with the appropriate values: "postgres://mmuser:<mmuser-password>@<host-name-or-IP>:5432/mattermost?sslmode=disable&connect_timeout=10"
.- If you are using MySQL:
- Set
"DriverName"
to"mysql"
- Set
"DataSource"
to the following value, replacing<mmuser-password>
and<host-name-or-IP>
with the appropriate values. Also make sure that the database name ismattermost
instead ofmattermost_test
:
"mmuser:<mmuser-password>@tcp(<host-name-or-IP>:3306)/mattermost?charset=utf8mb4,utf8&writeTimeout=30s"
- Also set
"SiteURL"
to the full base URL of the site (e.g."https://mattermost.example.com"
). - Test the Mattermost server to make sure everything works.
- Change to the
mattermost
directory:
cd /opt/mattermost
- Start the Mattermost server as the user mattermost:
sudo -u mattermost ./bin/mattermost
When the server starts, it shows some log information and the text Server is listening on :8065
. You can stop the server by pressing CTRL+C in the terminal window.
- Set up Mattermost to use systemd for starting and stopping.
- Create a systemd unit file:
sudo touch /lib/systemd/system/mattermost.service
- Open the unit file as root in a text editor, and copy the following lines into the file:
[Unit]Description=MattermostAfter=network.targetAfter=postgresql.serviceBindsTo=postgresql.service[Service]Type=notifyExecStart=/opt/mattermost/bin/mattermostTimeoutStartSec=3600KillMode=mixedRestart=alwaysRestartSec=10WorkingDirectory=/opt/mattermostUser=mattermostGroup=mattermostLimitNOFILE=49152[Install]WantedBy=postgresql.service
Note
If you are using MySQL, replace postgresql.service
with mysql.service
in 2 places in the [Unit]
section and 1 place in the [Install]
section.
Note
If you have installed MySQL or PostgreSQL on a dedicated server, then you need to:
- Remove
After=postgresql.service
andBindsTo=postgresql.service
orAfter=mysql.service
andBindsTo=mysql.service
lines in the[Unit]
section - Replace the
WantedBy=postgresql.service
orWantedBy=mysql.service
line in the[Install]
section withWantedBy=multi-user.target
or the Mattermost service will not start.
Note
Setting WantedBy
to your local database service ensures that whenever the database service is started, the Mattermost server starts too. This prevents the Mattermost server from stopping to work after an automatic update of the database.
- Make systemd load the new unit.
sudo systemctl daemon-reload
- Check to make sure that the unit was loaded.
sudo systemctl status mattermost.service
You should see an output similar to the following:
● mattermost.service - MattermostLoaded: loaded (/lib/systemd/system/mattermost.service; disabled; vendor preset: enabled)Active: inactive (dead)
- Start the service.
sudo systemctl start mattermost.service
- Verify that Mattermost is running.
curl http://localhost:8065
You should see the HTML that’s returned by the Mattermost server.
- Set Mattermost to start on machine start up.
sudo systemctl enable mattermost.service
Now that the Mattermost server is up and running, you can do some initial configuration and setup.
More articles on Technical