Setting up a mail server on Ubuntu 18.04?

February 12, 2019 4.6k views
Ubuntu 18.04 Apache

Hi person! :-)

I’ve been going through a lot of DO’s forums, OS forums, and server forums, so I’m listing everything here which may address many of the recommendations I’ve come across.

What I’m attempting: personalized family accounts - personal Wordpress websites available through /var/www/html, personal Rainloop e-mail through /var/www/rainloop, personal Filerun storage through /var/www/filerun. Oh, and some server games so we can play each other online through the server, but that’s not important here.

If you see server setup problems or file problems you would change - even if you’re unsure what’s going on with the domain - I’m happy to accept critiques on that, as well.

The current setup:

Ubuntu PhpMyAdmin on 18.04 droplet

  • phpMyAdmin installed and properly accessible via https://mydomain.tld/phpmyadmin
  • MySQL installed
  • Apache2 installed
  • Wordpress installed at /var/www/html and working properly at https://mydomain.tld
  • Rainloop installed at /var/www/rainloop but not working
  • LetsEncrypt SSL certificate

What I’m currently working on is the e-mail.
My goal is to reach the webmail at https://email.mydomain.tld

  1. http://email.mydomain.tld just redirects to https://mydomain.tld
  2. With SSL it says, “This site can’t be reached”
  3. http://webmail.mydomain.tld breaks as if the domain doesn’t exist.
  4. With SSL it says, “The site can’t be reached”

