TLS’ifying your server

There is a newer issue of thie ISPmail guide available if you are using Debian Jessie!

(If you are unfamiliar with the abbreviation “TLS“: it is the successor to SSL.)

The internet has become an evil place. Many pieces of hardware and software as well as technical staff sits between your computer and your mail server. Some may not be trusted. And others are even paid to spy on you. So today it is out of question to run any unencrypted services over the internet – especially when it comes to managing your server and using it for your email. Consider any password wasted that you sent through HTTP. So before you use PHPMyAdmin to send your database ‘root’ password through it let us make your Apache web server secure. Setting up TLS is not hard. And while we are at it we will create the key and certificate that we use for HTTPS webmail, Secure POP3, Secure IMAP and Secure SMTP.

You just have to decide whether to buy a certificate or to create one yourself. In comparison:

Type Advantages Disadvantages Application
Self-signed No costs.
Quickly created.
Users will get a warning message about an untrusted certificate. Private mail server for yourself and friends who don’t mind ignoring the warning message about an unverified identity.
Self-signed with own PKI No costs.
You can create further certificates that your users will consider trusted if they have your root certificate installed.
Takes 15 minutes longer for you. Requires that all users install your root certificate as trusted on their computers. This may make sense only if you can automatically distribute the certificate in a corporate environment. Private mail server for yourself and friends who don’t mind installing your root certificate once.
No-cost certificate from StartSSL No costs.
Users will likely not get a warning.
Takes 15 minutes longer. Public mail server.
Paid certificate Users will not get a warning. Costs money (50€-200€ per year).
Probably a lengthy registration process.
You support the certificate mafia.
You don’t improve the communication security in any way.
For anyone who has too much money.

Note: Whichever method you use – always create the key on your own server. Never trust a key that has been created by anyone else. It appears simpler because you can omit one step. But the other party now knows your secret key and could in theory intercept your encrypted traffic. I will describe how to do that properly in any of the following sections. Just choose your option and follow the instructions.

Option 1: Self-signed

The simplest option. Just run this command on your server and you have a valid all-purpose certificate that is valid for the next ten years:

openssl req -new -x509 -days 3650 -nodes -newkey rsa:4096 -out /etc/ssl/certs/mailserver.pem -keyout /etc/ssl/private/mailserver.pem

You will be asked for several pieces of information. Enter whatever you like. The only important field is the “Common Name” that must contain the fully-qualified host name that you want your server to be known on the internet.

Finally make sure that the secret key is only accessible by the ‘root’ user:

chmod go= /etc/ssl/private/mailserver.pem

Option 2: Self-signed with own PKI

This option is a bit better than using a simple self-signed certificate. But your users or customers need to install your root certificate manually to establish a trust relationship to your PKI in both their browsers and their email programs.

Please search the internet for “openssl” and “” for further information on how to set up your own PKI. I won’t cover there here.

Once the PKI is set up just create a certificate signing request (CSR) using:

/usr/lib/ssl/misc/ -newreq

You will be asked for several pieces of information. Enter whatever you like. The only important field is the “Common Name” that must contain the fully-qualified host name that you want your server to be known on the internet.

Then sign that request (create the certificate):

/usr/lib/ssl/misc/ -sign

You should now find three files in your current directory:

  • newreq.pem – feel free to delete it
  • newkey.pem – move it to /etc/ssl/private/mailserver.pem
  • newcert.pem – move it to /etc/ssl/certs/mailserver.pem

Finally make sure that the secret key is only accessible by the ‘root’ user:

chmod go= /etc/ssl/private/mailserver.pem

Option 3: No-cost certificate from StartSSL

This is almost always the best option. It doesn’t cost you money and will give you a basic trustworthy certificate that most browsers and email clients will accept.

Create a key file:

openssl genrsa -des3 -out /etc/ssl/private/mailserver.pem 4096

Create a certificate signing request (CSR) file from that key:

openssl req -new -key /etc/ssl/private/mailserver.pem -out /etc/ssl/certs/mailserver.csr

You will be asked for several pieces of information. Enter whatever you like. The only important field is the “Common Name” that must contain the fully-qualified host name that you want your server to be known on the internet.

Now go to StartSSL and sign up for an account. Use the “Validation Wizard” to validate your email address first. Then use the validation wizard again to validate the domain you want to use for your email server.

