Skip to content

Roundcube webmail install — PHP, IMAP backend, and production hardening

Install Roundcube against an existing IMAP/SMTP stack, configure PHP-FPM and Nginx, plumb the SQLite/MySQL/PostgreSQL config, and harden for a multi-user deployment.

Roundcube is the long-running PHP webmail client — minimal, themable, and unopinionated about what runs underneath. This article is the install procedure we follow when a client needs browser access to a self-hosted IMAP/SMTP stack — PHP-FPM and Nginx, a SQLite/MySQL/PostgreSQL backing store for preferences, the IMAP and SMTP wiring to the Postfix/Dovecot pair from postfix-install-ubuntu and dovecot-install-imaps, and the hardening you do before publishing the FQDN.

How to verify

Pre-install audit. PHP and webserver may already be present; check before installing both.

dpkg -l | grep -E 'php8|nginx|apache2|mariadb|postgresql'
ls -ld /etc/php /etc/nginx /usr/share/roundcube 2>/dev/null
ss -ltnp | grep -E ':(80|443|8443)\b'
php -v
nginx -v 2>&1 || apache2 -v 2>&1

You want PHP 8.1+ available, a webserver running, and either MariaDB/MySQL or PostgreSQL on the box (or a remote DB reachable). SQLite works for a single-user box but doesn’t scale to multi-user concurrent.

What’s happening

Roundcube is a stateless PHP app. Each request authenticates against the IMAP backend (the IMAP password is the Roundcube login — there is no separate Roundcube credential), pulls the message list or body via IMAP, and renders. Outbound mail goes through SMTP submission. The DB stores per-user preferences, contacts, cached message headers, and search index — not the messages themselves; those stay in the IMAP store.

The component layout:

  • /usr/share/roundcube/ (Debian package) or /var/www/roundcube/ (manual install) — the PHP code.
  • /etc/roundcube/ — the config files (config.inc.php, debian-db.php).
  • A database — roundcube schema, roundcube user.
  • A webserver — Nginx with PHP-FPM in the engagements we run.
  • The IMAP server (Dovecot on this box, or remote) and SMTP submission server.

The Debian package handles most of this; we cover the manual layout below because the package version often lags behind the Roundcube release stream by a year or more.

