Deployment Overview of Element on Server¶
Prerequisites and Basic Requirements¶
The deployment requires a Linux server running Ubuntu with root privileges. The system must have internet access to download Docker images and SSL certificates. The following components are required:
- Operating System: Ubuntu (specific version codename is detected dynamically during installation).
- Privileges: Root access or
sudoprivileges are required to install Docker and manage system services. - Domain: A valid domain name (
matrix_domain) must be configured and pointed to the server's IP address. - Ports:
- Port
8008: Internal Synapse API. - Port
8080: Internal Element Web interface. - Port
443: HTTPS traffic for the public interface. - Port
8448: Alternative HTTPS port for Matrix federation.
File and Directory Structure¶
The application utilizes a specific directory structure to separate configuration, data, and certificates. The base directory for the Matrix stack is defined as matrix_base_dir (typically /data/matrix or similar, depending on the variable assignment).
- Base Directory:
{{ matrix_base_dir }} - Synapse Data:
{{ matrix_base_dir }}/files- Stores media, signing keys, and the Synapse configuration file (
homeserver.yaml). - Owned by user ID
991and group ID991.
- Stores media, signing keys, and the Synapse configuration file (
- Database Schemas:
{{ matrix_base_dir }}/schemas- Stores PostgreSQL schema definitions.
- Element Configuration:
{{ matrix_base_dir }}/element-config.json- Contains the JSON configuration for the Element web client.
-
Docker Compose:
{{ matrix_base_dir }}/docker-compose.yml- Defines the services for Synapse, Element, and the database.
-
Nginx and SSL Directory:
/root/nginx - Compose File:
/root/nginx/compose.yml- Defines the Nginx and Certbot stack.
- User Configuration:
/data/nginx/user_conf.d/- Contains the specific server block configuration for the Matrix domain (
{{ matrix_domain }}.conf).
- Contains the specific server block configuration for the Matrix domain (
- SSL Secrets:
/etc/letsencrypt- Managed by the
nginx_secretsvolume for storing Let's Encrypt certificates.
- Managed by the
Application Installation Process¶
The deployment is performed using Docker Compose to orchestrate the Matrix homeserver (Synapse), the Element web client, and the PostgreSQL database.
- Docker Installation: If Docker is not present, the system installs the following packages:
docker-cedocker-ce-clicontainerd.iodocker-buildx-plugin-
docker-compose-plugin -
Docker Daemon Configuration: The Docker daemon is configured with an empty JSON object in
/etc/docker/daemon.jsonto ensure default settings are applied. -
Service Deployment: The application stack is deployed using the
docker-compose.ymlfile located in the base directory. The process involves: - Pulling the latest images for
postgres:15-alpine,matrixdotorg/synapse:latest, andvectorim/element-web:latest. - Creating the necessary volumes and networks.
-
Starting the containers in the order defined by dependencies (Database first, then Synapse, then Element).
-
Health Check: After startup, the system verifies the Synapse service by checking the health endpoint at
http://127.0.0.1:8008/healthuntil it returns a200status code.
Docker Containers and Their Deployment¶
The application consists of three primary containers managed via Docker Compose:
- Database (
db): - Image:
postgres:15-alpine - Environment Variables:
POSTGRES_USER:synapsePOSTGRES_PASSWORD:synapsePOSTGRES_INITDB_ARGS:--encoding=UTF-8 --lc-collate=C --lc-ctype=C
-
Volume:
postgres-datamounted to/var/lib/postgresql/data. -
Synapse (
synapse): - Image:
matrixdotorg/synapse:latest - Configuration Path:
/data/homeserver.yaml - Volume:
./filesmounted to/data. - Port Mapping:
8008:8008(Internal only). -
Dependencies: Waits for the
dbcontainer. -
Element (
element): - Image:
vectorim/element-web:latest - Configuration:
./element-config.jsonmounted to/app/config.json. - Port Mapping:
127.0.0.1:8080:80(Bound to localhost only).
Proxy Servers¶
Access to the application is provided through an Nginx reverse proxy with SSL termination managed by Certbot.
- Nginx Stack:
- Image:
jonasal/nginx-certbot:latest - Deployment: Managed via
/root/nginx/compose.yml. - Network Mode:
host. - Environment:
CERTBOT_EMAIL:[email protected]- Configuration loaded from
/data/nginx/nginx-certbot.env.
-
Volumes:
nginx_secretsmounted to/etc/letsencrypt./data/nginx/user_conf.dmounted to/etc/nginx/user_conf.d./usr/share/element-webmounted read-only to/usr/share/element-web.
-
Server Configuration: The Nginx configuration file (
{{ matrix_domain }}.conf) listens on ports443and8448for SSL traffic. - SSL Certificates:
- Certificate:
/etc/letsencrypt/live/{{ matrix_domain }}/fullchain.pem - Key:
/etc/letsencrypt/live/{{ matrix_domain }}/privkey.pem - Chain:
/etc/letsencrypt/live/{{ matrix_domain }}/chain.pem
- Certificate:
- Routing:
- Requests to
/_matrixor/_synapse/clientare proxied tohttp://127.0.0.1:8008(Synapse). - All other requests are proxied to
http://127.0.0.1:8080(Element).
- Requests to
- Headers:
Host,X-Forwarded-For, andX-Forwarded-Protoare set to ensure proper request handling.client_max_body_sizeis set to50Mfor Matrix API requests.
Databases¶
The application uses PostgreSQL version 15 for data storage.
- Connection Method: Synapse connects to the database via the internal Docker network using the hostname
db. - Database Name:
synapse - User:
synapse - Password:
synapse - Storage: Data is persisted in the Docker volume
postgres-data, which maps to the host filesystem. - Connection Pooling:
- Minimum connections (
cp_min):5 - Maximum connections (
cp_max):10
Permission Settings¶
File and directory permissions are strictly defined to ensure security and proper operation of the containers.
- Base Directory:
{{ matrix_base_dir }} - Owner:
root - Group:
root -
Mode:
0755 -
Synapse Data Directory:
{{ matrix_base_dir }}/files - Owner:
991(Synapse user) - Group:
991 -
Mode:
0755(Recursive) -
Schema Directory:
{{ matrix_base_dir }}/schemas - Owner:
root - Group:
root -
Mode:
0755 -
Configuration Files:
homeserver.yaml: Owned by991:991, Mode0644.element-config.json: Owned byroot:root, Mode0644.-
docker-compose.yml: Owned byroot:root, Mode0644. -
Nginx Configuration:
/root/nginx: Owned byroot:root, Mode0755./root/nginx/compose.yml: Owned byroot:root, Mode0644./data/nginx/user_conf.d/{{ matrix_domain }}.conf: Owned byroot:root, Mode0644.
Starting, Stopping, and Updating¶
The services are managed using Docker Compose commands executed from the respective project directories.
-
Starting the Matrix Stack: Execute the following command from the base directory:
-
Starting the Nginx/Certbot Stack: Execute the following command from the
/root/nginxdirectory: -
Updating Images: To pull the latest versions of the containers without recreating them immediately:
To recreate containers with the new images: -
Stopping Services: To stop the Matrix stack:
To stop the Nginx stack: -
Cleaning Docker Resources: The deployment process includes commands to clean the builder cache and system resources: