<\/span><\/h2>\n\n\n\nLog in to your Debian 11 VPS through SSH as a root user or as a regular user with sudo privileges<\/p>\n\n\n\n
$ ssh master@IP_Address -p Port_number<\/pre>\n\n\n\nRemember to replace “master” with a user that has sudo privileges, or root. Additionally, replace “IP_Address” and “Port_Number” with your server\u2019s IP address and SSH port.<\/p>\n\n\n\n
You can check whether you have the proper Debian version installed on your server with the following command:<\/p>\n\n\n\n
$ lsb_release -a<\/pre>\n\n\n\nYou should get this output:<\/p>\n\n\n\n
No LSB modules are available. Distributor ID: Debian Description: Debian GNU\/Linux 11 (bullseye) Release: 11 Codename: bullseye<\/pre>\n\n\n\nTo update the system, you can run this command:<\/p>\n\n\n\n
$ sudo apt update && sudo apt upgrade<\/pre>\n\n\n\n<\/span>Step 2. Set Hostname<\/span><\/h2>\n\n\n\nYou would need to create a DNS A record for your hostname because the server hostname should be an FQDN. For example, we will create debian11.yourdomain.com<\/strong> as the server hostname, let’s run this command:<\/p>\n\n\n\n$ sudo hostnamectl set-hostname debian11.yourdomain.com<\/pre>\n\n\n\nThe above command is just an example, you would need to use your own registered domain name to create a hostname.<\/p>\n\n\n\n
<\/span>Step 3. Install Apache, MariaDB, and PHP<\/span><\/h2>\n\n\n\nNow, let’s install the webserver, database server, and PHP<\/p>\n\n\n\n
$ sudo apt install apache2 mariadb-server php-{xml,pear,imap,intl,common,json,curl,mbstring,mysql,gd,imagick,zip,opcache,sqlite3} libapache2-mod-php<\/pre>\n\n\n\nYou can skip this step if these are already installed on your server.<\/p>\n\n\n\n
<\/span>Step 4. Create a System User<\/span><\/h2>\n\n\n\nLet’s run the following commands to create a system user called ‘vmail’ and create a directory and give the proper permission.<\/p>\n\n\n\n
$ sudo useradd -r -u 150 -g mail -d \/var\/vmail -s \/sbin\/nologin -c \"Virtual Mail User\" vmail $ sudo mkdir -p \/var\/vmail $ sudo chmod -R 770 \/var\/vmail $ sudo chown -R vmail:mail \/var\/vmail<\/pre>\n\n\n\n<\/span>Step 4. Create a Database<\/span><\/h2>\n\n\n\nIf your MySQL root user does not have a password, you can run this command to log in to the MySQL shell.<\/p>\n\n\n\n
$ sudo mysql<\/pre>\n\n\n\nIf a password is required to log in to MySQL shell, then run this command:<\/p>\n\n\n\n
$ mysql -u root -p<\/pre>\n\n\n\nOnce logged in, let’s run the following commands:<\/p>\n\n\n\n
MariaDB [(none)]> CREATE DATABASE postfixadmin;\nMariaDB [(none)]> GRANT ALL PRIVILEGES ON postfixadmin.* TO 'postfixadmin'@'localhost' IDENTIFIED BY '5tr0n6p455w0rd';\nMariaDB [(none)]> FLUSH PRIVILEGES;\nMariaDB [(none)]> \\q<\/pre>\n\n\n\n<\/span>Step 5. Install Postfix<\/span><\/h2>\n\n\n\nInstall Postfix mail server with this command:<\/p>\n\n\n\n
$ sudo apt install postfix-mysql<\/pre>\n\n\n\nOnce executes, you will see the following<\/p>\n\n\n\n <\/figure>\n\n\n\nChoose Internet Site<\/strong> then hit Ok <\/strong>to continue<\/p>\n\n\n\nIn this step, you will be asked for the system mail name, make sure the system mail name is the same as your Debian 11 hostname. We set the system hostname at the beginning of this tutorial.<\/p>\n\n\n\n
Once completed we need to create several configuration files for Postfix to communicate with the database.<\/p>\n\n\n\n
$ sudo mkdir -p \/etc\/postfix\/sql<\/pre>\n\n\n\nLet’s create the files, make sure to replace the password, database name, database user with the ones you created earlier.<\/p>\n\n\n\n
$ sudo nano \/etc\/postfix\/sql\/mysql_virtual_alias_maps.cf<\/pre>\n\n\n\nuser = postfixadmin\npassword = 5tr0n6p455w0rd\nhosts = localhost\ndbname = postfixadmin\nquery = SELECT goto FROM alias WHERE address='%s' AND active = '1'<\/pre>\n\n\n\n$ sudo nano \/etc\/postfix\/sql\/mysql_virtual_domains_maps.cf<\/pre>\n\n\n\nuser = postfixadmin password = 5tr0n6p455w0rd hosts = localhost dbname = postfixadmin query = SELECT domain FROM domain WHERE domain='%s' AND active = '1'<\/pre>\n\n\n\n$ sudo nano \/etc\/postfix\/sql\/mysql_virtual_mailbox_maps.cf<\/pre>\n\n\n\nuser = postfixadmin password = 5tr0n6p455w0rd hosts = localhost dbname = postfixadmin query = SELECT maildir FROM mailbox WHERE username='%s' AND active = '1'<\/pre>\n\n\n\n$ sudo nano \/etc\/postfix\/sql\/mysql_virtual_alias_domain_maps.cf<\/pre>\n\n\n\nuser = postfixadmin password = 5tr0n6p455w0rd hosts = localhost dbname = postfixadmin query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('%u', '@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'<\/pre>\n\n\n\n$ sudo nano \/etc\/postfix\/sql\/mysql_virtual_alias_domain_mailbox_maps.cf<\/pre>\n\n\n\nuser = postfixadmin password = 5tr0n6p455w0rd hosts = localhost dbname = postfixadmin query = SELECT maildir FROM mailbox,alias_domain WHERE alias_domain.alias_domain = '%d' and mailbox.username = CONCAT('%u', '@', alias_domain.target_domain) AND mailbox.active = 1 AND alias_domain.active='1'<\/pre>\n\n\n\n$ sudo nano \/etc\/postfix\/sql\/mysql_virtual_mailbox_limit_maps.cf<\/pre>\n\n\n\nuser = postfixadmin password = 5tr0n6p455w0rd hosts = localhost dbname = postfixadmin query = SELECT quota FROM mailbox WHERE username='%s' AND active = '1'<\/pre>\n\n\n\n$ sudo nano \/etc\/postfix\/sql\/mysql_virtual_alias_domain_catchall_maps.cf<\/pre>\n\n\n\nuser = postfixadmin password = 5tr0n6p455w0rd hosts = localhost dbname = postfixadmin query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'<\/pre>\n\n\n\nLet us now proceed with editing \/etc\/postfix\/main.cf<\/code> file, we can run the following commands:<\/p>\n\n\n\n$ sudo postconf -e \"myhostname = $(hostname -f)\"\n$ sudo postconf -e \"mydestination = localhost\"\n$ sudo postconf -e \"mynetworks = 127.0.0.0\/8\"\n$ sudo postconf -e \"inet_protocols = ipv4\"\n$ sudo postconf -e \"inet_interfaces = all\"\n$ sudo postconf -e \"smtpd_tls_cert_file = \/etc\/ssl\/certs\/ssl-cert-snakeoil.pem\"\n$ sudo postconf -e \"smtpd_tls_key_file = \/etc\/ssl\/private\/ssl-cert-snakeoil.key\"\n$ sudo postconf -e \"smtpd_use_tls = yes\"\n$ sudo postconf -e \"smtpd_tls_auth_only = yes\"\n$ sudo postconf -e \"smtpd_sasl_type = dovecot\"\n$ sudo postconf -e \"smtpd_sasl_path = private\/auth\"\n$ sudo postconf -e \"smtpd_sasl_auth_enable = yes\"\n$ sudo postconf -e \"smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination\"\n$ sudo postconf -e \"virtual_transport = lmtp:unix:private\/dovecot-lmtp\"\n$ sudo postconf -e \"virtual_mailbox_domains = proxy:mysql:\/etc\/postfix\/sql\/mysql_virtual_domains_maps.cf\"\n$ sudo postconf -e \"virtual_alias_maps = proxy:mysql:\/etc\/postfix\/sql\/mysql_virtual_alias_maps.cf, proxy:mysql:\/etc\/postfix\/sql\/mysql_virtual_alias_domain_maps.cf, proxy:mysql:\/etc\/postfix\/sql\/mysql_virtual_alias_domain_catchall_maps.cf\"\n$ sudo postconf -e \"virtual_mailbox_maps = proxy:mysql:\/etc\/postfix\/sql\/mysql_virtual_mailbox_maps.cf, proxy:mysql:\/etc\/postfix\/sql\/mysql_virtual_alias_domain_mailbox_maps.cf\"<\/pre>\n\n\n\nOnce completed, we can continue and edit \/etc\/postfix\/master.cf <\/code>file:<\/p>\n\n\n\n$ sudo nano \/etc\/postfix\/master.cf<\/pre>\n\n\n\nModify or add the following lines to the file.<\/p>\n\n\n\n
smtp inet n - y - - smtpd\n#smtp inet n - y - 1 postscreen\n#smtpd pass - - y - - smtpd\n#dnsblog unix - - y - 0 dnsblog\n#tlsproxy unix - - y - 0 tlsproxy\nsubmission inet n - y - - smtpd\n -o syslog_name=postfix\/submission\n -o smtpd_tls_security_level=encrypt\n -o smtpd_sasl_auth_enable=yes\n# -o smtpd_reject_unlisted_recipient=no\n# -o smtpd_client_restrictions=$mua_client_restrictions\n# -o smtpd_helo_restrictions=$mua_helo_restrictions\n# -o smtpd_sender_restrictions=$mua_sender_restrictions\n# -o smtpd_recipient_restrictions=\n# -o smtpd_relay_restrictions=permit_sasl_authenticated,reject\n -o smtpd_client_restrictions=permit_sasl_authenticated,reject\n -o milter_macro_daemon_name=ORIGINATING\nsmtps inet n - y - - smtpd\n -o syslog_name=postfix\/smtps\n# -o smtpd_tls_wrappermode=yes\n -o smtpd_sasl_auth_enable=yes\n# -o smtpd_reject_unlisted_recipient=no\n -o smtpd_client_restrictions=permit_sasl_authenticated,reject\n# -o smtpd_client_restrictions=$mua_client_restrictions\n# -o smtpd_helo_restrictions=$mua_helo_restrictions\n# -o smtpd_sender_restrictions=$mua_sender_restrictions\n# -o smtpd_recipient_restrictions=\n# -o smtpd_relay_restrictions=permit_sasl_authenticated,reject\n -o milter_macro_daemon_name=ORIGINATING<\/pre>\n\n\n\nSave the changes then exit.<\/p>\n\n\n\n
<\/span>Step 6. Install Dovecot<\/span><\/h2>\n\n\n\n$ sudo apt install dovecot-mysql dovecot-imapd dovecot-pop3d dovecot-lmtpd<\/pre>\n\n\n\nOnce dovecot is installed, we need to edit some configuration files.<\/p>\n\n\n\n
Open the \/etc\/dovecot\/conf.d\/10-auth.conf<\/code> file<\/p>\n\n\n\n$ sudo nano \/etc\/dovecot\/conf.d\/10-auth.conf<\/pre>\n\n\n\nThen find this line<\/p>\n\n\n\n
auth_mechanisms = plain<\/pre>\n\n\n\nreplace it with this line:<\/p>\n\n\n\n
auth_mechanisms = plain login<\/pre>\n\n\n\nThen, comment !include auth-system.conf.ext<\/strong> and uncomment #!include auth-sql.conf.ext<\/strong><\/p>\n\n\n\nIt should look like this:<\/p>\n\n\n\n
#!include auth-system.conf.ext !include auth-sql.conf.ext<\/pre>\n\n\n\nIn the 10-auth.conf file, we include auth-sql.conf.ext. So, let’s edit the file<\/p>\n\n\n\n
$ sudo nano \/etc\/dovecot\/dovecot-sql.conf.ext<\/pre>\n\n\n\nthen append these lines:<\/p>\n\n\n\n
driver = mysql connect = host=localhost dbname=postfixadmin user=postfixadmin password=5tr0n6p455w0rd default_pass_scheme = MD5-CRYPT password_query = SELECT username as user, password, '\/var\/vmail\/%d\/%n' as userdb_home, 'maildir:\/var\/vmail\/%d\/%n' as userdb_mail, 150 as userdb_uid, 8 as userdb_gid FROM mailbox WHERE username = '%u' AND active = '1' user_query = SELECT '\/var\/vmail\/%d\/%u' as home, 'maildir:\/var\/vmail\/%d\/%u' as mail, 150 AS uid, 8 AS gid, concat('dirsize:storage=', quota) AS quota FROM mailbox WHERE username = '%u' AND active = '1'<\/pre>\n\n\n\nThe lines above contain the database credentials, make sure they match the ones you created in the previous step.<\/p>\n\n\n\n
Save the file and exit<\/p>\n\n\n\n
Next, open the \/etc\/dovecot\/conf.d\/10-mail.conf<\/code> file and modify the following values:<\/p>\n\n\n\n$ sudo nano \/etc\/dovecot\/conf.d\/10-mail.conf<\/pre>\n\n\n\nmail_location = maildir:\/var\/vmail\/%d\/%n mail_uid = vmail mail_gid = mail mail_privileged_group = mail first_valid_uid = 150 last_valid_uid = 150<\/pre>\n\n\n\nBy default, dovecot SSL support in Debian 11 is enabled. You can verify it by checking the file \/etc\/dovecot\/conf.d\/10-ssl.conf<\/code>, make sure it has this line:<\/p>\n\n\n\nssl = yes<\/pre>\n\n\n\nIf you want to edit the postmaster email address, you can edit the file \/etc\/dovecot\/conf.d\/15-lda.conf<\/strong> and find the postmaster_address<\/strong> line. By default, the postmaster email address would be postmaster@yourdomain.com<\/strong>. So, if you want to receive the postmaster email at admin@yourdomain.com<\/strong>, then you need to add this line:<\/p>\n\n\n\npostmaster_address = admin@yourdomain.com<\/pre>\n\n\n\nNow, let’s proceed with editing other dovecot configuration files.<\/p>\n\n\n\n
$ sudo nano \/etc\/dovecot\/conf.d\/10-master.conf<\/pre>\n\n\n\nThere are several changes you need to make to this file.<\/p>\n\n\n\n
Find the service lmtp section and change it to the following:<\/li><\/ol>\n\n\n\nservice lmtp {\n unix_listener \/var\/spool\/postfix\/private\/dovecot-lmtp {\n mode = 0600\n user = postfix\n group = postfix\n }\n}<\/pre>\n\n\n\nFind the service auth section and change it to:<\/li><\/ol>\n\n\n\nservice auth {\n unix_listener \/var\/spool\/postfix\/private\/auth {\n mode = 0666\n user = postfix\n group = postfix\n}\n\nunix_listener auth-userdb {\n mode = 0600\n user = vmail\n\n}\nuser = dovecot\n}<\/pre>\n\n\n\nChange the service auth-worker section to the following:<\/li><\/ol>\n\n\n\nservice auth-worker {\n user = vmail\n}<\/pre>\n\n\n\nDo not forget to save the file then exit<\/p>\n\n\n\n
Before restarting Dovecot, we need to change the permissions.<\/p>\n\n\n\n
$ sudo chown -R vmail:dovecot \/etc\/dovecot\n$ sudo chmod -R o-rwx \/etc\/dovecot\n$ sudo systemctl restart postfix dovecot<\/pre>\n\n\n\n