The procedure

  1. Install PHP-FPM with the required extensions. Roundcube needs mbstring, intl, xml, gd, curl, mysql (or pgsql), ldap, zip:

    sudo apt update
    sudo apt install -y nginx php8.1-fpm \
        php8.1-mbstring php8.1-intl php8.1-xml php8.1-gd php8.1-curl \
        php8.1-mysql php8.1-zip php8.1-imagick php8.1-ldap
    systemctl is-active php8.1-fpm nginx
  2. Provision the database. MariaDB example — a dedicated DB and user with limited grants:

    sudo mariadb <<SQL
    CREATE DATABASE roundcube CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
    CREATE USER 'roundcube'@'localhost' IDENTIFIED BY 'strong-random-pw';
    GRANT ALL PRIVILEGES ON roundcube.* TO 'roundcube'@'localhost';
    FLUSH PRIVILEGES;
    SQL
  3. Fetch and stage Roundcube from upstream. Pick the current stable from https://github.com/roundcube/roundcubemail/releases:

    cd /var/www
    curl -L https://github.com/roundcube/roundcubemail/releases/download/1.6.7/roundcubemail-1.6.7-complete.tar.gz | sudo tar xz
    sudo mv roundcubemail-1.6.7 roundcube
    sudo chown -R www-data:www-data /var/www/roundcube/temp /var/www/roundcube/logs
  4. Import the schema. The package ships SQL files for each supported DB:

    sudo mariadb roundcube < /var/www/roundcube/SQL/mysql.initial.sql
  5. Configure Roundcube. Copy the sample, edit:

    sudo cp /var/www/roundcube/config/config.inc.php.sample /var/www/roundcube/config/config.inc.php
    sudo chown root:www-data /var/www/roundcube/config/config.inc.php
    sudo chmod 640 /var/www/roundcube/config/config.inc.php
    sudo $EDITOR /var/www/roundcube/config/config.inc.php

    Key parameters:

    $config['db_dsnw'] = 'mysql://roundcube:strong-random-pw@localhost/roundcube';
    
    $config['imap_host'] = 'ssl://mx1.example.com:993';
    $config['smtp_host'] = 'tls://mx1.example.com:587';
    $config['smtp_user'] = '%u';   // pass through the logged-in user
    $config['smtp_pass'] = '%p';   // pass through the logged-in password
    
    $config['support_url'] = 'mailto:postmaster@example.com';
    $config['product_name'] = 'Example Webmail';
    
    $config['des_key'] = 'GENERATE-A-RANDOM-24-CHAR-STRING';  // openssl rand -base64 18
    
    $config['plugins'] = ['archive', 'zipdownload', 'managesieve', 'password'];
    $config['enable_installer'] = false;

    The des_key encrypts the IMAP password kept in the user session; never reuse the sample value.

  6. Configure Nginx as the front. /etc/nginx/sites-available/roundcube:

    server {
        listen 443 ssl http2;
        server_name webmail.example.com;
    
        ssl_certificate     /etc/letsencrypt/live/webmail.example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/webmail.example.com/privkey.pem;
    
        root /var/www/roundcube;
        index index.php;
    
        client_max_body_size 50M;
    
        location / {
            try_files $uri $uri/ /index.php?$args;
        }
    
        location ~ ^/(config|temp|logs)/ {
            deny all;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
            fastcgi_param HTTPS on;
        }
    
        location ~ /\.ht {
            deny all;
        }
    }

    Then:

    sudo ln -s /etc/nginx/sites-available/roundcube /etc/nginx/sites-enabled/
    sudo nginx -t && sudo systemctl reload nginx
  7. Smoke test and disable the installer. Browse to https://webmail.example.com/installer/ — only if it’s available, which means enable_installer is true (it should be false in production). If you do enable it temporarily, run the checks, then re-disable. Then log in with an IMAP user (alice@example.com and the password from the Dovecot passwd file) and send a message to confirm the SMTP path.

Hardening notes

  • Increase session.cookie_secure and session.cookie_samesite in php.ini — Roundcube’s session cookie should be Secure; HttpOnly; SameSite=Strict.
  • Limit per-user IMAP connections via Dovecot’s mail_max_userip_connections to prevent a malicious script from exhausting connections.
  • Set $config['ip_check'] = true to bind sessions to the source IP — defeats session-cookie theft from a different IP, at the cost of breaking VPN switchers; trade-off for the client.
  • Enable the managesieve plugin and connect to Dovecot’s managesieve daemon on port 4190 — users get a real “filters” UI; without it, the only spam-folder filing is server-side via the global Sieve.
  • Patch monthly. Roundcube has had XSS issues in the past; the upstream releases are usually one-line CVE fixes, easy to deploy.

Common pitfalls

  • The temp/ and logs/ directories are not writable by www-data, so every action fails with a generic “An error occurred” — chmod those two paths to 0750 owned by www-data:www-data.
  • The des_key is left at the sample value; sessions persist but anyone who reads the sample config online can decrypt the session payload — always regenerate.
  • The IMAP host is set to localhost while Dovecot is listening only on the public IP — ss -ltnp shows the bind; either bind Dovecot to localhost or use the public FQDN with valid TLS.
  • The webmail FQDN gets its own Let’s Encrypt cert, but the cert renewal post-hook only reloads nginx — if Dovecot is using the same cert (we share it across mail FQDNs sometimes), Dovecot keeps the old cert in memory.
  • The Roundcube installer URL is accessible after deploy — the installer surface area is a known attack vector; setting enable_installer = false is not optional.

Stack Harbor stands up Roundcube as a tenant-facing front to the same Postfix/Dovecot box — single TLS, single auth, the webmail FQDN behind Nginx with a separate cert renewal hook, and the installer disabled before the URL is shared. If you have a webmail that is “installed but nobody patches it,” that monthly hygiene is part of our Managed Operations practice.