Powerful Redmine in AWS

In this post I will show how to install Redmine on AWS using several useful AWS services (EC2, RDS, S3, SES, ElastiCache) and make it auto-scalable.

Historical Note (2014): This walkthrough originally targeted Ubuntu 14.04 LTS (PV), Ruby 2.x, and Redmine 2.5. Inline corrections below fix command typos, YAML indentation, Apache XML tags, and insecure defaults for anyone referencing the architecture today.

Redmine

AWS account and SSH key:

Generate your SSH key with this command (please run it as your own user (DO NOT USE sudo)):

ssh-keygen -t rsa -b 2048

In AWS Key Pairs, click Import Key Pair, Choose File, select your id_rsa.pub, and give it a Key Pair Name.

Amazon EC2

Security Group:

EC2 Group

Inbound | SSH (recommended instead of All Traffic) | TCP | 22 | YOUR_IP/32

Inbound | HTTP | TCP | 80 | 0.0.0.0/0

Outbound | All traffic | All | All | 0.0.0.0/0

RDS Group

Inbound | MYSQL | TCP | 3306 | InstancePrivateIP

Outbound | All traffic | All | All | 0.0.0.0/0

ElastiCache Group

Inbound | Custom TCP Rule | TCP | 11211 | InstancePrivateIP

Outbound | All traffic | All | All | 0.0.0.0/0

An EC2 t1.micro with 8 GB is sufficient; Ubuntu 14.04 will be used here.

While creating the EC2 instance:

Use this image: Ubuntu Server 14.04 LTS (PV), 64-bit.

Select your EC2 Security Group.

Amazon S3

S3 bucket for Redmine files: create one in the AWS S3 Console.

Memcached: create a node in the AWS ElastiCache Console.

RDS (we are going to use MySQL): create an instance in the AWS RDS Console.

Redmine GitHub https://github.com/redmine/redmine

Redmine S3 GitHub https://github.com/ka8725/redmine_s3

Redmine Backlogs https://github.com/backlogs/redmine_backlogs

SES for Redmine outbound emails. Check out more details here.

Create an IAM user with permissions for this S3 Redmine bucket.

Generate access keys for this user and keep them safe.

Least-privilege IAM policy example (scoped to the Redmine S3 bucket instead of s3:*):

Amazon IAM

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["s3:ListBucket"],
      "Resource": "arn:aws:s3:::yourbucketname"
    },
    {
      "Effect": "Allow",
      "Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
      "Resource": "arn:aws:s3:::yourbucketname/*"
    }
  ]
}

Amazon RDS

Launch RDS with the RDS Security Group, connect using mysql-client (you don’t have SSH shell access to an RDS instance), and create a user for Redmine that can only access the Redmine database.

Example:

mysql -h mysqlrdsaddress.com -u myuser -p

Enter your password, then create a dedicated database and least-privilege user (preferring your private VPC CIDR instead of %):

CREATE USER 'myredmine'@'10.0.0.%' IDENTIFIED BY 'STRONG_PASSWORD';
CREATE DATABASE myredminedatabase CHARACTER SET utf8mb4;
GRANT ALL PRIVILEGES ON myredminedatabase.* TO 'myredmine'@'10.0.0.%';
FLUSH PRIVILEGES;

Launch the EC2 instance with the EC2 Security Group and your Key Pair, then SSH into it as ubuntu@yourmachineip. If you can’t log in, check your security group and key pair.

Install the required packages:

$ sudo apt-get install apache2 libapache2-mod-passenger git postfix libmysqlclient-dev libmagickwand-dev imagemagick bundler

Postfix will ask a few questions; just press Enter until it finishes.

Configure your .bashrc:

You can skip this, but this is so cool and easy… 🙂

vim ~/.bashrc +46

Uncomment line 46 to force a color prompt.

Change line 60 to show the Git branch.

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1 " (%s)") \$ '

Log out and log back in.

Go to the directory where you want to place Redmine (in this post I will use /srv, which will match the Apache 2 configuration) and clone the Redmine repository:

$ cd /srv

$ sudo git clone https://github.com/redmine/redmine

Enter the redmine directory and create a branch tracking the latest stable version:

$ cd redmine

$ sudo git checkout -b myProd --track origin/2.5-stable

Configure Redmine:

$ cd config

You can skip this step if you don’t want to use ElastiCache.

$ sudo cp additional_environment.rb.example additional_environment.rb

Amazon ElastiCache

