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

จาก Wiki_EAI
ไปยังการนำทาง ไปยังการค้นหา
(กำหนดให้รุ่นปรับปรุงนี้สำหรับการแปลภาษา)
 
(ไม่แสดง 43 รุ่นระหว่างกลางโดยผู้ใช้ 3 คน)
แถว 1: แถว 1:
== EAI Email Server ==
+
<languages/>
 +
<translate>
  
 +
=EAI Email Server= <!--T:1-->
 
คู่มือนี้ได้จัดทำขึ้นมาเพื่อทดสอบและเป็นแนวทางการใช้งาน Email Server ในรูปภาษาไทย(EAI) โดยใช้ Opensource  Software ในการทดสอบเพื่อหาวิธีการที่สามารถใช้งานได้ง่ายและปลอดภัย และสามารถนำไปประยุกใช้งานได้
 
คู่มือนี้ได้จัดทำขึ้นมาเพื่อทดสอบและเป็นแนวทางการใช้งาน Email Server ในรูปภาษาไทย(EAI) โดยใช้ Opensource  Software ในการทดสอบเพื่อหาวิธีการที่สามารถใช้งานได้ง่ายและปลอดภัย และสามารถนำไปประยุกใช้งานได้
Software ที่นำมาทดสอบ:
+
==Software ที่นำมาทดสอบ:==
Centos 7.4
+
# Centos 7.4
Postfix 3.2.3
+
# Postfix 3.2.3
Dovecot 2.2.10
+
# Dovecot 2.2.10
MariaDB 10.3.11
+
# MariaDB 10.3.11
Squirrelmail 5.5.56
+
# Squirrelmail 5.5.56
 
*คู่มือนี้ไม่ได้อธิบายถึงการติดตั้ง Centos 7.4
 
*คู่มือนี้ไม่ได้อธิบายถึงการติดตั้ง Centos 7.4
 
*Postfix ต้องเป็น Version 3.x.x ขึ้นไป จะรองรับ EAI
 
*Postfix ต้องเป็น Version 3.x.x ขึ้นไป จะรองรับ EAI
แถว 13: แถว 15:
 
*ผู้จัดทำใช้ putty และ winscp เป็นเครื่องมือในการเชื่อมต่อกับ server
 
*ผู้จัดทำใช้ putty และ winscp เป็นเครื่องมือในการเชื่อมต่อกับ server
  
== การติดตั้ง EAI Email Server ==
+
== ขั้นตอนการติดตั้ง EAI Email Server บน CentOS 7 == <!--T:2-->
[[1. ตั้งค่า hostname CentOS 7]]
+
=== 1. ตั้งค่า hostname ของ EAI Email Server บน CentOS 7 ===
 +
1. อัพเดตแพคเกจของ mail serverให้เป็นเวอร์ชั่นล่าสุด ด้วยคำสั่ง
 +
'''# yum -y update'''
  
[[2. ตั้งค่า firewall CentOS 7]]
+
<!--T:3-->
 +
2. กำหนดชื่อ hostname ในตัวอย่างนี้กำหนดชื่อว่า mail.eai.in.th ด้วยคำสั่ง
 +
'''# hostnamectl set-hostname ''[[mail.eai.in.th]]'''''
  
[[3. ติดตั้ง mariadb, postfix, dovecot CentOS 7]]
+
<!--T:4-->
 +
3. เพิ่มค่า hostname ชื่อเดียวกับชื่อที่กำหนดข้างต้นในไฟล์ hosts
 +
'''# vi /etc/hosts'''
  
[[4. ตั้งค่า postfix เพื่อให้เชื่อมต่อกับ MariaDB]]
+
<!--T:5-->
 +
''ตัวอย่างไฟล์ hosts''
 +
127.0.0.1  localhost localhost.localdomain localhost4 localhost4.localdomain4 '''''[[mail.eai.in.th]]'''''
 +
::1        localhost localhost.localdomain localhost6 localhost6.localdomain6 '''''[[mail.eai.in.th]]'''''
  
[[5. วิธีการสร้าง SSL]]
+
<!--T:6-->
 +
''*ระวัง envelopment ของทาง partner ว่าติดตั้ง centos ในรูปแบบใดมาให้ (อาจจะต้องติดตั้ง service เพิ่ม หากติดตั้งมาแบบ minimal เช่นใช้คำสั่ง vi ไม่ได้)''
  
[[6. ตั้งค่า postfix]]
+
<!--T:7-->
 +
4. ติดตั้ง wget เพื่อใช้ดาวน์โหลดซอฟแวร์ที่จำเป็นต้องใช้งานเพิ่ม
 +
'''# yum -y install wget'''
  
[[7. ตั้งค่า Dovecot CentOS 7]]
+
<!--T:8-->
 +
5. ติดตั้ง telnet เพื่อทดสอบการทำงานของ email server
 +
'''# yum -y install telnet'''
  
[[8. ทดสอบ POP3 Server CentOS 7]]
+
=== 2. ตั้งค่า firewall ของ EAI Email Server บน CentOS 7 === <!--T:9-->
 +
1. ตรวจสอบสถานะ firewalld service บน mail server
 +
*ต้องมีสถานะ active (running)
  
[[9. กำหนด Email Alias CentOS 7]]
+
<!--T:10-->
 +
'''# systemctl status firewalld'''
  
[[10. ตรวจสอบการใช้งาน SMTP-AUTH and TLS CentOS 7]]
+
<!--T:11-->
 +
''ตัวอย่าง''
  
[[10. ตั้งค่า Domain และ User]]
+
<!--T:12-->
 +
'''Active: <span style='color:red'>active (running)</span> since Mon 2019-11-18 14:12:03 +07; 1h 13min ago'''
  
[[10. ตั้งค่า Domain และ User CentOS 7]]
+
<!--T:13-->
 +
2. ตรวจสอบ firewall zone และ network interface ที่เปิดอยู่
 +
'''# firewall-cmd --get-active-zones'''
 +
''ตัวอย่าง''
 +
'''public
 +
'''interfaces: ens32
  
[[11. ทดสอบการรับ-ส่ง Email และตรวจสอบ mailbox]]
+
<!--T:14-->
 +
3. ตรวจสอบรายการ services ของ firewall ที่เปิดอยู่ที่ zone public
 +
'''# firewall-cmd --zone=public --list-services'''
  
[[12. ติดตั้ง Squirrelmail Server ซึ่งเป็น Webserver เพื่อใช้ทดสอบส่ง Email ภาษาไทย]]
+
<!--T:15-->
 +
''ตัวอย่าง''
 +
''' cockpit dhcpv6-client ssh '''
 +
 
 +
<!--T:16-->
 +
4. เปิด firewall services ที่ zone public ของ mail server (สามารถเปิดเท่าต้องการได้)
 +
'''# firewall-cmd --permanent --zone=public --add-service=http
 +
'''# firewall-cmd --permanent --zone=public --add-service=https
 +
'''# firewall-cmd --permanent --zone=public --add-service=mysql
 +
'''# firewall-cmd --permanent --zone=public --add-service=smtp
 +
'''# firewall-cmd --permanent --zone=public --add-service=smtps
 +
'''# firewall-cmd --permanent --zone=public --add-service=imap
 +
'''# firewall-cmd --permanent --zone=public --add-service=imaps
 +
'''# firewall-cmd --reload
 +
 
 +
<!--T:17-->
 +
5. ตรวจสอบรายการ services ของ firewall ที่เปิดอยู่ที่ zone public อีกครั้ง จะพบ services เปิดเพิ่มขึ้นมา
 +
 
 +
<!--T:18-->
 +
''*ref: configure firewall services for mail server (https://spalinux.com/2015/06/centos-7-basic-configure-firewall-by-firewall-cmd)''
 +
'''# firewall-cmd --zone=public --list-services'''
 +
 
 +
<!--T:19-->
 +
''ตัวอย่าง''
 +
''' cockpit dhcpv6-client <span style='color:red'>http https imap imaps mysql smtp smtps</span> ssh '''
 +
 
 +
<!--T:20-->
 +
6. ในตัวอย่างนี้เราแนะนำให้ปิดการทำงานของ SELinux เพื่อความสะดวกในการตั้งค่า (ไม่แนะนำให้ทำกับเครื่อง production)
 +
*เปลี่ยน SELinux=enforcing เป็น SELinux=disabled
 +
'''# vi /etc/selinux/config'''
 +
 
 +
<!--T:21-->
 +
''ตัวอย่าง''
 +
 
 +
<!--T:22-->
 +
'''# 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:23-->
 +
''ตัวอย่าง''
 +
 
 +
<!--T:24-->
 +
''' disabled '''
 +
 
 +
=== 3. สร้าง SSL ของ EAI Email Server บน CentOS 7 === <!--T:25-->
 +
*เป็นขั้นตอนการสร้าง SSL key เตรียมไว้ใช้งานกับ postfix และ dovecot
 +
1. สร้าง private key
 +
'''# cd /etc/pki/tls/certs
 +
'''# make '''[[eai.key]]''' '''
 +
 
 +
<!--T:26-->
 +
'' ตัวอย่าง ''
 +
 
 +
<!--T:27-->
 +
'''umask 77 ; \
 +
'''/usr/bin/openssl genrsa -aes128 2048 > eai.key
 +
'''Generating RSA private key, 2048 bit long modulus
 +
'''...
 +
'''...
 +
'''e is 65537 (0x10001)
 +
'''Enter pass phrase: '''[[#set passphrase]]'''
 +
'''Verifying - Enter pass phrase:  '''[[#confirm]]''' '''
 +
 
 +
<!--T:28-->
 +
2. ถอด Passphase ออกจาก private key ที่สร้างขึ้น
 +
'''# openssl rsa -in '''[[eai.key]]''' -out '''[[eai.key]]''' '''
 +
 
 +
<!--T:29-->
 +
'' ตัวอย่าง ''
 +
 
 +
<!--T:30-->
 +
'''Enter pass phrase for eai.key:  '''[[#input passphrase]]'''
 +
'''writing RSA key'''
 +
 
 +
<!--T:31-->
 +
3. สร้าง csr
 +
'''# make eai.csr'''
 +
 
 +
<!--T:32-->
 +
'' ตัวอย่าง ''
 +
 
 +
<!--T:33-->
 +
'''umask 77 ; \
 +
'''/usr/bin/openssl req -utf8 -new -key eai.key -out eai.csr
 +
'''You are about to be asked to enter information that will be incorporated
 +
'''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:34-->
 +
4. ทำการ generate key
 +
'''# openssl x509 -in '''[[eai.csr]]''' -out '''[[eai.crt]]''' -req -signkey '''[[eai.key]]''' -days 3650'''
 +
 
 +
<!--T:35-->
 +
'' ตัวอย่าง ''
 +
 
 +
<!--T:36-->
 +
'''Signature ok
 +
'''subject=/C=JP/ST=Hiroshima/L=Hiroshima/O=GTS/OU=Server World/CN=www.srv.world/emailAddress=xxx@eai.in.th
 +
'''Getting Private key'''
 +
 
 +
=== 4. ติดตั้ง mariadb, postfix, dovecot ของ EAI Email Server บน CentOS 7 === <!--T:37-->
 +
1. เริ่มติดตั้ง mariadb, postfix, dovecot ด้วยคำสั่ง
 +
'''# yum --enablerepo=centosplus install postfix'''
 +
'''# yum install dovecot mariadb-server dovecot-mysql'''
 +
 
 +
<!--T:38-->
 +
2. อัพเดตจาก postfix version 2.x.x เป็น version 3.x.x
 +
*บน CentOS 7 จะติดตั้ง postfix version 2.x.x เป็นค่าเริ่มต้น เราต้อง remove ออกก่อน เนื่องจาก postfix version 2.x.x ยังไม่รองรับ EAI และอัพเกรดเป็น version 3.x.x ขึ้นไป
 +
'''# yum -y remove postfix'''
 +
'''# yum -y remove ssmtp'''
 +
'''# yum -y remove sendmail'''
 +
 
 +
<!--T:39-->
 +
*สร้างไฟล์ repo ชื่อ gf.repo เพื่อติดตั้ง postfix version 3.x.x ขึ้นไป
 +
'''# vi /etc/yum.repos.d/gf.repo'''
 +
 
 +
<!--T:40-->
 +
''ตัวอย่าง''
 +
 
 +
<!--T:41-->
 +
'''[gf]
 +
'''name=Ghettoforge packages that won't overwrite core distro packages.
 +
'''mirrorlist=http://mirrorlist.ghettoforge.org/el/7/gf/$basearch/mirrorlist
 +
'''enabled=1
 +
'''gpgcheck=1
 +
'''gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-gf.el7
 +
'''failovermethod=priority
 +
 
 +
'''[gf-plus]
 +
'''name=Ghettoforge packages that will overwrite core distro packages.
 +
'''mirrorlist=http://mirrorlist.ghettoforge.org/el/7/plus/$basearch/mirrorlist
 +
'''# Please read http://ghettoforge.org/index.php/Usage *before* enabling this repository!
 +
'''enabled=1
 +
'''gpgcheck=1
 +
'''gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-gf.el7'''
 +
'''failovermethod=priority'''
 +
 
 +
<!--T:42-->
 +
*โหลด key ที่ใช้กับ repo วางไว้ใน path : /etc/pki/rpm-gpg/
 +
'''# cd /etc/pki/rpm-gpg/
 +
'''# wget http://mirror.ghettoforge.org/distributions/gf/RPM-GPG-KEY-gf.el7'''
 +
 
 +
<!--T:43-->
 +
*ติดตั้ง postfix version 3.x.x
 +
'''# yum -y install postfix3 postfix-mysql
 +
 
 +
<!--T:44-->
 +
*ตรวจสอบ postfix version 3.x.x
 +
'''# postconf -d | grep mail_version
 +
 
 +
  <!--T:45-->
 +
'''mail_version = 3.5.7
 +
 
 +
<!--T:46-->
 +
3. ตั้งค่า mariadb, postfix, dovecot ให้เริ่มทำงานทุกครั้งที่เปิดเครื่อง
 +
'''# systemctl start mariadb'''
 +
'''# systemctl enable mariadb'''
 +
'''# systemctl start postfix'''
 +
'''# systemctl enable postfix'''
 +
'''# systemctl start dovecot'''
 +
'''# systemctl enable dovecot'''
 +
 
 +
<!--T:47-->
 +
4. ตรวจสอบสถานะการทำงานของ mariadb, postfix, dovecot
 +
'''# systemctl status mariadb'''
 +
'''# systemctl status postfix'''
 +
'''# systemctl status dovecot'''
 +
 
 +
=== 5. ตั้งค่า mariadb ของ EAI Email Server บน CentOS 7 === <!--T:48-->
 +
1. ตั้งค่า UTF8 ให้กับ mysql เพื่อให้รองรับภาษาไทย โดยเพิ่มคำสั่งด้านล่างลงใน /etc/my.cnf ใต้ [mysqld]
 +
'''# vi /etc/my.cnf'''
 +
 
 +
<!--T:49-->
 +
''ตัวอย่าง''
 +
 
 +
<!--T:50-->
 +
'''[mysqld]
 +
'''<span style='color:red'>character-set-server = utf8</span>'''
 +
'''<span style='color:red'>collation-server = utf8_general_ci</span>'''
 +
'''<span style='color:red'>skip-character-set-client-handshake</span>'''
 +
'''<span style='color:red'>bind-address=127.0.0.1</span>'''
 +
'''datadir=/var/lib/mysql
 +
'''socket=/var/lib/mysql/mysql.sock
 +
'''# Disabling symbolic-links is recommended to prevent assorted security risks
 +
'''symbolic-links=0
 +
'''# 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 mariadb according to the
 +
'''# instructions in http://fedoraproject.org/wiki/Systemd
 +
 +
'''[mysqld_safe]
 +
'''log-error=/var/log/mariadb/mariadb.log
 +
'''pid-file=/var/run/mariadb/mariadb.pid
 +
 +
'''#
 +
'''# include all files from the config directory
 +
'''#
 +
'''!includedir /etc/my.cnf.d'''
 +
 
 +
<!--T:51-->
 +
2. สั่ง restart mariadb
 +
'''# systemctl restart mariadb'''
 +
 
 +
<!--T:52-->
 +
3. ตั้งค่าความปลอดภัยการเข้างาน mysql โดยจะมีการตั้ง password ของ root user เพื่อ login เข้าใช้งาน (*กำหนดเพียง password ใหม่ และในส่วนอื่นๆให้ enter ผ่านได้เลย)
 +
'''# mysql_secure_installation'''
 +
 
 +
<!--T:53-->
 +
4. Login ข้าใช้งาน mysql โดยใช้ password ของ root user ที่ได้ตั้งไว้
 +
'''# mysql -u root -p'''
 +
 
 +
<!--T:54-->
 +
5. สร้าง DB เพื่อใช้งานการเก็บข้อมูลของการใช้งาน Email Server  
 +
'''# CREATE DATABASE '''[[eaimail]]''';
 +
'''# USE '''[[eaimail]]''';'''
 +
 
 +
<!--T:55-->
 +
6. สร้าง user พร้อมกำหนดสิทธิ์การใช้งานให้กับ eaimail db
 +
'''# GRANT SELECT, INSERT, UPDATE, DELETE ON '''[[eaimail]]'''.* TO ''''[[yourdbuser]]''''@'localhost' IDENTIFIED BY ''''[[yourdbpassword]]'''';
 +
'''# GRANT SELECT, INSERT, UPDATE, DELETE ON '''[[eaimail]]'''.* TO ''''[[yourdbuser]]''''@'localhost.localdomain' IDENTIFIED BY ''''[[yourdbpassword]]'''';
 +
'''# FLUSH PRIVILEGES;'''
 +
 
 +
<!--T:56-->
 +
7. ตรวจสอบ userที่สร้างขึ้นใหม่โดย
 +
'''# SHOW GRANTS FOR ''''[[yourdbuser]]''''@'localhost';'''
 +
 
 +
<!--T:57-->
 +
8. สร้างตารางใช้เก็บค่า Domain
 +
'''# CREATE TABLE domains (domain varchar(50) NOT NULL, PRIMARY KEY (domain) );'''
 +
 
 +
<!--T:58-->
 +
9. สร้างตารางใช้สำหรับ forward mail
 +
'''# CREATE TABLE forwardings (source varchar(80) NOT NULL, destination TEXT NOT NULL, PRIMARY KEY (source) );'''
 +
 
 +
<!--T:59-->
 +
10. สร้างตาราง user เพื่อใช้สร้าง mailbox และออกจาก mysql
 +
'''# CREATE TABLE users (email varchar(80) NOT NULL, password varchar(20) NOT NULL, PRIMARY KEY (email) );
 +
'''# quit'''
 +
 
 +
<!--T:60-->
 +
11. สั่ง restart mariadb
 +
'''# systemctl restart mariadb'''
 +
 
 +
<!--T:61-->
 +
12. ตั้งค่า domain และ user ใน database eaimail
 +
*เข้า mariadb และเลือก database eaimail, ใส่ชื่อโดเมนภาษาอังกฤษ และภาษาไทย, ใส่ชื่อ Email-Account ภาษาอังกฤษและภาษาไทย พร้อม password เพื่อเริ่มใช้งาน
 +
'''# mysql -u root -p'''
 +
 
 +
<!--T:62-->
 +
'''# USE '''[[eaimail]]''';
 +
'''# INSERT INTO domains (domain) VALUES (''''[[eai.in.th]]'''');
 +
'''# INSERT INTO domains (domain) VALUES (''''[[อีเอไอ.ไทย]]'''');
 +
'''# INSERT INTO users (email, password) VALUES (''''[[thai@eai.in.th]]'''', ENCRYPT(''''[[thaiuserpassword]]''''));
 +
'''# INSERT INTO users (email, password) VALUES (''''[[ไทย@อีเอไอ.ไทย]]'''', ENCRYPT(''''[[thaiuserpassword]]''''));'''
 +
 
 +
<!--T:63-->
 +
* กำหนด Email ที่เข้ามายัง Email Account ภาษาไทย ให้ forward เข้า Email-Account ภาษาอังกฤษ เนื่องจากระบบไม่สามารถสร้าง mailbox เป็น ภาษาไทยได้
 +
'''# INSERT INTO forwardings (source,destination) VALUES (''''[[ไทย@อีเอไอ.ไทย]]'''',''''[[thai@eai.in.th]]'''');
 +
'''# quit'''
 +
 
 +
=== 6. ตั้งค่า postfix ของ EAI Email Server บน CentOS 7 === <!--T:64-->
 +
1. สร้างไฟล์ mysql-virtual_domains.cf เพื่อใช้สำหรับดึงชื่อโดเมนจาก database
 +
'''# vi /etc/postfix/mysql-virtual_domains.cf'''
 +
 
 +
<!--T:65-->
 +
''ตัวอย่าง''
 +
 
 +
<!--T:66-->
 +
'''user = '''[[yourdbuser]]'''
 +
'''password = '''[[yourdbpassword]]'''
 +
'''dbname = '''[[eaimail]]'''
 +
'''query = SELECT domain AS virtual FROM domains WHERE domain='%s' '''
 +
'''hosts = 127.0.0.1'''
 +
 
 +
<!--T:67-->
 +
2. สร้างไฟล์ mysql-virtual_forwardings.cf เพื่อใช้ดึงข้อมูลการ forward email จาก database
 +
'''# vi /etc/postfix/mysql-virtual_forwardings.cf'''
 +
 
 +
<!--T:68-->
 +
''ตัวอย่าง''
 +
 
 +
<!--T:69-->
 +
'''user = '''[[yourdbuser]]'''
 +
'''password = '''[[yourdbpassword]]'''
 +
'''dbname = '''[[eaimail]]'''
 +
'''query = SELECT destination FROM forwardings WHERE source='%s' '''
 +
'''hosts = 127.0.0.1'''
 +
 
 +
<!--T:70-->
 +
3. สร้างไฟล์ mysql-virtual_mailboxes.cf เพื่อใช้ดึงข้อมูลสำหรับ สร้าง mailbox และใช้ login จาก database
 +
'''# vi /etc/postfix/mysql-virtual_mailboxes.cf'''
 +
 
 +
<!--T:71-->
 +
''ตัวอย่าง''
 +
 
 +
<!--T:72-->
 +
'''user = '''[[yourdbuser]]'''
 +
'''password = '''[[yourdbpassword]]'''
 +
'''dbname = '''[[eaimail]]'''
 +
'''query = SELECT CONCAT(SUBSTRING_INDEX(email,'@',-1),'/',SUBSTRING_INDEX(email,'@',1),'/') FROM users WHERE email='%s' '''
 +
'''hosts = 127.0.0.1'''
 +
 
 +
<!--T:73-->
 +
4. สร้างไฟล์ mysql-virtual_email2email.cf เพื่อใช้สำหรับ ส่งต่อ email ทั้งหมด ไปยัง smtp server อื่น ซึ่งใช้ในกรณีที่ host เป็น IP Address
 +
'''# vi /etc/postfix/mysql-virtual_email2email.cf'''
 +
 
 +
<!--T:74-->
 +
''ตัวอย่าง''
 +
 
 +
<!--T:75-->
 +
'''user = yourdbuser'''
 +
'''password = yourdbpassword'''
 +
'''dbname = eaimail'''
 +
'''query = SELECT email FROM users WHERE email='%s' '''
 +
'''hosts = 127.0.0.1'''
 +
 
 +
<!--T:76-->
 +
5. กำหนดสิทธิ์ให้ postfix ใช้งานไฟล์ที่สร้างขึ้น
 +
'''# chmod o= /etc/postfix/mysql-virtual_*.cf'''
 +
'''# chgrp postfix /etc/postfix/mysql-virtual_*.cf'''
 +
 
 +
<!--T:77-->
 +
6. สร้าง user และ group เพื่อใช้กับ mailbox
 +
'''# groupadd -g 5000 vmail '''
 +
'''# useradd -g vmail -u 5000 vmail -d /home/vmail -m'''
 +
 
 +
<!--T:78-->
 +
7. แก้ไขไฟล์ /etc/postfix/main.cf โดยเปิดการใช้งานหรือเพิ่มค่า configure ตามด้านล่าง *(สังเกตุหัวข้อ virtual_alias_domains ที่เพิ่มเป็น Domain ภาษาไทย )
 +
'''# vi /etc/postfix/main.cf'''
 +
 
 +
<!--T:79-->
 +
''ตัวอย่าง''
 +
 
 +
<!--T:80-->
 +
'''inet_interfaces = all
 +
'''inet_protocols = ipv4
 +
'''mydestination =  localhost, localhost.localdomain
 +
'''alias_database = hash:/etc/aliases
 +
'''sendmail_path = /usr/sbin/sendmail.postfix
 +
'''newaliases_path = /usr/bin/newaliases.postfix
 +
'''mailq_path = /usr/bin/mailq.postfix
 +
'''meta_directory = /etc/postfix
 +
'''shlib_directory = no
 +
'''myhostname = '''[[mail.eai.in.th]]'''
 +
'''mydomain = '''[[eai.in.th]]'''
 +
'''myorigin = $mydomain
 +
'''readme_directory = no
 +
'''mynetworks = 127.0.0.1
 +
'''message_size_limit = 30720000
 +
'''smtpd_sasl_auth_enable = yes
 +
'''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_cert_file = /etc/pki/tls/certs/'''[[eai.crt]]'''
 +
'''smtpd_tls_key_file = /etc/pki/tls/certs/'''[[eai.key]]'''
 +
'''smtpd_tls_security_level = may
 +
'''smtpd_tls_auth_only = yes
 +
'''smtpd_tls_loglevel = 1
 +
'''alias_maps = hash:/etc/aliases
 +
'''virtual_mailbox_base = /home/vmail
 +
'''virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql-virtual_mailboxes.cf
 +
'''virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql-virtual_domains.cf
 +
'''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'''
 +
 
 +
<!--T:81-->
 +
8. แก้ไขไฟล์ /etc/postfix/master.cf โดยเพิ่มคำสั่งลงด้านล่างสุดของไฟล์ (ระวังเรื่องช่องว่างหน้าคำสั่ง )
 +
'''# vi /etc/postfix/master.cf'''
 +
 
 +
<!--T:82-->
 +
''ตัวอย่าง''
 +
 
 +
<!--T:83-->
 +
'''dovecot  unix  -      n      n      -      -      pipe
 +
'''    flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/deliver -f ${sender} -d ${recipient}'''
 +
 
 +
<!--T:84-->
 +
9. แก้ไขไฟล์ /etc/postfix/master.cf เปิดใช้งาน หรือ เพิ่ม เพียงแค่คำสั่งด้าานล่าง นอกเหนือจากนั้น ให้ปิดการใช้งาน #
 +
 
 +
<!--T:85-->
 +
''ตัวอย่าง''
 +
 
 +
<!--T:86-->
 +
'''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'''
 +
 
 +
<!--T:87-->
 +
10.  restart postfix
 +
 
 +
<!--T:88-->
 +
''ตัวอย่าง''
 +
 
 +
<!--T:89-->
 +
'''# systemctl restart postfix'''
 +
 
 +
<!--T:90-->
 +
11. กำหนด alias email โดยแก้ใขไฟล์ aliases
 +
*เพิ่ม 2 บรรทัดตามด้านล่าง
 +
'''# vi /etc/aliases'''
 +
 
 +
<!--T:91-->
 +
'''postmaster: root
 +
'''root: postmaster@eai.in.th'''
 +
 
 +
<!--T:92-->
 +
12. สั่ง update aliases และ restart postfix
 +
'''# newaliases
 +
'''# systemctl restart postfix'''
 +
 
 +
=== 7. ตั้งค่า dovecot ของ EAI Email Server บน CentOS 7 === <!--T:93-->
 +
1. back-up ค่า config เดิมไว้ก่อน
 +
'''# mv /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf-backup'''
 +
 +
2. ตั้งค่า config dovecot โดยสร้างไฟล์ให้ และตั้งค่าตามด้านล่าง
 +
'''# vi /etc/dovecot/dovecot.conf'''
 +
 
 +
<!--T:94-->
 +
''ตัวอย่าง''
 +
 
 +
<!--T:95-->
 +
'''protocols = imap pop3 lmtp
 +
'''log_timestamp = "%Y-%m-%d %H:%M:%S "
 +
'''mail_location = maildir:/home/vmail/%d/%n/Maildir
 +
 +
'''ssl_cert = </etc/pki/tls/certs/'''[[eai.crt]]'''
 +
'''ssl_key = </etc/pki/tls/certs/'''[[eai.key]]'''
 +
'''ssl_dh_parameters_length = 2048
 +
'''ssl_cipher_list = ALL:!LOW:!SSLv2:!EXP:!aNULL
 +
'''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
 +
'''}
 +
'''auth_mechanisms = plain login
 +
'''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@'''[[eai.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
 +
'''}'''
 +
 
 +
<!--T:96-->
 +
3. สร้าง script เพื่อให้ dovecot ใช้งานกับ SQL ที่เราได้กำหนดก่อนหน้านี้
 +
'''# vi /etc/dovecot/dovecot-sql.conf.ext'''
 +
 
 +
<!--T:97-->
 +
''ตัวอย่าง''
 +
 
 +
<!--T:98-->
 +
'''driver = mysql
 +
'''connect = host=127.0.0.1 dbname='''[[eaimail]]''' user='''[[yourdbuser]]''' password='''[[yourdbpassword]]'''
 +
'''default_pass_scheme = CRYPT
 +
'''password_query = SELECT email as user, password FROM users WHERE email='%u';'''
 +
 
 +
<!--T:99-->
 +
4. กำหนดสิทธิ์อนุญาตให้ user ที่อยู่ใน dovecot group ใช้งาน
 +
'''# chgrp dovecot /etc/dovecot/dovecot-sql.conf.ext
 +
'''# chmod o= /etc/dovecot/dovecot-sql.conf.ext'''
 +
 
 +
<!--T:100-->
 +
5. restart dovecot
 +
'''# systemctl restart dovecot'''
 +
 
 +
<!--T:101-->
 +
6. ตรวจสอบ log การทำงาน log ของ mail server สามารถดูได้ที่ /var/log/maillog ซึ่งจะได้ผลตามได้ล่าง หากมี error ให้ตรวจสอบ log และกลับไปทำการแก้ไข
 +
'''# tail /var/log/maillog'''
 +
 
 +
<!--T:102-->
 +
''ตัวอย่าง''
 +
 
 +
<!--T:103-->
 +
'''Mar 18 17:10:26 localhost postfix/postfix-script[3274]: starting the Postfix mail system
 +
'''Mar 18 17:10:26 localhost postfix/master[3276]: daemon started -- version 2.10.1, configuration /etc/postfix
 +
'''<span style='color:red;'>Mar 18 17:12:28 localhost dovecot: master: Dovecot v2.2.10 starting up for imap, pop3 (core dumps disabled)</span>'''
 +
 
 +
=== 8. ทดสอบ EAI Email Server บน CentOS 7 === <!--T:104-->
 +
1. ทดสอบ pop3
 +
'''# telnet localhost pop3'''
 +
 
 +
<!--T:105-->
 +
''ตัวอย่าง''
 +
 
 +
<!--T:106-->
 +
'''Trying 127.0.0.1...
 +
'''Connected to localhost.localdomain.
 +
'''Escape character is '^]'.
 +
'''+OK Dovecot ready.'''
 +
 
 +
<!--T:107-->
 +
2. ทดสอบ smtp
 +
'''# telnet localhost 25
 +
'''# ehlo localhost'''
 +
 
 +
<!--T:108-->
 +
''ตัวอย่าง''
 +
 
 +
<!--T:109-->
 +
'''250-mail.eai.in.th
 +
'''250-PIPELINING
 +
'''250-SIZE 30720000
 +
'''250-VRFY
 +
'''250-ETRN
 +
'''250-STARTTLS
 +
'''250-AUTH PLAIN
 +
'''250-AUTH=PLAIN
 +
'''250-ENHANCEDSTATUSCODES
 +
'''250-8BITMIME
 +
'''250-DSN
 +
'''250 SMTPUTF8'''
 +
 
 +
<!--T:110-->
 +
3.ทดสอบรับ-ส่งอีเมลด้วย mailx
 +
*ตรวจสอบ log ของการส่ง Email : /var/log/maillog ซึ้งจะได้ผลตามด้านล่าง
 +
'''# yum -y install mailx
 +
'''# mailx thai@eai.in.th (ใส่ subject แล้ว enter ตามด้วยเนื้อหา และใช้ Ctrl+D เพื่อส่งออก)'''
 +
 
 +
<!--T:111-->
 +
'''# tail /var/log/maillog
 +
''ตัวอย่าง''
 +
 
 +
<!--T:112-->
 +
'''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.eai.in.th>
 +
'''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=<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'''
 +
 
 +
<!--T:113-->
 +
** หากติดปัญหาจาก log คือerror: unsupported dictionary type: mysql เนื่องจากตอนแก้ใข manual ฉบับนี้ ได้ใช้ postfix 3.2.4 ซึ่งยังมีบัคที่ต้องแก้ไขเพิ่มเติม หากติดปัญหาคือเพิ่ม  file ชื่อว่า mysql.cf ลงไปที่  /etc/postfix/dynamicmaps.cf.d/
 +
'''# cd /etc/postfix/dynamicmaps.cf.d/
 +
'''# vi mysql.cf'''
 +
 
 +
<!--T:114-->
 +
'''mysql /usr/lib/postfix/postfix-mysql.so dict_mysql_open'''
 +
 
 +
<!--T:115-->
 +
4. ตรวจสอบ log ผลการส่งของ dovecot : /home/vmail/dovecot-deliver.log ซึ่งจะได้ผลตามด้านล่าง
 +
'''# tail /home/vmail/dovecot-deliver.log
 +
 
 +
<!--T:116-->
 +
'''2017-11-13 15:19:08 lda(tanagan@eai.in.th): Info: msgid=<20171113081907.E9AB020199E5@mail.eai.in.th>: saved mail to INBOX'''
 +
 +
5. ตรวจสอบ mailbox เมื่อมีการรับส่ง-ส่ง Email จะสร้างขึ้นโดย dovecot ซึ่งจะได้ผลตามภาพ
 +
'''# cd /home/vmail/eai.in.th/thai/Maildir
 +
'''# find'''
 +
 
 +
<!--T:117-->
 +
''ตัวอย่าง''
 +
 
 +
<!--T:118-->
 +
'''.
 +
'''./dovecot-uidlist
 +
'''./cur
 +
'''./new
 +
'''./new/1285609582.P6115Q0M368794.li172-137
 +
'''./dovecot.index
 +
'''./dovecot.index.log
 +
'''./tmp'''
 +
 
 +
<!--T:119-->
 +
6. ตรวจสอบ mailbox โดยใช้ mail client คือ mutt เพื่อดู email ภายใน mailbox
 +
'''# yum -y install mutt
 +
'''# mutt -f .'''
 +
 
 +
<!--T:120-->
 +
7. ทดสอบการส่งเมลในรูปแบบ eai
 +
*ใช้เครื่องหมาย “.” เพื่อจบข้อความและส่งข้อความกด Ctrl+] เพื่อออก
 +
 
 +
<!--T:121-->
 +
'''# telnet localhost 25
 +
'''# EHLO localhost
 +
'''# MAIL FROM: ไทย@อีเอไอ.ไทย SMTPUTF8
 +
'''# RCPT TO: ธนากานต์@คน.ไทย
 +
'''# data
 +
'''# Subject: eai test
 +
'''# เทสอีเมลส์ภาษาไทย
 +
'''# .
 +
 
 +
=== 9. ติดตั้ง squirrelmail ของ EAI Email Server บน CentOS 7 === <!--T:122-->
 +
 
 +
<!--T:123-->
 +
1. ติดตั้ง squirrelmail
 +
'''# yum -y install epel-release
 +
'''# yum install squirrelmail'''
 +
 
 +
<!--T:124-->
 +
2. หาก server ยังไม่ได้ทำการติดตั้ง apache ให้ทำการติดตั้ง
 +
'''# yum -y install httpd
 +
'''# systemctl enable httpd
 +
'''# systemctl start httpd
 +
'''# systemctl status httpd'''
 +
 
 +
<!--T:125-->
 +
3. นำ squirrelmail ที่ได้เตรียมไว้ให้นำไปไว้ที่ /var/www/html/
 +
'''# cd /var/www/html/'''
 +
(copy squirrelmail ไปไว้ /var/www/html/ ซึ่งผู้จัดทำใช้ winscp ในการทำงาน)
 +
 
 +
 
 +
<!--T:126-->
 +
4. สร้าง directory เพิ่มให้กับ squirrelmail
 +
'''# cd /var/lib/squirrelmail/
 +
'''# mkdir data
 +
'''# chmod ugo+rwx data/'''
 +
 
 +
<!--T:127-->
 +
5. ทดสอบ login เข้าใช้งานผ่าน web browsers โดยใช้ IPของ Server ที่ได้รับและตามด้วย /squirrelmail  เช่น http://119.59.113.240/squirrelmail/
 +
'''# ทดสอบรับและส่งเมล'''
 +
 
 +
<!--T:128-->
 +
[[Category: EAI]]
 +
 
 +
</translate>

รุ่นแก้ไขปัจจุบันเมื่อ 12:33, 12 กรกฎาคม 2564

ภาษาอื่น ๆ:
English • ‎ไทย

EAI Email Server

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

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

  1. Centos 7.4
  2. Postfix 3.2.3
  3. Dovecot 2.2.10
  4. MariaDB 10.3.11
  5. Squirrelmail 5.5.56
  • คู่มือนี้ไม่ได้อธิบายถึงการติดตั้ง Centos 7.4
  • Postfix ต้องเป็น Version 3.x.x ขึ้นไป จะรองรับ EAI
  • ผู้จัดทำได้ทำการจด domain เป็น eai.in.th และ อีเอไอ.ไทย ไว้เสร็จเรียบร้อยแล้ว
  • ผู้จัดทำใช้ putty และ winscp เป็นเครื่องมือในการเชื่อมต่อกับ server

ขั้นตอนการติดตั้ง EAI Email Server บน CentOS 7

1. ตั้งค่า hostname ของ EAI Email Server บน CentOS 7

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

# yum -y update

2. กำหนดชื่อ hostname ในตัวอย่างนี้กำหนดชื่อว่า mail.eai.in.th ด้วยคำสั่ง

# hostnamectl set-hostname mail.eai.in.th

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

# vi /etc/hosts

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

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 mail.eai.in.th
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6 mail.eai.in.th

*ระวัง envelopment ของทาง partner ว่าติดตั้ง centos ในรูปแบบใดมาให้ (อาจจะต้องติดตั้ง service เพิ่ม หากติดตั้งมาแบบ minimal เช่นใช้คำสั่ง vi ไม่ได้)

4. ติดตั้ง wget เพื่อใช้ดาวน์โหลดซอฟแวร์ที่จำเป็นต้องใช้งานเพิ่ม

# yum -y install wget

5. ติดตั้ง telnet เพื่อทดสอบการทำงานของ email server

# yum -y install telnet

2. ตั้งค่า firewall ของ EAI Email Server บน CentOS 7

1. ตรวจสอบสถานะ firewalld service บน mail server

  • ต้องมีสถานะ active (running)
# systemctl status firewalld

ตัวอย่าง

Active: active (running) since Mon 2019-11-18 14:12:03 +07; 1h 13min ago

2. ตรวจสอบ firewall zone และ network interface ที่เปิดอยู่

# firewall-cmd --get-active-zones

ตัวอย่าง

public
interfaces: ens32

3. ตรวจสอบรายการ services ของ firewall ที่เปิดอยู่ที่ zone public

# firewall-cmd --zone=public --list-services

ตัวอย่าง

 cockpit dhcpv6-client ssh 

4. เปิด firewall services ที่ zone public ของ mail server (สามารถเปิดเท่าต้องการได้)

# firewall-cmd --permanent --zone=public --add-service=http
# firewall-cmd --permanent --zone=public --add-service=https
# firewall-cmd --permanent --zone=public --add-service=mysql
# firewall-cmd --permanent --zone=public --add-service=smtp
# firewall-cmd --permanent --zone=public --add-service=smtps
# firewall-cmd --permanent --zone=public --add-service=imap
# firewall-cmd --permanent --zone=public --add-service=imaps
# firewall-cmd --reload

5. ตรวจสอบรายการ services ของ firewall ที่เปิดอยู่ที่ zone public อีกครั้ง จะพบ services เปิดเพิ่มขึ้นมา

*ref: configure firewall services for mail server (https://spalinux.com/2015/06/centos-7-basic-configure-firewall-by-firewall-cmd)

# firewall-cmd --zone=public --list-services

ตัวอย่าง

 cockpit dhcpv6-client http https imap imaps mysql smtp smtps ssh  

6. ในตัวอย่างนี้เราแนะนำให้ปิดการทำงานของ SELinux เพื่อความสะดวกในการตั้งค่า (ไม่แนะนำให้ทำกับเครื่อง production)

  • เปลี่ยน SELinux=enforcing เป็น SELinux=disabled
# vi /etc/selinux/config

ตัวอย่าง

# 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=disabled
# 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

ตัวอย่าง

 disabled 

3. สร้าง SSL ของ EAI Email Server บน CentOS 7

  • เป็นขั้นตอนการสร้าง SSL key เตรียมไว้ใช้งานกับ postfix และ dovecot

1. สร้าง private key

# cd /etc/pki/tls/certs
# make eai.key 

ตัวอย่าง

umask 77 ; \
/usr/bin/openssl genrsa -aes128 2048 > eai.key
Generating RSA private key, 2048 bit long modulus
...
...
e is 65537 (0x10001)
Enter pass phrase: #set passphrase
Verifying - Enter pass phrase:  #confirm 

2. ถอด Passphase ออกจาก private key ที่สร้างขึ้น

# openssl rsa -in eai.key -out eai.key 

ตัวอย่าง

Enter pass phrase for eai.key:  #input passphrase
writing RSA key

3. สร้าง csr

# make eai.csr

ตัวอย่าง

umask 77 ; \
/usr/bin/openssl req -utf8 -new -key eai.key -out eai.csr
You are about to be asked to enter information that will be incorporated
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 

4. ทำการ generate key

# openssl x509 -in eai.csr -out eai.crt -req -signkey eai.key -days 3650

ตัวอย่าง

Signature ok
subject=/C=JP/ST=Hiroshima/L=Hiroshima/O=GTS/OU=Server World/CN=www.srv.world/emailAddress=xxx@eai.in.th
Getting Private key

4. ติดตั้ง mariadb, postfix, dovecot ของ EAI Email Server บน CentOS 7

1. เริ่มติดตั้ง mariadb, postfix, dovecot ด้วยคำสั่ง

# yum --enablerepo=centosplus install postfix
# yum install dovecot mariadb-server dovecot-mysql

2. อัพเดตจาก postfix version 2.x.x เป็น version 3.x.x

  • บน CentOS 7 จะติดตั้ง postfix version 2.x.x เป็นค่าเริ่มต้น เราต้อง remove ออกก่อน เนื่องจาก postfix version 2.x.x ยังไม่รองรับ EAI และอัพเกรดเป็น version 3.x.x ขึ้นไป
# yum -y remove postfix
# yum -y remove ssmtp
# yum -y remove sendmail
  • สร้างไฟล์ repo ชื่อ gf.repo เพื่อติดตั้ง postfix version 3.x.x ขึ้นไป
# vi /etc/yum.repos.d/gf.repo

ตัวอย่าง

[gf]
name=Ghettoforge packages that won't overwrite core distro packages.
mirrorlist=http://mirrorlist.ghettoforge.org/el/7/gf/$basearch/mirrorlist
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-gf.el7
failovermethod=priority
 
[gf-plus]
name=Ghettoforge packages that will overwrite core distro packages.
mirrorlist=http://mirrorlist.ghettoforge.org/el/7/plus/$basearch/mirrorlist
# Please read http://ghettoforge.org/index.php/Usage *before* enabling this repository!
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-gf.el7
failovermethod=priority
  • โหลด key ที่ใช้กับ repo วางไว้ใน path : /etc/pki/rpm-gpg/
# cd /etc/pki/rpm-gpg/
# wget http://mirror.ghettoforge.org/distributions/gf/RPM-GPG-KEY-gf.el7
  • ติดตั้ง postfix version 3.x.x
# yum -y install postfix3 postfix-mysql
  • ตรวจสอบ postfix version 3.x.x
# postconf -d | grep mail_version
 mail_version = 3.5.7

3. ตั้งค่า mariadb, postfix, dovecot ให้เริ่มทำงานทุกครั้งที่เปิดเครื่อง

# systemctl start mariadb
# systemctl enable mariadb
# systemctl start postfix
# systemctl enable postfix
# systemctl start dovecot
# systemctl enable dovecot

4. ตรวจสอบสถานะการทำงานของ mariadb, postfix, dovecot

# systemctl status mariadb
# systemctl status postfix
# systemctl status dovecot

5. ตั้งค่า mariadb ของ EAI Email Server บน CentOS 7

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

# vi /etc/my.cnf

ตัวอย่าง

[mysqld]
character-set-server = utf8
collation-server = utf8_general_ci
skip-character-set-client-handshake
bind-address=127.0.0.1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# 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 mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

2. สั่ง restart mariadb

# systemctl restart mariadb

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

# mysql_secure_installation

4. Login ข้าใช้งาน mysql โดยใช้ password ของ root user ที่ได้ตั้งไว้

# mysql -u root -p

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

# CREATE DATABASE eaimail;
# USE eaimail;

6. สร้าง user พร้อมกำหนดสิทธิ์การใช้งานให้กับ eaimail db

# GRANT SELECT, INSERT, UPDATE, DELETE ON eaimail.* TO 'yourdbuser'@'localhost' IDENTIFIED BY 'yourdbpassword';
# GRANT SELECT, INSERT, UPDATE, DELETE ON eaimail.* TO 'yourdbuser'@'localhost.localdomain' IDENTIFIED BY 'yourdbpassword';
# FLUSH PRIVILEGES;

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

# SHOW GRANTS FOR 'yourdbuser'@'localhost';

8. สร้างตารางใช้เก็บค่า Domain

# CREATE TABLE domains (domain varchar(50) NOT NULL, PRIMARY KEY (domain) );

9. สร้างตารางใช้สำหรับ forward mail

# CREATE TABLE forwardings (source varchar(80) NOT NULL, destination TEXT NOT NULL, PRIMARY KEY (source) );

10. สร้างตาราง user เพื่อใช้สร้าง mailbox และออกจาก mysql

# CREATE TABLE users (email varchar(80) NOT NULL, password varchar(20) NOT NULL, PRIMARY KEY (email) );
# quit

11. สั่ง restart mariadb

# systemctl restart mariadb

12. ตั้งค่า domain และ user ใน database eaimail

  • เข้า mariadb และเลือก database eaimail, ใส่ชื่อโดเมนภาษาอังกฤษ และภาษาไทย, ใส่ชื่อ Email-Account ภาษาอังกฤษและภาษาไทย พร้อม password เพื่อเริ่มใช้งาน
# mysql -u root -p
# USE eaimail;
# INSERT INTO domains (domain) VALUES ('eai.in.th');
# INSERT INTO domains (domain) VALUES ('อีเอไอ.ไทย');
# INSERT INTO users (email, password) VALUES ('thai@eai.in.th', ENCRYPT('thaiuserpassword'));
# INSERT INTO users (email, password) VALUES ('ไทย@อีเอไอ.ไทย', ENCRYPT('thaiuserpassword'));
  • กำหนด Email ที่เข้ามายัง Email Account ภาษาไทย ให้ forward เข้า Email-Account ภาษาอังกฤษ เนื่องจากระบบไม่สามารถสร้าง mailbox เป็น ภาษาไทยได้
# INSERT INTO forwardings (source,destination) VALUES ('ไทย@อีเอไอ.ไทย','thai@eai.in.th');
# quit

6. ตั้งค่า postfix ของ EAI Email Server บน CentOS 7

1. สร้างไฟล์ mysql-virtual_domains.cf เพื่อใช้สำหรับดึงชื่อโดเมนจาก database

# vi /etc/postfix/mysql-virtual_domains.cf

ตัวอย่าง

user = yourdbuser
password = yourdbpassword
dbname = eaimail
query = SELECT domain AS virtual FROM domains WHERE domain='%s' 
hosts = 127.0.0.1 

2. สร้างไฟล์ mysql-virtual_forwardings.cf เพื่อใช้ดึงข้อมูลการ forward email จาก database

# vi /etc/postfix/mysql-virtual_forwardings.cf

ตัวอย่าง

user = yourdbuser
password = yourdbpassword
dbname = eaimail
query = SELECT destination FROM forwardings WHERE source='%s' 
hosts = 127.0.0.1

3. สร้างไฟล์ mysql-virtual_mailboxes.cf เพื่อใช้ดึงข้อมูลสำหรับ สร้าง mailbox และใช้ login จาก database

# vi /etc/postfix/mysql-virtual_mailboxes.cf

ตัวอย่าง

user = yourdbuser 
password = yourdbpassword
dbname = eaimail
query = SELECT CONCAT(SUBSTRING_INDEX(email,'@',-1),'/',SUBSTRING_INDEX(email,'@',1),'/') FROM users WHERE email='%s' 
hosts = 127.0.0.1

4. สร้างไฟล์ mysql-virtual_email2email.cf เพื่อใช้สำหรับ ส่งต่อ email ทั้งหมด ไปยัง smtp server อื่น ซึ่งใช้ในกรณีที่ host เป็น IP Address

# vi /etc/postfix/mysql-virtual_email2email.cf

ตัวอย่าง

user = yourdbuser
password = yourdbpassword
dbname = eaimail
query = SELECT email FROM users WHERE email='%s' 
hosts = 127.0.0.1

5. กำหนดสิทธิ์ให้ postfix ใช้งานไฟล์ที่สร้างขึ้น

# chmod o= /etc/postfix/mysql-virtual_*.cf
# chgrp postfix /etc/postfix/mysql-virtual_*.cf

6. สร้าง user และ group เพื่อใช้กับ mailbox

# groupadd -g 5000 vmail 
# useradd -g vmail -u 5000 vmail -d /home/vmail -m

7. แก้ไขไฟล์ /etc/postfix/main.cf โดยเปิดการใช้งานหรือเพิ่มค่า configure ตามด้านล่าง *(สังเกตุหัวข้อ virtual_alias_domains ที่เพิ่มเป็น Domain ภาษาไทย )

# vi /etc/postfix/main.cf

ตัวอย่าง

inet_interfaces = all
inet_protocols = ipv4
mydestination =  localhost, localhost.localdomain
alias_database = hash:/etc/aliases
sendmail_path = /usr/sbin/sendmail.postfix
newaliases_path = /usr/bin/newaliases.postfix
mailq_path = /usr/bin/mailq.postfix
meta_directory = /etc/postfix
shlib_directory = no
myhostname = mail.eai.in.th
mydomain = eai.in.th
myorigin = $mydomain
readme_directory = no
mynetworks = 127.0.0.1
message_size_limit = 30720000
smtpd_sasl_auth_enable = yes
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_cert_file = /etc/pki/tls/certs/eai.crt
smtpd_tls_key_file = /etc/pki/tls/certs/eai.key
smtpd_tls_security_level = may
smtpd_tls_auth_only = yes
smtpd_tls_loglevel = 1
alias_maps = hash:/etc/aliases	
virtual_mailbox_base = /home/vmail
virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql-virtual_mailboxes.cf
virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql-virtual_domains.cf
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

8. แก้ไขไฟล์ /etc/postfix/master.cf โดยเพิ่มคำสั่งลงด้านล่างสุดของไฟล์ (ระวังเรื่องช่องว่างหน้าคำสั่ง )

# vi /etc/postfix/master.cf

ตัวอย่าง

dovecot   unix  -       n       n       -       -       pipe
    flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/deliver -f ${sender} -d ${recipient}

9. แก้ไขไฟล์ /etc/postfix/master.cf เปิดใช้งาน หรือ เพิ่ม เพียงแค่คำสั่งด้าานล่าง นอกเหนือจากนั้น ให้ปิดการใช้งาน #

ตัวอย่าง

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

10. restart postfix

ตัวอย่าง

# systemctl restart postfix

11. กำหนด alias email โดยแก้ใขไฟล์ aliases

  • เพิ่ม 2 บรรทัดตามด้านล่าง
# vi /etc/aliases
postmaster: root
root: postmaster@eai.in.th

12. สั่ง update aliases และ restart postfix

# newaliases
# systemctl restart postfix

7. ตั้งค่า dovecot ของ EAI Email Server บน CentOS 7

1. back-up ค่า config เดิมไว้ก่อน

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

2. ตั้งค่า config dovecot โดยสร้างไฟล์ให้ และตั้งค่าตามด้านล่าง

# vi /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/pki/tls/certs/eai.crt
ssl_key = </etc/pki/tls/certs/eai.key
ssl_dh_parameters_length = 2048 
ssl_cipher_list = ALL:!LOW:!SSLv2:!EXP:!aNULL
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
}
auth_mechanisms = plain login
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@eai.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
}

3. สร้าง script เพื่อให้ dovecot ใช้งานกับ SQL ที่เราได้กำหนดก่อนหน้านี้

# vi /etc/dovecot/dovecot-sql.conf.ext

ตัวอย่าง

driver = mysql
connect = host=127.0.0.1 dbname=eaimail user=yourdbuser password=yourdbpassword
default_pass_scheme = CRYPT
password_query = SELECT email as user, password FROM users WHERE email='%u';

4. กำหนดสิทธิ์อนุญาตให้ user ที่อยู่ใน dovecot group ใช้งาน

# chgrp dovecot /etc/dovecot/dovecot-sql.conf.ext
# chmod o= /etc/dovecot/dovecot-sql.conf.ext

5. restart dovecot

# systemctl restart dovecot

6. ตรวจสอบ log การทำงาน log ของ mail server สามารถดูได้ที่ /var/log/maillog ซึ่งจะได้ผลตามได้ล่าง หากมี error ให้ตรวจสอบ log และกลับไปทำการแก้ไข

# tail /var/log/maillog

ตัวอย่าง

Mar 18 17:10:26 localhost postfix/postfix-script[3274]: starting the Postfix mail system
Mar 18 17:10:26 localhost postfix/master[3276]: daemon started -- version 2.10.1, configuration /etc/postfix
Mar 18 17:12:28 localhost dovecot: master: Dovecot v2.2.10 starting up for imap, pop3 (core dumps disabled)

8. ทดสอบ EAI Email Server บน CentOS 7

1. ทดสอบ pop3

# telnet localhost pop3

ตัวอย่าง

Trying 127.0.0.1...
Connected to localhost.localdomain.
Escape character is '^]'.
+OK Dovecot ready.

2. ทดสอบ smtp

# telnet localhost 25
# ehlo localhost

ตัวอย่าง

250-mail.eai.in.th
250-PIPELINING
250-SIZE 30720000
250-VRFY
250-ETRN
250-STARTTLS
250-AUTH PLAIN
250-AUTH=PLAIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-DSN
250 SMTPUTF8

3.ทดสอบรับ-ส่งอีเมลด้วย mailx

  • ตรวจสอบ log ของการส่ง Email : /var/log/maillog ซึ้งจะได้ผลตามด้านล่าง
# yum -y install mailx
# mailx thai@eai.in.th (ใส่ subject แล้ว enter ตามด้วยเนื้อหา และใช้ Ctrl+D เพื่อส่งออก)
# tail /var/log/maillog

ตัวอย่าง

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.eai.in.th>
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=<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
    • หากติดปัญหาจาก log คือerror: unsupported dictionary type: mysql เนื่องจากตอนแก้ใข manual ฉบับนี้ ได้ใช้ postfix 3.2.4 ซึ่งยังมีบัคที่ต้องแก้ไขเพิ่มเติม หากติดปัญหาคือเพิ่ม file ชื่อว่า mysql.cf ลงไปที่ /etc/postfix/dynamicmaps.cf.d/
# cd /etc/postfix/dynamicmaps.cf.d/
# vi mysql.cf
mysql /usr/lib/postfix/postfix-mysql.so dict_mysql_open

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

# tail /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

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

# cd /home/vmail/eai.in.th/thai/Maildir
# find

ตัวอย่าง

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

6. ตรวจสอบ mailbox โดยใช้ mail client คือ mutt เพื่อดู email ภายใน mailbox

# yum -y install mutt
# mutt -f .

7. ทดสอบการส่งเมลในรูปแบบ eai

  • ใช้เครื่องหมาย “.” เพื่อจบข้อความและส่งข้อความกด Ctrl+] เพื่อออก
# telnet localhost 25
# EHLO localhost
# MAIL FROM: ไทย@อีเอไอ.ไทย SMTPUTF8			
# RCPT TO: ธนากานต์@คน.ไทย
# data
# Subject: eai test
# เทสอีเมลส์ภาษาไทย
# .

9. ติดตั้ง squirrelmail ของ EAI Email Server บน CentOS 7

1. ติดตั้ง squirrelmail

# yum -y install epel-release
# yum install squirrelmail

2. หาก server ยังไม่ได้ทำการติดตั้ง apache ให้ทำการติดตั้ง

# yum -y install httpd
# systemctl enable httpd
# systemctl start httpd
# systemctl status httpd

3. นำ squirrelmail ที่ได้เตรียมไว้ให้นำไปไว้ที่ /var/www/html/

# cd /var/www/html/

(copy squirrelmail ไปไว้ /var/www/html/ ซึ่งผู้จัดทำใช้ winscp ในการทำงาน)


4. สร้าง directory เพิ่มให้กับ squirrelmail

# cd /var/lib/squirrelmail/
# mkdir data
# chmod ugo+rwx data/

5. ทดสอบ login เข้าใช้งานผ่าน web browsers โดยใช้ IPของ Server ที่ได้รับและตามด้วย /squirrelmail เช่น http://119.59.113.240/squirrelmail/

# ทดสอบรับและส่งเมล