在Ubuntu 18.04上使用PostfixAdmin設置郵件服務器


在本教程中,我們將介紹在Ubuntu 18.04上使用Postfix,Dovecot,SQLite和PostfixAdmin安裝郵件服務器所需的步驟。
PostfixAdmin是一個基於Web的免費界面。使用PostfixAdmin,我們可以為許多用戶配置和管理基於Postfix的電子郵件服務器。
Postfix是一種郵件傳輸代理(MTA),可以在Internet上的不同郵件服務器之間中繼郵件。
Dovecot是一種流行的本地傳遞代理(LDA),它將從后綴接收到的郵件傳遞到其最終目的地(郵箱,程序等)。
將這三個軟件包組合在一起,您的服務器將可以發送和接收電子郵件,以及用於管理電子郵件服務器的基於Web的界面。
要求:
就本教程而言,我們將使用Ubuntu 18.04 。
還需要完全SSH根訪問權限或具有sudo特權的用戶。
步驟1、入門設置
使用以下命令以root用戶身份通過​​SSH連接到服務器:
ssh root@IP_ADDRESS -p PORT_NUMBER
並將IP_ADDRESS和PORT_NUMBER替換為您的實際服務器IP地址和SSH端口號。
在開始安裝之前,您需要將系統軟件包更新為最新版本。
您可以通過運行以下命令來執行此操作:
apt-get update
apt-get upgrade
步驟2、創建系統用戶
在創建系統用戶之前,我們需要安裝一些必需的軟件包。
sudo apt-get -y install wget nano dbconfig-common sqlite3
現在,我們將使用以下命令創建一個新的系統用戶。該用戶將是所有郵箱的所有者。
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
步驟3、安裝PHP 7.3和所有必需的PHP模塊
我們將使用以下命令安裝最新穩定版本的PHP 7.3和所有必需的PHP模塊:
apt install software-properties-common python-software-properties
add-apt-repository ppa:ondrej/php
apt update
sudo apt-get install php7.3 php7.3-cli php7.3-common php-fpm php-cli php7.3-mbstring php7.3-imap php7.3-sqlite3
一旦安裝了這些,我們就可以安裝Nginx,它將用作PostfixAdmin的Web服務器。
步驟4、安裝和配置Nginx
要從官方Ubuntu存儲庫安裝Nginx,我們將運行以下命令:
sudo apt-get install nginx
現在,我們需要創建一個具有以下內容的新Nginx服務器塊。首先,打開文件:
sudo nano /etc/nginx/sites-available/postfixadmin.your_domain.com
然后添加以下內容:

server {
listen 80;
server_name postfixadmin.your_domain.com;
root /var/www/postfixadmin-3.0;
index index.php;
charset utf-8;


location / {
try_files $uri $uri/ index.php;
}

location ~* \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
}

 

確保使用您的實際注冊域名替換your_domain.com。
為了激活服務器塊,我們將創建一個符號鏈接:
sudo ln -s /etc/nginx/sites-available/postfixadmin.your_domain.com /etc/nginx/sites-enabled/postfixadmin.your_domain.com
為了使Nginx意識到這一點,我們需要重新啟動它。
sudo service nginx restart
步驟5、安裝和配置PostfixAdmin
PostfixAdmin支持MySQL,PostgreSQL和SQLite數據庫。在本教程中,我們將使用SQLite作為我們的數據庫系統。
現在,我們將下載PostfixAdmin並將其解壓縮到/var/www/目錄中:
wget -q -O - "http://downloads.sourceforge.net/project/postfixadmin/postfixadmin/postfixadmin-3.0/postfixadmin-3.0.tar.gz" | sudo tar -xzf - -C /var/www
打開郵件配置文件並編輯以下值:

sudo nano /var/www/postfixadmin-3.0/config.inc.php
$CONF['configured'] = true;
$CONF['database_type'] = 'sqlite';
$CONF['database_name'] = '/var/vmail/postfixadmin.db';
// $CONF['database_host'] = 'localhost';
// $CONF['database_user'] = 'postfix';
// $CONF['database_password'] = 'postfixadmin';
// $CONF['database_name'] = 'postfix';

$CONF['domain_path'] = 'NO';
$CONF['domain_in_mailbox'] = 'YES';

現在,更改文件夾的所有者,使其屬於Web服務器:
sudo chown -R www-data: /var/www/postfixadmin-3.0
然后,我們將創建SQLite數據庫:
sudo touch /var/vmail/postfixadmin.db
sudo chown vmail:mail /var/vmail/postfixadmin.db
sudo usermod -a -G mail www-data
如果一切設置正確,則應打開瀏覽器並轉到:
https://postfixadmin.your_domain.com/setup.php
您應該看到類似以下的內容:
Depends on: SQLite – OK
Testing database connection – OK – sqlite://:xxxxx@//var/vmail/postfixadmin.db
要創建新的管理員用戶,請運行以下命令:
bash /var/www/postfixadmin-3.0/scripts/postfixadmin-cli admin add admin@your_domain.com --password strong_password --password2 strong_password --superadmin 1 --active 1
確保使用實際的強密碼替換strong_password。
如果您不想使用命令行執行此操作,還可以從PostfixAdmin Web界面創建一個新的admin用戶。
步驟6、安裝和配置Postfix
我們可以使用以下命令安裝Postfix:
sudo apt-get install postfix
我們需要創建以下文件:
sudo nano /etc/postfix/sqlite_virtual_alias_maps.cf
將以下內容添加到文件中:
dbpath = /var/vmail/postfixadmin.db
query = SELECT goto FROM alias WHERE address='%s' AND active = '1'
現在是下一個文件:
sudo nano /etc/postfix/sqlite_virtual_alias_domain_maps.cf
添加到它:
dbpath = /var/vmail/postfixadmin.db
query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = printf('%u', '@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'
保存該文件,然后添加下一個文件:
sudo nano /etc/postfix/sqlite_virtual_alias_domain_catchall_maps.cf
將此內容插入其中:
dbpath = /var/vmail/postfixadmin.db
query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = printf('@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'
保存並關閉文件。讓我們創建下一個:
sudo nano /etc/postfix/sqlite_virtual_domains_maps.cf
將此添加到文件中:
dbpath = /var/vmail/postfixadmin.db
query = SELECT domain FROM domain WHERE domain='%s' AND active = '1'
保存並再次關閉。再一次,讓我們創建所需的下一個文件。
sudo nano /etc/postfix/sqlite_virtual_mailbox_maps.cf
這就是應該添加的內容:
dbpath = /var/vmail/postfixadmin.db
query = SELECT maildir FROM mailbox WHERE username='%s' AND active = '1'
到最后一個文件。使用以下命令創建它:
sudo nano /etc/postfix/sqlite_virtual_alias_domain_mailbox_maps.cf
然后添加到它:
dbpath = /var/vmail/postfixadmin.db
query = SELECT maildir FROM mailbox,alias_domain WHERE alias_domain.alias_domain = '%d' and mailbox.username = printf('%u', '@', alias_domain.target_domain) AND mailbox.active = 1 AND alias_domain.active='1'
最后,保存並關閉文件。
現在,我們將通過執行以下命令來編輯 main.cf 配置文件:
postconf -e "myhostname = $(hostname -A)"
我們需要編輯文件,使其看起來像這樣。我們將添加剛剛創建的所有文件:

postconf -e "virtual_mailbox_domains = sqlite:/etc/postfix/sqlite_virtual_domains_maps.cf"
postconf -e "virtual_alias_maps = sqlite:/etc/postfix/sqlite_virtual_alias_maps.cf, sqlite:/etc/postfix/sqlite_virtual_alias_domain_maps.cf, sqlite:/etc/postfix/sqlite_virtual_alias_domain_catchall_maps.cf"
postconf -e "virtual_mailbox_maps = sqlite:/etc/postfix/sqlite_virtual_mailbox_maps.cf, sqlite:/etc/postfix/sqlite_virtual_alias_domain_mailbox_maps.cf"

postconf -e "smtpd_tls_cert_file = /etc/ssl/certs/ssl-cert-snakeoil.pem"
postconf -e "smtpd_tls_key_file = /etc/ssl/private/ssl-cert-snakeoil.key"
postconf -e "smtpd_use_tls = yes"
postconf -e "smtpd_tls_auth_only = yes"

postconf -e "smtpd_sasl_type = dovecot"
postconf -e "smtpd_sasl_path = private/auth"
postconf -e "smtpd_sasl_auth_enable = yes"
postconf -e "smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination"

postconf -e "mydestination = localhost"
postconf -e "mynetworks = 127.0.0.0/8"
postconf -e "inet_protocols = ipv4"

postconf -e "virtual_transport = lmtp:unix:private/dovecot-lmtp"

完成后,保存並關閉文件。
另外,我們需要編輯master.cf文件。像這樣打開文件:
sudo nano /etc/postfix/master.cf
找到提交的inet n和smtps inet n部分,並確保文件看起來像這樣。

smtp inet n - y - - smtpd
#smtp inet n - y - 1 postscreen
#smtpd pass - - y - - smtpd
#dnsblog unix - - y - 0 dnsblog
#tlsproxy unix - - y - 0 tlsproxy
submission inet n - y - - smtpd
-o syslog_name=postfix/submission
-o smtpd_tls_security_level=encrypt
-o smtpd_sasl_auth_enable=yes
# -o smtpd_reject_unlisted_recipient=no
-o smtpd_client_restrictions=permit_sasl_authenticated,reject
# -o smtpd_helo_restrictions=$mua_helo_restrictions
# -o smtpd_sender_restrictions=$mua_sender_restrictions
# -o smtpd_recipient_restrictions=
# -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
-o milter_macro_daemon_name=ORIGINATING
smtps inet n - y - - smtpd
-o syslog_name=postfix/smtps
# -o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes
# -o smtpd_reject_unlisted_recipient=no
-o smtpd_client_restrictions=permit_sasl_authenticated,reject
# -o smtpd_helo_restrictions=$mua_helo_restrictions
# -o smtpd_sender_restrictions=$mua_sender_restrictions
# -o smtpd_recipient_restrictions=
# -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
-o milter_macro_daemon_name=ORIGINATING


保存並關閉文件。現在重新啟動Postfix服務並啟用它。
systemctl enable postfix
systemctl restart postfix
步驟7、安裝和配置Dovecot
現在已完成所有其他設置,我們需要安裝具有SQLite支持的Dovecot:
sudo apt-get install dovecot-imapd dovecot-lmtpd dovecot-pop3d dovecot-sqlite
我們需要打開 /etc/dovecot/conf.d/10-mail.conf 文件並更改以下值:

mail_location = maildir:/var/vmail/%d/%n
mail_privileged_group = mail
mail_uid = vmail
mail_gid = mail
first_valid_uid = 150
last_valid_uid = 150

然后,我們需要打開 /etc/dovecot/conf.d/10-auth.conf 文件並更改以下值,使其看起來像這樣:
auth_mechanisms = plain login
#!include auth-system.conf.ext
!include auth-sql.conf.ext
我們將創建一個新 dovecot-sql.conf.ext 文件:
sudo nano /etc/dovecot/dovecot-sql.conf.ext
將其添加為內容:

driver = sqlite
connect = /var/vmail/postfixadmin.db
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/%n' as home, 'maildir:/var/vmail/%d/%n' as mail, \
150 AS uid, 8 AS gid, printf('dirsize:storage=', quota) AS quota \
FROM mailbox WHERE username = '%u' AND active = '1'
user_query = \

在 /etc/dovecot/conf.d/10-ssl.conf 文件中,啟用SSL支持:
ssl = yes
我們將打開 /etc/dovecot/conf.d/15-lda.conf 文件並設置 postmaster_address 電子郵件地址。
postmaster_address = postmaster@vps.your_domain.com
確保使用域名替換your_domain.com。
另外,我們需要打開/etc/dovecot/conf.d/10-master.conf文件,找到服務lmtp部分,並把它改成這樣:
service lmtp {
unix_listener /var/spool/postfix/private/dovecot-lmtp {
mode = 0600
user = postfix
group = postfix
}
}
然后找到該service auth部分並將其更改為:
service auth {
unix_listener /var/spool/postfix/private/auth {
mode = 0666
user = postfix
group = postfix
}
unix_listener auth-userdb {
mode = 0600
user = vmail
#group = vmail
}
user = dovecot
}
關閉文件,並設置權限:
chown -R vmail:dovecot /etc/dovecot
chmod -R o-rwx /etc/dovecot
啟用並重新啟動鴿舍服務:
systemctl enable dovecot
systemctl restart dovecot
如果一切設置正確,則應該能夠登錄到PostfixAdmin Web界面並創建第一個虛擬域和郵箱。A5互聯https://www.a5idc.net/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM