Creating an IMAP & webmail server

From Newroco Tech Docs
Revision as of 13:15, 16 May 2017 by Emilian.mitocariu (talk | contribs) (Created page with "First follow instructions from here: [https://docswiki.newro.co/index.php/Creating_a_simple_mail_server Creating a simple mail server] ==Dovecot== Install the following packa...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

First follow instructions from here: Creating a simple mail server

Dovecot

Install the following packages

apt-get install dovecot-ldap dovecot-lmtpd

Create self-signed certificates for dovecot

openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout /etc/ssl/private/imap.key -out /etc/ssl/certs/imap.crt

Add certificate for dovecot in file /etc/dovecot/conf.d/10-ssl.conf

ssl = yes
ssl_cert = </etc/ssl/certs/imap.crt
ssl_key = </etc/ssl/private/imap.key

Create a samba/ldap user for dovecot to use, more info can be found here Samba. Copy the certificate from samba server found in /var/lib/samba/private/tls/cert.pem to the mail server in /opt/bin/samba-cert.pem

Modify file /etc/ldap/ldap.conf like this

#TLS_CACERT     /etc/ssl/certs/ca-certificates.crt
TLS_CACERT      /opt/samba/samba-cert.pem

Add a line in /etc/hosts with the samba domain

<samba-server-ip> hostname.domain.local

Add/modify following lines in /etc/dovecot/dovecot-ldap.conf.ext

hosts = hostname.domain.local
dn = cn=<samba-user>,cn=Users,dc=DOMAIN,dc=LOCAL
dnpass = <samba-user-password>
tls = yes
tls_ca_cert_file = /opt/samba/samba-cert.pem
auth_bind = yes
ldap_version = 3
base = dc=DOMAIN,dc=LOCAL
scope = subtree
user_attrs = dNumber=uid,gidNumber=gid
user_filter = (&(&(objectClass=Person)(sAMAccountName=%n)))
pass_attrs = sAMAccountName=user,userPassword=password
pass_filter = (&(&(objectClass=Person)(sAMAccountName=%n)))

Uncomment this line from /etc/dovecot/conf.d/10-auth.conf

!include auth-ldap.conf.ext

File /etc/dovecot/conf.d/auth-ldap.conf.ext should look like this

passdb {
  driver = ldap
  args = /etc/dovecot/dovecot-ldap.conf.ext
}
userdb {
  driver = ldap
  args = /etc/dovecot/dovecot-ldap.conf.ext
  default_fields = home=/var/mail/vmail/%n
}

You should have this in /etc/dovecot/conf.d/10-master.conf so postfix can send the emails to dovecot

service lmtp {
  unix_listener /var/spool/postfix/private/dovecot-lmtp {
    group = postfix
    mode = 0600
    user = postfix
  }
}

And this in /etc/dovecot/dovecot.conf

protocols = imap lmtp

And finally restart dovecot

service dovecot restart

Postfix

To make postfix send the emails to dovecot add these lines to /etc/postfix/main.cf

local_transport = lmtp:unix:private/dovecot-lmtp
local_recipient_maps =

And if using a relay add this to the same file

relayhost = <relay-ip>

Restart postfix

service postfix restart

Roundcube

Roundcube is a webmail app.

Prerequisites

Install a lamp server

apt-get install lamp-server^

Install php extensions

apt-get install php-xml php7.0-mbstring

Set your timezone in /etc/php/7.0/apache2/php.ini

date.timezone = "UTC"

Connect to mysql

mysql -u root -p

And create a database and a user for roundcube

create database roundcubedb;
create user 'roundcubeuser' identified by 'password';
grant all privileges on roundcubedb.* to 'roundcubeuser';
flush privileges;
exit

Download roundcube (latest version can be found here: roundcube)

wget https://github.com/roundcube/roundcubemail/releases/download/1.2.5/roundcubemail-1.2.5-complete.tar.gz

Extract and move to /var/www

tar -xzf roundcubemail-1.2.5-complete.tar.gz
mv roundcubemail-1.2.5 /var/www/roundcube

Change ownership

chown -R www-data:www-data /var/www/roundcube

Import roundcube database into mysql

mysql -u root -p roundcubedb < /var/www/webmail/SQL/mysql.initial.sql

Add the domain used for dovecot certificate(and the one you want to use for your mails) in /etc/hosts on the same line with 127.0.1.1

127.0.1.1 example.com etc.

Add/modify following lines in /var/www/roundcube/config/config.inc.php

$config['db_dsnw'] = 'mysql://roundcubeuser:password@localhost/roundcubedb';
$config['default_host'] = 'imaps://example.com';
$config['default_port'] = 993;
$config['imap_conn_options'] = array(
  'ssl'         => array(
     'verify_peer'  => true,
     'verify_depth' => 3,
     'cafile'       => '/etc/ssl/certs/imap.crt',
   ),
);
$config['smtp_server'] = 'localhost';
$config['smtp_port'] = 25;

Edit /etc/apache2/sites-available/000-default.conf to point apache to the right directory

DocumentRoot /var/www/roundcube

Finally restart apache

service apache2 restart