GithubHelp home page GithubHelp logo

rioastamal / lightsail-miab-installer Goto Github PK

View Code? Open in Web Editor NEW
14.0 3.0 3.0 128 KB

a user-friendly command-line tool to streamline the setup of Mail-in-a-Box on Amazon Lightsail

License: MIT License

Shell 100.00%
amazon-lightsail amazon-ses aws dovecot email lightsail mail-server postfix mail-in-a-box

lightsail-miab-installer's Introduction

About lightsail-miab-installer

lightsail-miab-installer is a user-friendly command-line tool designed to streamline the setup of Mail-in-a-Box (a comprehensive mail server package) on Amazon Lightsail. This installer is configured to integrate with Amazon S3 for backups and Amazon SES for email sending (relay), it offers a quick and efficient way to host your own email solution.

To begin installation, run the following command.

sh lightsail-miab-installer.sh \
  --installation-id demo \
  --az ap-southeast-1a  \
  --email [email protected] \
  --hostname box.example.com

Your mail server should up and running in few minutes, and you can access it using a web interface. By default it will be installed on $5 USD/mo Amazon Lightsail instance.

All the data in your Amazon S3 bucket will be preserved, allowing you to restore it on another machine if needed. Additionally, you have the option to delete the stack using the CloudFormation web console or AWS CLI.

Navigate:

Requirements

Prerequisites for running this script:

  • An active AWS account with sufficient permissions.
  • AWS CLI v2.
  • SSH client
  • Basic shell utilities such as awk aws base64 cat cut date openssl sed ssh tee tr wc
  • jq

lightsail-miab-installer has been tested using following shells:

  • bash v4.2 (Linux)
  • zsh v5.8.1 (macOS, Linux)

Installation

Download the archive or clone the repository.

curl -o 'lightsail-miab-installer.zip' -s -L https://github.com/rioastamal/lightsail-miab-installer/archive/refs/heads/main.zip
unzip lightsail-miab-installer.zip
cd lightsail-miab-installer-main/

Usage and Examples

Running lightsail-miab-installer with --help flag will gives you list of options and examples.

sh lightsail-miab-installer.sh --help
Usage: lightsail-miab-installer.sh [OPTIONS]

Where OPTIONS:
  --az AZ                 Instance availability zone specified by AZ. Default to
                          'us-east-1a'.
  --destroy               Destroy installation specified by --installation-id.
  --disable-s3-backup     Do not configure Mail-in-a-Box to backup mailserver
                          data to Amazon S3.
  --disable-smtp-relay    Do not configure Postfix to use Amazon SES as SMTP 
                          relay.
  --dry-run               Dry run mode, print CloudFormation template and exit.
  --email EMAIL           Mail-in-a-Box administrator email specified by EMAIL.
                          An example '[email protected]'.
  --help                  Print this help and exit.
  --hostname HOSTNAME     Mail-in-a-Box primary hostname specified by HOSTNAME.
                          An example 'box.example.com'.
  --installation-id ID    Installation identifier by ID, e.g 'demo'.
  --instance-type TYPE    Amazon Lightsail plan specified by TYPE. Valid value:
                          '5_usd', '10_usd', '20_usd', '40_usd', '80_usd', or 
                          '160_usd'. Default is '5_usd'.
  --password PASSWD       Mail-in-a-Box administrator password specified by 
                          PASSWD.
  --restore               Restore installation data from backup which stored on
                          S3 bucket. See --restore-help for more info.
  --restore-help          Print help information how to restore from backup.
  --version               Print script version.

--------------------------- lightsail-miab-installer ---------------------------

lightsail-miab-installer is a powerful command line tool powered by 
Mail-in-a-Box, designed to simplify the setup of a complete mail server on 
Amazon Lightsail.

lightsail-miab-installer is free software licensed under MIT. Visit the project 
homepage at http://github.com/rioastamal/lightsail-miab-installer.

Command below will install Mail-in-a-Box on $5/mo Amazon Lightsail instance (1 RAM), Availability Zone ap-southeast-1c - Asia Pasific (Singapore), box.example.com as hostname, [email protected] as Administrator's email and lightsaildemo123 as Administrator's password.

sh lightsail-miab-installer.sh \
  --installation-id demo \
  --az ap-southeast-1c \
  --hostname box.example.com \
  --email '[email protected]' \
  --password 'lightsaildemo123'

Here, I am specifying demo as installation id, and the corresponding CloudFormation stack name would be miab-demo.

Specify hostname

Although you can access the server via its IP address, it is necessary to specify a hostname. This allows Mail-in-a-Box to generate SSL certificates for you. Having SSL certificates for your box prevents any warnings when accessing your Admin panel from a web browser or when making API calls.

To specify hostname you can use --hostname option.

sh lightsail-miab-installer.sh \
  --installation-id demo \
  --az ap-southeast-1c \
  --hostname box.example.org \
  --email '[email protected]' \
  --password 'lightsaildemo123'

Mail-in-a-box recommends using box subdomain when configuring the hostname. For example, if your domain is example.org you can specify box.example.org as the hostname.

Specify email and password for Administrator

To be able to use Mail-in-a-Box you need to specify Administrator's email and password for accessing Admin panel and to calls API. You can specify email using --email and password using --password option.

sh lightsail-miab-installer.sh \
  --installation-id demo \
  --az ap-southeast-1c \
  --hostname box.example.com \
  --email '[email protected]' \
  --password 'MyEmailServer123'

Domain of the email doesn't have to be the same as the hostname.

Specify instance type

Default Lightsail plan used is $5 USD/mo with 1GB of RAM and 40GB of SSD disk. If you want to change this, you can specify using --instance-type option.

sh lightsail-miab-installer.sh \
  --installation-id demo \
  --az ap-southeast-1c \
  --hostname box.example.com \
  --email '[email protected]' \
  --password 'lightsaildemo123' \
  --instance-type 20_usd

Command above will use $20/mo plan, which offers 4GB of RAM, 2 Core CPU and 80GB SSD disk. You can find details about all available plans on the Amazon Lightsail pricing page.

Specify availability zone

Default availability zone is us-east-1a. To change the availability zone you can use --az option, e.g eu-west-1a Europe (Ireland).

sh lightsail-miab-installer.sh \
  --installation-id demo \
  --az eu-west-1a \
  --hostname box.example.com \
  --email '[email protected]' \
  --password 'lightsaildemo123' \
  --instance-type 20_usd

Restore from backup

To restore from backup, you need to specify several configurations from previous installation:

  1. Amazon S3 buckets that are used to store the mail backup and Nextcloud data
  2. Mail-in-a-Box backup secret key can be found on your old box machine at /home/user-data/backup/secret_key.txt or in the AWS SSM Parameter Store at /MailInABox/miab-[INSTALLATION_ID]/BackupSecretKey.
export LMIAB_BACKUP_SECRET_KEY='_YOUR_BACKUP_SECRET_KEY_'
export LMIAB_MAIL_BACKUP_BUCKET='_YOUR_OLD_MAIL_BACKUP_BUCKET_'
export LMIAB_NEXTCLOUD_BACKUP_BUCKET='_YOUR_OLD_NEXTCLOUD_BACKUP_BUCKET_'

Add --restore option when creating new box.

sh lightsail-miab-installer.sh \
  --installation-id new-demo \
  --az ap-southeast-1c \
  --hostname new-box.example.com \
  --email '[email protected]' \
  --password 'lightsaildemo123' \
  --restore

Assuming that you will use your old IP address for your new box, you need to add a DNS A record of new-box.example.com to point to your old IP. Afterward, on the Amazon Lightsail instance, you should detach the static IP from the old box and assign it to the new one.

Destroy installation

You have two options to destroy your Mail-in-a-Box installation which was installed by lightsail-miab-installer. You can use --destroy option as shown in the example below.

sh lightsail-miab-installer.sh \
  --installation-id demo \
  --az ap-southeast-1c \
  --destroy

There will be a confirmation, you need to type "yes" to destroy.

This action will destroy CloudFormation stack 'demo' (ap-southeast-1).
Type 'yes' to continue:

Dry run mode

To execute the script in dry run mode, use the --dry-run option. This will print the CloudFormation template and then exit. Running the script in this mode can be beneficial for inspecting the resources that will be created.

sh lightsail-miab-installer.sh \
  --installation-id demo \
  --az eu-west-1a \
  --hostname box.example.com \
  --email '[email protected]' \
  --password 'lightsaildemo123' \
  --instance-type 20_usd \
  --dry-run

Post installation

