{"id":223,"date":"2024-06-01T13:24:08","date_gmt":"2024-06-01T19:24:08","guid":{"rendered":"https:\/\/www.7softinteractive.net\/?p=223"},"modified":"2024-06-01T13:24:08","modified_gmt":"2024-06-01T19:24:08","slug":"how-to-set-up-multiple-mail-domains-on-a-single-server-in-ubuntu-linux-with-mysql","status":"publish","type":"post","link":"https:\/\/www.7softinteractive.net\/?p=223","title":{"rendered":"How to Set Up Multiple Mail Domains on a Single Server in Ubuntu Linux (with MySQL)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>Ensure your server is up-to-date and has Postfix, Dovecot, and MySQL installed. You can install MySQL with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update\nsudo apt install mysql-server<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Install Necessary Packages<\/h3>\n\n\n\n<p>Update your package list and install Postfix, Dovecot, and the MySQL integration packages:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update\nsudo apt install postfix postfix-mysql dovecot-imapd dovecot-pop3d dovecot-mysql<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Configure MySQL<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. Secure MySQL installation:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mysql_secure_installation<\/code><\/pre>\n\n\n\n<p>Follow the prompts to secure your MySQL installation.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. Create the database and user for mail:<\/h4>\n\n\n\n<p>Log in to MySQL:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mysql -u root -p<\/code><\/pre>\n\n\n\n<p>Run the following SQL commands to set up the database:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE DATABASE mailserver;\nGRANT ALL PRIVILEGES ON mailserver.* TO 'mailuser'@'localhost' IDENTIFIED BY 'your_password';\nFLUSH PRIVILEGES;\nEXIT;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. Create the necessary tables:<\/h4>\n\n\n\n<p>Log in to MySQL and create the tables for domains, users, and aliases:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mysql -u root -p mailserver<\/code><\/pre>\n\n\n\n<p>Run the following SQL commands:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE `domains` (\n  `id` int(11) NOT NULL AUTO_INCREMENT,\n  `domain` varchar(50) NOT NULL,\n  `active` tinyint(1) NOT NULL DEFAULT '1',\n  PRIMARY KEY (`id`),\n  UNIQUE KEY `domain` (`domain`)\n);\n\nCREATE TABLE `users` (\n  `id` int(11) NOT NULL AUTO_INCREMENT,\n  `email` varchar(100) NOT NULL,\n  `password` varchar(100) NOT NULL,\n  `domain_id` int(11) NOT NULL,\n  `active` tinyint(1) NOT NULL DEFAULT '1',\n  PRIMARY KEY (`id`),\n  UNIQUE KEY `email` (`email`),\n  FOREIGN KEY (`domain_id`) REFERENCES `domains`(`id`) ON DELETE CASCADE\n);\n\nCREATE TABLE `aliases` (\n  `id` int(11) NOT NULL AUTO_INCREMENT,\n  `source` varchar(100) NOT NULL,\n  `destination` varchar(100) NOT NULL,\n  `domain_id` int(11) NOT NULL,\n  PRIMARY KEY (`id`),\n  FOREIGN KEY (`domain_id`) REFERENCES `domains`(`id`) ON DELETE CASCADE\n);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Configure Postfix<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. Edit the Postfix main configuration file:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/postfix\/main.cf<\/code><\/pre>\n\n\n\n<p>Add the following configurations:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mydestination = localhost\nvirtual_mailbox_domains = mysql:\/etc\/postfix\/mysql-virtual-mailbox-domains.cf\nvirtual_mailbox_maps = mysql:\/etc\/postfix\/mysql-virtual-mailbox-maps.cf\nvirtual_alias_maps = mysql:\/etc\/postfix\/mysql-virtual-alias-maps.cf<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. Create MySQL lookup files:<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\"><code>mysql-virtual-mailbox-domains.cf<\/code>:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/postfix\/mysql-virtual-mailbox-domains.cf<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>user = mailuser\npassword = your_password\nhosts = 127.0.0.1\ndbname = mailserver\nquery = SELECT domain FROM domains WHERE domain='%s' AND active=1<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><code>mysql-virtual-mailbox-maps.cf<\/code>:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/postfix\/mysql-virtual-mailbox-maps.cf<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>user = mailuser\npassword = your_password\nhosts = 127.0.0.1\ndbname = mailserver\nquery = SELECT email FROM users WHERE email='%s' AND active=1<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><code>mysql-virtual-alias-maps.cf<\/code>:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/postfix\/mysql-virtual-alias-maps.cf<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>user = mailuser\npassword = your_password\nhosts = 127.0.0.1\ndbname = mailserver\nquery = SELECT destination FROM aliases WHERE source='%s'<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. Set proper permissions for the MySQL lookup files:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo chown root:root \/etc\/postfix\/mysql-virtual-*.cf\nsudo chmod 640 \/etc\/postfix\/mysql-virtual-*.cf<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4. Restart Postfix:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart postfix<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Configure Dovecot<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. Edit the Dovecot main configuration file:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/dovecot\/dovecot.conf<\/code><\/pre>\n\n\n\n<p>Ensure the following lines are present:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>!include conf.d\/*.conf<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. Configure Dovecot to use MySQL:<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\"><code>10-auth.conf<\/code>:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/dovecot\/conf.d\/10-auth.conf<\/code><\/pre>\n\n\n\n<p>Uncomment the following line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>!include auth-sql.conf.ext<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><code>auth-sql.conf.ext<\/code>:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/dovecot\/conf.d\/auth-sql.conf.ext<\/code><\/pre>\n\n\n\n<p>Add the following configuration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>passdb {\n  driver = sql\n  args = \/etc\/dovecot\/dovecot-sql.conf.ext\n}\n\nuserdb {\n  driver = sql\n  args = \/etc\/dovecot\/dovecot-sql.conf.ext\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><code>dovecot-sql.conf.ext<\/code>:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/dovecot\/dovecot-sql.conf.ext<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>driver = mysql\nconnect = host=127.0.0.1 dbname=mailserver user=mailuser password=your_password\ndefault_pass_scheme = SHA512-CRYPT\n\npassword_query = SELECT email as user, password FROM users WHERE email='%u' AND active=1\nuser_query = SELECT email as user, '\/var\/mail\/vhosts\/%d\/%n' as home, 'maildir:\/var\/mail\/vhosts\/%d\/%n' as mail, 5000 as uid, 5000 as gid FROM users WHERE email='%u' AND active=1<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. Set up mail directories and permissions:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mkdir -p \/var\/mail\/vhosts\/example.com\nsudo mkdir -p \/var\/mail\/vhosts\/anotherdomain.com\nsudo groupadd -g 5000 vmail\nsudo useradd -g vmail -u 5000 vmail -d \/var\/mail\nsudo chown -R vmail:vmail \/var\/mail\nsudo chmod -R 770 \/var\/mail<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4. Restart Dovecot:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart dovecot<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Testing Your Configuration<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Check Postfix and Dovecot status:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl status postfix\nsudo systemctl status dovecot<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Send test emails to your configured domains to ensure they are being delivered correctly and can be accessed via IMAP\/POP3.<\/strong><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Optional: Configure SSL\/TLS<\/h3>\n\n\n\n<p>For secure email transmission, configure SSL\/TLS in both Postfix and Dovecot.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. For Postfix:<\/h4>\n\n\n\n<p>Add to <code>\/etc\/postfix\/main.cf<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>smtpd_tls_cert_file=\/etc\/ssl\/certs\/your-cert.pem\nsmtpd_tls_key_file=\/etc\/ssl\/private\/your-key.pem\nsmtpd_use_tls=yes<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. For Dovecot:<\/h4>\n\n\n\n<p>Edit <code>\/etc\/dovecot\/conf.d\/10-ssl.conf<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssl = yes\nssl_cert = &lt;\/etc\/ssl\/certs\/your-cert.pem\nssl_key = &lt;\/etc\/ssl\/private\/your-key.pem<\/code><\/pre>\n\n\n\n<p>Restart both services after making these changes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart postfix\nsudo systemctl restart dovecot<\/code><\/pre>\n\n\n\n<p>Running into problems with SaslAuthd here is a link that helped me to solve it <a href=\"https:\/\/www.mail-archive.com\/debian-bugs-dist@lists.debian.org\/msg1934701.html\">https:\/\/www.mail-archive.com\/debian-bugs-dist@lists.debian.org\/msg1934701.html<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Prerequisites Ensure your server is up-to-date and has Postfix, Dovecot, and MySQL installed. You can install MySQL with: Step 1: Install Necessary Packages Update your package list and install Postfix, Dovecot, and the MySQL integration packages: Step 2: Configure MySQL 1. Secure MySQL installation: Follow the prompts to secure your MySQL installation. 2. Create the<\/p>\n","protected":false},"author":2,"featured_media":185,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-223","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=\/wp\/v2\/posts\/223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=223"}],"version-history":[{"count":1,"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=\/wp\/v2\/posts\/223\/revisions"}],"predecessor-version":[{"id":224,"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=\/wp\/v2\/posts\/223\/revisions\/224"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=\/wp\/v2\/media\/185"}],"wp:attachment":[{"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}