How to setup mail server on digitalocean

Setting up a mail server on DigitalOcean involves several steps, including creating a new droplet, installing and configuring the mail server software, and setting up DNS records. Here's a step-by-step guide to help you set up a mail server on DigitalOcean:

Step 1: Create a new droplet

  1. Log in to your DigitalOcean account and click on the "Create" button.
  2. Select "Droplets" and choose the "Ubuntu" or "Debian" distribution.
  3. Choose the size of the droplet that suits your needs.
  4. Set the hostname and choose a password.
  5. Click "Create" to create the droplet.

Step 2: Install Postfix

  1. Connect to your droplet using SSH.
  2. Update the package list: sudo apt update
  3. Install Postfix: sudo apt install postfix
  4. Configure Postfix: sudo dpkg-reconfigure postfix

Step 3: Configure Postfix

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

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

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

    Replace yourdomain.com with your actual domain name.

  3. Save and exit the file.

Step 4: Configure DNS records

  1. Create a TXT record for your domain: yourdomain.com -> TXT -> v=spf1 a mx ~all
  2. Create an MX record for your domain: yourdomain.com -> MX -> 10 mail.yourdomain.com
  3. Create an A record for your mail server: mail.yourdomain.com -> A -> yourdropletIP

Step 5: Install and configure Dovecot

  1. Install Dovecot: sudo apt install dovecot-imapd
  2. Configure Dovecot: sudo dpkg-reconfigure dovecot-imapd
  3. Edit the Dovecot configuration file: sudo nano /etc/dovecot/dovecot.conf
  4. Add the following lines to the end of the file:
    mail_location = maildir:~/Maildir
  5. Save and exit the file.

Step 6: Configure MySQL (optional)

If you want to use MySQL to store your email accounts, you'll need to install and configure it.

  1. Install MySQL: sudo apt install mysql-server
  2. Configure MySQL: sudo dpkg-reconfigure mysql-server
  3. Create a new database and user for your mail server: sudo mysql -u root -p
  4. Run the following commands to create the database and user:
    CREATE DATABASE mail;
    GRANT ALL PRIVILEGES ON mail.* TO 'mailuser'@'localhost' IDENTIFIED BY 'mailpassword';

    Replace mail with your actual database name, and mailuser and mailpassword with your actual username and password.

Step 7: Configure Postfix to use MySQL

  1. Edit the Postfix configuration file: sudo nano /etc/postfix/main.cf
  2. Add the following lines to the end of the file:
    virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cf
    virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf
  3. Create the MySQL configuration files: sudo nano /etc/postfix/mysql_virtual_alias_maps.cf and sudo nano /etc/postfix/mysql_virtual_mailbox_maps.cf
  4. Add the following lines to each file:
    user = mailuser
    password = mailpassword
    hosts = localhost
    dbname = mail
  5. Save and exit the files.

Step 8: Restart Postfix and Dovecot

  1. Restart Postfix: sudo service postfix restart
  2. Restart Dovecot: sudo service dovecot restart

That's it! Your mail server should now be set up and ready to use. You can test it by sending an email to your domain and checking if it's delivered correctly.

Note: This is a basic setup, and you may need to configure additional settings depending on your specific requirements. Additionally, you should ensure that your droplet has a static IP address and that your DNS records are propagated correctly.