(Rainloop’s website uses “webmail.mydomain.tld” for installation. Even if I can just get it installed, I’m still fine with using https://webmail.mydomain.tld, instead of “email”.)

Current DNS record setup:

Type | Hostname | Value | Priority | TTL (seconds)

MX records, added several priorities because I can’t figure it out
MX | webmail.mydomain.tld | mail handled by DO.HOST.IP.ADDRESS | 0 | 14400
MX | webmail.mydomain.tld | mail handled by DO.HOST.IP.ADDRESS | 1 | 14400
MX | webmail.mydomain.tld | mail handled by DO.HOST.IP.ADDRESS | 5 | 14400
MX | webmail.mydomain.tld | mail handled by DO.HOST.IP.ADDRESS | 10 | 14400
MX | mydomain.tld | mail handled by DO.HOST.IP.ADDRESS | 0 | 14400
MX | mydomain.tld | mail handled by DO.HOST.IP.ADDRESS | 1 | 14400
MX | mydomain.tld | mail handled by DO.HOST.IP.ADDRESS | 5 | 14400
MX | mydomain.tld | mail handled by DO.HOST.IP.ADDRESS | 10 | 14400
MX | email.mydomain.tld | mail handled by DO.HOST.IP.ADDRESS | 0 | 14400
MX | email.mydomain.tld | mail handled by DO.HOST.IP.ADDRESS | 1 | 14400
MX | email.mydomain.tld | mail handled by DO.HOST.IP.ADDRESS | 5 | 14400
MX | email.mydomain.tld | mail handled by DO.HOST.IP.ADDRESS | 10 | 14400

(Once I can get it properly set up, I intend to start removing these one by one, to figure out which ones I need to keep.)

Wordpress installation, working properly
CNAME | wordpress.mydomain.tld | is an alias of mydomain.tld. | 43200

For future storage installation (not there yet)
CNAME | mydrive.domain.tld | is an alias of mydomain.tld. | 43200

Linking domain to host IP
A | mydomain.tld | directs to DO.HOST.IP.ADDRESS | 3600

To fix “www”
CNAME | www.mydomain.tld | is an alias of mydomain.tld. | 43200

Nameservers
NS | mydomain.tld | directs to ns1.digitalocean.com. | 1800
NS | mydomain.tld | directs to ns2.digitalocean.com. | 1800
NS | mydomain.tld | directs to ns3.digitalocean.com. | 1800

Current .htaccess and .conf Setups:

/var/www/.htaccess
Options -Indexes

/var/www/html/.htaccess
<Directory /var/www/>
AllowOverride All
</Directory>

RewriteCond %{HTTP_HOST} ^([^.]+).mail.mydomain.tld$
RewriteRule ^/(.*)$ http://mail.mydomain.tld/%1/$1 [L,R]

Options -Indexes
DirectoryIndex index.php index.html /example.php

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUESTFILENAME} !-f
RewriteCond %{REQUESTFILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

/var/www/rainloop/.htaccess
None yet, intend to edit once I’ve gotten it set up.

/etc/apache2/apache2.conf
ServerName mydomain.tld

rainloop.conf (VirtualHost *:80) and rainloop-le-ssl.conf (VirtualHost *:443)
Both list the correct domain (ServerName email.mydomain.tld) and point to the correct folder (DocumentRoot /var/www/rainloop).

rainloop.conf

<VirtualHost *:80>
ServerName email.mydomain.tld
DocumentRoot “/var/www/rainloop/”
ServerAdmin my@email.address

ErrorLog “/var/log/apache2/rainlooperrorlog” (word just got italicized in this post because of underscores)
TransferLog “/var/log/apache2/rainloopaccesslog”

<Directory />
Options +Indexes +FollowSymLinks +ExecCGI
AllowOverride All
Order deny,allow
Allow from all
Require all granted
</Directory>

rainloop-le-ssl.conf

<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName email.mydomain.tld
DocumentRoot “/var/www/rainloop/”
ServerAdmin my@email.address

ErrorLog “/var/log/apache2/rainlooperrorlog”
TransferLog “/var/log/apache2/rainloopaccesslog”

<Directory />
Options +Indexes +FollowSymLinks +ExecCGI
AllowOverride All
Order deny,allow
Allow from all
Require all granted
</Directory>

SSLCertificateFile /etc/letsencrypt/live/email.mydomain.tld/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/email.mydomain.tld/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

</VirtualHost>

/etc/apache2/wordpress.conf
<Directory /var/www/>
Options FollowSymLinks
</Directory>

If you see server setup problems or file problems you would change - even if you’re unsure what’s going on with the domain - I’m happy to accept critiques on that, as well.

5 Answers

Okay, so I finally figured it out. For anyone else who wants it:

  1. I set the FQDN of email.mydomain.tld
  2. I deleted the other MX records, but kept just this one: MX | email.mydomain.tld | mail handled by DO.HOST.IP.ADDRESS | 0 | 14400
  3. I added CNAME: CNAME | email.domain.tld | is an alias of mydomain.tld. | 43200
  4. And then for the server to accept e-mails, I followed the instructions here: https://www.tecmint.com/install-postfix-mail-server-with-webmail-in-debian/

Nevermind, forget everything here. I’ve skipped over some steps. This did work at face value, but it didn’t work with receiving mail, nor did it allow for proper subdomaining. Reset everything - just going to keep working through it.

  • Thanks for sharing your issue. Can you update us if you find a solution? I’m in a similar dilemma trying to setup wordpress, apache, mysql, and mail so wordpress’s mail() can send notifications.

    • Sure, somewhere along the way, I found that I needed to rename my droplet to “email.mydomain.tld”, in addition to setting the FQDN. That got rid of the MX errors for email, but still allowed my domain to actually serve normal webpages.

      Also, after experimenting with other one-click droplet installs, found that using Plesk is probably the easiest for setups. It pretty much rolls everything out for you when you choose an action (makes all the necessary modifications on your server).

      The only downside is that it’s proprietary and therefore restricted in its functionality, so if you want multiple users and domain emails, you need to go with a subscription (alternatively, you can just add extra email usernames manually via CLI through your server, just not via Plesk).

      I normally create a database before installing a web app. You can create the database after installing, but you usually can’t access the web app until you actually create a database for it. Wordpress installations take care of both at once.

      There are lots of other server administrator panels out there, though, so if you want to install one through the command line, then you can set that up and use it through the browser, to basically do all the same stuff.

      I don’t deal with Wordpress a whole lot, but I’d assume that whichever plugin you’re using would have its own “settings” page, where you should be able to just enter in the email address you’re sending from, and since it’s all on the same server, it should just work from there. Look for its settings page.

Also, here’s the firewall settings:

ufw status

To Action From

443/tcp ALLOW Anywhere
80/tcp ALLOW Anywhere
3306 ALLOW Anywhere
OpenSSH ALLOW Anywhere
22 ALLOW my.home.ip.address
3306 ALLOW my.home.ip.address
443 ALLOW my.home.ip.address
80 ALLOW my.home.ip.address
Anywhere ALLOW my.home.ip.address
22/tcp ALLOW Anywhere
Apache ALLOW Anywhere
Apache Full ALLOW Anywhere
Apache Secure ALLOW Anywhere
443/tcp (v6) ALLOW Anywhere (v6)
80/tcp (v6) ALLOW Anywhere (v6)
3306 (v6) ALLOW Anywhere (v6)
OpenSSH (v6) ALLOW Anywhere (v6)
22/tcp (v6) ALLOW Anywhere (v6)
Apache (v6) ALLOW Anywhere (v6)
Apache Full (v6) ALLOW Anywhere (v6)
Apache Secure (v6) ALLOW Anywhere (v6)

@stevenhost am not able to follow through,

How did you set FQDN of email.mydomain.tld?

  • On the one hand, I just want to ignore this question.
    On the other hand, I would feel guilty in doing so.
    I’ll compromise, so here’s my reply:
    Google the words “set FQDN” + [your distribution].

@stevenhost I did finally just Google it and set it properly.

Though I found out I needed Dovecot, Postfix, plus Rainloop. And I had missed the first two.

So I had Rainloop working, but could create users email accounts.

  • Don’t forget to run incoming and outgoing emails over SSL (ports 465 and 995). By default, Dovecot and Postfix routes emails unencrypted. Rainloop is just a webmail app to access those emails from your server, so the SSL certificate installed to the “https://email.” subdomain only covers that much.

Have another answer? Share your knowledge.