ผลต่างระหว่างรุ่นของ "คู่มือการพัฒนา EAI Mail Server บน Ubuntu 20"

จาก Wiki_EAI
ไปยังการนำทาง ไปยังการค้นหา
 
(ไม่แสดง 77 รุ่นระหว่างกลางโดยผู้ใช้ 2 คน)
แถว 1: แถว 1:
 
<languages/>
 
<languages/>
 
<translate>
 
<translate>
 
 
= EAI Email Server = <!--T:1-->
 
= EAI Email Server = <!--T:1-->
  
แถว 8: แถว 7:
  
 
== Software ที่นำมาทดสอบ == <!--T:3-->
 
== Software ที่นำมาทดสอบ == <!--T:3-->
# Ubuntu 20.04.3 LTS
+
# Ubuntu 20.04.3 LTS [[https://www.kon.in.th/download/mailthai-for-participants.zip workshop virtualbox image ]]
 
# Postfix 3.4.13
 
# Postfix 3.4.13
 
# Dovecot 2.3.7.2
 
# Dovecot 2.3.7.2
แถว 14: แถว 13:
 
# Roundcube 1.4.4 [[https://roundcube.net/download/ ดาวน์โหลด]] [[https://github.com/roundcube/roundcubemail/releases/download/1.4.4/roundcubemail-1.4.4-complete.tar.gz tar]]
 
# Roundcube 1.4.4 [[https://roundcube.net/download/ ดาวน์โหลด]] [[https://github.com/roundcube/roundcubemail/releases/download/1.4.4/roundcubemail-1.4.4-complete.tar.gz tar]]
  
== EAI Workshop Server == <!--T:5-->
+
== Workshop Slide [[https://kon.in.th/download/EAI_Training_2022.pdf ดาวน์โหลด]] ==
# [EAI Workshop [https://eai-lab.bknix.co.th/]]
+
# [[https://kon.in.th/download/EAI_Training_2022.pdf ดาวน์โหลด]]
#* user: eai
 
#* pass: eai+ws@thnic
 
  
# ดาวน์โหลดและติดตั้ง VirtualBox และ VirtualBox Extension Pack
+
== EAI Workshop Authentication  == <!--T:5-->
# ดาวน์โหลดไฟล์ CentOS8 for Virtualbox (centos-eai-wokshop.ova) ที่จัดเตรียมไว้ให้ตามลิงค์ด้านบน
+
* [[https://eai-lab.bknix.co.th/ EAI Workshop]]
# ดับเบิ้ลคลิ๊กที่ไฟล์ centos-eai-wokshop.ova เพื่อ import เข้าโปรแกรม Virtualbox [https://kon.in.th/%E0%B8%A7%E0%B8%B4%E0%B8%81%E0%B8%B4/1.png]
+
user: eai
# หลังจาก import เสร็จเรียบร้อยแล้ว ที่โปรแกรม VirtualBox ให้ดับเบิ้ลคลิ๊กที่ centos-eai-wokshop ด้านซ้ายมือ เพื่อเปิด server
+
  pass: eaiws@2022
# ถ้าพบข้อผิดพลาด "ไม่สามารถเริ่มการทำงานเครื่อง centos-eai-workshop เนื่องจากไม่พบแผงวงจรเครือข่าย" ให้กด "เปลี่ยนการติดตั้งเครือข่าย" จะขึ้นหน้าเปลี่ยนเครือข่าย ให้กด "ตกลง" [https://kon.in.th/%E0%B8%A7%E0%B8%B4%E0%B8%81%E0%B8%B4/2.png]
 
# เมื่อเปิดเครื่องได้แล้ว ให้ login ด้วย user, pass ที่เตรียมไว้ให้ข้างต้น และใช้คำสั่ง "ifconfig" เพื่อตรวจสอบ IP ของเครื่อง [https://kon.in.th/%E0%B8%A7%E0%B8%B4%E0%B8%81%E0%B8%B4/3.png]
 
# ให้จด IP นี้ไว้ใช้สำหรับ ssh เข้าเครื่อง Server ของเราในการทำงาน workshop (*แต่ละเครื่องจะได้ IP แตกต่างกัน)
 
(สำหรับเครื่อง mac ต้อง allow ให้ virtualbox ทำงานได้ตามนี้ [https://kon.in.th/%E0%B8%A7%E0%B8%B4%E0%B8%81%E0%B8%B4/4.png])
 
  
<!--T:6-->
+
* โดเมนที่ใช้เป็นตัวอย่างใน workshop
* หมายเหตุ
+
English : mailthai99.in.th
: คู่มือนี้ไม่ได้อธิบายถึงการติดตั้ง Centos 8
+
ภาษาไทย : จดหมาย99.ไทย
: Postfix ต้องเป็น Version 3.x.x ขึ้นไป จะรองรับ EAI
+
(Punycode) : xn--15-6qi0c9cg5dxc.xn--o3cw4h
: หากจะนำไปใช้งานจริงจะต้องจดทะเบียนโดเมน และมี dns server ด้วย
+
* Ubuntu Server:
 +
User: user
 +
Pass: userpass
 +
* Passphase for SSL Key
 +
Key: mailthai.key, mailthai.crt, mailthai.csr
 +
Pass: mailpass
 +
* MySQL
 +
User: root
 +
Pass: rootpass
 +
 +
DB: eaimail
 +
User: dbuser
 +
Pass: dbuserpass
 +
 +
DB: roundcubemail
 +
User: rcmail
 +
Pass: rcmailpass
 +
 +
* Roundcube
 +
User: thai@mailthai<span style="color: green;>XX</span>.in.th
 +
Pass: thaipass
 +
User: ไทย@จดหมาย<span style="color: green;>XX</span>.ไทย
 +
Pass: thaipass
  
 
= การติดตั้ง EAI Email Server = <!--T:7-->
 
= การติดตั้ง EAI Email Server = <!--T:7-->
  
== 1. ตั้งค่า hostname == <!--T:8-->
+
== ตั้งค่า hostname == <!--T:8-->
  
 
<!--T:9-->
 
<!--T:9-->
1. อัพเดตแพคเกจของ mail serverให้เป็นเวอร์ชั่นล่าสุด ด้วยคำสั่ง
+
===<span style="color:green;">เข้าใช้งานเครื่องด้วย สิทธิ์ root </span>===
  '''# yum -y update'''
+
'''sudo su
 +
''รหัสผ่าน <span style="color:green;">: userpass </span> ''
 +
 
 +
=== อัพเดตแพคเกจของ mail serverให้เป็นเวอร์ชั่นล่าสุด ด้วยคำสั่ง ===
 +
  '''apt update && apt -y upgrade'''
  
 
<!--T:10-->
 
<!--T:10-->
2. กำหนดชื่อ hostname ในตัวอย่างนี้กำหนดชื่อว่า mail.eai.in.th ด้วยคำสั่ง
+
=== <span style="color:green;">กำหนดชื่อ hostname และตรวจสอบด้วยคำสั่ง </span> ===
'''# hostnamectl set-hostname ''[[mail.eai.in.th]]'''''
+
* กำหนดชื่อ hostname  
 +
'''hostnamectl set-hostname ''mail.mailthai<span style="color:green">99</span>.in.th'' '''
 +
''ตัวอย่าง เช่น <span style="color:green">mail.mailthai99.in.th</span>
 +
* ตรวจสอบ hostname
 +
'''hostnamectl'''
 +
''ตัวอย่าง''
 +
Static hostname: mail.mailthai99.in.th
  
 
<!--T:11-->
 
<!--T:11-->
3. เพิ่มค่า hostname ชื่อเดียวกับชื่อที่กำหนดข้างต้นในไฟล์ hosts  
+
=== <span style="color:green;">เพิ่มค่า hostname ชื่อเดียวกับชื่อที่กำหนดข้างต้นในไฟล์ hosts </span> ===
  '''# vi /etc/hosts'''
+
  '''nano /etc/hosts'''
 +
''ตัวอย่าง เช่น <span style="color:green">mail.mailthai99.in.th</span>
  
 
<!--T:12-->
 
<!--T:12-->
 
''ตัวอย่างไฟล์ hosts''
 
''ตัวอย่างไฟล์ hosts''
  127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 '''''[[mail.eai.in.th]]'''''
+
  127.0.0.1   localhost mail.mailthai<span style="color:green">99</span>.in.th
::1        localhost localhost.localdomain localhost6 localhost6.localdomain6 '''''[[mail.eai.in.th]]'''''
 
  
<!--T:13-->
+
'' กด crtl+X และกด y เพื่อ save และออกจาก nano
''*ระวัง envelopment ของทาง partner ว่าติดตั้ง centos ในรูปแบบใดมาให้ (อาจจะต้องติดตั้ง service เพิ่ม หากติดตั้งมาแบบ minimal เช่นใช้คำสั่ง vi ไม่ได้)''
 
  
<!--T:14-->
+
=== ตั้งค่าเวลาของ Mail Server และตรวจสอบ ===
4. ติดตั้ง wget เพื่อใช้ดาวน์โหลดซอฟแวร์ที่จำเป็นต้องใช้งานเพิ่ม
+
* ตั้งค่าเวลาของ Mail Server
  '''# yum -y install wget'''
+
''' timedatectl set-timezone "Asia/Bangkok"
 +
* ตรวจสอบเวลาของ Mail Server
 +
''' timedatectl  status
 +
''ตัวอย่าง''
 +
<span style="color:green">Local time: Fri 2022-01-14 12:06:03 +07</span>
 +
Universal time: Fri 2022-01-14 05:06:03 UTC
 +
RTC time: Fri 2022-01-14 05:06:03   
 +
<span style="color:green">Time zone: Asia/Bangkok (+07, +0700)</span>
 +
System clock synchronized: yes                       
 +
NTP service: active                   
 +
  RTC in local TZ: no
  
<!--T:15-->
+
== ตั้งค่า firewall == <!--T:16-->
5. ติดตั้ง telnet เพื่อทดสอบการทำงานของ email server
+
1. เปิด firewall service และตรวจสอบสถานะ ด้วยคำสั่ง
'''# yum -y install telnet'''
 
  
== 2. ตั้งค่า firewall == <!--T:16-->
+
<!--T:17-->
1. ตรวจสอบสถานะ firewalld service บน mail server
+
* เปิด firewall service
*ต้องมีสถานะ active (running)
+
  '''ufw enable'''
 
 
  <!--T:17-->
 
'''# systemctl status firewalld'''
 
  
 
<!--T:18-->
 
<!--T:18-->
 
''ตัวอย่าง''
 
''ตัวอย่าง''
  
<!--T:19-->
+
<!--T:19-->
'''Active: <span style='color:red'>active (running)</span> since Mon 2019-11-18 14:12:03 +07; 1h 13min ago'''
+
Command may disrupt existing ssh connections. Proceed with operation (y|n)? <span style="color:green">y # ให้ตอบ y</span>
 +
Firewall is active and enabled on system startup
  
 
<!--T:20-->
 
<!--T:20-->
2. ตรวจสอบ firewall zone และ network interface ที่เปิดอยู่
+
2. เปิด firewall services ของ email server
  '''# firewall-cmd --get-active-zones'''
+
  '''ufw allow in ssh'''
 +
'''ufw allow http'''
 +
'''ufw allow https'''
 +
'''ufw allow smtp'''
 +
'''ufw allow smtps'''
 +
'''ufw allow imap'''
 +
'''ufw allow imaps'''
 +
'''ufw allow mysql'''
 +
 
 
''ตัวอย่าง''
 
''ตัวอย่าง''
  '''public
+
  Rule added
  '''interfaces: ens32
+
  Rule added (v6)
  
 
<!--T:21-->
 
<!--T:21-->
3. ตรวจสอบรายการ services ของ firewall ที่เปิดอยู่ที่ zone public
+
3. ตรวจสอบ firewall services ที่เปิดอยู่
  '''# firewall-cmd --zone=public --list-services'''
+
  '''ufw status'''
  
 
<!--T:22-->
 
<!--T:22-->
 
''ตัวอย่าง''
 
''ตัวอย่าง''
''' cockpit dhcpv6-client ssh '''
 
  
 
<!--T:23-->
 
<!--T:23-->
4. เปิด firewall services ที่ zone public ของ mail server (สามารถเปิดเท่าต้องการได้)
+
Status: active
  '''# firewall-cmd --permanent --zone=public --add-service={http,https,mysql,smtp,smtps,imap,imaps}
+
  To                        Action      From
  '''# firewall-cmd --reload
+
  --                         ------     ----
 
+
  22/tcp                    ALLOW      Anywhere                 
<!--T:24-->
+
  80/tcp                    ALLOW      Anywhere                 
5. ตรวจสอบรายการ services ของ firewall ที่เปิดอยู่ที่ zone public อีกครั้ง จะพบ services เปิดเพิ่มขึ้นมา
+
  443/tcp                    ALLOW      Anywhere                 
 
+
  25/tcp                    ALLOW      Anywhere                 
<!--T:25-->
+
  465/tcp                    ALLOW      Anywhere                 
''*ref: configure firewall services for mail server (https://spalinux.com/2015/06/centos-7-basic-configure-firewall-by-firewall-cmd)''
+
  143/tcp                    ALLOW       Anywhere                 
  '''# firewall-cmd --zone=public --list-services'''
+
  993/tcp                    ALLOW       Anywhere                 
 
+
  3306/tcp                  ALLOW       Anywhere             
<!--T:26-->
+
  22/tcp (v6)                ALLOW      Anywhere (v6)           
''ตัวอย่าง''
+
  80/tcp (v6)                ALLOW      Anywhere (v6)           
  ''' cockpit dhcpv6-client <span style='color:red'>http https imap imaps mysql smtp smtps</span> ssh '''
+
  443/tcp (v6)              ALLOW       Anywhere (v6)           
 
+
  25/tcp (v6)                ALLOW       Anywhere (v6)           
<!--T:27-->
+
  465/tcp (v6)              ALLOW      Anywhere (v6)           
6. ในตัวอย่างนี้เราแนะนำให้ปิดการทำงานของ SELinux เพื่อความสะดวกในการตั้งค่า (ไม่แนะนำให้ทำกับเครื่อง production)
+
  143/tcp (v6)              ALLOW      Anywhere (v6)           
*เปลี่ยน SELinux=enforcing เป็น SELinux=disabled
+
  993/tcp (v6)              ALLOW      Anywhere (v6)
  '''# vi /etc/selinux/config'''
+
  3306/tcp (v6)              ALLOW      Anywhere (v6)   
 
 
<!--T:28-->
 
''ตัวอย่าง''
 
 
 
  <!--T:29-->
 
'''# This file controls the state of SELinux on the system.
 
  '''# SELINUX= can take one of these three values:
 
  '''#       enforcing - SELinux security policy is enforced.
 
  '''#       permissive - SELinux prints warnings instead of enforcing.
 
  '''#       disabled - No SELinux policy is loaded.
 
  '''SELINUX=<span style='color:red'>disabled</span>
 
  '''# SELINUXTYPE= can take one of these two values:
 
  '''#       targeted - Targeted processes are protected,
 
  '''#       mls - Multi Level Security protection.
 
  '''SELINUXTYPE=targeted
 
 
7. ตรวจสอบสถานะ SELinux อีกครั้ง
 
* สั่ง restart server และทำการตรวจสอบ
 
  '''# reboot'''
 
'''# sestatus'''
 
 
 
<!--T:30-->
 
''ตัวอย่าง''
 
 
 
<!--T:31-->
 
''' disabled '''
 
 
 
== 3. สร้าง SSL Cert == <!--T:32-->
 
*เป็นขั้นตอนการสร้าง SSL key เตรียมไว้ใช้งานกับ postfix และ dovecot
 
1. สร้าง private key
 
  '''# cd /etc/pki/tls/certs
 
  '''# /usr/bin/openssl genrsa -aes128 2048 > '''[[eai.key]]'''
 
 
 
<!--T:33-->
 
'' ตัวอย่าง ''
 
'''Generating RSA private key, 2048 bit long modulus
 
'''...
 
'''...
 
'''e is 65537 (0x10001)
 
'''Enter pass phrase: '''[[#set passphrase]]'''
 
'''Verifying - Enter pass phrase:  '''[[#confirm]]'''
 
 
 
<!--T:34-->
 
2. ถอด Passphase ออกจาก private key ที่สร้างขึ้น
 
  '''# /usr/bin/openssl rsa -in '''[[eai.key]]''' -out '''[[eai.key]]'''
 
 
 
<!--T:35-->
 
'' ตัวอย่าง ''
 
  
<!--T:36-->
+
<!--T:31-->
'''Enter pass phrase for eai.key:  '''[[#input passphrase]]'''
+
''*ref: configure firewall services for mail server (https://linuxconfig.org/ubuntu-20-04-open-http-port-80-and-https-port-443-with-ufw)''
'''writing RSA key'''
 
  
<!--T:37-->
+
== สร้าง SSL Cert == <!--T:32-->
3. สร้าง csr
+
* เป็นขั้นตอนการสร้าง SSL cert/key เตรียมไว้ใช้งานกับ postfix และ dovecot
'''# /usr/bin/openssl req -utf8 -new -key '''[[eai.key]]''' -out '''[[eai.csr]]'''
+
* ในเครื่อง workshop ได้เตรียม cert mailthai.crt, mailthai.key, mailthai.csr ไว้แล้ว
  
<!--T:38-->
+
<span style='color:green'>1. สร้าง private key</span>
'' ตัวอย่าง ''
+
  '''cd /etc/ssl/private'''
'''You are about to be asked to enter information that will be incorporated
+
  '''openssl req -new -x509 -days 365 -nodes -newkey rsa:2048 -keyout mailthai<span style='color:green'>XX</span>.key -out mailthai<span style='color:green'>XX</span>.crt -subj "/C=TH/ST=Bangkok/L=Dusit/O=EAI/OU=IT/CN=mail.mailthai<span style='color:green'>XX</span>.in.th" '''
'''into your certificate request.
 
  '''What you are about to enter is what is called a Distinguished Name or a DN.
 
'''There are quite a few fields but you can leave some blank
 
'''For some fields there will be a default value,
 
'''If you enter '.', the field will be left blank.
 
  '''-----
 
'''Country Name (2 letter code) [XX]:'''[[TH]]''' '''[[#country]]'''
 
'''State or Province Name (full name) []:'''[[Bangkok]]'''   '''[[#state]]'''
 
'''Locality Name (eg, city) [Default City]:'''[[Dusit]]''' '''[[#city]]'''
 
'''Organization Name (eg, company) [Default Company Ltd]:'''[[EAI]]'''  '''[[#company]]'''
 
'''Organizational Unit Name (eg, section) []:'''[[IT]]'''  '''[[#department]]'''
 
'''Common Name (eg, your name or your server's hostname) []:'''[[mail.eai.in.th]]'''  '''[[#server's FQDN]]'''
 
'''Email Address []:'''[[xxx@eai.in.th]]''' '''[[#email address]]'''
 
'''Please enter the following 'extra' attributes
 
'''to be sent with your certificate request
 
'''A challenge password []: '''[[#Enter]]'''
 
'''An optional company name []: '''[[#Enter]]'''
 
  
<!--T:39-->
 
4. ทำการ generate key
 
'''# /usr/bin/openssl x509 -in '''[[eai.csr]]''' -out '''[[eai.crt]]''' -req -signkey '''[[eai.key]]''' -days 365'''
 
  
<!--T:40-->
+
<span style='color:green'>2. ตรวจสอบ key ที่สร้างขึ้น</span>
 +
'''ls'''
 
'' ตัวอย่าง ''
 
'' ตัวอย่าง ''
 +
mailthai99.crt  mailthai99.csr
  
<!--T:41-->
+
== ติดตั้ง MySQL, Postfix, Dovecot == <!--T:42-->
'''Signature ok
+
1. ติดตั้ง MySQL, Postfix , Dovecot และ packet ที่จำเป็นสำหรับ email sever ด้วยคำสั่ง
'''subject=/C=JP/ST=Hiroshima/L=Hiroshima/O=GTS/OU=Server World/CN=www.srv.world/emailAddress=xxx@eai.in.th
+
  '''apt -y install postfix postfix-mysql dovecot-core dovecot-mysql dovecot-imapd dovecot-pop3d dovecot-lmtpd  mysql-server'''
'''Getting Private key'''
 
 
 
 
 
== 4. ติดตั้ง mariadb, postfix, dovecot == <!--T:42-->
 
1. ติดตั้ง mariadb , Postfix , Dovecot และ Packet ที่จำเป็นกับ Server
 
  '''# yum -y install postfix dovecot mariadb-server dovecot-mysql postfix-mysql'''
 
  
 
<!--T:43-->
 
<!--T:43-->
2. สั่ง start service ของ postfix, dovecot, mariadb ทุกครั้งที่เปิด Server
+
2. สั่ง start service ของ MySQL, Postfix, Dovecot ด้วยคำสั่ง
  '''# systemctl start postfix && systemctl start dovecot && systemctl start mariadb'''
+
  '''systemctl start mysql && systemctl start postfix && systemctl start dovecot'''
'''# systemctl enable postfix && systemctl enable dovecot && systemctl enable mariadb'''
 
  
 
<!--T:44-->
 
<!--T:44-->
3. ดูสถานะการทำงานของ postfix, dovecot, maridb
+
3. ดูสถานะการทำงานของ MySQL, Postfix, Dovecot
  '''# systemctl status postfix'''
+
  '''systemctl status mysql'''
  '''# systemctl status dovecot'''
+
  '''systemctl status postfix'''
  '''# systemctl status mariadb'''
+
  '''systemctl status dovecot'''
 +
''ตัวอย่าง ''
 +
Active: active (running) since Wed 2022-01-05 14:42:29 +07; 33min ago
  
== 5. ตั้งค่า mariadb == <!--T:45-->
+
== ตั้งค่า MySQL == <!--T:45-->
1. ตั้งค่า UTF8 ให้กับ mysql เพื่อให้รองรับภาษาไทย โดยเพิ่มคำสั่งด้าล่างลงใน /etc/my.cnf.d/maridb-server.cnf ใต้ [mysqld]
+
=== ตั้งค่า UTF8 ให้กับ mysql เพื่อให้รองรับภาษาไทย โดยเพิ่มคำสั่งด้านล่างลงในไฟล์ mysqld.cnf ใต้ [mysqld] ===
  '''# vi /etc/my.cnf.d/mariadb-server.cnf'''
+
  '''nano /etc/mysql/mysql.conf.d/mysqld.cnf'''
 +
'' ตัวอย่าง ไฟล์ mysqld.cnf''
  
<!--T:46-->
+
<!--T:46-->
#
+
  # Here is entries for some specific programs
# These groups are read by MariaDB server.
+
  # The following values assume you have at least 32M ram
# Use it for options that only the server (but not clients) should see
+
   
  #
 
# See the examples of server my.cnf files in /usr/share/mysql/
 
#
 
# this is read by the standalone daemon and embedded servers
 
[server]
 
# this is only for the mysqld standalone daemon
 
  # Settings user and group are ignored when systemd is used.
 
# If you need to run mysqld under a different user or group,
 
# customize your systemd unit file for mysqld/mariadb according to the
 
  # instructions in http://fedoraproject.org/wiki/Systemd
 
 
  [mysqld]
 
  [mysqld]
  '''''[[character-set-server = utf8]]'''''
+
  <span style='color:green'>character-set-server = utf8</span>
  '''''[[collation-server = utf8_general_ci]]'''''
+
  <span style='color:green'>collation-server = utf8_general_ci</span>
  '''''[[skip-character-set-client-handshake]]'''''
+
  <span style='color:green'>skip-character-set-client-handshake</span>
  '''''[[bind-address=127.0.0.1]]'''''
+
  <span style='color:green'>bind-address=127.0.0.1</span>
datadir=/var/lib/mysql
+
  #  
  socket=/var/lib/mysql/mysql.sock
+
  # * Basic Settings
  log-error=/var/log/mariadb/mariadb.log
 
  pid-file=/run/mariadb/mariadb.pid
 
 
  #
 
  #
  # * Galera-related settings
+
  user            = mysql
#
 
[galera]
 
# Mandatory settings
 
#wsrep_on=ON
 
#wsrep_provider=
 
#wsrep_cluster_address=
 
#binlog_format=row
 
#default_storage_engine=InnoDB
 
#innodb_autoinc_lock_mode=2
 
#
 
# Allow server to accept connections on all interfaces.
 
#
 
#bind-address=0.0.0.0
 
#
 
# Optional setting
 
#wsrep_slave_threads=1
 
#innodb_flush_log_at_trx_commit=0
 
# this is only for embedded server
 
[embedded]
 
# This group is only read by MariaDB servers, not by MySQL.
 
# If you use the same .cnf file for MySQL and MariaDB,
 
# you can put MariaDB-only options here
 
[mariadb]
 
# This group is only read by MariaDB-10.3 servers.
 
# If you use the same .cnf file for MariaDB of different versions,
 
# use this group for options that older servers don't understand
 
[mariadb-10.3]
 
  
 
<!--T:47-->
 
<!--T:47-->
2. สั่ง restart mariadb
+
=== สั่ง restart MySQL ===
  '''# systemctl restart mariadb'''
+
  '''systemctl restart mysql'''
  
 
<!--T:48-->
 
<!--T:48-->
3. ตั้งค่าความปลอดภัยการเข้างาน mysql โดยจะมีการตั้ง password ของ root user เพื่อ login เข้าใช้งาน (*กำหนดเพียง password ใหม่ และในส่วนอื่นๆให้ enter ผ่านได้เลย)
+
=== ตั้งค่าความปลอดภัยการเข้าใช้งาน MySQL โดยจะมีการตั้งค่า password ของ user root เพื่อ login เข้าใช้งาน (* กำหนดเพียง password ใหม่ ในส่วนอื่นๆ ให้ enter ผ่านได้เลย) ===
  '''#  mysql_secure_installation'''
+
  '''mysql_secure_installation'''
 +
''ตัวอย่าง ''
 +
Please set the password for root here.
 +
 +
New password:                    <span style='color:green'># rootpass</span>
 +
 +
  Re-enter new password:          <span style='color:green'># rootpass</span>
  
 
<!--T:49-->
 
<!--T:49-->
4. Login ข้าใช้งาน mysql โดยใช้ password ของ root user ที่ได้ตั้งไว้
+
===  Login เข้าใช้งาน MySQL โดยใช้ password ของ user root ที่ได้ตั้งค่าไว้ ===
  '''# mysql -u root -p'''
+
  '''mysql -u root -p'''
 +
''รหัสผ่าน <span style='color:green'>rootpass</span>''
  
 
<!--T:50-->
 
<!--T:50-->
5. สร้าง DB เพื่อใช้งานการเก็บข้อมูลของการใช้งาน Email Server
+
=== สร้าง Database เพื่อใช้เก็บข้อมูลของการใช้งาน Email Server ===
  '''# CREATE DATABASE ''[[eaimail]]'';'''
+
*สร้าง Database
  '''# USE ''[[eaimail]]'';'''
+
  '''CREATE DATABASE eaimail;'''
 +
''ตัวอย่าง''
 +
Query OK, 1 row affected (0.02 sec)
 +
 
 +
<span style='color:green'>*เลือกใช้งาน Database eaimail</span>
 +
  '''USE eaimail;'''
 +
''ตัวอย่าง''
 +
Database changed
  
<!--T:51-->
+
=== สร้าง user พร้อมกำหนดสิทธิ์การใช้งานให้กับ eaimail database ที่สร้างขึ้นมา ===
6. สร้าง user พร้อมกำหนดสิทธิ์การใช้งานให้กับ eaimail database ที่สร้างขึ้นมา
+
  '''CREATE USER dbuser@localhost IDENTIFIED BY 'dbuserpass';'''
  '''# GRANT SELECT, INSERT, UPDATE, DELETE ON eaimail.* TO ''[['yourdbuser']]''@'localhost' IDENTIFIED BY ''[['yourdbpassword']]'';
+
  '''GRANT SELECT, INSERT, UPDATE, DELETE ON eaimail.* TO dbuser@localhost;'''
  '''# GRANT SELECT, INSERT, UPDATE, DELETE ON eaimail.* TO ''[['yourdbuser']]''@'localhost.localdomain' IDENTIFIED BY ''[['yourdbpassword']]'';'''
+
  '''FLUSH PRIVILEGES;'''
  '''# FLUSH PRIVILEGES;'''
 
  
 
<!--T:52-->
 
<!--T:52-->
7. ตรวจสอบ userที่สร้างขึ้นใหม่โดย
+
=== ตรวจสอบ user ที่สร้างขึ้นใหม่โดย ===
  '''# SHOW GRANTS FOR ''[['yourdbuser']]''@'localhost';'''
+
  '''SHOW GRANTS FOR dbuser@localhost;'''
 +
''ตัวอย่าง''
 +
+----------------------------------------------------------------------------------------------------------------+
 +
| Grants for dbuser@localhost                                                |
 +
+----------------------------------------------------------------------------------------------------------------+
 +
| GRANT USAGE ON *.* TO `dbuser`@`localhost`                                  |
 +
| GRANT SELECT, INSERT, UPDATE, DELETE ON `eaimail`.* TO `dbuser`@`localhost` |
 +
+----------------------------------------------------------------------------------------------------------------+
 +
2 rows in set (0.00 sec)
  
 
<!--T:53-->
 
<!--T:53-->
8. สร้างตารางใช้เก็บค่า domains, forwardings สำหรับ forward อีเมลภาษาไทยไปภาษาอังกฤษ, users เพื่อใช้สร้าง mailbox  
+
=== สร้างตารางใช้เก็บค่า domains, forwardings สำหรับ forward อีเมลภาษาไทยไปภาษาอังกฤษ, users เพื่อใช้สร้าง mailbox ===
  '''# CREATE TABLE domains (domain varchar(50) NOT NULL, PRIMARY KEY (domain) );'''
+
  '''CREATE TABLE domains (domain varchar(50) NOT NULL, PRIMARY KEY (domain));'''
  '''# CREATE TABLE forwardings (source varchar(80) NOT NULL, destination TEXT NOT NULL, PRIMARY KEY (source) );'''
+
  '''CREATE TABLE forwardings (source varchar(80) NOT NULL, destination TEXT NOT NULL, PRIMARY KEY (source));'''
  '''# CREATE TABLE users (email varchar(80) NOT NULL, password varchar(20) NOT NULL, PRIMARY KEY (email) );'''
+
  '''CREATE TABLE users (email varchar(80) NOT NULL, password varchar(256) NOT NULL, PRIMARY KEY (email));'''
  
 
<!--T:54-->
 
<!--T:54-->
9. เลือก DB ของ mail server และกำหนด Domain ภาษาอังกฤษ และภาษาไทย และ Email-Account ภาษาอังกฤษ) พร้อม password เพื่อเริ่มใช้งาน
+
=== <span style='color:green'> เพิ่มข้อมูล Domain และ Email Account ทั้งภาษาอังกฤษ และภาษาไทย เพื่อเริ่มใช้งาน</span> ===
  '''# INSERT INTO domains (domain) VALUES ([['eai.in.th']]);'''
+
  '''INSERT INTO domains (domain) VALUES ('mailthai<span style='color:green'>XX</span>.in.th');'''
  '''# INSERT INTO domains (domain) VALUES ([['อีเอไอ.ไทย']]);'''
+
  '''INSERT INTO domains (domain) VALUES ('จดหมาย<span style='color:green'>XX</span>.ไทย');'''
  '''# INSERT INTO users (email, password) VALUES ([['thai@eai.in.th']], ENCRYPT([['thaiuserpassword']]));'''
+
  '''INSERT INTO users (email, password) VALUES ('thai@mailthai<span style='color:green'>XX</span>.in.th', '$6$7GQtdsDwSjzB7EAm$0.W6ZBcGEylrQMHKv1j7PZd/6EPoXAYClNhxxs3V2mG/NrHKDsycOTWwETniwI9O/OUeQkqBT5KPfNkDUb8hk/');'''
  '''# INSERT INTO users (email, password) VALUES ([['ไทย@อีเอไอ.ไทย']], ENCRYPT([['thaiuserpassword']]));'''
+
  '''INSERT INTO users (email, password) VALUES ('ไทย@จดหมาย<span style='color:green'>XX</span>.ไทย', '$6$7GQtdsDwSjzB7EAm$0.W6ZBcGEylrQMHKv1j7PZd/6EPoXAYClNhxxs3V2mG/NrHKDsycOTWwETniwI9O/OUeQkqBT5KPfNkDUb8hk/');'''
 +
''ตัวอย่าง เช่น mailthai99.in.th, จดหมาย99.ไทย, thai@mailthai99.in.th, ไทย@จดหมาย99.ไทย''
 +
 
 +
''หมายเหตุ สร้างรหัสผ่านด้วยคำสั่ง doveadm pw -s SHA512-CRYPT ''
  
 
<!--T:55-->
 
<!--T:55-->
10. กำหนด Email ที่เข้ามายัง Email Account ภาษาไทย ให้ forward เข้า Email-Account ภาษาอังกฤษ เนื่องจากระบบไม่สามารถสร้าง mailbox เป็น ภาษาไทยได้
+
=== <span style='color:green'> กำหนด Email ที่เข้ามายัง Email Account ภาษาไทย ให้ forward เข้า Email-Account ภาษาอังกฤษ เนื่องจากระบบไม่สามารถสร้าง mailbox เป็น ภาษาไทยได้</span> ===
  '''# INSERT INTO forwardings (source,destination) VALUES ([['ไทย@อีเอไอ.ไทย']],[['thai@eai.in.th']]);'''
+
  '''INSERT INTO forwardings (source,destination) VALUES ('ไทย@จดหมาย<span style='color:green'>XX</span>.ไทย','thai@mailthai<span style='color:green'>XX</span>.in.th');'''
  '''# quit'''
+
  '''quit;'''
  
== 6. ตั้งค่า postfix == <!--T:56-->
+
== ตั้งค่า postfix == <!--T:56-->
=== สร้างไฟล์เชื่อมต่อระหว่าง postfix และ mariadb ===
+
=== <span style='color:green'>สร้างไฟล์เชื่อมต่อระหว่าง Postfix และ MySQL</span> ===
* สร้างไฟล์ script เพื่อใช้สำหรับเรียกค่า domain จาก DB
+
'''cd /etc/postfix/'''
  '''# vi /etc/postfix/mysql-virtual_domains.cf'''
+
* สร้างไฟล์ script เพื่อใช้สำหรับเรียกค่า domain จาก Database
 +
  '''nano /etc/postfix/mysql-virtual_domains.cf'''
 +
''ตัวอย่างไฟล์''
  
<!--T:57-->
+
<!--T:57-->
user = '''''[[yourdbuser]]'''''
+
user = dbuser
  password = '''''[[yourdbpassword]]'''''
+
  password = dbuserpass
  dbname = '''''[[eaimail]]'''''
+
  dbname = eaimail
  query = SELECT domain AS virtual FROM domains WHERE domain='%s'
+
  query = SELECT domain FROM domains WHERE domain='%s'
 
  hosts = 127.0.0.1
 
  hosts = 127.0.0.1
  
 
<!--T:58-->
 
<!--T:58-->
 
* สร้างไฟล์ script เพื่อใช้สำหรับ forward email
 
* สร้างไฟล์ script เพื่อใช้สำหรับ forward email
  '''# vi /etc/postfix/mysql-virtual_forwardings.cf'''
+
  '''nano /etc/postfix/mysql-virtual_forwardings.cf'''
 +
''ตัวอย่างไฟล์''
  
<!--T:59-->
+
<!--T:59-->
user = '''''[[yourdbuser]]'''''
+
user = dbuser
  password = '''''[[yourdbpassword]]'''''
+
  password = dbuserpass
  dbname = '''''[[eaimail]]'''''
+
  dbname = eaimail
 
  query = SELECT destination FROM forwardings WHERE source='%s'
 
  query = SELECT destination FROM forwardings WHERE source='%s'
 
  hosts = 127.0.0.1
 
  hosts = 127.0.0.1
  
 
<!--T:60-->
 
<!--T:60-->
* สร้างไฟล์ script เพื่อใช้สำหรับ สร้าง mailbox และใช้ login  
+
* สร้างไฟล์ script เพื่อใช้สำหรับ สร้าง mailbox และใช้ login
  '''# vi /etc/postfix/mysql-virtual_mailboxes.cf'''
+
  '''nano /etc/postfix/mysql-virtual_mailboxes.cf'''
 +
''ตัวอย่างไฟล์''
  
<!--T:61-->
+
<!--T:61-->
user = '''''[[yourdbuser]] '''''
+
user = dbuser
  password = '''''[[yourdbpassword]]'''''
+
  password = dbuserpass
  dbname = '''''[[eaimail]]'''''
+
  dbname = eaimail
 
  query = SELECT CONCAT(SUBSTRING_INDEX(email,'@',-1),'/',SUBSTRING_INDEX(email,'@',1),'/') FROM users WHERE email='%s'
 
  query = SELECT CONCAT(SUBSTRING_INDEX(email,'@',-1),'/',SUBSTRING_INDEX(email,'@',1),'/') FROM users WHERE email='%s'
 
  hosts = 127.0.0.1
 
  hosts = 127.0.0.1
แถว 358: แถว 314:
 
<!--T:62-->
 
<!--T:62-->
 
* สร้างไฟล์ script เพื่อใช้สำหรับ ส่งต่อ email ทั้งหมด ไปยัง smtp server อื่น ซึ่งใช้ในกรณีที่ host เป็น IP Address
 
* สร้างไฟล์ script เพื่อใช้สำหรับ ส่งต่อ email ทั้งหมด ไปยัง smtp server อื่น ซึ่งใช้ในกรณีที่ host เป็น IP Address
  '''# vi /etc/postfix/mysql-virtual_email2email.cf'''
+
  '''nano /etc/postfix/mysql-virtual_email2email.cf'''
 +
''ตัวอย่างไฟล์''
  
<!--T:63-->
+
<!--T:63-->
user = '''''[[yourdbuser]] '''''
+
user = dbuser
  password = '''''[[yourdbpassword]]'''''
+
  password = dbuserpass
  dbname = '''''[[eaimail]]'''''
+
  dbname = eaimail
 
  query = SELECT email FROM users WHERE email='%s'
 
  query = SELECT email FROM users WHERE email='%s'
 
  hosts = 127.0.0.1
 
  hosts = 127.0.0.1
แถว 369: แถว 326:
 
<!--T:64-->
 
<!--T:64-->
 
* กำหนดสิทธิ์ให้กับ script ที่สร้างขึ้น
 
* กำหนดสิทธิ์ให้กับ script ที่สร้างขึ้น
  '''# chmod 640 /etc/postfix/mysql-virtual_*.cf && chgrp postfix /etc/postfix/mysql-virtual_*.cf'''
+
  '''chmod 640 /etc/postfix/mysql-virtual_*.cf && chgrp postfix /etc/postfix/mysql-virtual_*.cf'''
 +
''ตัวอย่าง''
 +
-rw-r-----  1 root postfix  124 Jan  7 23:36 mysql-virtual_domains.cf
 +
-rw-r-----  1 root postfix  120 Jan  7 23:39 mysql-virtual_email2email.cf
 +
-rw-r-----  1 root postfix  133 Jan  7 23:37 mysql-virtual_forwardings.cf
 +
-rw-r-----  1 root postfix  190 Jan  7 23:38 mysql-virtual_mailboxes.cf
  
 
=== สร้าง user vmail สำหรับอ่าน/เขียน mailbox === <!--T:65-->
 
=== สร้าง user vmail สำหรับอ่าน/เขียน mailbox === <!--T:65-->
 
* สร้าง group เพื่อใช้กับ mailbox  
 
* สร้าง group เพื่อใช้กับ mailbox  
  '''# groupadd -g 5000 vmail'''
+
  '''groupadd -g 5000 vmail'''
  '''# useradd -g vmail -u 5000 vmail -d /home/vmail -m'''
+
  '''useradd -g vmail -u 5000 vmail -d /home/vmail -m'''
 +
 
 +
=== <span style='color:green'>config การทำงานของ postfix ด้วยคำสั่ง postconf (คำสั่งนี้จะไปแก้ไขไฟล์ "/etc/postfix/main.cf")</span> === <!--T:66-->
 +
'''postconf "myhostname=mail.mailthai<span style='color:green;'>XX</span>.in.th" && \
 +
'''postconf "mydomain=mailthai<span style='color:green;'>XX</span>.in.th" && \
 +
'''postconf "virtual_alias_domains=จดหมาย<span style='color:green;'>XX</span>.ไทย" && \
 +
'''postconf "smtpd_tls_cert_file=/etc/ssl/private/mailthai<span style='color:green;'>XX</span>.crt" && \
 +
'''postconf "smtpd_tls_key_file=/etc/ssl/private/mailthai<span style='color:green;'>XX</span>.key" && \
 +
'''postconf "myorigin=\$mydomain" && \
 +
'''postconf "inet_interfaces=all" && \
 +
'''postconf "inet_protocols=ipv4" && \
 +
'''postconf "mydestination=localhost, localhost.localdomain" && \
 +
'''postconf "mynetworks=127.0.0.1" && \
 +
'''postconf "readme_directory=no" && \
 +
'''postconf "shlib_directory=no" && \
 +
'''postconf "smtpd_sasl_auth_enable=yes" && \
 +
'''postconf "smtpd_sasl_type=dovecot" && \
 +
'''postconf "smtpd_sasl_path=private/auth" && \
 +
'''postconf "smtpd_sasl_authenticated_header=yes" && \
 +
'''postconf "broken_sasl_auth_clients=yes" && \
 +
'''postconf "smtpd_use_tls=yes" && \
 +
'''postconf "smtpd_recipient_restrictions=permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination" && \
 +
'''postconf "smtpd_tls_auth_only=yes" && \
 +
'''postconf "smtpd_tls_loglevel=1" && \
 +
'''postconf "virtual_mailbox_base=/home/vmail" && \
 +
'''postconf "virtual_mailbox_maps=proxy:mysql:/etc/postfix/mysql-virtual_mailboxes.cf" && \
 +
'''postconf "virtual_mailbox_domains=proxy:mysql:/etc/postfix/mysql-virtual_domains.cf" && \
 +
'''postconf "virtual_alias_maps=proxy:mysql:/etc/postfix/mysql-virtual_forwardings.cf, mysql:/etc/postfix/mysql-virtual_email2email.cf" && \
 +
'''postconf "virtual_uid_maps=static:5000" && \
 +
'''postconf "virtual_gid_maps=static:5000" && \
 +
'''postconf "virtual_transport=dovecot" && \
 +
'''postconf "proxy_read_maps=\$local_recipient_maps \$mydestination \$virtual_alias_maps \$virtual_alias_domains \$virtual_mailbox_maps \$virtual_mailbox_domains \$relay_recipient_maps \$relay_domains \$canonical_maps \$sender_canonical_maps \$recipient_canonical_maps \$relocated_maps \$transport_maps \$mynetworks" && \
 +
'''postconf "smtputf8_enable=yes" && \
 +
'''postconf "newaliases_path=null" && \
 +
'''postconf "mailq_path=null" && \
 +
'''postconf "setgid_group=postdrop" && \
 +
'''postconf "html_directory=null" && \
 +
'''postconf "manpage_directory = null" &&  \
 +
'''postconf "sample_directory = null" && \
 +
'''postconf "sendmail_path=null"
  
=== แก้ไขไฟล์ "/etc/postfix/main.cf" === <!--T:66-->
+
=== ตรวจสอบการ config postfix ด้วยคำสั่ง ===
'''# postconf "myhostname=''[[mail.eai.in.th]]'''"
+
  ''' postconf -n'''
  '''# postconf "mydomain='''[[eai.in.th]]'''"
 
'''# postconf "myorigin=\$mydomain"
 
'''# postconf "inet_interfaces=all"
 
'''# postconf "inet_protocols=ipv4"
 
'''# postconf "mydestination=localhost, localhost.localdomain"
 
'''# postconf "mynetworks=127.0.0.1"
 
'''# postconf "readme_directory=no"
 
'''# postconf "smtpd_tls_cert_file=/etc/pki/tls/certs/'''[[eai.crt]]'''"
 
'''# postconf "smtpd_tls_key_file=/etc/pki/tls/certs/'''[[eai.key]]'''"
 
'''# postconf "shlib_directory=no"
 
'''# postconf "smtpd_sasl_auth_enable=yes"
 
'''# postconf "smtpd_sasl_type=dovecot"
 
'''# postconf "smtpd_sasl_path=private/auth"
 
'''# postconf "smtpd_sasl_authenticated_header=yes"
 
'''# postconf "broken_sasl_auth_clients=yes"
 
'''# postconf "smtpd_use_tls=yes"
 
'''# postconf "smtpd_recipient_restrictions=permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination"
 
'''# postconf "smtpd_tls_auth_only=yes"
 
'''# postconf "smtpd_tls_loglevel=1"
 
'''# postconf "virtual_mailbox_base=/home/vmail"
 
'''# postconf "virtual_mailbox_maps=proxy:mysql:/etc/postfix/mysql-virtual_mailboxes.cf"
 
'''# postconf "virtual_mailbox_domains=proxy:mysql:/etc/postfix/mysql-virtual_domains.cf"
 
'''# postconf "virtual_alias_maps=proxy:mysql:/etc/postfix/mysql-virtual_forwardings.cf, mysql:/etc/postfix/mysql-virtual_email2email.cf"
 
'''# postconf "virtual_uid_maps=static:5000"
 
'''# postconf "virtual_gid_maps=static:5000"
 
'''# postconf "virtual_alias_domains='''[[อีเอไอ.ไทย]]'''"
 
'''# postconf "virtual_transport=dovecot"
 
'''# postconf "proxy_read_maps=\$local_recipient_maps \$mydestination \$virtual_alias_maps \$virtual_alias_domains \$virtual_mailbox_maps \$virtual_mailbox_domains \$relay_recipient_maps \$relay_domains \$canonical_maps \$sender_canonical_maps \$recipient_canonical_maps \$relocated_maps \$transport_maps \$mynetworks"
 
'''# postconf "smtputf8_enable=yes"
 
  
 
<!--T:67-->
 
<!--T:67-->
 
* ตัวอย่างไฟล์
 
* ตัวอย่างไฟล์
  smtp_tls_security_level = may
+
  broken_sasl_auth_clients = yes
  meta_directory = /etc/postfix
+
  command_directory = /usr/sbin
 +
compatibility_level = 2
 +
daemon_directory = /usr/lib/postfix/sbin
 +
data_directory = /var/lib/postfix
 +
debugger_command = PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin ddd $daemon_directory/$process_name $process_id & sleep 5
 +
html_directory = null
 +
inet_interfaces = all
 +
inet_protocols = ipv4
 +
mailq_path = null
 +
manpage_directory = null
 +
mydestination = localhost, localhost.localdomain
 +
mydomain = <span style='color:green;'>mailthai99.in.th</span>
 +
myhostname = <span style='color:green;'>mail.mailthai99.in.th</span>
 +
mynetworks = 127.0.0.1
 +
myorigin = $mydomain
 +
newaliases_path = null
 +
proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps $virtual_alias_domains $virtual_mailbox_maps $virtual_mailbox_domains $relay_recipient_maps $relay_domains $canonical_maps $sender_canonical_maps $recipient_canonical_maps $relocated_maps $transport_maps $mynetworks
 +
readme_directory = no
 +
sample_directory = null
 +
sendmail_path = null
 +
setgid_group = postdrop
 
  shlib_directory = no
 
  shlib_directory = no
  myhostname = mail.eai.in.th
+
  smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
mydomain = eai.in.th
+
  smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
myorigin = $mydomain
 
  mynetworks = 127.0.0.1
 
 
  smtpd_sasl_auth_enable = yes
 
  smtpd_sasl_auth_enable = yes
 +
smtpd_sasl_authenticated_header = yes
 +
smtpd_sasl_path = private/auth
 
  smtpd_sasl_type = dovecot
 
  smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
 
smtpd_sasl_authenticated_header = yes
 
broken_sasl_auth_clients = yes
 
smtpd_use_tls = yes
 
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
 
 
  smtpd_tls_auth_only = yes
 
  smtpd_tls_auth_only = yes
 +
smtpd_tls_cert_file = <span style='color:green;'>/etc/ssl/private/mailthai99.crt</span>
 +
smtpd_tls_key_file = <span style='color:green;'>/etc/ssl/private/mailthai99.key</span>
 
  smtpd_tls_loglevel = 1
 
  smtpd_tls_loglevel = 1
 +
smtpd_use_tls = yes
 +
smtputf8_enable = yes
 +
unknown_local_recipient_reject_code = 550
 +
virtual_alias_domains = <span style='color:green;'>จดหมาย99.ไทย</span>
 +
virtual_alias_maps = <span style='color:green;'>proxy:mysql:/etc/postfix/mysql-virtual_forwardings.cf, mysql:/etc/postfix/mysql-virtual_email2email.cf</span>
 +
virtual_gid_maps = static:5000
 
  virtual_mailbox_base = /home/vmail
 
  virtual_mailbox_base = /home/vmail
  virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql-virtual_mailboxes.cf
+
  virtual_mailbox_domains = <span style='color:green;'>proxy:mysql:/etc/postfix/mysql-virtual_domains.cf</span>
  virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql-virtual_domains.cf
+
  virtual_mailbox_maps = <span style='color:green;'>proxy:mysql:/etc/postfix/mysql-virtual_mailboxes.cf</span>
 +
virtual_transport = dovecot
 
  virtual_uid_maps = static:5000
 
  virtual_uid_maps = static:5000
virtual_gid_maps = static:5000
 
virtual_alias_domains = อีเอไอ.ไทย
 
virtual_transport = dovecot
 
virtual_alias_maps = proxy:mysql:/etc/postfix/mysql-virtual_forwardings.cf, mysql:/etc/postfix/mysql-virtual_email2email.cf
 
smtputf8_enable = yes
 
proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps $virtual_alias_domains $virtual_mailbox_maps $virtual_mailbox_domains $relay_recipient_maps $relay_domains $canonical_maps
 
$sender_canonical_maps $recipient_canonical_maps $relocated_maps $transport_maps $mynetworks
 
  
=== แก้ไขไฟล์ "/etc/postfix/master.cf" === <!--T:68-->
+
=== <span style='color:green'>config การทำงานของ postfix ด้วยคำสั่ง postconf (คำสั่งนี้จะไปแก้ไขไฟล์ "/etc/postfix/master.cf")</span> === <!--T:68-->
  '''# postconf -M smtps/inet="smtps inet n - - - - smtpd"
+
  '''postconf -M smtps/inet="smtps inet n - - - - smtpd" && \
  '''# postconf -P "smtps/inet/syslog_name=postfix/smtps"
+
  '''postconf -P "smtps/inet/syslog_name=postfix/smtps" && \
  '''# postconf -P "smtps/inet/smtpd_tls_wrappermode=yes"
+
  '''postconf -P "smtps/inet/smtpd_tls_wrappermode=yes" && \
  '''# postconf -P "smtps/inet/smtpd_sasl_auth_enable=yes"
+
  '''postconf -P "smtps/inet/smtpd_sasl_auth_enable=yes" && \
  '''# postconf -P "smtps/inet/smtpd_client_restrictions=permit_sasl_authenticated,reject"
+
  '''postconf -P "smtps/inet/smtpd_client_restrictions=permit_sasl_authenticated,reject" && \
  '''# postconf -P "smtps/inet/smtpd_relay_restrictions=permit_sasl_authenticated,reject"
+
  '''postconf -P "smtps/inet/smtpd_relay_restrictions=permit_sasl_authenticated,reject" && \
  '''# postconf -P "smtps/inet/milter_macro_daemon_name=ORIGINATING"
+
  '''postconf -P "smtps/inet/milter_macro_daemon_name=ORIGINATING" && \
  '''# postconf -vM dovecot/unix="dovecot  unix  -      n      n      -      -      pipe flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/deliver -f ${sender} -d ${recipient}"
+
  '''postconf -M dovecot/unix="dovecot  unix  -      n      n      -      -      pipe flags=DRhu user=vmail:vmail argv=/usr/lib/dovecot/deliver -f \${sender} -d \${recipient}"
 +
 
 +
=== ตรวจสอบการ config postfix ด้วยคำสั่ง ===
 +
'''postconf -M'''
  
 
<!--T:69-->
 
<!--T:69-->
แถว 458: แถว 450:
 
     -o milter_macro_daemon_name=ORIGINATING'''
 
     -o milter_macro_daemon_name=ORIGINATING'''
 
  dovecot  unix  -      n      n      -      -      pipe
 
  dovecot  unix  -      n      n      -      -      pipe
     flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/deliver -f ${sender} -d ${recipient}'''
+
     flags=DRhu user=vmail:vmail argv=/usr/lib/dovecot/deliver -f ${sender} -d ${recipient}'''
 +
 
 +
=== ตั้งค่า mysql postfix driver === <!--T:70-->
 +
'''nano /etc/postfix/dynamicmaps.cf'''
 +
''ตัวอย่าง''
 +
mysql  /usr/lib/postfix/postfix-mysql.so      dict_mysql_open
  
<!--T:70-->
+
=== <span style='color:green'>สั่ง restart postfix</span> === <!--T:70-->
*  restart postfix
+
  '''systemctl restart postfix'''
  '''# systemctl restart postfix'''
 
  
== 7. ตั้งค่า dovecot == <!--T:71-->
+
== ตั้งค่า dovecot == <!--T:71-->
=== สร้างไฟล์เชื่อมต่อระหว่าง dovecot และ mariadb ===
+
=== <span style='color:green'>สร้างไฟล์เชื่อมต่อระหว่าง dovecot และ MySQL</span> ===
 
* สร้าง script เพื่อให้ dovecot ใช้งานกับ SQL ที่เราได้กำหนดก่อนหน้านี้
 
* สร้าง script เพื่อให้ dovecot ใช้งานกับ SQL ที่เราได้กำหนดก่อนหน้านี้
  '''# vi /etc/dovecot/dovecot-sql.conf.ext'''
+
  '''nano /etc/dovecot/dovecot-sql.conf.ext'''
  
<!--T:72-->
+
<!--T:72-->
'''driver = mysql'''
+
''ตัวอย่างไฟล์''
  '''connect = host=127.0.0.1 dbname=''[[eaimail]]'' user=''[[yourdbuser]]'' password=''[[yourdbpassword]]'''''
+
driver = mysql
  '''default_pass_scheme = CRYPT'''
+
  connect = host=127.0.0.1 dbname=eaimail user=dbuser password=dbuserpass
  '''password_query = SELECT email as user, password FROM users WHERE email='%u';'''
+
  default_pass_scheme = SHA512-CRYPT
 +
  password_query = SELECT email as user, password FROM users WHERE email='%u';
  
 
<!--T:73-->
 
<!--T:73-->
 
* กำหนดสิทธิ์อนุญาตให้ user ที่อยู่ใน dovecot group ใช้งาน
 
* กำหนดสิทธิ์อนุญาตให้ user ที่อยู่ใน dovecot group ใช้งาน
  '''# chmod 640 /etc/dovecot/dovecot-sql.conf.ext && chgrp dovecot /etc/dovecot/dovecot-sql.conf.ext
+
  '''chmod 640 /etc/dovecot/dovecot-sql.conf.ext && chgrp dovecot /etc/dovecot/dovecot-sql.conf.ext'''
 +
''ตัวอย่าง''
 +
-rw-r-----  1 root dovecot 6023 Jan  7 23:54 dovecot-sql.conf.ext
  
=== แก้ไขไฟล์ "/etc/dovecot/dovecot.conf" === <!--T:74-->
+
=== <span style='color:green'>Backup ไฟล์ dovecot.conf เดิมไว้ก่อน</span> ===<!--T:74-->
* Backup ค่า config เดิมไว่ก่อน
+
  '''mv /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf-backup'''
  '''# mv /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf-backup'''
+
''ตัวอย่าง''
 +
-rw-r--r--  1 root root    4401 Apr 12  2021 dovecot.conf-backup
  
 
<!--T:75-->
 
<!--T:75-->
* ตั้งค่า config dovecot โดยสร้างไฟล์ให้ และใส่คำสั่งตามด้านล่าง
+
=== <span style='color:green'>สร้างไฟล์ "/etc/dovecot/dovecot.conf” และแก้ไขไฟล์ "/etc/dovecot/dovecot.conf" ด้วยการใส่คำสั่งตามด้านล่าง</span> ===
  '''# vi /etc/dovecot/dovecot.conf'''
+
  '''nano /etc/dovecot/dovecot.conf'''
 
''ตัวอย่าง''
 
''ตัวอย่าง''
  
<!--T:76-->
+
<!--T:76-->
'''protocols = imap pop3 lmtp
+
protocols = imap pop3 lmtp
  '''log_timestamp = "%Y-%m-%d %H:%M:%S "
+
  log_timestamp = "%Y-%m-%d %H:%M:%S"
  '''mail_location = maildir:/home/vmail/%d/%n/Maildir
+
  mail_location = maildir:/home/vmail/%d/%n/Maildir
   
+
  ssl_cert = </etc/ssl/private/mailthai<span style='color:green;'>XX</span>.crt
'''ssl_cert = </etc/pki/tls/certs/'''[[eai.crt]]'''
+
  ssl_key = </etc/ssl/private/mailthai<span style='color:green;'>XX</span>.key
  '''ssl_key = </etc/pki/tls/certs/'''[[eai.key]]'''
+
  ssl_cipher_list = ALL:!LOW:!SSLv2:!EXP:!aNULL
'''ssl_dh_parameters_length = 2048
+
  auth_mechanisms = plain login
  '''ssl_cipher_list = ALL:!LOW:!SSLv2:!EXP:!aNULL
+
namespace {
  '''namespace {
+
type = private
'''    type = private
+
separator = .
'''    separator = .
+
prefix = INBOX.
'''    prefix = INBOX.
+
inbox = yes
'''    inbox = yes
+
  }
  '''}
+
  service auth {
  '''service auth {
+
unix_listener auth-master {
'''    unix_listener auth-master {
+
mode = 0600
'''        mode = 0600
+
user = vmail
'''        user = '''[[vmail]]'''
+
}
'''    }
+
unix_listener /var/spool/postfix/private/auth {
'''    unix_listener /var/spool/postfix/private/auth {
+
mode = 0666
'''        mode = 0666
+
user = postfix
'''        user = '''[[postfix]]'''
+
group = postfix
'''        group = '''[[postfix]]'''
+
}
'''    }
+
user = root
'''user = root
+
  }
  '''}
+
  service auth-worker {
  '''auth_mechanisms = plain login
+
user = root
'''service auth-worker {
+
  }
'''    user = root
+
  protocol lda {
  '''}
+
log_path = /home/vmail/dovecot-deliver.log
  '''protocol lda {
+
auth_socket_path = /var/run/dovecot/auth-master
'''    log_path = /home/vmail/dovecot-deliver.log
+
postmaster_address = postmaster@mailthai<span style='color:green;'>XX</span>.in.th
'''    auth_socket_path = /var/run/dovecot/auth-master
+
  }
'''    postmaster_address = postmaster@'''[[eai.in.th]]'''
+
  protocol pop3 {
  '''}
+
pop3_uidl_format = %08Xu%08Xv
  '''protocol pop3 {
+
pop3_client_workarounds = outlook-no-nuls oe-ns-eoh
'''    pop3_uidl_format = %08Xu%08Xv
+
  }
'''        pop3_client_workarounds = outlook-no-nuls oe-ns-eoh
+
  passdb {
  '''}
+
driver = sql
  '''passdb {
+
args = <span style='color:green;'>/etc/dovecot/dovecot-sql.conf.ext</span>
'''    driver = sql
+
  }
'''    args = /etc/dovecot/dovecot-sql.conf.ext
+
  userdb {
  '''}
+
driver = static
  '''userdb {
+
args = uid=5000 gid=5000 home=/home/vmail/%d/%n allow_all_users=yes
'''    driver = static
+
  }
'''    args = uid=5000 gid=5000 home=/home/vmail/%d/%n allow_all_users=yes
+
service stats {
  '''}'''
+
unix_listener stats-reader {
 +
user =  vmail
 +
group = vmail
 +
mode = 0660
 +
}
 +
unix_listener stats-writer {
 +
user = vmail
 +
group = vmail
 +
mode = 0660
 +
}
 +
}
  
<!--T:77-->
+
=== <span style='color:green'>สั่ง restart dovecot</span> ===<!--T:77-->
* สั่ง restart dovecot
+
  '''systemctl restart dovecot'''
  '''# systemctl restart dovecot'''
 
  
== 8. ทดสอบการทำงานของ EAI Email Server == <!--T:78-->
+
== ทดสอบการทำงานของ Email Server == <!--T:78-->
1. ทดสอบ imap
+
=== ทดสอบ imap ด้วยคำสั่ง telnet ===
  '''# telnet localhost imap'''
+
  ''' telnet localhost imap'''
 +
''ตัวอย่าง ''
  
 
<!--T:79-->
 
<!--T:79-->
2. ได้ผลตามด้านล่าง และ ใช้คำสั่ง quit เพื่ออแกจาก telnet
+
  Trying 127.0.0.1...
  '''Trying 127.0.0.1...'''
+
  Connected to localhost.
  '''Connected to localhost.localdomain.'''
+
  Escape character is '^]'.
  '''Escape character is '^]'.'''
+
  * OK [CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE LITERAL+ STARTTLS AUTH=PLAIN AUTH=LOGIN] Dovecot (Ubuntu) ready.
  '''+OK Dovecot ready.'''
+
'' หมายเหตุ กด Ctrl+] และตามด้วย q เพื่อออก
 
 
<!--T:80-->
 
3. แก้ใขไฟล์ /etc/aliases โดยเพิ่มบรรทัดตามด้านล่าง
 
'''# vi /etc/aliases'''
 
 
 
<!--T:81-->
 
'''mailer-daemon: postmaster'''
 
'''postmaster: root'''
 
'''''[[root: postmaster@eai.in.th]]'''''
 
 
 
<!--T:82-->
 
4 สั่ง update aliases และ restart postfix
 
'''# newaliases'''
 
'''# systemctl restart postfix'''
 
  
 +
=== ทดสอบ smtp ด้วยคำสั่ง telnet ===
 
<!--T:83-->
 
<!--T:83-->
5. ทดสอบ smtp server
+
  '''telnet localhost smtp'''
  '''# telnet localhost 25'''
+
  '''ehlo localhost'''
  '''# ehlo localhost'''
 
  
 
<!--T:84-->
 
<!--T:84-->
6. จะได้ผลลัพธ์ตามด้านล่าง
+
''ตัวอย่าง ''
  '''250-mail.eai.in.th'''
+
  <span style='color:green'>250-mail.mailthai99.in.th</span>
  '''250-PIPELINING'''
+
  250-PIPELINING
  '''250-SIZE 30720000'''
+
  250-SIZE 30720000
  '''250-VRFY'''
+
  250-VRFY
  '''250-ETRN'''
+
  250-ETRN
  '''250-STARTTLS'''
+
  250-STARTTLS  
  '''250-ENHANCEDSTATUSCODES'''
+
  250-ENHANCEDSTATUSCODES
  '''250-8BITMIME'''
+
  250-8BITMIME
  '''250-DSN'''
+
  250-DSN
  '''250 SMTPUTF8'''
+
  <span style='color:green'>250 SMTPUTF8</span>
 +
'' หมายเหตุ กด Ctrl+] และตามด้วย q เพื่อออก
  
 
<!--T:85-->
 
<!--T:85-->
7. ติดตั้ง mailx และทดสอบส่ง email
+
=== ทดสอบการส่ง email ด้วย telnet ===
  '''# yum install mailx'''
+
'''telnet localhost smtp'''
  '''# mailx thai@eai.in.th ''(ใส่ subject แล้ว enter ตามด้วยเนื้อหา และใช้ Ctrl+D เพื่อส่งออก)'''''
+
  '''ehlo localhost'''
 +
'''MAIL FROM: <span style='color:green;'>thai@mailthaiXX.in.th</span>
 +
  '''RCPT TO: <span style='color:green;'>thai@mailthai99.in.th</span>
 +
'''data
 +
'''Subject: test mailthaiXX.in.th to mailthai99.in.th
 +
'''test
 +
''' . <span style='color:green;'>ใช้เครื่องหมาย “.” เพื่อจบข้อความและส่งข้อความ</span>
 +
'' หมายเหตุ กด Ctrl+] และตามด้วย q เพื่อออก
 +
 
 +
'''telnet localhost smtp'''
 +
'''ehlo localhost'''
 +
'''MAIL FROM: <span style='color:green;'>thai@mailthaiXX.in.th SMTPUTF8</span>
 +
'''RCPT TO: <span style='color:green;'>ไทย@จดหมาย99.ไทย</span>
 +
'''data
 +
'''Subject: test mailthaiXX.in.th to mailthai99.in.th
 +
'''test
 +
''' . <span style='color:green;'>ใช้เครื่องหมาย “.” เพื่อจบข้อความและส่งข้อความ</span>
 +
'' หมายเหตุ กด Ctrl+] และตามด้วย q เพื่อออก
  
<!--T:86-->
+
=== ตรวจสอบ log ของการส่ง Email : /var/log/mail.log ซึ้งจะได้ผลตามด้านล่าง ===
8. ตรวจสอบ log ของการส่ง Email : /var/log/maillog ซึ้งจะได้ผลตามด้านล่าง
+
  Nov 13 15:19:07 mail postfix/pickup[31954]: E9AB020199E5: uid=0 from=<root>
  '''Nov 13 15:19:07 mail postfix/pickup[31954]: E9AB020199E5: uid=0 from=<root>'''
+
  Nov 13 15:19:07 mail postfix/cleanup[1938]: E9AB020199E5: message-id=<20171113081907.E9AB020199E5@mail.mailthai99.in.th>
  '''Nov 13 15:19:07 mail postfix/cleanup[1938]: E9AB020199E5: message-id=<20171113081907.E9AB020199E5@mail.eai.in.th>'''
+
  Nov 13 15:19:07 mail postfix/qmgr[21141]: E9AB020199E5: from=<root@mailthai99.in.th>, size=434, nrcpt=1 (queue active)
  '''Nov 13 15:19:07 mail postfix/qmgr[21141]: E9AB020199E5: from=<root@eai.in.th>, size=434, nrcpt=1 (queue active)'''
+
  Nov 13 15:19:08 mail postfix/pipe[1946]: E9AB020199E5: to=<thai@mailthai99.in.th>, relay=dovecot, delay=0.09, delays=0.04/0.01/0/0.03, dsn=2.0.0, status=sent (delivered via dovecot service)
  '''Nov 13 15:19:08 mail postfix/pipe[1946]: E9AB020199E5: to=<tanagan@eai.in.th>, relay=dovecot, delay=0.09, delays=0.04/0.01/0/0.03, dsn=2.0.0, status=sent (delivered via dovecot service)'''
+
  Nov 13 15:19:08 mail postfix/qmgr[21141]: E9AB020199E5: removed
  '''Nov 13 15:19:08 mail postfix/qmgr[21141]: E9AB020199E5: removed'''
 
  
 
<!--T:87-->
 
<!--T:87-->
9. ตรวจสอบ log ผลการส่งของ dovecot : /home/vmail/dovecot-deliver.log ซึ่งจะได้ผลตามด้านล่าง
+
=== ตรวจสอบ log ของ dovecot : /home/vmail/dovecot-deliver.log ซึ่งจะได้ผลตามด้านล่าง ===
  '''2017-11-13 15:19:08 lda(tanagan@eai.in.th): Info: msgid=<20171113081907.E9AB020199E5@mail.eai.in.th>: saved mail to INBOX'''
+
  '''2017-11-13 15:19:08 lda(thai@mailthai99.in.th): Info: msgid=<20171113081907.E9AB020199E5@mail.eai.in.th>: saved mail to INBOX'''
  
 
<!--T:88-->
 
<!--T:88-->
10. ตรวจสอบ mailbox เมื่อมีการรับส่ง-ส่ง Email จะสร้างขึ้นโดย dovecot ซึ่งจะได้ผลตามภาพ
+
=== ตรวจสอบ mailbox เมื่อมีการรับส่ง-ส่ง Email จะสร้างขึ้นโดย dovecot ซึ่งจะได้ผลตามตัวอย่าง ===
  '''# cd /home/vmail/eai.in.th/thai/Maildir'''
+
  '''cd /home/vmail/mailthai<span style='color:green'>XX</span>.in.th/Maildir'''
  '''# find'''
+
  '''find'''
 +
''ตัวอย่างเช่น mailthai99.in.th"
  
 
  <!--T:89-->
 
  <!--T:89-->
แถว 616: แถว 630:
 
  '''./tmp'''
 
  '''./tmp'''
  
<!--T:90-->
+
== ติดตั้ง roundcube webmail == <!--T:92-->
11. ตรวจสอบ mailbox โดยใช้ mail client คือ mutt เพื่อดู email ภายใน mailbox
 
'''# yum install mutt'''
 
'''# mutt -f .'''
 
 
 
<!--T:91-->
 
12. ทดสอบการส่งเมลในรูปแบบ eai
 
'''# telnet localhost 25'''
 
'''# EHLO localhost'''
 
'''# MAIL FROM: ธนากานต์@อีเอไอ.ไทย SMTPUTF8'''
 
'''# RCPT TO: ธนากานต์@คน.ไทย'''
 
'''# data'''
 
'''# Subject: eai test'''
 
'''# เทสอีเมลส์ภาษาไทย'''
 
'''# .'''
 
*ใช้เครื่องหมาย “.” เพื่อจบข้อความและส่งข้อความ
 
*กด Ctrl+] เพื่อออก
 
 
 
== 9. ติดตั้ง roundcube webmail == <!--T:92-->
 
  
 
=== ติดตั้ง apache php === <!--T:93-->
 
=== ติดตั้ง apache php === <!--T:93-->
 
<!--T:94-->
 
* ติดตั้ง repo ของ php 7.4 เพิ่มเติม และเปิดให้สามารถติดตั้ง php 7.4 ได้
 
'''# yum -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
 
'''# yum module reset php && yum module enable php:remi-7.4 -y
 
  
 
<!--T:95-->
 
<!--T:95-->
* ใช้คำสั่งติดตั้ง php, php library และ apache web server
+
* ใช้คำสั่ง apt ติดตั้ง php, php library และ apache web server
  '''# yum -y install httpd php php-common php-devel php-imap php-imagick php-json php-xml php-mbstring php-pear php-pdo php-intl php-ldap php-gd php-zip php-pdo_mysql php-curl php-bz2 php-gmp
+
  '''apt -y install apache2 php php-imap php-imagick php-json php-xml php-mbstring php-pear php-intl php-ldap php-gd php-zip php-mysql php-curl php-bz2 php-gmp phpmyadmin'''
  
 
<!--T:96-->
 
<!--T:96-->
* ใช้คำสั่ง start service apache และ php และให้เริ่มทำงานโดยอัติโนมัติทุกครั้งที่เปิดเครื่อง
+
* ใช้คำสั่ง start service apache และ php  
  '''# systemctl start httpd && systemctl start php-fpm
+
  '''systemctl start apache2'''
'''# ​systemctl enable httpd && systemctl enable php-fpm
 
  
 
<!--T:97-->
 
<!--T:97-->
* ตรวจสอบสถานะการทำงานของ apache และ
+
* ตรวจสอบสถานะการทำงานของ apache  
  '''# systemctl status httpd
+
  '''systemctl status apache2'''
'''# systemctl status php-fpm
 
  
=== ตั้งค่า php === <!--T:98-->
+
=== ตั้งค่า php (เราจะตั้งค่าเฉพาะส่วนที่จำเป็นต่อการใช้งาน roundcube) === <!--T:98-->
* ตั้งค่า timezone (เราจะตั้งค่าเฉพาะส่วนที่จำเป็นต่อการใช้งาน roundcube)
+
* ตั้งค่า timezone
  '''# vi /etc/php.ini  
+
  '''nano /etc/php/7.4/apache2/php.ini'''
  
<!--T:99-->
+
<!--T:99-->
 +
''ตัวอย่าง''
 
[Date]
 
[Date]
 
  ; Defines the default timezone used by the date functions
 
  ; Defines the default timezone used by the date functions
แถว 667: แถว 657:
 
  date.timezone = Asia/Bangkok
 
  date.timezone = Asia/Bangkok
  
=== ตั้งค่า mariadb database === <!--T:100-->
+
=== ตั้งค่า MySQL Database === <!--T:100-->
* สร้าง database roundcubemail โดยกำหนด username=rcmail, password=passrc
+
* สร้าง database roundcubemail โดยกำหนด username=rcmail, password=rcmailpass
  '''# mysql -u root -p
+
  '''mysql -u root -p'''
  
<!--T:101-->
+
<!--T:101-->  
'''# CREATE DATABASE roundcubemail CHARACTER SET utf8 COLLATE utf8_general_ci;
+
CREATE DATABASE roundcubemail CHARACTER SET utf8 COLLATE utf8_general_ci;
  '''# GRANT ALL PRIVILEGES ON roundcubemail.* TO rcmail@localhost IDENTIFIED BY 'passrc';
+
  CREATE USER rcmail@localhost IDENTIFIED BY 'rcmailpass' ;
  '''# FLUSH PRIVILEGES;
+
GRANT ALL PRIVILEGES ON roundcubemail.* TO rcmail@localhost;
 +
  FLUSH PRIVILEGES;
 +
quit;
  
 
=== ตั้งค่า roundcube === <!--T:102-->
 
=== ตั้งค่า roundcube === <!--T:102-->
  '''# cd /var/www/html/
+
  '''cd /var/www/html/
  '''# wget https://github.com/roundcube/roundcubemail/releases/download/1.4.4/roundcubemail-1.4.4-complete.tar.gz
+
  '''wget https://github.com/roundcube/roundcubemail/releases/download/1.4.4/roundcubemail-1.4.4-complete.tar.gz
  '''# tar zvfx /var/www/html/roundcubemail-1.4.4-complete.tar.gz  
+
  '''tar zvfx /var/www/html/roundcubemail-1.4.4-complete.tar.gz  
  '''# mv /var/www/html/roundcubemail-1.4.4 /var/www/html/roundcubemail
+
  '''mv /var/www/html/roundcubemail-1.4.4 /var/www/html/roundcubemail
  '''# cp /var/www/html/roundcubemail/config/config.inc.php.sample /var/www/html/roundcubemail/config/config.inc.php
+
  '''cp /var/www/html/roundcubemail/config/config.inc.php.sample /var/www/html/roundcubemail/config/config.inc.php
  '''# chown -R apache:apache /var/www/html/roundcubemail
+
  '''chown -R www-data:www-data /var/www/html/roundcubemail
  
 
=== สร้าง virtualhost === <!--T:103-->
 
=== สร้าง virtualhost === <!--T:103-->
  '''# vi /etc/httpd/conf.d/rcmail.conf
+
  '''rm /etc/apache2/sites-enabled/000-default.conf'''
 +
'''nano /etc/apache2/sites-available/rcmail.conf'''
  
 
  <!--T:104-->
 
  <!--T:104-->
<VirtualHost *:80>
+
<VirtualHost *:80>
    ServerName mail.eai.in.th
+
ServerName mail.mailthai99.in.th
    ServerAdmin webmaster@eai.in.th
+
ServerAdmin webmaster@mailtha99.in.th
    DocumentRoot /var/www/html/roundcubemail
+
DocumentRoot /var/www/html/roundcubemail
    <Directory /var/www/html/roundcubemail>
+
<Directory /var/www/html/roundcubemail>
        Options -Indexes +FollowSymLinks
+
Options -Indexes +FollowSymLinks
        AllowOverride All
+
AllowOverride All
    </Directory>
+
</Directory>
    ErrorLog /var/log/httpd/mail.eai.in.th-error.log
+
ErrorLog /var/log/apache2/mail.mailthai01.th-error.log
    CustomLog /var/log/httpd/mail.eai.in.th-access.log combined
+
CustomLog /var/log/apache2/mail.mailthai01.th-access.log combined
 
  </VirtualHost>
 
  </VirtualHost>
  
<!--T:105-->
+
<!--T:105-->
'''# systemctl restart httpd
+
'''ln -s /etc/apache2/sites-available/rcmail.conf /etc/apache2/sites-enabled/rcmail.conf'''
 +
'''systemctl restart apache2'''
  
<!--T:106-->
+
=== <span style='color:green'>ทดสอบเข้าเว็บโซต์</span> === <!--T:106-->
* ทดสอบเข้าเว็บโซต์
+
  '''https://eai-lab.bknix.co.th/mailXX/'''
  '''http://mail.eai.in.th/installer/
+
''ตัวอย่าง''
 +
DATABASE ERROR: CONNECTION FAILED!
 +
Unable to connect to the database!
 +
Please contact your server-administrator.
  
<!--T:107-->
+
=== <span style='color:green'>เข้าหน้า config roundcube</span> === <!--T:106-->
* ตั้งค่าเชื่อมต่อ database
+
*แก้ไขไฟล์ config.inc.php
 
+
  '''nano /var/www/html/roundcubemail/config/config.inc.php'''
<!--T:108-->
+
''ตัวอย่าง''
host = localhost
+
  $config['enable_installer'] = true;
  dbname =roundcubemail
 
user = rcmail
 
password = passrc
 
  db_prefix = rc_
 
  
<!--T:109-->
+
*การใช้ ssl แบบ self-sign อาจจะทำให้ roundcube ไม่สามารถเชื่อมต่อ mail server ได้ แก้ปัญหาโดยการ config เพิ่มเติม ดังนี้
* ตั้งค่า IMAP
+
  '''nano /var/www/html/roundcubemail/config/defaults.inc.php'''
  ssl://mail.eai.in.th
 
993
 
  
<!--T:110-->
+
''ตัวอย่าง''
* ตั้งค่า SMTP
 
ssl://mail.eai.in.th
 
465
 
 
 
<!--T:111-->
 
* user, pass ของ eai email
 
thai@eai.in.th และ ไทย@อีเอไอ.ไทย
 
thaiuserpassword
 
 
 
<!--T:112-->
 
* การใช้ ssl แบบ self-sign อาจจะทำให้ roundcube ไม่สามารถเชื่อมต่อ mail server ได้ แก้ปัญหาโดยการ config เพิ่มเติม ดังนี้
 
'''# vi /var/www/html/roundcubemail/config/defaults.inc.php
 
 
  $config['imap_conn_options'] = array(
 
  $config['imap_conn_options'] = array(
 
   'ssl'        => array(
 
   'ssl'        => array(
แถว 748: แถว 727:
 
   ),
 
   ),
 
  );
 
  );
 +
 +
*เข้าหน้าเว็บ config
 +
'''https://eai-lab.bknix.co.th/mailXX/installer/'''
 +
 +
=== <span style='color:green'>ตั้งค่าเชื่อมต่อ database</span>  === <!--T:107-->
 +
host = localhost
 +
dbname =roundcubemail
 +
user = rcmail
 +
password = rcmailpass
 +
db_prefix = rc_
 +
 +
=== <span style='color:green'>ตั้งค่า IMAP/SMTP</span>  === <!--T:109-->
 +
* ตั้งค่า IMAP
 +
'''ssl://mail.mailthaiXX.in.th'''
 +
'''993'''
 +
 +
<!--T:110-->
 +
* ตั้งค่า SMTP
 +
'''ssl://mail.mailthaiXX.in.th'''
 +
'''465'''
 +
''หมายเหตุ user: thai@mailthaiXX.in.th และ ไทย@จดหมายXX.ไทย, pass: thaipass
  
 
</translate>
 
</translate>
  
 
[[Category: EAI]]
 
[[Category: EAI]]

รุ่นแก้ไขปัจจุบันเมื่อ 23:06, 17 พฤศจิกายน 2565

เนื้อหา

EAI Email Server

คู่มือนี้ได้จัดทำขึ้นมาเพื่อทดสอบและเป็นแนวทางการติดตั้ง Email Server ให้สามารถรับส่ง email address ภาษาไทย หรือ Email Address Internationalization (EAI) โดยใช้ Opensource Software เพื่อหาวิธีการที่สามารถใช้งานได้ และสามารถนำไปพัฒนาต่อยอดได้

Software ที่นำมาทดสอบ

  1. Ubuntu 20.04.3 LTS [workshop virtualbox image ]
  2. Postfix 3.4.13
  3. Dovecot 2.3.7.2
  4. MySQL 8.0.27
  5. Roundcube 1.4.4 [ดาวน์โหลด] [tar]

Workshop Slide [ดาวน์โหลด]

  1. [ดาวน์โหลด]

EAI Workshop Authentication

user: eai
pass: eaiws@2022
  • โดเมนที่ใช้เป็นตัวอย่างใน workshop
English : mailthai99.in.th
ภาษาไทย : จดหมาย99.ไทย
(Punycode) : xn--15-6qi0c9cg5dxc.xn--o3cw4h
  • Ubuntu Server:
User: user
Pass: userpass
  • Passphase for SSL Key
Key: mailthai.key, mailthai.crt, mailthai.csr
Pass: mailpass
  • MySQL
User: root
Pass: rootpass

DB: eaimail
User: dbuser
Pass: dbuserpass

DB: roundcubemail
User: rcmail
Pass: rcmailpass

  • Roundcube
User: thai@mailthaiXX.in.th
Pass: thaipass
User: ไทย@จดหมายXX.ไทย
Pass: thaipass

การติดตั้ง EAI Email Server

ตั้งค่า hostname

เข้าใช้งานเครื่องด้วย สิทธิ์ root

sudo su

รหัสผ่าน : userpass

อัพเดตแพคเกจของ mail serverให้เป็นเวอร์ชั่นล่าสุด ด้วยคำสั่ง

apt update && apt -y upgrade

กำหนดชื่อ hostname และตรวจสอบด้วยคำสั่ง

  • กำหนดชื่อ hostname
hostnamectl set-hostname mail.mailthai99.in.th 

ตัวอย่าง เช่น mail.mailthai99.in.th

  • ตรวจสอบ hostname
hostnamectl

ตัวอย่าง

Static hostname: mail.mailthai99.in.th 

เพิ่มค่า hostname ชื่อเดียวกับชื่อที่กำหนดข้างต้นในไฟล์ hosts

nano /etc/hosts

ตัวอย่าง เช่น mail.mailthai99.in.th

ตัวอย่างไฟล์ hosts

127.0.0.1   	localhost mail.mailthai99.in.th

กด crtl+X และกด y เพื่อ save และออกจาก nano

ตั้งค่าเวลาของ Mail Server และตรวจสอบ

  • ตั้งค่าเวลาของ Mail Server
 timedatectl set-timezone "Asia/Bangkok" 
  • ตรวจสอบเวลาของ Mail Server
 timedatectl  status

ตัวอย่าง

Local time: Fri 2022-01-14 12:06:03 +07
Universal time: Fri 2022-01-14 05:06:03 UTC
RTC time: Fri 2022-01-14 05:06:03    
Time zone: Asia/Bangkok (+07, +0700)
System clock synchronized: yes                        
NTP service: active                     
RTC in local TZ: no

ตั้งค่า firewall

1. เปิด firewall service และตรวจสอบสถานะ ด้วยคำสั่ง

  • เปิด firewall service
ufw enable

ตัวอย่าง

Command may disrupt existing ssh connections. Proceed with operation (y|n)? y # ให้ตอบ y
Firewall is active and enabled on system startup

2. เปิด firewall services ของ email server

ufw allow in ssh
ufw allow http
ufw allow https
ufw allow smtp
ufw allow smtps
ufw allow imap
ufw allow imaps
ufw allow mysql

ตัวอย่าง

Rule added
Rule added (v6)

3. ตรวจสอบ firewall services ที่เปิดอยู่

ufw status

ตัวอย่าง

Status: active
To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere                  
80/tcp                     ALLOW       Anywhere                  
443/tcp                    ALLOW       Anywhere                  
25/tcp                     ALLOW       Anywhere                  
465/tcp                    ALLOW       Anywhere                  
143/tcp                    ALLOW       Anywhere                  
993/tcp                    ALLOW       Anywhere                  
3306/tcp                   ALLOW       Anywhere               
22/tcp (v6)                ALLOW       Anywhere (v6)             
80/tcp (v6)                ALLOW       Anywhere (v6)             
443/tcp (v6)               ALLOW       Anywhere (v6)             
25/tcp (v6)                ALLOW       Anywhere (v6)             
465/tcp (v6)               ALLOW       Anywhere (v6)             
143/tcp (v6)               ALLOW       Anywhere (v6)             
993/tcp (v6)               ALLOW       Anywhere (v6)
3306/tcp (v6)              ALLOW       Anywhere (v6)  

*ref: configure firewall services for mail server (https://linuxconfig.org/ubuntu-20-04-open-http-port-80-and-https-port-443-with-ufw)

สร้าง SSL Cert

  • เป็นขั้นตอนการสร้าง SSL cert/key เตรียมไว้ใช้งานกับ postfix และ dovecot
  • ในเครื่อง workshop ได้เตรียม cert mailthai.crt, mailthai.key, mailthai.csr ไว้แล้ว

1. สร้าง private key

cd /etc/ssl/private
openssl req -new -x509 -days 365 -nodes -newkey rsa:2048 -keyout mailthaiXX.key -out mailthaiXX.crt -subj "/C=TH/ST=Bangkok/L=Dusit/O=EAI/OU=IT/CN=mail.mailthaiXX.in.th" 


2. ตรวจสอบ key ที่สร้างขึ้น

ls

ตัวอย่าง

mailthai99.crt  mailthai99.csr

ติดตั้ง MySQL, Postfix, Dovecot

1. ติดตั้ง MySQL, Postfix , Dovecot และ packet ที่จำเป็นสำหรับ email sever ด้วยคำสั่ง

apt -y install postfix postfix-mysql dovecot-core dovecot-mysql dovecot-imapd dovecot-pop3d dovecot-lmtpd  mysql-server

2. สั่ง start service ของ MySQL, Postfix, Dovecot ด้วยคำสั่ง

systemctl start mysql && systemctl start postfix && systemctl start dovecot

3. ดูสถานะการทำงานของ MySQL, Postfix, Dovecot

systemctl status mysql
systemctl status postfix
systemctl status dovecot

ตัวอย่าง

Active: active (running) since Wed 2022-01-05 14:42:29 +07; 33min ago

ตั้งค่า MySQL

ตั้งค่า UTF8 ให้กับ mysql เพื่อให้รองรับภาษาไทย โดยเพิ่มคำสั่งด้านล่างลงในไฟล์ mysqld.cnf ใต้ [mysqld]

nano /etc/mysql/mysql.conf.d/mysqld.cnf

ตัวอย่าง ไฟล์ mysqld.cnf

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

[mysqld]
character-set-server = utf8
collation-server = utf8_general_ci
skip-character-set-client-handshake
bind-address=127.0.0.1
#  
# * Basic Settings
#
user            = mysql

สั่ง restart MySQL

systemctl restart mysql

ตั้งค่าความปลอดภัยการเข้าใช้งาน MySQL โดยจะมีการตั้งค่า password ของ user root เพื่อ login เข้าใช้งาน (* กำหนดเพียง password ใหม่ ในส่วนอื่นๆ ให้ enter ผ่านได้เลย)

mysql_secure_installation

ตัวอย่าง

Please set the password for root here.

New password:                    # rootpass

Re-enter new password:           # rootpass

Login เข้าใช้งาน MySQL โดยใช้ password ของ user root ที่ได้ตั้งค่าไว้

mysql -u root -p

รหัสผ่าน rootpass

สร้าง Database เพื่อใช้เก็บข้อมูลของการใช้งาน Email Server

  • สร้าง Database
CREATE DATABASE eaimail;

ตัวอย่าง

Query OK, 1 row affected (0.02 sec)

*เลือกใช้งาน Database eaimail

USE eaimail;

ตัวอย่าง

Database changed

สร้าง user พร้อมกำหนดสิทธิ์การใช้งานให้กับ eaimail database ที่สร้างขึ้นมา

CREATE USER dbuser@localhost IDENTIFIED BY 'dbuserpass';
GRANT SELECT, INSERT, UPDATE, DELETE ON eaimail.* TO dbuser@localhost;
FLUSH PRIVILEGES;

ตรวจสอบ user ที่สร้างขึ้นใหม่โดย

SHOW GRANTS FOR dbuser@localhost;

ตัวอย่าง

+----------------------------------------------------------------------------------------------------------------+
| Grants for dbuser@localhost                                                 			|
+----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `dbuser`@`localhost`                                  		|
| GRANT SELECT, INSERT, UPDATE, DELETE ON `eaimail`.* TO `dbuser`@`localhost` |
+----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

สร้างตารางใช้เก็บค่า domains, forwardings สำหรับ forward อีเมลภาษาไทยไปภาษาอังกฤษ, users เพื่อใช้สร้าง mailbox

CREATE TABLE domains (domain varchar(50) NOT NULL, PRIMARY KEY (domain));
CREATE TABLE forwardings (source varchar(80) NOT NULL, destination TEXT NOT NULL, PRIMARY KEY (source));
CREATE TABLE users (email varchar(80) NOT NULL, password varchar(256) NOT NULL, PRIMARY KEY (email));

เพิ่มข้อมูล Domain และ Email Account ทั้งภาษาอังกฤษ และภาษาไทย เพื่อเริ่มใช้งาน

INSERT INTO domains (domain) VALUES ('mailthaiXX.in.th');
INSERT INTO domains (domain) VALUES ('จดหมายXX.ไทย');
INSERT INTO users (email, password) VALUES ('thai@mailthaiXX.in.th', '$6$7GQtdsDwSjzB7EAm$0.W6ZBcGEylrQMHKv1j7PZd/6EPoXAYClNhxxs3V2mG/NrHKDsycOTWwETniwI9O/OUeQkqBT5KPfNkDUb8hk/');
INSERT INTO users (email, password) VALUES ('ไทย@จดหมายXX.ไทย', '$6$7GQtdsDwSjzB7EAm$0.W6ZBcGEylrQMHKv1j7PZd/6EPoXAYClNhxxs3V2mG/NrHKDsycOTWwETniwI9O/OUeQkqBT5KPfNkDUb8hk/');

ตัวอย่าง เช่น mailthai99.in.th, จดหมาย99.ไทย, thai@mailthai99.in.th, ไทย@จดหมาย99.ไทย

หมายเหตุ สร้างรหัสผ่านด้วยคำสั่ง doveadm pw -s SHA512-CRYPT

กำหนด Email ที่เข้ามายัง Email Account ภาษาไทย ให้ forward เข้า Email-Account ภาษาอังกฤษ เนื่องจากระบบไม่สามารถสร้าง mailbox เป็น ภาษาไทยได้

INSERT INTO forwardings (source,destination) VALUES ('ไทย@จดหมายXX.ไทย','thai@mailthaiXX.in.th');
quit;

ตั้งค่า postfix

สร้างไฟล์เชื่อมต่อระหว่าง Postfix และ MySQL

cd /etc/postfix/
  • สร้างไฟล์ script เพื่อใช้สำหรับเรียกค่า domain จาก Database
nano /etc/postfix/mysql-virtual_domains.cf

ตัวอย่างไฟล์

user = dbuser
password = dbuserpass
dbname = eaimail
query = SELECT domain FROM domains WHERE domain='%s'
hosts = 127.0.0.1
  • สร้างไฟล์ script เพื่อใช้สำหรับ forward email
nano /etc/postfix/mysql-virtual_forwardings.cf

ตัวอย่างไฟล์

user = dbuser
password = dbuserpass
dbname = eaimail
query = SELECT destination FROM forwardings WHERE source='%s'
hosts = 127.0.0.1
  • สร้างไฟล์ script เพื่อใช้สำหรับ สร้าง mailbox และใช้ login
nano /etc/postfix/mysql-virtual_mailboxes.cf

ตัวอย่างไฟล์

user = dbuser 
password = dbuserpass
dbname = eaimail
query = SELECT CONCAT(SUBSTRING_INDEX(email,'@',-1),'/',SUBSTRING_INDEX(email,'@',1),'/') FROM users WHERE email='%s'
hosts = 127.0.0.1
  • สร้างไฟล์ script เพื่อใช้สำหรับ ส่งต่อ email ทั้งหมด ไปยัง smtp server อื่น ซึ่งใช้ในกรณีที่ host เป็น IP Address
nano /etc/postfix/mysql-virtual_email2email.cf

ตัวอย่างไฟล์

user = dbuser
password = dbuserpass
dbname = eaimail
query = SELECT email FROM users WHERE email='%s'
hosts = 127.0.0.1
  • กำหนดสิทธิ์ให้กับ script ที่สร้างขึ้น
chmod 640 /etc/postfix/mysql-virtual_*.cf && chgrp postfix /etc/postfix/mysql-virtual_*.cf

ตัวอย่าง

-rw-r-----   1 root postfix   124 Jan  7 23:36 mysql-virtual_domains.cf
-rw-r-----   1 root postfix   120 Jan  7 23:39 mysql-virtual_email2email.cf
-rw-r-----   1 root postfix   133 Jan  7 23:37 mysql-virtual_forwardings.cf
-rw-r-----   1 root postfix   190 Jan  7 23:38 mysql-virtual_mailboxes.cf

สร้าง user vmail สำหรับอ่าน/เขียน mailbox

  • สร้าง group เพื่อใช้กับ mailbox
groupadd -g 5000 vmail
useradd -g vmail -u 5000 vmail -d /home/vmail -m

config การทำงานของ postfix ด้วยคำสั่ง postconf (คำสั่งนี้จะไปแก้ไขไฟล์ "/etc/postfix/main.cf")

postconf "myhostname=mail.mailthaiXX.in.th" && \
postconf "mydomain=mailthaiXX.in.th" && \ 
postconf "virtual_alias_domains=จดหมายXX.ไทย" && \
postconf "smtpd_tls_cert_file=/etc/ssl/private/mailthaiXX.crt" && \
postconf "smtpd_tls_key_file=/etc/ssl/private/mailthaiXX.key" && \
postconf "myorigin=\$mydomain" && \
postconf "inet_interfaces=all" && \
postconf "inet_protocols=ipv4" && \
postconf "mydestination=localhost, localhost.localdomain" && \
postconf "mynetworks=127.0.0.1" && \
postconf "readme_directory=no" && \
postconf "shlib_directory=no" && \
postconf "smtpd_sasl_auth_enable=yes" && \
postconf "smtpd_sasl_type=dovecot" && \
postconf "smtpd_sasl_path=private/auth" && \
postconf "smtpd_sasl_authenticated_header=yes" && \
postconf "broken_sasl_auth_clients=yes" && \
postconf "smtpd_use_tls=yes" && \
postconf "smtpd_recipient_restrictions=permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination" && \
postconf "smtpd_tls_auth_only=yes" && \
postconf "smtpd_tls_loglevel=1" && \
postconf "virtual_mailbox_base=/home/vmail" && \
postconf "virtual_mailbox_maps=proxy:mysql:/etc/postfix/mysql-virtual_mailboxes.cf" && \
postconf "virtual_mailbox_domains=proxy:mysql:/etc/postfix/mysql-virtual_domains.cf" && \
postconf "virtual_alias_maps=proxy:mysql:/etc/postfix/mysql-virtual_forwardings.cf, mysql:/etc/postfix/mysql-virtual_email2email.cf" && \
postconf "virtual_uid_maps=static:5000" && \
postconf "virtual_gid_maps=static:5000" && \
postconf "virtual_transport=dovecot" && \
postconf "proxy_read_maps=\$local_recipient_maps \$mydestination \$virtual_alias_maps \$virtual_alias_domains \$virtual_mailbox_maps \$virtual_mailbox_domains \$relay_recipient_maps \$relay_domains \$canonical_maps \$sender_canonical_maps \$recipient_canonical_maps \$relocated_maps \$transport_maps \$mynetworks" && \
postconf "smtputf8_enable=yes" && \
postconf "newaliases_path=null" && \
postconf "mailq_path=null" && \
postconf "setgid_group=postdrop" && \
postconf "html_directory=null" && \
postconf "manpage_directory = null" &&  \
postconf "sample_directory = null" && \
postconf "sendmail_path=null"

ตรวจสอบการ config postfix ด้วยคำสั่ง

 postconf -n
  • ตัวอย่างไฟล์
broken_sasl_auth_clients = yes
command_directory = /usr/sbin
compatibility_level = 2
daemon_directory = /usr/lib/postfix/sbin
data_directory = /var/lib/postfix
debugger_command = PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin ddd $daemon_directory/$process_name $process_id & sleep 5
html_directory = null
inet_interfaces = all
inet_protocols = ipv4 
mailq_path = null 
manpage_directory = null
mydestination = localhost, localhost.localdomain
mydomain = mailthai99.in.th
myhostname = mail.mailthai99.in.th
mynetworks = 127.0.0.1
myorigin = $mydomain
newaliases_path = null
proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps $virtual_alias_domains $virtual_mailbox_maps $virtual_mailbox_domains $relay_recipient_maps $relay_domains $canonical_maps $sender_canonical_maps $recipient_canonical_maps $relocated_maps $transport_maps $mynetworks
readme_directory = no
sample_directory = null
sendmail_path = null
setgid_group = postdrop
shlib_directory = no
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
smtpd_sasl_auth_enable = yes
smtpd_sasl_authenticated_header = yes
smtpd_sasl_path = private/auth
smtpd_sasl_type = dovecot
smtpd_tls_auth_only = yes
smtpd_tls_cert_file = /etc/ssl/private/mailthai99.crt
smtpd_tls_key_file = /etc/ssl/private/mailthai99.key
smtpd_tls_loglevel = 1
smtpd_use_tls = yes
smtputf8_enable = yes
unknown_local_recipient_reject_code = 550
virtual_alias_domains = จดหมาย99.ไทย
virtual_alias_maps = proxy:mysql:/etc/postfix/mysql-virtual_forwardings.cf, mysql:/etc/postfix/mysql-virtual_email2email.cf
virtual_gid_maps = static:5000
virtual_mailbox_base = /home/vmail
virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql-virtual_domains.cf
virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql-virtual_mailboxes.cf
virtual_transport = dovecot
virtual_uid_maps = static:5000

config การทำงานของ postfix ด้วยคำสั่ง postconf (คำสั่งนี้จะไปแก้ไขไฟล์ "/etc/postfix/master.cf")

postconf -M smtps/inet="smtps inet n - - - - smtpd" && \
postconf -P "smtps/inet/syslog_name=postfix/smtps" && \
postconf -P "smtps/inet/smtpd_tls_wrappermode=yes" && \
postconf -P "smtps/inet/smtpd_sasl_auth_enable=yes" && \
postconf -P "smtps/inet/smtpd_client_restrictions=permit_sasl_authenticated,reject" && \
postconf -P "smtps/inet/smtpd_relay_restrictions=permit_sasl_authenticated,reject" && \
postconf -P "smtps/inet/milter_macro_daemon_name=ORIGINATING" && \
postconf -M dovecot/unix="dovecot   unix  -       n       n       -       -       pipe flags=DRhu user=vmail:vmail argv=/usr/lib/dovecot/deliver -f \${sender} -d \${recipient}"

ตรวจสอบการ config postfix ด้วยคำสั่ง

postconf -M
  • ตัวอย่างไฟล์
smtps inet n - - - - smtpd
   -o syslog_name=postfix/smtps
   -o smtpd_tls_wrappermode=yes 
   -o smtpd_sasl_auth_enable=yes 
   -o smtpd_client_restrictions=permit_sasl_authenticated,reject 
   -o smtpd_relay_restrictions=permit_sasl_authenticated,reject 
   -o milter_macro_daemon_name=ORIGINATING
dovecot   unix  -       n       n       -       -       pipe
    flags=DRhu user=vmail:vmail argv=/usr/lib/dovecot/deliver -f ${sender} -d ${recipient}

ตั้งค่า mysql postfix driver

nano /etc/postfix/dynamicmaps.cf

ตัวอย่าง

mysql   /usr/lib/postfix/postfix-mysql.so       dict_mysql_open

สั่ง restart postfix

systemctl restart postfix

ตั้งค่า dovecot

สร้างไฟล์เชื่อมต่อระหว่าง dovecot และ MySQL

  • สร้าง script เพื่อให้ dovecot ใช้งานกับ SQL ที่เราได้กำหนดก่อนหน้านี้
nano /etc/dovecot/dovecot-sql.conf.ext

ตัวอย่างไฟล์

driver = mysql
connect = host=127.0.0.1 dbname=eaimail user=dbuser password=dbuserpass
default_pass_scheme = SHA512-CRYPT
password_query = SELECT email as user, password FROM users WHERE email='%u';
  • กำหนดสิทธิ์อนุญาตให้ user ที่อยู่ใน dovecot group ใช้งาน
chmod 640 /etc/dovecot/dovecot-sql.conf.ext && chgrp dovecot /etc/dovecot/dovecot-sql.conf.ext

ตัวอย่าง

-rw-r-----   1 root dovecot 6023 Jan  7 23:54 dovecot-sql.conf.ext

Backup ไฟล์ dovecot.conf เดิมไว้ก่อน

mv /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf-backup

ตัวอย่าง

-rw-r--r--   1 root root    4401 Apr 12  2021 dovecot.conf-backup

สร้างไฟล์ "/etc/dovecot/dovecot.conf” และแก้ไขไฟล์ "/etc/dovecot/dovecot.conf" ด้วยการใส่คำสั่งตามด้านล่าง

nano /etc/dovecot/dovecot.conf

ตัวอย่าง

protocols = imap pop3 lmtp
log_timestamp = "%Y-%m-%d %H:%M:%S"
mail_location = maildir:/home/vmail/%d/%n/Maildir
ssl_cert = </etc/ssl/private/mailthaiXX.crt
ssl_key = </etc/ssl/private/mailthaiXX.key
ssl_cipher_list = ALL:!LOW:!SSLv2:!EXP:!aNULL
auth_mechanisms = plain login
namespace {
	type = private
	separator = .
	prefix = INBOX.
	inbox = yes
}
service auth {
	unix_listener auth-master {
		mode = 0600
		user = vmail
	}
	unix_listener /var/spool/postfix/private/auth {
		mode = 0666
		user = postfix
		group = postfix
	}
	user = root
}
service auth-worker {
	user = root
}
protocol lda {
	log_path = /home/vmail/dovecot-deliver.log
	auth_socket_path = /var/run/dovecot/auth-master
	postmaster_address = postmaster@mailthaiXX.in.th
}
protocol pop3 {
	pop3_uidl_format = %08Xu%08Xv
	pop3_client_workarounds = outlook-no-nuls oe-ns-eoh
}
passdb {
	driver = sql
	args = /etc/dovecot/dovecot-sql.conf.ext
}
userdb {
	driver = static
	args = uid=5000 gid=5000 home=/home/vmail/%d/%n allow_all_users=yes
}
service stats {
	unix_listener stats-reader {
		user =  vmail
		group = vmail
		mode = 0660
	}
	unix_listener stats-writer {
		user = vmail
		group = vmail
		mode = 0660
	}
}

สั่ง restart dovecot

systemctl restart dovecot

ทดสอบการทำงานของ Email Server

ทดสอบ imap ด้วยคำสั่ง telnet

 telnet localhost imap

ตัวอย่าง

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
* OK [CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE LITERAL+ STARTTLS AUTH=PLAIN AUTH=LOGIN] Dovecot (Ubuntu) ready.

หมายเหตุ กด Ctrl+] และตามด้วย q เพื่อออก

ทดสอบ smtp ด้วยคำสั่ง telnet

telnet localhost smtp
ehlo localhost

ตัวอย่าง

250-mail.mailthai99.in.th
250-PIPELINING
250-SIZE 30720000
250-VRFY
250-ETRN
250-STARTTLS 
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-DSN
250 SMTPUTF8

หมายเหตุ กด Ctrl+] และตามด้วย q เพื่อออก

ทดสอบการส่ง email ด้วย telnet

telnet localhost smtp
ehlo localhost
MAIL FROM: thai@mailthaiXX.in.th
RCPT TO: thai@mailthai99.in.th
data
Subject: test mailthaiXX.in.th to mailthai99.in.th
test 
 . 			ใช้เครื่องหมาย “.” เพื่อจบข้อความและส่งข้อความ

หมายเหตุ กด Ctrl+] และตามด้วย q เพื่อออก

telnet localhost smtp
ehlo localhost
MAIL FROM: thai@mailthaiXX.in.th SMTPUTF8
RCPT TO: ไทย@จดหมาย99.ไทย
data
Subject: test mailthaiXX.in.th to mailthai99.in.th
test 
 . 			ใช้เครื่องหมาย “.” เพื่อจบข้อความและส่งข้อความ

หมายเหตุ กด Ctrl+] และตามด้วย q เพื่อออก

ตรวจสอบ log ของการส่ง Email : /var/log/mail.log ซึ้งจะได้ผลตามด้านล่าง

Nov 13 15:19:07 mail postfix/pickup[31954]: E9AB020199E5: uid=0 from=<root>
Nov 13 15:19:07 mail postfix/cleanup[1938]: E9AB020199E5: message-id=<20171113081907.E9AB020199E5@mail.mailthai99.in.th>
Nov 13 15:19:07 mail postfix/qmgr[21141]: E9AB020199E5: from=<root@mailthai99.in.th>, size=434, nrcpt=1 (queue active)
Nov 13 15:19:08 mail postfix/pipe[1946]: E9AB020199E5: to=<thai@mailthai99.in.th>, relay=dovecot, delay=0.09, delays=0.04/0.01/0/0.03, dsn=2.0.0, status=sent (delivered via dovecot service)
Nov 13 15:19:08 mail postfix/qmgr[21141]: E9AB020199E5: removed

ตรวจสอบ log ของ dovecot : /home/vmail/dovecot-deliver.log ซึ่งจะได้ผลตามด้านล่าง

2017-11-13 15:19:08 lda(thai@mailthai99.in.th): Info: msgid=<20171113081907.E9AB020199E5@mail.eai.in.th>: saved mail to INBOX

ตรวจสอบ mailbox เมื่อมีการรับส่ง-ส่ง Email จะสร้างขึ้นโดย dovecot ซึ่งจะได้ผลตามตัวอย่าง

cd /home/vmail/mailthaiXX.in.th/Maildir
find

ตัวอย่างเช่น mailthai99.in.th"

.
./dovecot-uidlist
./cur
./new
./new/1285609582.P6115Q0M368794.li172-137
./dovecot.index
./dovecot.index.log
./tmp

ติดตั้ง roundcube webmail

ติดตั้ง apache php

  • ใช้คำสั่ง apt ติดตั้ง php, php library และ apache web server
apt -y install apache2 php php-imap php-imagick php-json php-xml php-mbstring php-pear php-intl php-ldap php-gd php-zip php-mysql php-curl php-bz2 php-gmp phpmyadmin
  • ใช้คำสั่ง start service apache และ php
systemctl start apache2
  • ตรวจสอบสถานะการทำงานของ apache
systemctl status apache2

ตั้งค่า php (เราจะตั้งค่าเฉพาะส่วนที่จำเป็นต่อการใช้งาน roundcube)

  • ตั้งค่า timezone
nano /etc/php/7.4/apache2/php.ini

ตัวอย่าง [Date]

; Defines the default timezone used by the date functions
; http://php.net/date.timezone 
date.timezone = Asia/Bangkok

ตั้งค่า MySQL Database

  • สร้าง database roundcubemail โดยกำหนด username=rcmail, password=rcmailpass
mysql -u root -p


CREATE DATABASE roundcubemail CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER rcmail@localhost IDENTIFIED BY 'rcmailpass' ;
GRANT ALL PRIVILEGES ON roundcubemail.* TO rcmail@localhost;
FLUSH PRIVILEGES;
quit;

ตั้งค่า roundcube

cd /var/www/html/
wget https://github.com/roundcube/roundcubemail/releases/download/1.4.4/roundcubemail-1.4.4-complete.tar.gz
tar zvfx /var/www/html/roundcubemail-1.4.4-complete.tar.gz 
mv /var/www/html/roundcubemail-1.4.4 /var/www/html/roundcubemail
cp /var/www/html/roundcubemail/config/config.inc.php.sample /var/www/html/roundcubemail/config/config.inc.php
chown -R www-data:www-data /var/www/html/roundcubemail

สร้าง virtualhost

rm /etc/apache2/sites-enabled/000-default.conf
nano /etc/apache2/sites-available/rcmail.conf
 <VirtualHost *:80>
	ServerName mail.mailthai99.in.th
	ServerAdmin webmaster@mailtha99.in.th
	DocumentRoot /var/www/html/roundcubemail
	<Directory /var/www/html/roundcubemail>
		Options -Indexes +FollowSymLinks
		AllowOverride All
	</Directory>
	ErrorLog /var/log/apache2/mail.mailthai01.th-error.log
	CustomLog /var/log/apache2/mail.mailthai01.th-access.log combined
</VirtualHost>
ln -s /etc/apache2/sites-available/rcmail.conf /etc/apache2/sites-enabled/rcmail.conf
systemctl restart apache2

ทดสอบเข้าเว็บโซต์

https://eai-lab.bknix.co.th/mailXX/

ตัวอย่าง

DATABASE ERROR: CONNECTION FAILED!
Unable to connect to the database!
Please contact your server-administrator.

เข้าหน้า config roundcube

  • แก้ไขไฟล์ config.inc.php
nano /var/www/html/roundcubemail/config/config.inc.php

ตัวอย่าง

$config['enable_installer'] = true;
  • การใช้ ssl แบบ self-sign อาจจะทำให้ roundcube ไม่สามารถเชื่อมต่อ mail server ได้ แก้ปัญหาโดยการ config เพิ่มเติม ดังนี้
nano /var/www/html/roundcubemail/config/defaults.inc.php

ตัวอย่าง

$config['imap_conn_options'] = array(
  'ssl'         => array(
     'verify_peer'      => false,
     'verify_peer_name' => false,
  ),
);
 
$config['smtp_conn_options'] = array(
  'ssl'         => array(
     'verify_peer'      => false,
     'verify_peer_name' => false,
  ),
);
  • เข้าหน้าเว็บ config
https://eai-lab.bknix.co.th/mailXX/installer/

ตั้งค่าเชื่อมต่อ database

host = localhost
dbname =roundcubemail
user = rcmail
password = rcmailpass
db_prefix = rc_

ตั้งค่า IMAP/SMTP

  • ตั้งค่า IMAP
ssl://mail.mailthaiXX.in.th
993
  • ตั้งค่า SMTP
ssl://mail.mailthaiXX.in.th
465

หมายเหตุ user: thai@mailthaiXX.in.th และ ไทย@จดหมายXX.ไทย, pass: thaipass