How to setup Mattermost on Ubuntu 20.04 with PostgreSQL
A step-by-step tutorial
Mattermost is a self-hosted and open source alternative to Slack. It is a messaging platform that uses either PostgreSQL or MySQL in the backend (PostgreSQL will be used in this tutorial).
This tutorial will be taking you through the process of setting up Mattermost on Ubuntu 20.04. The process is not complicated, and I have added explanations (and things you should note) for each step to help you through it :)
Before starting, you should first update and upgrade Ubuntu.
sudo apt update
sudo apt updgrade
After, you should reboot the system by running:
reboot
First, you want to download PostgreSQL by running the command below:
sudo apt -y install postgresql postgresql-contrib
Once it finishes downloading, run the following command to login into the PostgreSQL database:
sudo --login --user postgres
Next, what you want to do is write a series of commands. *Note, copy all commands as is, so make sure the commands are in uppercase and that there is a ';' at the end of each statement (I left both parts out on my first attempt at running mattermost… it didn't work :p).
psql
CREATE DATABASE mattermost;
After running the command above, you should see printed on the command-line that the database was created. You can run \l to see all databases, and you should see the mattermost database there.
CREATE USER <username> WITH PASSWORD '<password>';
You should see that the role was created. Use your own username and password, and make sure you remember the username and password you chose for the user, as it will be used for future configurations.
GRANT ALL PRIVILEGES ON DATABASE mattermost to <username>;
\q
The last command exits the psql CLI.
Run the following command to exit the PostgreSQL account:
exit
Next, we want to add a new user to run our mattermost instance (the user is named mattermost):
sudo useradd --system --user-group mattermost
We are then going to install Mattermost by running the following command (here, version 5.37.0 is being installed, check the most recent version of Mattermost at the time you are installing it).
wget https://releases.mattermost.com/5.37.0/mattermost-5.37.0-linux-amd64.tar.gz
You want to then extract the archive and move it to the /opt folder
tar xvf mattermost-5.37.0-linux-amd64.tar.gz
sudo mv mattermost /opt
You should then create the storage directory for files that are posted to Mattermost
sudo mkdir /opt/mattermost/data
Give the mattermost user ownership and permission to the directory:
sudo chown -R mattermost:mattermost /opt/mattermost
Give the write permission to the mattermost user as well:
sudo chmod -R g+w /opt/mattermost
Next, you have to go into the configuration file that is stored in the directory:
sudo nano /opt/mattermost/config/config.json
In this file, you'll find keys with certain key-value pairs. There are four values you should change in your configuration of Mattermost. * Note, I am attempting to run Mattermost locally, and so that is why I am using 'localhost:8065'. You should change the values below depending on what domain name you want Mattermost to run on.
["ServiceSettings"]["SiteURL"] = "http://localhost:8065"
["ServiceSettings"]["ListenAddress"] = "localhost:8065"
Next, you want to rewrite a value so that Mattermost is using PostgreSQL in the backend.
["SqlSettings"]["DriverName"] = "postgres"
Make sure you put in the value below for the "Datasource" key. Make sure to put in your username and password where prompted. Also, make sure to keep the port in this URL as 5432 because otherwise, it'll give you a "failing to ping database" message after starting Mattermost.
["SqlSettings"]["DataSource"] = "postgres://<username>:<password>@localhost:5432/mattermost?sslmode=disable\u0026connect_timeout=10"
You should save and close the file after modifying the values.
Next, you have to go into the mattermost.service file by running the command below.
sudo nano /etc/systemd/system/mattermost.service
Copy and paste the below code snippet into the file.
[Unit]
Description=Mattermost
After=network.target
After=postgresql.service
Requires=postgresql.service
[Service]
Type=notify
ExecStart=/opt/mattermost/bin/mattermost
TimeoutStartSec=3600
Restart=always
RestartSec=10
WorkingDirectory=/opt/mattermost
User=mattermost
Group=mattermost
LimitNOFILE=49152
[Install]
WantedBy=multi-user.target
Save and close the file.
Then run the following commands to start and enable Mattermost.
sudo systemctl daemon-reload
sudo systemctl start mattermost.service
sudo systemctl enable mattermost.service
After running the commands above, you can check the status of Mattermost by running:
sudo systemctl mattermost.service
Outputted onto the screen, you can see the status of Mattermost. If it is running (active), then you can navigate to Mattermost using your own domain name and you should see Mattermost running there!