Then use the “Certificates Wizard” to create a “Web Server SSL/TLS certificate”. Choose your email domain, paste the contents of the CSR file you created previously and if all goes well receive your certificate file. Move that file to /etc/ssl/certs/mailserver.pem.

In addition to the Apache configuration described below please ensure to install the chaining certificate, too. A sample configuration section is available in the StartSSL documentation.

Option 4: Paid certificate

Are you sure you want to do it? Then just search the web for “SSL certificate” and throw your money at any certificate authority. Crap comes in many flavors.

Create a key file:

openssl genrsa -des3 -out /etc/ssl/private/mailserver.pem 4096

Create a certificate signing request (CSR) file from that key:

openssl req -new -key /etc/ssl/private/mailserver.pem -out /etc/ssl/certs/mailserver.csr

Now that you considered investing money I suggest you are very careful when entering the pieces of information when creating the CSR file. The common must match the the fully-qualified host name that you want your server to be known on the internet.

Use the CSR file to request a certificate from the SSL authority.

Enable HTTPS in the Apache web server

Now that you have a valid key and certificate let’s use it for the web server so that it speaks HTTPS instead of HTTP.

Go to /etc/apache2/sites-available and find the “default-ssl” file. This is a good configuration template for enabling HTTPS. Edit it and change these two lines:

SSLCertificateFile    /etc/ssl/certs/mailserver.pem
SSLCertificateKeyFile /etc/ssl/private/mailserver.pem

Enable this SSL configuration:

a2ensite default-ssl

Also enable the “mod_ssl” module:

a2enmod ssl

As suggest reload the Apache web server:

service apache2 reload

If everything works fine you will see an “[ ok ]” message. Go to https://YOURSERVER/ and see if you an “It works!” message. If not please run “apache2ctl configtest” to see if Apache detects any obvious problems. Or check out the /var/log/apache2/error.log log file for further hints.

Set up HTTP to HTTPS redirection

For those users who forgot to type “https” instead of “http” let us also set up an automatic redirection so that they will be forwarded to the secure URL. Edit the /etc/apache2/sites-available/default file and insert

Redirect permanent / https://YOUR.MAIL.SERVER/

anywhere within the VirtualHost section. Of course you have to replace YOUR.MAIL.SERVER with the fully-qualified domain name of your mail server.

