6

Configure Mail Server with PostfixAdmin on CentOS 7|8

 2 years ago
source link: https://computingforgeeks.com/configure-mail-server-with-postfixadmin-on-centos/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Configure Mail Server with PostfixAdmin on CentOS 7|8

PostfixAdmin is a web-based interface based on PHP that allows users to manage their electronic mails. It is a popular mail server nowadays because it makes it easy to manage your postfix service, domains, mail aliases, adding and removing mail users, disk quotas e.t.c through a web interface.

PostfixAdmin is a free and open-source software licensed under the GNU General Public License as published by the Free Software Foundation.

Features of PostfixAdmin.

PostfixAdmin is highly preferred due to the following amazing features:

  • It supports fetchmail integration.
  • It has the Squirrelmail plugin
  • It allows users to manage their own mailbox (change alias, password and vacation message).
  • It makes it easy to manage mailboxes, virtual domains and aliases
  • Allows one to receive vacation/out-of-office messages through the web interface.
  • It has a commandline client postfixadmin-cli for those who don’t want to click around in a web interface.
  • It displays used quota

In this guide, we will systematically go through how to configure a Mail Server with PostfixAdmin on CentOS 7|CentOS 8.

Getting Started.

For this guide, you will require the following:

  1. CentOS 7/8 installed on your machine.
  2. a user with sudo privileges

Then ensure that your system is up-to-date and the required packages installed

sudo yum -y update
sudo yum install vim wget

Step 1 – Install LEMP Stack on CentOS 7|CentOS 8

A LEMP server stands for Linux, Nginx, MariaDB, and PHP, which can all be installed on CentOS 7| 8 using the below command.

First, add the EPEL repository to your system.

sudo yum install epel-release

Install Nginx PHP and MariaDB on CentOS 7| 8.

sudo yum install nginx mariadb-server mariadb php php-mysqlnd php-fpm php-opcache php-cli php-gd php-curl

Since PHP runs by default using the user apache, we will change to Nginx as below.

sudo vim /etc/php-fpm.d/www.conf

Edit the file as below.

user = nginx
group = nginx
listen = /run/php-fpm/www.sock
listen.owner = nginx
listen.group = nginx

Now assign the correct permissions to /var/lib/php directory as below.

sudo chown -R nginx:nginx /var/lib/php

Restart and enable PHP FPM to run on boot.

sudo systemctl enable php-fpm
sudo systemctl start php-fpm

Start and enable the Nginx web server.

sudo systemctl enable nginx
sudo systemctl start nginx

Step 2 – Configure MariaDB for PostfixAdmin on CentOS 8|7

After the installation of MariaDB, it is still not secure. We are therefore required to harden it by setting the root password as below.

First, start and enable MariaDB.

sudo systemctl enable --now mariadb.service

Then proceed to harden it.

sudo mysql_secure_installation

Proceed as below.

Enter current password for root (enter for none): Press Enter
....
Set root password? [Y/n] y
New password: Enter Password
Re-enter new password: Re-Enter Password
......
Remove anonymous users? [Y/n] y
...
Disallow root login remotely? [Y/n] y
...
Remove test database and access to it? [Y/n] y
....
Reload privilege tables now? [Y/n] y
.....
Thanks for using MariaDB!

Login to the MariaDB shell and create a Postfix user and database.

mysql -u root -p

Enter the set password and proceed as below.

CREATE DATABASE postfixadmin;
GRANT ALL PRIVILEGES ON postfixadmin.* TO 'postfixadmin'@'%' IDENTIFIED BY 'StrongDBPassw0rd';
FLUSH PRIVILEGES;
exit

Step 3 – Install PostfixAdmin on CentOS 8| CentOS 7

We will download the latest version of PostfixAdmin from the official PostfixAdmin download page. You can also use Wget to pull the archive as below.

wget https://sourceforge.net/projects/postfixadmin/files/postfixadmin-3.3.8/PostfixAdmin%203.3.8.tar.gz -O PostfixAdmin-tar.gz

Extract the downloaded file.

tar -xvf PostfixAdmin-tar.gz

Now move the extracted file to the /var/www/html/ directory.

sudo mv postfixadmin-postfixadmin-* /var/www/html/postfixadmin

Create a directory for PostfixAdmin installation.

sudo mkdir /var/www/html/postfixadmin/templates_c

Change the ownership of the directory to Nginx as below.

sudo chown -R nginx:nginx /var/www/html/postfixadmin/

Now create the mail configuration file and make the below adjustments.

sudo vim /var/www/html/postfixadmin/config.local.php

Edit the file as below.

<?php
$CONF['configured'] = true;
 
$CONF['database_type'] = 'mysqli';
$CONF['database_host'] = 'localhost';
$CONF['database_user'] = 'postfixadmin';
$CONF['database_password'] = 'StrongDBPassw0rd';
$CONF['database_name'] = 'postfixadmin';

$CONF['default_aliases'] = array (
 'abuse' => '[email protected]',
 'hostmaster' => '[email protected]',
 'postmaster' => '[email protected]',
 'webmaster' => '[email protected]'
);
 
$CONF['fetchmail'] = 'NO';
$CONF['show_footer_text'] = 'NO';
 
$CONF['quota'] = 'YES';
$CONF['domain_quota'] = 'YES';
$CONF['quota_multiplier'] = '1024000';
$CONF['used_quotas'] = 'YES';
$CONF['new_quota_table'] = 'YES';
 
$CONF['aliases'] = '0';
$CONF['mailboxes'] = '0';
$CONF['maxquota'] = '0';
$CONF['domain_quota_default'] = '0';
?>

Create the schema for the PostfixAdmin database.

sudo -u nginx php /var/www/html/postfixadmin/public/upgrade.php

Sample Output:

<p>updating to version 1 (MySQL)...
   done
<p>updating to version 2 (MySQL)...
   done
<p>updating to version 3 (MySQL)...
   done
<p>updating to version 4 (MySQL)...
   done
<p>updating to version 5 (MySQL)...
   done
<p>updating to version 79 (MySQL)...
   done
<p>updating to version 81 (MySQL)...
...
<p>updating to version 1839 (all databases)...
   done
<p>updating to version 1840 (MySQL and PgSQL)...
   done
<p>updating to version 1842 (all databases)...
   done
<p>updating to version 1843 (all databases)...
   done

Create a super admin account for PostfixAdmin using the command.

sudo bash /var/www/html/postfixadmin/scripts/postfixadmin-cli admin add

Sample Output:

Welcome to Postfixadmin-CLI v0.3
---------------------------------------------------------------

Admin:  
> [email protected]

Password:  
> Passw0rd12

Password (again):  
> Passw0rd12

Super admin:
(Super admins have access to all domains, can manage domains and admin accounts.) (y/n) 
> y

Domain:  
> example.com

Active: (y/n) 
> y

The admin [email protected] has been added!

---------------------------------------------------------------

Step 4 – Configure Nginx for PostfixAdmin on CentOS 8|7

Next, we will create a virtual host file for PostfixAdmin.

sudo vim /etc/nginx/conf.d/domain.com.conf

In the file, add the below lines, replacing appropriately, if you do not have an FQDN, replace mail.example.com with your IP_address

server {
        listen 80;
        root /var/www/html/postfixadmin/public;
        index index.html index.htm index.php;
        server_name mail.example.com;

        location ~ \.php$ {
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          fastcgi_pass unix:/var/run/php-fpm/www.sock;
          fastcgi_index index.php;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_intercept_errors off;
          fastcgi_buffer_size 16k;
          fastcgi_buffers 4 16k;
          fastcgi_connect_timeout 600;
          fastcgi_send_timeout 600;
          fastcgi_read_timeout 600;
        }


   location / {
       try_files $uri $uri/ =404;
   }

}

Restart Nginx.

sudo systemctl restart nginx

Check the status of the service.

$ systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/nginx.service.d
           └─php-fpm.conf
   Active: active (running) since Thu 2021-10-21 07:33:09 EDT; 8s ago
  Process: 26634 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 26631 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 26629 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 26636 (nginx)
    Tasks: 2 (limit: 14608)
   Memory: 4.0M
   CGroup: /system.slice/nginx.service
           ├─26636 nginx: master process /usr/sbin/nginx
           └─26637 nginx: worker process

Oct 21 07:33:09 mail.example.com systemd[1]: Stopped The nginx HTTP and reverse proxy server.
Oct 21 07:33:09 mail.example.com systemd[1]: Starting The nginx HTTP and reverse proxy server...
Oct 21 07:33:09 mail.example.com nginx[26631]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Oct 21 07:33:09 mail.example.com nginx[26631]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Oct 21 07:33:09 mail.example.com systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Oct 21 07:33:09 mail.example.com systemd[1]: Started The nginx HTTP and reverse proxy server.

Allow HTTP service through the firewall.

sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --reload

Set SELinux in Permissive mode.

sudo setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config

Step 5 – Access PostfixAdmin Web Interface

Now that everything has been configured accordingly, access the PostfixAdmin web interface on your preferred browser using the URL http//:hostname or http://IP_Address. You will be granted this login window.

Login using the credentials for the super admin account as below.

On successful login, you should see this PostfixAdmin dashboard.

From this dashboard, there is a couple of activities you can perform such as Adding alias, adding mailboxes, sending mails e.t.c.

The first activity we will perform is adding a domain. This is done by clicking on Domain List then New Domain as below.

Enter your valid domain name then click add domain when done. On the domain list, your new domain should appear as shown.

Under the Virtual list, you can add alias, mailboxes and alias domains. Add a mailbox as below.

There is an option of sending emails using PostfixAdmin.

Lastly, check logs in PostfixAdmin by clicking on view log on the far right and view activities as below.

Step 6 – Install Postfix on CentOS 8|7

With the mail server set up, we might be interested in installing Postfix which is a free and open-source mail transfer agent (MTA) that routes and delivers electronic mail from one server to another over the internet.

Postfix can be used to handle the sending and delivery of emails from our mail server. Since it is available in the default CentOS repositories, install it with the command.

sudo yum install postfix

Edit the postfix configuration file.

sudo vi /etc/postfix/main.cf

In the file, add your hostname and domain as below.

##Add your hostname
myhostname = smtp.example.com
....
##Uncomment the lines below
mydomain = example.com
....myorigin = $mydomain

##Edit the lines below(to all)
inet_interfaces = all
......
inet_protocols = all
.......
#Comment the line as below
# mydestination = $myhostname, localhost.$mydomain, localhost,

##Add your IP range.
mynetworks = 192.168.100.0/24, 127.0.0.0/8
##Uncomment the line below.
home_mailbox = Maildir/

In the file, replace example.com with your domain name, 192.168.100.0/24 w2ith your IP range.

Restart and enable Postfix.

sudo systemctl restart postfix
sudo systemctl enable postfix

Allow the required Postfix ports through the firewall.

sudo firewall-cmd --permanent --add-port=25/tcp
sudo  firewall-cmd --permanent --add-port=587/tcp
sudo  firewall-cmd --reload

Now you can send and receive emails from your setup mail server using Postfix. Test Postfix using the command.

mail -s “Local Outbound SMTP Test” test@example.com < /dev/null

That is it for now! I hope you enjoyed this guide on how to configure Mail Server with PostfixAdmin on CentOS 7|CentOS 8. In case you find any trouble while configuring PostfixAdmin, let us know.

Interested in more?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK