How to install mail server in centos 7

Installing a mail server on CentOS 7 involves several steps, including installing the necessary packages, configuring the mail server, and setting up the mail clients. Here's a step-by-step guide to help you install a mail server on CentOS 7:

Step 1: Install the necessary packages

You'll need to install the following packages:

  1. Postfix: a popular open-source mail transfer agent (MTA)
  2. Dovecot: a popular open-source IMAP and POP3 server
  3. MySQL or MariaDB: a database management system to store email accounts and settings

Run the following command to install the packages:

sudo yum install postfix dovecot mariadb-server

Step 2: Configure Postfix

Postfix is the MTA that will handle email delivery. You'll need to configure it to use the MySQL database and set up the mail server's domain and hostname.

  1. Edit the Postfix configuration file: sudo nano /etc/postfix/main.cf

  2. Add the following lines to the end of the file:

    mydestination = example.com
    myhostname = mail.example.com
    mynetworks = 127.0.0.0/8 [::1]/128

    Replace example.com with your domain name and mail.example.com with your mail server's hostname.

  3. Save and close the file.

Step 3: Configure Dovecot

Dovecot is the IMAP and POP3 server that will handle email retrieval. You'll need to configure it to use the MySQL database and set up the mail server's domain and hostname.

  1. Edit the Dovecot configuration file: sudo nano /etc/dovecot/dovecot.conf
  2. Add the following lines to the end of the file:
    mail_location = maildir:~/Maildir
    namespace inbox {
    inbox = yes
    mailbox = Inbox
    mailbox = Drafts
    mailbox = Sent
    mailbox = Trash
    }
  3. Save and close the file.

Step 4: Configure MySQL or MariaDB

You'll need to create a database and user for the mail server.

  1. Start the MySQL or MariaDB service: sudo systemctl start mariadb
  2. Secure the MySQL or MariaDB installation: sudo mysql_secure_installation
  3. Create a new database and user: sudo mysql -u root -p
    CREATE DATABASE mail;
    CREATE USER 'mailuser'@'localhost' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON mail.* TO 'mailuser'@'localhost';

    Replace mail with the name of your database, mailuser with the username, and password with the password.

Step 5: Configure Postfix to use MySQL

You'll need to configure Postfix to use the MySQL database to store email accounts and settings.

  1. Edit the Postfix configuration file: sudo nano /etc/postfix/mysql.cf

  2. Add the following lines to the end of the file:

    user = mailuser
    password = password
    hosts = localhost
    dbname = mail

    Replace mailuser with the username, password with the password, localhost with the hostname of your MySQL or MariaDB server, and mail with the name of your database.

  3. Save and close the file.

Step 6: Restart the services

Restart the Postfix and Dovecot services to apply the changes:

sudo systemctl restart postfix
sudo systemctl restart dovecot

Step 7: Test the mail server

You can test the mail server by sending an email to a test account using the telnet command:

telnet mail.example.com 25

Replace mail.example.com with your mail server's hostname.

Step 8: Configure mail clients

You'll need to configure your mail clients (e.g., Thunderbird, Outlook) to use the mail server. You can do this by setting the following settings:

That's it! You should now have a functional mail server on CentOS 7.