# ssh root@IP_Address -p Port_number<\/pre>\nYou can check whether you have the proper Debian version installed on your server with the following command:<\/p>\n
# lsb_release -a<\/pre>\nYou should get this output:<\/p>\n
Distributor ID: Debian\r\nDescription: Debian GNU\/Linux 9.9 (Stretch)\r\nRelease: 9.9\r\nCodename: stretch\r\n<\/pre>\nThen, run the following command to make sure that all installed packages on the server are updated to their latest available versions:<\/p>\n
# apt update && apt upgrade<\/pre>\n<\/span>Step 2: Create a System User<\/span><\/h2>\nFor security reasons, we will create a new system user who will be the owner of all mailboxes.<\/p>\n
# useradd -r -u 150 -g mail -d \/var\/vmail -s \/sbin\/nologin -c \"Virtual Mail User\" vmail\r\n# mkdir -p \/var\/vmail\r\n# chmod -R 770 \/var\/vmail\r\n# chown -R vmail:mail \/var\/vmail\r\n<\/pre>\n<\/span>Step 3: Install MariaDB Server<\/span><\/h2>\nPostfixAdmin supports MySQL\/MariaDB, PostgreSQL, and SQLite database systems. In this tutorial, we will use MySQL\/MariaDB as the database storage engine.<\/p>\n
# apt install mariadb-server<\/pre>\nTo start the MariaDB service and enable it to start on boot, execute the following commands:<\/p>\n
# systemctl start mariadb\r\n# systemctl enable mariadb\r\n<\/pre>\nNow, you can skip the following step if you prefer not to have a MySQL root password.<\/p>\n
# mysql_secure_installation<\/pre>\nWhen prompted, answer the questions below by following the guide.<\/p>\n
Enter current password for root (enter for none): Just press the [Enter] key since there is no default password\r\nSet root password? [Y\/n]: Y\r\nNew password: Enter password\r\nRe-enter new password: Repeat password\r\nRemove anonymous users? [Y\/n]: Y\r\nDisallow root login remotely? [Y\/n]: Y\r\nRemove test database and access to it? [Y\/n]: Y\r\nReload privilege tables now? [Y\/n]: Y\r\n<\/pre>\nIf you followed the above step, then you would have a newly set password for MySQL root user.<\/p>\n
Next, run this command to access the MySQL shell.<\/p>\n
# mysql -u root -p<\/pre>\nRemember to enter your root password if you ran the secure installation script.<\/p>\n
Let\u2019s proceed with creating a database for PostfixAdmin:<\/p>\n
MariaDB [(none)]> CREATE DATABASE postfixadmin;\r\nMariaDB [(none)]> GRANT ALL PRIVILEGES ON postfixadmin.* TO 'postfixadmin'@'localhost' IDENTIFIED BY 'M0d1fyth15<\/span>';\r\nMariaDB [(none)]> FLUSH PRIVILEGES;\r\nMariaDB [(none)]> \\q<\/pre>\nMake sure to replace the password for the database user to a good unique password.<\/p>\n
<\/span>Step 4: Install PHP and all Necessary PHP Modules<\/span><\/h2>\nThis is<\/p>\n
apt install apache2 php php-mbstring php-imap php-mysql libapache2-mod-php7.0<\/pre>\n<\/span>Step 5: Install Postfix<\/span><\/h2>\nInstall Postfix with the command below:<\/p>\n
# apt install postfix-mysql<\/pre>\n
\nChoose Internet Site and click on Ok to continue.<\/p>\n
<\/p>\n
You will be asked for the system mail name, make sure this is the same with your hostname.<\/p>\n
Once the installation is completed, we need to create configuration files:<\/p>\n
# mkdir -p \/etc\/postfix\/sql\/<\/pre>\nFrom here, several configuration files will be created. Make sure to replace any and all values that are different here to the necessary values, or your configuration may not work.<\/p>\n
# nano \/etc\/postfix\/sql\/mysql_virtual_alias_domain_catchall_maps.cf<\/pre>\nuser = postfixadmin\r\npassword = M0d1fyth15<\/span>\r\nhosts = localhost\r\ndbname = postfixadmin\r\nquery = 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'\r\n<\/pre>\n# nano \/etc\/postfix\/sql\/mysql_virtual_alias_domain_mailbox_maps.cf<\/pre>\nuser = postfixadmin\r\npassword = M0d1fyth15<\/span>\r\nhosts = localhost\r\ndbname = postfixadmin\r\nquery = 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'\r\n<\/pre>\n# nano \/etc\/postfix\/sql\/mysql_virtual_alias_domain_maps.cf<\/pre>\nuser = postfixadmin\r\npassword = M0d1fyth15<\/span>\r\nhosts = localhost\r\ndbname = postfixadmin\r\nquery = 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'\r\n<\/pre>\n# nano \/etc\/postfix\/sql\/mysql_virtual_alias_maps.cf<\/pre>\nuser = postfixadmin\r\npassword = M0d1fyth15<\/span>\r\nhosts = localhost\r\ndbname = postfixadmin\r\nquery = SELECT goto FROM alias WHERE address='%s' AND active = '1'\r\n<\/pre>\n# nano \/etc\/postfix\/sql\/mysql_virtual_domains_maps.cf<\/pre>\nuser = postfixadmin\r\npassword = M0d1fyth15<\/span>\r\nhosts = localhost\r\ndbname = postfixadmin\r\nquery = SELECT domain FROM domain WHERE domain='%s' AND active = '1'\r\n<\/pre>\n# nano \/etc\/postfix\/sql\/mysql_virtual_mailbox_limit_maps.cf<\/pre>\nuser = postfixadmin\r\npassword = M0d1fyth15<\/span>\r\nhosts = localhost\r\ndbname = postfixadmin\r\nquery = SELECT quota FROM mailbox WHERE username='%s' AND active = '1'\r\n<\/pre>\n# nano \/etc\/postfix\/sql\/mysql_virtual_mailbox_maps.cf<\/pre>\nuser = postfixadmin\r\npassword = M0d1fyth15<\/span>\r\nhosts = localhost\r\ndbname = postfixadmin\r\nquery = SELECT maildir FROM mailbox WHERE username='%s' AND active = '1'\r\n<\/pre>\nIn the configuration files above, we use ‘m0d1fyth15<\/span>‘ as the password, please change it to your own password.<\/p>\nNow, let’s edit the main.cf<\/code> file by invoking the following commands:<\/p>\npostconf -e \"myhostname = $(hostname -f)\"\r\npostconf -e \"virtual_mailbox_domains = proxy:mysql:\/etc\/postfix\/sql\/mysql_virtual_domains_maps.cf\"\r\npostconf -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\"\r\npostconf -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\"\r\npostconf -e \"smtpd_tls_cert_file = \/etc\/ssl\/certs\/ssl-cert-snakeoil.pem\"\r\npostconf -e \"smtpd_tls_key_file = \/etc\/ssl\/private\/ssl-cert-snakeoil.key\"\r\npostconf -e \"smtpd_use_tls = yes\"\r\npostconf -e \"smtpd_tls_auth_only = yes\"\r\npostconf -e \"smtpd_sasl_type = dovecot\"\r\npostconf -e \"smtpd_sasl_path = private\/auth\"\r\npostconf -e \"smtpd_sasl_auth_enable = yes\"\r\npostconf -e \"smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination\"\r\npostconf -e \"mydestination = localhost\"\r\npostconf -e \"mynetworks = 127.0.0.0\/8\"\r\npostconf -e \"inet_protocols = ipv4\"\r\npostconf -e \"inet_interfaces = all\"\r\npostconf -e \"virtual_transport = lmtp:unix:private\/dovecot-lmtp\"<\/pre>\nOpen the master.cf<\/code> file, find submission inet n<\/code> and smtps inet n<\/code> sections and edit as follows:<\/p>\n# nano \/etc\/postfix\/master.cf<\/pre>\nsmtp inet n - y - - smtpd\r\n#smtp inet n - y - 1 postscreen\r\n#smtpd pass - - y - - smtpd\r\n#dnsblog unix - - y - 0 dnsblog\r\n#tlsproxy unix - - y - 0 tlsproxy\r\nsubmission inet n - y - - smtpd\r\n-o syslog_name=postfix\/submission\r\n-o smtpd_tls_security_level=encrypt\r\n-o smtpd_sasl_auth_enable=yes\r\n# -o smtpd_reject_unlisted_recipient=no\r\n# -o smtpd_client_restrictions=$mua_client_restrictions\r\n# -o smtpd_helo_restrictions=$mua_helo_restrictions\r\n# -o smtpd_sender_restrictions=$mua_sender_restrictions\r\n# -o smtpd_recipient_restrictions=\r\n# -o smtpd_relay_restrictions=permit_sasl_authenticated,reject\r\n-o smtpd_client_restrictions=permit_sasl_authenticated,reject\r\n-o milter_macro_daemon_name=ORIGINATING\r\nsmtps inet n - y - - smtpd\r\n-o syslog_name=postfix\/smtps\r\n# -o smtpd_tls_wrappermode=yes\r\n-o smtpd_sasl_auth_enable=yes\r\n# -o smtpd_reject_unlisted_recipient=no\r\n-o smtpd_client_restrictions=permit_sasl_authenticated,reject\r\n# -o smtpd_client_restrictions=$mua_client_restrictions\r\n# -o smtpd_helo_restrictions=$mua_helo_restrictions\r\n# -o smtpd_sender_restrictions=$mua_sender_restrictions\r\n# -o smtpd_recipient_restrictions=\r\n# -o smtpd_relay_restrictions=permit_sasl_authenticated,reject\r\n-o milter_macro_daemon_name=ORIGINATING\r\n<\/pre>\nEnable and restart the Postfix service:<\/p>\n
# systemctl enable postfix\r\n# systemctl restart postfix<\/pre>\n<\/span>Step 6: Install Dovecot<\/span><\/h2>\nInstall Dovecot using the command below:<\/p>\n
# apt install dovecot-mysql<\/pre>\nOpen the \/etc\/dovecot\/conf.d\/10-mail.conf<\/span> file and change the following values:<\/p>\n# nano \/etc\/dovecot\/conf.d\/10-mail.conf<\/pre>\nmail_location = maildir:\/var\/vmail\/%d\/%n\r\nmail_privileged_group = mail\r\nmail_uid = vmail\r\nmail_gid = mail\r\nfirst_valid_uid = 150\r\nlast_valid_uid = 150\r\n<\/pre>\nOpen the \/etc\/dovecot\/conf.d\/10-auth.conf<\/span> file and change the following values:<\/p>\n# nano \/etc\/dovecot\/conf.d\/10-auth.conf<\/pre>\nauth_mechanisms = plain login\r\n#!include auth-system.conf.ext\r\n!include auth-sql.conf.ext\r\n<\/pre>\nEdit the dovecot-sql.conf.ext<\/code> file, then append these lines:<\/p>\n# nano \/etc\/dovecot\/dovecot-sql.conf.ext<\/pre>\ndriver = mysql\r\nconnect = host=localhost dbname=postfixadmin user=postfixadmin password=M0d1fyth15<\/span>\r\ndefault_pass_scheme = MD5-CRYPT\r\npassword_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'\r\nuser_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>\nRemember to replace the password as well as any other different values here as well.<\/p>\n
In the \/etc\/dovecot\/conf.d\/10-ssl.conf<\/span> file, enable SSL support:<\/p>\n# nano \/etc\/dovecot\/conf.d\/10-ssl.conf<\/pre>\nssl = yes<\/pre>\nOpen the \/etc\/dovecot\/conf.d\/15-lda.conf<\/span> file and set the postmaster_address<\/code> email address.<\/p>\n# nano \/etc\/dovecot\/conf.d\/15-lda.conf<\/pre>\npostmaster_address = postmaster@yourdomain.com<\/pre>\nOpen the \/etc\/dovecot\/conf.d\/10-master.conf<\/span> file, find the service lmtp section and change it to the following:<\/p>\n# nano \/etc\/dovecot\/conf.d\/10-master.conf<\/pre>\nservice lmtp {\r\n unix_listener \/var\/spool\/postfix\/private\/dovecot-lmtp {\r\n mode = 0600\r\n user = postfix\r\n group = postfix\r\n }\r\n}\r\n<\/pre>\nfind the service auth section and change it to:<\/p>\n
service auth {\r\n unix_listener \/var\/spool\/postfix\/private\/auth {\r\n mode = 0666\r\n user = postfix\r\n group = postfix\r\n}\r\n\r\nunix_listener auth-userdb {\r\n mode = 0600\r\n user = vmail\r\n\r\n}\r\nuser = dovecot\r\n}\r\n\r\n<\/pre>\nChange the service auth-worker section to the following:<\/p>\n
service auth-worker {\r\nuser = vmail\r\n}\r\n<\/pre>\nSet the permissions:<\/p>\n
# chown -R vmail:dovecot \/etc\/dovecot\r\n# chmod -R o-rwx \/etc\/dovecot<\/pre>\nEnable and restart the Dovecot service<\/p>\n
# systemctl enable dovecot\r\n# systemctl restart dovecot<\/pre>\n