34 thoughts on “TLS’ifying your server”

  1. Assuming that there aren’t conventions to the contrary, it might make life easier if the various SSL files had different extensions. I tried the SmartSSL route and came away with an ssl.crt file and two .pem files.

    I would suggest naming the initial key “mailserver.key” and add something to the StartSSL instructions about renaming the resultant ssl.crt to mailserver.crt (or mailserver.pem if convention dictates).

  2. Ophate Thurgan

    Apache2 will not start without the passphrase to the private key in /etc/ssl/private once ssl is enabled.

    Either update your instructions to skip entering a passphrase during generation or give instructions on adding the appropriate directive (SSLPassPhraseDialog) into apache2 to provide the passphrase.

    1. Following, i did:

      openssl rsa -in /etc/ssl/private/mailserver.pem -out /etc/ssl/private/mailserver.pem.nopass
      rm /etc/ssl/private/mailserver.pem
      mv /etc/ssl/private/mailserver.pem.nopass /etc/ssl/private/mailserver.pem
      chmod go= /etc/ssl/private/mailserver.pem

      to get rid of the password at all.

      1. of course this can be achieved by leaving out -des3: (reading man is always a good idea 😉
        openssl genrsa -out /etc/ssl/private/mailserver.pem 4096

  3. Hi!
    To verify a domain StartSSL will send an email to an account on the domain (they use postmaster@, webmaster@, …). This menas that you need to have a way to receive or forward that email. In this step will most likely not have a running mail system yet. So if you plan to use StartSSL the “TLS’ifying” can only be done AFTER the configuration of the system.

    Take care,

    1. Lawrence Jones

      The domain verification step on StartSSL offers a choice of email addresses that it finds in the “whois ” record. Even with a private registration pseudoaddress on godaddy (“”), the verification email from was forwarded to my private address that is on file with godaddy, and I was able to complete verification, before setting up the mail server on the domain. The user interface on StartSSL is confusing; you can verify and create an SSL certificate before your mail server is setup.

  4. Thanks for the great tutorial!

    Is there a reason why the community-driven CAcert authority is not considered as source for certificates? CAcert offers certificates for free. Obvious their root certificates are not installed widely yet, it is still an interesting option between an own CA and commercial CAs.

    1. Christoph Haas

      Thanks for the hint. No reason really. Just last time I checked (admittedly a while ago) CAcert was not included in common mail or web clients so that I did not consider it. Do you know if Firefox and Thunderbird have it installed by default?

      1. The Mozilla audit is on the way according to

        Have CAcert in Firefox would be convenient, but is not critical from my perspective:
        In my home city there is meanwhile a well CAcert community established. I really enjoy to not need to accept certificates / install roots any more when using services from individuals around.

        1. Not so sure about that.

          The audit process stalled in 2007 at CACert’s request and I don’t see much progress thus far.

          Six years plus to pass audit for a root certificate is a long time and makes me wonder if it’s stalled permanently. And no, it’s not installed by default in Firefox. It is however included in most Debian based distros.

          I wish I could be more positive, as I too would like to see an open source certificate widely used.

  5. I’m not able to make work https://MY.MAIL.SERVER/

    The error log report:

    [error] (9)Bad file descriptor: apr_socket_accept: (client socket)
    [warn] RSA server certificate is a CA certificate (BasicConstraints: CA == TRUE !?)
    [notice] Apache/2.2.22 (Debian) PHP/5.4.4-14+deb7u7 mod_ssl/2.2.22 OpenSSL/1.0.1e configured — resuming normal operations

    When I try to open the link from my browser nothing happen.

    Please can you give me a hint?


      1. Christoph Haas

        That is very unlikely. Are you sure that the FQDN points to the expected IP address? What means "does not work"? What happens instead?

          1. If you have iptables running, you’ll have to give access to port 443 over tcp.

  6. This line: chmod go= /etc/ssl/private/mailserver.pem
    did not work for me.

    Instead I went with:
    chmod 600 /etc/ssl/private/mailserver.pem

    Is there an error with the first line ?

  7. I’m curious if using a self-signed (or self-signed with your own, self-signed root CA/PKI) creates any problems when OTHER mail servers connect via TLS to postfix to deliver mail (i.e. inbound mail)? I’m wondering if those other, public mail servers will abort when they cannot verify the certificate?

    1. Christoph Haas

      Self-signed certificates on mail servers should not cause any problems. Remote mail servers cannot verify the certificate but I don't know any cases where they even try.

    1. Alex Jordan

      For moving between URL schemes this makes sense, but not for moving from HTTP to HTTPS (with the rest of the URL being the same). Why? Because you’re extremely unlikely to ever want to turn off your HTTPS support, and with browsers caching the redirect, you prevent someone from MITM’ing the HTTP connection and serving the site over HTTP, thus preventing the browser from ever seeing that there is a TLS version. The cache will cause the browser to not even send an HTTP request, which is good. (This also has the advantage that you don’t leak visited URLs to a passive attacker, only the IP you’re talking to.)

  8. It would be helpful if we could have a little more meat on the bones about installing the StartSSL certificate(s).

    Just like Lee Bosch, I too have ended up with three documents (2 pems and 1 crt) but unlike him, haven’t got any idea what I’m supposed to do with them in spite of reading this page and the comments many times over.

    A ca.pem already exists in /etc/ssl/certs, so what am I supposed to do, just overwrite the existing? And if I do that, what are the implications? Or, can I put these three documents in a completely separate folder and simply point apache2 to them?

    Can I rename them now they have been produced, or will that screw up the certification? Or has that renaming got to be done prior to production?

    I now read in the comments, installing these documents could cause problems in getting apache2 to start. So overcoming that potential problem would also be good advice.

    To say I’m confused, is putting it mildly. I’m not knocking what you’ve done Christoph (which is amazing), but sometimes, things that seem obvious to the experienced, are a complete mystery to the novice, however hard they try.

  9. Hello, thanks for great tutorial.
    But, please, I have few questions. after logging to phpmyadmin through https, it reports: “no right FQDN”. Yes, thats clear why, because I installed it as localhost ( /etc/hosts. And my CRT is not issued for, but for
    So, should I open my server to inet and than I can log into php myadmin? Isn’t to dangerous before securing whole local network, ports etc?
    or here is simplier path to the end?:)

    /etc/hosts: localdomain.localhost localhost

    thanks all

  10. Why does StartSSL require a subdomain in order to make the cert? I don’t have a subdomain nor do I want or need one. This is just for a personal site. How do I set up this cert without having to use a subdomain it doesn’t make any sense. I made one up call and now my apache is telling me that RSA server certificate CommonName (CN) `’ does NOT match server$ so now I can’t even get apache to start.

    1. Christoph Haas

      If you use "downloads" as a subdomain to the "" domain then you should have no problems.

      The certificate I'm using for was created for "" (with "www" being the subdomain). Fortunately StartSSL automatically generated the domain itself "" as a so called "alternate name". So the certificate is valid for both "" as well as "".

      1. Oh okay I didn’t know I could use www. I thought I was going to be forced into making a subdomain on my server and using that such as mail, downloads, or something like that. I understand thank you I’ll get that cert done asap.

  11. Apollo_DoctaCoonkies

    Due to fault in my server i’ve lost everything (including the .pem file in /etc/ssl/private/mailserver.pem).
    I can rescue one of them from the web portal of startssl but how can I recover the one in /etc/ssl/private/mailserver.pem ?

      1. Apollo_DoctaCoonkies

        Yes, do you know how can i perform the operation with startssl ? I guess I have to re-create also his certificate

  12. Alex Jordan

    Please change this:

    > openssl req -new -x509 -days 3650 -nodes -newkey rsa:4096 -out /etc/ssl/certs/mailserver.pem -keyout /etc/ssl/private/mailserver.pem

    to this:

    > openssl req -new -sha256 -x509 -days 3650 -nodes -newkey rsa:4096 -out /etc/ssl/certs/mailserver.pem -keyout /etc/ssl/private/mailserver.pem

    Change this:

    > openssl req -new -key /etc/ssl/private/mailserver.pem -out /etc/ssl/certs/mailserver.csr

    to this:

    > openssl req -new -sha256 -key /etc/ssl/private/mailserver.pem -out /etc/ssl/certs/mailserver.csr

    And finally this:

    > openssl req -new -key /etc/ssl/private/mailserver.pem -out /etc/ssl/certs/mailserver.csr

    to this:

    > openssl req -new -sha256 -key /etc/ssl/private/mailserver.pem -out /etc/ssl/certs/mailserver.csr

    The CA example should also be changed too, probably, but I’m not gonna dig around in the OpenSSL manual to confirm how, because ew.

    This change is because SHA1 is considered weakened, and will be disallowed in certificate signatures by browsers soon-ish. See this blog post[1] for more information.


  13. Hello Christoph,

    I’m in the process of setting up my own mail server on my VPS along with my website. I’m running into all kinds of problems due to configuration issues and certificates, so I ended up here. The tutorial is excellent, but I haven’t found the answer to my question yet. I hope you can answer it or tell me where I’m wrong in my assumptions.

    This mailserver is a very simple one. I don’t need any local mailboxes, just a few forwarding addresses (like to and to so no elaborate Dovecot installation.

    I’d like to set things up so that I have different ‘hostnames’ for server, mailserver and webserver. So is it possible to have a webserver hosting a website at, and a mailserver that sends mail from and both run on a server called

    I know I need a certificate for and add it to the Apache configuration. But do I need a certificate for my mailserver as well and if so what do I need to make it appear as rather than

    Or maybe my setup is too complicated. If so, please let me know how to simplify it.

    Once again, thanks for the excellent tutorial.

  14. I’ve just written a comment, but it doesn’t show up on the site. Maybe I’ve done something wrong or you have a moderation process in place. Anyway, if this one turns out to be a duplicate, just delete it.

    First of all thanks for the awesome tutorial. I’m in the process of setting up my own web server + mail server configuration and I’m running into all kinds of problems where my outbound mail is refused.

    My mail server requirements are very basic: just forwarding emails, e.g. to, to etc. No local mailboxes, so no Dovecot.

    What I’d like to have is:

    – server hostname:
    – mail server:
    – web server:

    is this possible? If so, what should be the configuration for the hostname in the postfix settings? Do I need a certificate for this simple setup? If so how should I set it up?

    Or is this all too complicated and do I need to simplify it? If so, how?

    Once again, thank you for the excellent tutorial.

  15. I already had a cerificate from StartSSL which I did’t renew. Is it possible build a new certificate with a different domain from StartSSL for free or do I have to pay?


    1. hi ths startssl does not work they have major problems themself and normal browsers can not login in theire site
      is it possible to make a example/tutorial using Ca-cert ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top