Add this line to the file. To get your address, go to the ElastiCache console and copy the ‘Configuration endpoint’.

$ sudo vim additional_environment.rb

Add this line:

config.cache_store = :mem_cache_store, "yourmemcacheaddress:11211"

Add this to the second line of your Gemfile:

$ sudo vim ../Gemfile

gem "memcache-client", "1.8.5"

Configure the database:

$ sudo cp database.yml.example database.yml

$ sudo vim database.yml

Change the production setup to your settings.

Example:

production:
  adapter: mysql2
  database: redmine
  host: mysql.mydomain.com
  username: myredmine
  password: "STRONG_PASSWORD"
  encoding: utf8mb4

Amazon SES

Configure SMTP email:

$ sudo cp configuration.yml.example configuration.yml

Use STARTTLS on port 587 with valid certificate verification and dedicated SES SMTP credentials (generated in the SES console rather than raw IAM API keys):

default:
  email_delivery:
    delivery_method: :smtp
    smtp_settings:
      enable_starttls_auto: true
      address: "email-smtp.us-east-1.amazonaws.com"
      port: 587
      authentication: :login
      user_name: "YOUR_SES_SMTP_USERNAME"
      password: "YOUR_SES_SMTP_PASSWORD"

Installing Redmine:

$ cd /srv/redmine

sudo touch /srv/redmine/log/production.log
sudo chown www-data:www-data /srv/redmine/log/production.log
sudo chmod 0640 /srv/redmine/log/production.log

$sudo bundle install --without development test

$ sudo rake generate_secret_token

$ sudo RAILS_ENV=production rake db:migrate

$ sudo RAILS_ENV=production rake redmine:load_default_data

Apache 2 Configuration:

$ sudo a2dissite 000-default

$ sudo vim /etc/apache2/sites-available/redmine.conf

Paste this VirtualHost configuration (using <VirtualHost> and <Directory> blocks):

<VirtualHost *:80>
  ServerAdmin [email protected]
  DocumentRoot /srv/redmine/public
  RailsEnv production

  <Directory /srv/redmine/public>
    PassengerResolveSymlinksInDocumentRoot on
    Require all granted
    AllowOverride all
    Options -MultiViews
  </Directory>

  ErrorDocument 404 /404.html
  ErrorDocument 500 /500.html
  ErrorLog ${APACHE_LOG_DIR}/redmine-error.log
  CustomLog ${APACHE_LOG_DIR}/redmine-access.log combined
</VirtualHost>

$ sudo a2ensite redmine

$ sudo service apache2 restart

Open your instance IP in a browser and log in with admin/admin.

Redmine plugins:

Redmine Backlogs

Go inside the plugins directory and clone Redmine Backlogs:

$ cd plugins

$ sudo git clone https://github.com/backlogs/redmine_backlogs

$ cd redmine_backlogs

$sudo git checkout -b myProd --track origin/master

$ cd ../..

$sudo bundle install --without development test

$ sudo RAILS_ENV=production bundle exec rake db:migrate

$ sudo RAILS_ENV=production bundle exec rake redmine:backlogs:install

$ sudo service apache2 restart

Log in and test.

Redmine S3

Go inside the plugins directory and clone Redmine S3:

$ cd plugins

$ sudo git clone https://github.com/ka8725/redmine_s3

cd redmine_s3

$sudo git checkout -b myProd --track origin/master

$ sudo cp config/s3.yml.example ../../config/s3.yml

$ cd ../..

$ sudo vim config/s3.yml

Add your Access Key ID, Secret Access Key, and bucket name under the production section.

Save the file, and to migrate your files run:

$ sudo bundle install --without development test

$ sudo RAILS_ENV=production rake redmine_s3:files_to_s3

$ sudo service apache2 restart

Log in and test.

Create an image of this instance (called an AMI in AWS).

Create a Load Balancer with this setup; under Health Check, configure it to ping /news.atom.

Create a Launch Template (which superseded legacy AWS Launch Configurations) using this AMI (it will appear under My AMIs).

Create an Auto Scaling group using this launch template (select all available subnets).

Click Advanced Details, check Receive traffic from Elastic Load Balancer, and select your load balancer.

Create an auto-scaling policy:

>= 75% CPU usage for 60 seconds: add 10% of the group in increments (or at least 1 instance).

<= 15% CPU usage for 60 seconds: remove 10% of the group in increments (or at least 1 instance).

Access your instances through the load balancer address, and you’re done 🙂

Comments are Disabled