mailcow Installation Guide

mailcow is a free and open-source Mail Server Stack. Dockerized email server with modern web UI

Mail stacks🟡 intermediate10 min⏱️ 15-30 minutes

mailcow is a free and open-source Mail Server Stack. Dockerized email server with modern web UI

1. Prerequisites

  • Hardware Requirements:
  • CPU: 2 cores minimum (4+ cores recommended)
  • RAM: 2GB minimum (4GB+ recommended)
  • Storage: 1GB for installation
  • Network: 443 ports
  • Operating System:
  • Linux: Any modern distribution (RHEL, Debian, Ubuntu, CentOS, Fedora, Arch, Alpine, openSUSE)
  • macOS: 10.14+ (Mojave or newer)
  • Windows: Windows Server 2016+ or Windows 10
  • FreeBSD: 11.0+
  • Network Requirements:
  • Port 443 (default mailcow port)
  • Dependencies:
  • docker, docker-compose
  • System Access: root or sudo privileges required
  • 2. Supported Operating Systems

    This guide supports installation on:

  • RHEL 8/9 and derivatives (CentOS Stream, Rocky Linux, AlmaLinux)
  • Debian 11/12
  • Ubuntu 20.04/22.04/24.04 LTS
  • Arch Linux (rolling release)
  • Alpine Linux 3.18+
  • openSUSE Leap 15.5+ / Tumbleweed
  • SUSE Linux Enterprise Server (SLES) 15+
  • macOS 12+ (Monterey and later)
  • FreeBSD 13+
  • Windows 10/11/Server 2019+ (where applicable)
  • 3. Installation

    RHEL/CentOS/Rocky Linux/AlmaLinux

    bash
    # Install EPEL repository if needed
    sudo dnf install -y epel-release
    
    # Install mailcow
    sudo dnf install -y mailcow docker, docker-compose
    
    # Enable and start service
    sudo systemctl enable --now mailcow
    
    # Configure firewall
    sudo firewall-cmd --permanent --add-service=mailcow
    sudo firewall-cmd --reload
    
    # Verify installation
    mailcow --version || systemctl status mailcow

    Debian/Ubuntu

    bash
    # Update package index
    sudo apt update
    
    # Install mailcow
    sudo apt install -y mailcow docker, docker-compose
    
    # Enable and start service
    sudo systemctl enable --now mailcow
    
    # Configure firewall
    sudo ufw allow 443
    
    # Verify installation
    mailcow --version || systemctl status mailcow

    Arch Linux

    bash
    # Install mailcow
    sudo pacman -S mailcow
    
    # Enable and start service
    sudo systemctl enable --now mailcow
    
    # Verify installation
    mailcow --version || systemctl status mailcow

    Alpine Linux

    bash
    # Install mailcow
    apk add --no-cache mailcow
    
    # Enable and start service
    rc-update add mailcow default
    rc-service mailcow start
    
    # Verify installation
    mailcow --version || rc-service mailcow status

    openSUSE/SLES

    bash
    # Install mailcow
    sudo zypper install -y mailcow docker, docker-compose
    
    # Enable and start service
    sudo systemctl enable --now mailcow
    
    # Configure firewall
    sudo firewall-cmd --permanent --add-service=mailcow
    sudo firewall-cmd --reload
    
    # Verify installation
    mailcow --version || systemctl status mailcow

    macOS

    bash
    # Using Homebrew
    brew install mailcow
    
    # Start service
    brew services start mailcow
    
    # Verify installation
    mailcow --version

    FreeBSD

    bash
    # Using pkg
    pkg install mailcow
    
    # Enable in rc.conf
    echo 'mailcow_enable="YES"' >> /etc/rc.conf
    
    # Start service
    service mailcow start
    
    # Verify installation
    mailcow --version || service mailcow status

    Windows

    powershell
    # Using Chocolatey
    choco install mailcow
    
    # Or using Scoop
    scoop install mailcow
    
    # Verify installation
    mailcow --version

    Initial Configuration

    Basic Configuration

    bash
    # Create configuration directory if needed
    sudo mkdir -p /opt/mailcow
    
    # Set up basic configuration
    sudo tee /opt/mailcow/mailcow.conf << 'EOF'
    # mailcow Configuration
    COMPOSE_PROJECT_NAME=mailcow
    EOF
    
    # Test configuration
    sudo mailcow -t || sudo mailcow configtest
    
    # Reload service
    sudo systemctl reload mailcow

    Security Hardening

    bash
    # Set appropriate permissions
    sudo chown -R mailcow:mailcow /opt/mailcow
    sudo chmod 750 /opt/mailcow
    
    # Enable security features
    # See security section for detailed hardening steps

    5. Service Management

    systemd (RHEL, Debian, Ubuntu, Arch, openSUSE)

    bash
    # Enable service
    sudo systemctl enable mailcow
    
    # Start service
    sudo systemctl start mailcow
    
    # Stop service
    sudo systemctl stop mailcow
    
    # Restart service
    sudo systemctl restart mailcow
    
    # Reload configuration
    sudo systemctl reload mailcow
    
    # Check status
    sudo systemctl status mailcow
    
    # View logs
    sudo journalctl -u mailcow -f

    OpenRC (Alpine Linux)

    bash
    # Enable service
    rc-update add mailcow default
    
    # Start service
    rc-service mailcow start
    
    # Stop service
    rc-service mailcow stop
    
    # Restart service
    rc-service mailcow restart
    
    # Check status
    rc-service mailcow status

    rc.d (FreeBSD)

    bash
    # Enable in /etc/rc.conf
    echo 'mailcow_enable="YES"' >> /etc/rc.conf
    
    # Start service
    service mailcow start
    
    # Stop service
    service mailcow stop
    
    # Restart service
    service mailcow restart
    
    # Check status
    service mailcow status

    launchd (macOS)

    bash
    # Using Homebrew services
    brew services start mailcow
    brew services stop mailcow
    brew services restart mailcow
    
    # Check status
    brew services list | grep mailcow

    Windows Service Manager

    powershell
    # Start service
    net start mailcow
    
    # Stop service
    net stop mailcow
    
    # Using PowerShell
    Start-Service mailcow
    Stop-Service mailcow
    Restart-Service mailcow
    
    # Check status
    Get-Service mailcow

    Advanced Configuration

    Performance Optimization

    bash
    # Configure performance settings
    cat >> /opt/mailcow/mailcow.conf << 'EOF'
    COMPOSE_PROJECT_NAME=mailcow
    EOF
    
    # Apply system tuning
    sudo sysctl -w net.core.somaxconn=65535
    sudo sysctl -w net.ipv4.tcp_max_syn_backlog=65535
    
    # Restart service
    sudo systemctl restart mailcow

    Clustering and High Availability

    bash
    # Configure clustering (if supported)
    # See official documentation for cluster setup
    
    # Basic load balancing setup example
    # Configure multiple instances on different ports

    Reverse Proxy Setup

    nginx Configuration

    nginx
    upstream mailcow_backend {
        server 127.0.0.1:443;
        server 127.0.0.1:{default_port}1 backup;
    }
    
    server {
        listen 80;
        server_name mailcow.example.com;
        return 301 https://$server_name$request_uri;
    }
    
    server {
        listen 443 ssl http2;
        server_name mailcow.example.com;
    
        ssl_certificate /etc/ssl/certs/mailcow.example.com.crt;
        ssl_certificate_key /etc/ssl/private/mailcow.example.com.key;
    
        location / {
            proxy_pass http://mailcow_backend;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            
            # WebSocket support (if needed)
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }

    Apache Configuration

    apache
    <VirtualHost *:80>
        ServerName mailcow.example.com
        Redirect permanent / https://mailcow.example.com/
    </VirtualHost>
    
    <VirtualHost *:443>
        ServerName mailcow.example.com
        
        SSLEngine on
        SSLCertificateFile /etc/ssl/certs/mailcow.example.com.crt
        SSLCertificateKeyFile /etc/ssl/private/mailcow.example.com.key
        
        ProxyRequests Off
        ProxyPreserveHost On
        
        ProxyPass / http://127.0.0.1:443/
        ProxyPassReverse / http://127.0.0.1:443/
        
        # WebSocket support (if needed)
        RewriteEngine on
        RewriteCond %{HTTP:Upgrade} websocket [NC]
        RewriteCond %{HTTP:Connection} upgrade [NC]
        RewriteRule ^/?(.*) "ws://127.0.0.1:443/$1" [P,L]
    </VirtualHost>

    HAProxy Configuration

    haproxy
    frontend mailcow_frontend
        bind *:80
        bind *:443 ssl crt /etc/ssl/certs/mailcow.pem
        redirect scheme https if !{ ssl_fc }
        default_backend mailcow_backend
    
    backend mailcow_backend
        balance roundrobin
        option httpchk GET /health
        server mailcow1 127.0.0.1:443 check
        server mailcow2 127.0.0.1:{default_port}1 check backup

    Security Configuration

    Basic Security Setup

    bash
    # Set appropriate permissions
    sudo chown -R mailcow:mailcow /opt/mailcow
    sudo chmod 750 /opt/mailcow
    
    # Configure firewall
    sudo firewall-cmd --permanent --add-service=mailcow
    sudo firewall-cmd --reload
    
    # Enable SELinux policies (if applicable)
    sudo setsebool -P httpd_can_network_connect on
    
    # Configure fail2ban
    sudo tee /etc/fail2ban/jail.d/mailcow.conf << 'EOF'
    [mailcow]
    enabled = true
    port = 443
    filter = mailcow
    logpath = /var/log/mailcow/*.log
    maxretry = 5
    bantime = 3600
    EOF

    SSL/TLS Configuration

    bash
    # Generate SSL certificates
    sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
        -keyout /etc/ssl/private/mailcow.key \
        -out /etc/ssl/certs/mailcow.crt
    
    # Configure SSL in mailcow
    # See official documentation for SSL configuration

    Database Setup

    PostgreSQL Backend (if applicable)

    bash
    # Create database and user
    sudo -u postgres psql << EOF
    CREATE DATABASE mailcow_db;
    CREATE USER mailcow_user WITH ENCRYPTED PASSWORD 'secure_password';
    GRANT ALL PRIVILEGES ON DATABASE mailcow_db TO mailcow_user;
    EOF
    
    # Configure mailcow to use PostgreSQL
    # See official documentation for database configuration

    MySQL/MariaDB Backend (if applicable)

    bash
    # Create database and user
    sudo mysql << EOF
    CREATE DATABASE mailcow_db;
    CREATE USER 'mailcow_user'@'localhost' IDENTIFIED BY 'secure_password';
    GRANT ALL PRIVILEGES ON mailcow_db.* TO 'mailcow_user'@'localhost';
    FLUSH PRIVILEGES;
    EOF

    Performance Optimization

    System Tuning

    bash
    # Kernel parameters
    sudo tee -a /etc/sysctl.conf << EOF
    net.core.somaxconn = 65535
    net.ipv4.tcp_max_syn_backlog = 65535
    net.ipv4.ip_local_port_range = 1024 65535
    net.core.netdev_max_backlog = 5000
    vm.swappiness = 10
    EOF
    
    sudo sysctl -p
    
    # mailcow specific tuning
    COMPOSE_PROJECT_NAME=mailcow

    Resource Limits

    bash
    # Configure system limits
    sudo tee -a /etc/security/limits.conf << EOF
    mailcow soft nofile 65535
    mailcow hard nofile 65535
    mailcow soft nproc 32768
    mailcow hard nproc 32768
    EOF

    Monitoring

    Prometheus Integration

    yaml
    # prometheus.yml configuration
    scrape_configs:
      - job_name: 'mailcow'
        static_configs:
          - targets: ['localhost:443']
        metrics_path: '/metrics'

    Health Checks

    bash
    # Basic health check script
    #!/bin/bash
    if systemctl is-active --quiet mailcow; then
        echo "mailcow is running"
        exit 0
    else
        echo "mailcow is not running"
        exit 1
    fi

    Log Monitoring

    bash
    # Configure log rotation
    sudo tee /etc/logrotate.d/mailcow << 'EOF'
    /var/log/mailcow/*.log {
        daily
        rotate 14
        compress
        delaycompress
        missingok
        notifempty
        create 0640 mailcow mailcow
        postrotate
            systemctl reload mailcow > /dev/null 2>&1 || true
        endscript
    }
    EOF

    9. Backup and Restore

    Backup Script

    bash
    #!/bin/bash
    # mailcow backup script
    BACKUP_DIR="/backup/mailcow"
    DATE=$(date +%Y%m%d_%H%M%S)
    
    mkdir -p "$BACKUP_DIR"
    
    # Stop service (if required)
    systemctl stop mailcow
    
    # Backup configuration
    tar -czf "$BACKUP_DIR/mailcow-config-$DATE.tar.gz" /opt/mailcow
    
    # Backup data (adjust paths as needed)
    tar -czf "$BACKUP_DIR/mailcow-data-$DATE.tar.gz" /var/lib/mailcow
    
    # Start service
    systemctl start mailcow
    
    # Clean old backups (keep 30 days)
    find "$BACKUP_DIR" -name "*.tar.gz" -mtime +30 -delete
    
    echo "Backup completed: $BACKUP_DIR"

    Restore Procedure

    bash
    # Stop service
    sudo systemctl stop mailcow
    
    # Restore configuration
    sudo tar -xzf /backup/mailcow/mailcow-config-*.tar.gz -C /
    
    # Restore data
    sudo tar -xzf /backup/mailcow/mailcow-data-*.tar.gz -C /
    
    # Set permissions
    sudo chown -R mailcow:mailcow /opt/mailcow
    sudo chown -R mailcow:mailcow /var/lib/mailcow
    
    # Start service
    sudo systemctl start mailcow

    6. Troubleshooting

    Common Issues

    1. Service won't start:

    bash
    # Check logs
    sudo journalctl -u mailcow -n 100
    sudo tail -f /var/log/mailcow/*.log
    
    # Check configuration
    sudo mailcow -t || sudo mailcow configtest
    
    # Check permissions
    ls -la /opt/mailcow
    ls -la /var/lib/mailcow

    2. Connection refused:

    bash
    # Check if service is listening
    sudo ss -tlnp | grep 443
    sudo netstat -tlnp | grep 443
    
    # Check firewall
    sudo firewall-cmd --list-all
    sudo iptables -L -n
    
    # Test connection
    telnet localhost 443
    nc -zv localhost 443

    3. Performance issues:

    bash
    # Check resource usage
    top -p $(pgrep supervisord)
    htop -p $(pgrep supervisord)
    
    # Check connections
    ss -ant | grep :443 | wc -l
    
    # Monitor I/O
    iotop -p $(pgrep supervisord)

    Debug Mode

    bash
    # Run in debug mode
    sudo mailcow -d
    # or
    sudo mailcow debug
    
    # Increase log verbosity
    # Edit configuration to enable debug logging

    Integration Examples

    Docker Compose

    yaml
    version: '3.8'
    services:
      mailcow:
        image: mailcow:latest
        container_name: mailcow
        ports:
          - "443:443"
        volumes:
          - ./config:/opt/mailcow
          - ./data:/var/lib/mailcow
        environment:
          - mailcow_CONFIG=/opt/mailcow/mailcow.conf
        restart: unless-stopped
        networks:
          - mailcow_net
    
    networks:
      mailcow_net:
        driver: bridge

    Kubernetes Deployment

    yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: mailcow
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: mailcow
      template:
        metadata:
          labels:
            app: mailcow
        spec:
          containers:
          - name: mailcow
            image: mailcow:latest
            ports:
            - containerPort: 443
            volumeMounts:
            - name: config
              mountPath: /opt/mailcow
          volumes:
          - name: config
            configMap:
              name: mailcow-config
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: mailcow
    spec:
      selector:
        app: mailcow
      ports:
      - port: 443
        targetPort: 443
      type: LoadBalancer

    Ansible Playbook

    yaml
    ---
    - name: Install and configure mailcow
      hosts: all
      become: yes
      tasks:
        - name: Install mailcow
          package:
            name: mailcow
            state: present
        
        - name: Configure mailcow
          template:
            src: mailcow.conf.j2
            dest: /opt/mailcow/mailcow.conf
            owner: mailcow
            group: mailcow
            mode: '0640'
          notify: restart mailcow
        
        - name: Start and enable mailcow
          systemd:
            name: mailcow
            state: started
            enabled: yes
      
      handlers:
        - name: restart mailcow
          systemd:
            name: mailcow
            state: restarted

    Maintenance

    Update Procedures

    bash
    # RHEL/CentOS/Rocky/AlmaLinux
    sudo dnf update mailcow
    
    # Debian/Ubuntu
    sudo apt update && sudo apt upgrade mailcow
    
    # Arch Linux
    sudo pacman -Syu mailcow
    
    # Alpine Linux
    apk update && apk upgrade mailcow
    
    # openSUSE
    sudo zypper update mailcow
    
    # FreeBSD
    pkg update && pkg upgrade mailcow
    
    # Always backup before updates
    tar -czf /backup/mailcow-pre-update-$(date +%Y%m%d).tar.gz /opt/mailcow
    
    # Restart after updates
    sudo systemctl restart mailcow

    Regular Maintenance Tasks

    bash
    # Clean logs
    find /var/log/mailcow -name "*.log" -mtime +30 -delete
    
    # Verify integrity
    sudo mailcow --verify || sudo mailcow check
    
    # Update databases (if applicable)
    sudo mailcow-update-db
    
    # Optimize performance
    sudo mailcow-optimize
    
    # Check for security updates
    sudo mailcow --security-check

    Additional Resources

  • Official Documentation: https://docs.mailcow.org/
  • GitHub Repository: https://github.com/mailcow/mailcow
  • Community Forum: https://forum.mailcow.org/
  • Wiki: https://wiki.mailcow.org/
  • Comparison vs Mail-in-a-Box, iRedMail, Mailu, Postal: https://docs.mailcow.org/comparison
  • ---

    Note: This guide is part of the HowToMgr collection. Always refer to official documentation for the most up-to-date information.