After the installation, there are a few things you should check:

  • Ensure that the nameserver of your domain is correctly pointing to the box. The process of changing the nameserver depends on your DNS provider. Refer to your DNS provider's documentation for instructions.
  • Provision SSL certificates by logging into the Admin panel and navigating to System > TLS (SSL) Certificates. Then, click the Provision button.
  • Verify that your domain is successfully verified on Amazon SES before sending any emails. You can find more details in the "Verified identities" section of Amazon SES. Note that the verification process may take several minutes. If it takes too long, you can try removing the identity and creating a new one.
  • For production use, if your Amazon SES account is still in sandbox mode, it is necessary to request the removal of sandbox restrictions for your account.
  • If you are using an external DNS service for your domain, verify that the related DNS records, such as MX, SPF, DKIM, and DMARC settings, are properly configured. These settings are crucial for email delivery and security. Ensure they are accurately set according to the guidelines provided by your DNS service or in the Mail-in-a-Box documentation.

FAQ

I cannot send an email, what's wrong?

Check the rejection messages for specific reasons and review the mail server logs at /var/log/mail.log or /var/log/syslog for further insights.

I am not receiving any emails, what's wrong?

There several reason for this.

  1. Make sure your MX record is pointing to the box. You can use online DNS lookup tool or from command line.

    dig +short example.com MX

    It should output the address of servers which responsible for handling the email delivery.

    10 box.example.com.
    
  2. Check mail server log at /var/log/mail.log or /var/log/syslog for more details.

  3. Make sure firewall for incoming port 25 is open both on OS and on Amazon Lightsail instance.

The installation is stuck, what should I do?

See the log file at .out/[REGION]-[CLOUDFORMATION_STACK_NAME]-[TIME].log. If you did not find the issue then open CloudFormation console. Most of the time this is caused by CloudFormation failed to create a resource such as failed to create Amazon Lightsail Instance due permission issue or you do not have enough quota.

Is it safe to delete installation via CloudFormation?

Yes it is totally safe. It will destroy all resources created by lightsail-miab-installer.

How do I skip SSH passphrase?

lightsail-miab-installer uses SSH to connect to node in Kubernetes to perform tasks. If your SSH key having a passphrase it may quite annoying to enter the passphrase multiple times during installation process.

One of the solution is by using ssh-agent and ssh-add. Before running the installation, issue command below.

eval $( ssh-agent )
ssh-add /path/to/your/ssh-private.key

Changelog

v1.2 (2024-01-09)

v1.1 (2023-08-23)

  • Added support for new shell: zsh
  • Added support for macOS

v1.0 (2023-07-18)

  • Automatic restore from S3 backup

v1.0-RC1 (2023-06-20)

  • Initial release candidate

Todo

  • Automatically generate TLS certificates for main domain
  • Automatically restore from S3 backup
  • Test on different shells (dash, zsh)

Credits

This project is heavily inspired from:

Contributing

Fork this repo and send me a PR.

License

This project is licensed under MIT License.

lightsail-miab-installer's People

Contributors

paulj-net avatar rioastamal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

lightsail-miab-installer's Issues

--dry-run causes error.

Great script, thanks for the work.

using --dry-run as last parameter gives:
lightsail-miab-installer.sh: 1521: shift: can't shift that many

shouldn't be a shift on line 1479.

can workaround using a dummy argument: --dry-run dummy

Can't get it to work

sh lightsail-miab-installer.sh --installation-id demo --az ap-southeast-1c --email '[email protected]' --password 'Password123$' --hostname box.wp-fcfs.com

gets me:
lightsail-miab-installer.sh: 1395: Bad substitution

Unexpected EOF Syntax Error

Problem Description:
When attempting to deploy Mail-in-a-Box on AWS using the lightsail-miab-installer.sh script, I encountered a syntax error preventing the script from executing properly. The reported error pertains to an unexpected EOF (End Of File), suggesting an issue with quotation mark handling within the script.

Command Executed:
Please replace sh lightsail-miab-installer.sh \ --installation-id mail \ --az eu-west-3a \ --hostname mail.emenella.fr \ --email '[email protected]' \ --password 'XXXxxxXXX' \ --instance-type 20_usd with the exact command you used to execute the script. For example, if you ran the script directly, it might look like this:

Steps to Reproduce the Error:

  1. Execute the lightsail-miab-installer.sh script on macOS Monterey version 12.7.2 (21G1974) using zsh.
  2. Observe the following error shortly after the script starts executing:
    lightsail-miab-installer.sh: line 1425: unexpected EOF while looking for matching `''
    lightsail-miab-installer.sh: line 1540: syntax error: unexpected end of file

Execution Environment:

  • Operating System: macOS Monterey version 12.7.2 (21G1974)
  • Shell: zsh
  • Attempting to deploy Mail-in-a-Box on AWS via the lightsail-miab-installer.sh script

Expected Outcome:
Resolution of the syntax issue to allow the script to execute correctly without errors.

Thank you in advance for your assistance and time spent addressing this issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.