INSTALLATION OF DOCKER AND DISCOURSE ON AWS EC2 LINUX WITH EXISTING HTTPD REVERSE PROXY
Hi All
I have figured it out. I went back to the drawing board and started fresh with clean docker and discourse install. My process is detailed below:
Assumptions
• You already have an AWS EC2 Linux instance
• You can access your EC2 instance using SSH
• Apache 2 is already configured and running on your server
ACCESS YOUR EC2 INSTANCE USING SSH
-
Open CMD / Terminal
-
SSH into AWS EC2 instance
Example of screen once connected to EC2 instance
PERFORM SYSTEM UPDATES
- Perform system updates by running the following command in the console / terminal
$ sudo yum update
INSTALL DOCKER
- Search for the AWS Docker package by running the following command in the console / terminal
$ sudo yum search docker
- Get version information by running the following command in the console / terminal
$ sudo yum info docker
- Install docker by running the following command in the console / terminal
$ sudo yum install docker
- Add group membership for the default ec2-user so you can run all docker commands without using the sudo command by running the following command in the console / terminal
$ sudo usermod -a -G docker ec2-user
$ id ec2-user
# Reload a Linux user's group assignments to docker without logout
$ newgrp docker
- Install docker-compose by running the following command in the console / terminal
# 1. Get pip3 (python installer package)
$ sudo yum install python3-pip
# 2. Then run any one of the following
$ sudo pip3 install docker-compose # with root access
# OR
$ pip3 install --user docker-compose # without root access for security reasons
# Check permissions
$ sudo chmod -v +x /usr/local/bin/docker-compose
- Enable docker service to start automatically on boot by running the following command in the console / terminal
$ sudo systemctl enable docker.service
- Start the docker service by running the following command in the console / terminal
$ sudo systemctl start docker.service
- Check the docker service is running by running the following command in the console / terminal, the output will look similar to this
Basic docker commands that are good to know :
# Docker version
$ docker version
# Docker compose version
$ docker-compose version
# Start Docker service
$ sudo systemctl start docker.service
# Stop Docker service
$ sudo systemctl stop docker.service
# Restart Docker service
$ sudo systemctl restart docker.service
# Docker service status
$ sudo systemctl status docker.service
INSTALL DISCOURSE
- Create discourse folder
$ sudo mkdir /var/discourse
- Clone the discourse docker image to the newly created directory
$ sudo git clone https://github.com/discourse/discourse_docker.git /var/discourse
- Copy the standalone.yml file to the containers folder
$ cp /var/discourse/samples/standalone.yml /var/discourse/containers/discourse.yml
- Edit the new discourse.yml file as follows, (the important bits exposed ports for http and email details for smtp, modify in the file don’t copy and paste)
Editing options:
1. You can use nano and edit via command line / terminal
2. On AWS if you are using MATE with PLUMA, you can use PLUMA to edit the file
3. You can use WINSCP to SSH into the machine and edit the file via the Windows GUI
## this is the all-in-one, standalone Discourse Docker container template
##
## After making changes to this file, you MUST rebuild
## /var/discourse/launcher rebuild app
##
## BE *VERY* CAREFUL WHEN EDITING!
## YAML FILES ARE SUPER SUPER SENSITIVE TO MISTAKES IN WHITESPACE OR ALIGNMENT!
## visit http://www.yamllint.com/ to validate this file as needed
templates:
- "templates/postgres.template.yml"
- "templates/redis.template.yml"
- "templates/web.template.yml"
## Uncomment the next line to enable the IPv6 listener
#- "templates/web.ipv6.template.yml"
- "templates/web.ratelimited.template.yml"
## Uncomment these two lines if you wish to add Lets Encrypt (https)
#- "templates/web.ssl.template.yml"
#- "templates/web.letsencrypt.ssl.template.yml"
## which TCP/IP ports should this container expose?
## If you want Discourse to share a port with another webserver like Apache or nginx,
## see https://meta.discourse.org/t/17247 for details
expose:
- "8080:80" # http (run on local port 8080, docker port 80)
#- "443:443" # https (make sure this is commented out)
params:
db_default_text_search_config: "pg_catalog.english"
## Set db_shared_buffers to a max of 25% of the total memory.
## will be set automatically by bootstrap based on detected RAM, or you can override
#db_shared_buffers: "256MB"
## can improve sorting performance, but adds memory usage per-connection
#db_work_mem: "40MB"
## Which Git revision should this container use? (default: tests-passed)
#version: tests-passed
env:
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LANGUAGE: en_US.UTF-8
# DISCOURSE_DEFAULT_LOCALE: en
## How many concurrent web requests are supported? Depends on memory and CPU cores.
## will be set automatically by bootstrap based on detected CPUs, or you can override
#UNICORN_WORKERS: 3
## TODO: The domain name this Discourse instance will respond to
## Required. Discourse will not work with a bare IP number.
DISCOURSE_HOSTNAME: 'faq.mobiloan.io'
## Uncomment if you want the container to be started with the same
## hostname (-h option) as specified above (default "$hostname-$config")
#DOCKER_USE_HOSTNAME: true
## TODO: List of comma delimited emails that will be made admin and developer
## on initial signup example 'user1@example.com,user2@example.com'
DISCOURSE_DEVELOPER_EMAILS: ‘ enter email here ’
## TODO: The SMTP mail server used to validate new accounts and send notifications
# SMTP ADDRESS, username, and password are required
# WARNING the char '#' in SMTP password can cause problems!
DISCOURSE_SMTP_ADDRESS: ENTER SMTP HERE
DISCOURSE_SMTP_PORT: 587
DISCOURSE_SMTP_USER_NAME: ENTER USERNAME HERE
DISCOURSE_SMTP_PASSWORD: ENTER PASSWORD HERE
DISCOURSE_SMTP_ENABLE_START_TLS: true # (optional, default true, we use Amazon SES)
#DISCOURSE_SMTP_DOMAIN: discourse.example.com # (required by some providers)
#DISCOURSE_NOTIFICATION_EMAIL: noreply@discourse.example.com # (address to send notifications from)
## If you added the Lets Encrypt template, uncomment below to get a free SSL certificate
#LETSENCRYPT_ACCOUNT_EMAIL: me@example.com
## The http or https CDN address for this Discourse instance (configured to pull)
## see https://meta.discourse.org/t/14857 for details
#DISCOURSE_CDN_URL: https://discourse-cdn.example.com
## The maxmind geolocation IP address key for IP address lookup
## see https://meta.discourse.org/t/-/137387/23 for details
#DISCOURSE_MAXMIND_LICENSE_KEY: 1234567890123456
## The Docker container is stateless; all data is stored in /shared
volumes:
- volume:
host: /var/discourse/shared/standalone
guest: /shared
- volume:
host: /var/discourse/shared/standalone/log/var-log
guest: /var/log
## Plugins go here
## see https://meta.discourse.org/t/19157 for details
hooks:
after_code:
- exec:
cd: $home/plugins
cmd:
- git clone https://github.com/discourse/docker_manager.git
## Any custom commands to run after building
run:
- exec: echo "Beginning of custom commands"
## If you want to set the 'From' email address for your first registration, uncomment and change:
## After getting the first signup email, re-comment the line. It only needs to run once.
#- exec: rails r "SiteSetting.notification_email='info@unconfigured.discourse.org'"
- exec: echo "End of custom commands"
- Once the file is edited and saved, rebuild the discourse app using the following command in the terminal
$ /var/discourse/launcher rebuild discourse
Note if your yaml file is called something else then you would replace discourse with your yaml file name
Note that the rebuild takes time.
- One your app has been rebuilt the app should be running on port 8080. To verify this run the following command in the terminal
$ sudo lsof -i -P -n | grep LISTEN
Example output of ports
Notice docker is listening on port 8080, httpd (apache) is listening on port 80.
In terms of traffic routing apache will receive the web request and where appropriate will be routed to the discourse container on port 8080.
To make sure the routing happens you must configure the virtual host in the apache conf file.
APACHE VIRTUAL HOST CONFIGURATION
- The apache conf file is located at /etc/httpd/conf.d/00-virtualhosts.conf. httpd / apache / apache2 are different apache versions. Your files might be in different locations if you are not using the httpd service but the virtual host config in this case will be the same.
Editing options:
1. You can use nano and edit via command line / terminal
2. On AWS if you are using MATE with PLUMA, you can use PLUMA to edit the file
3. You can use WINSCP to SSH into the machine and edit the file via the Windows GUI
Edit the /etc/httpd/conf.d/00-virtualhosts.conf file by adding the following:
# FAQ (DISCOURSE ROUTES)
<VirtualHost *:80>
ServerName sub.domain.com
ProxyPreserveHost On
ProxyPass "/" "http://localhost:8080/"
ProxyPassReverse "/" "http://localhost:8080/"
</VirtualHost>
- You need to create the relevant CNAME DNS record having the sub domain pointing to your AWS server / load balancer.
Our setup is a little complicated as we have our domain provider who points routes all traffic to an AWS load balancer.
The load balancer handles our SSL/TLS through an AWS provided certificate.
The loan balancer also routes traffic to the AWS instance.
Apache is installed on the AWS instance and routes the traffic to our:
- Node express applications
- Docker / discourse application.
CONCLUSION
This is only one solution, but it is the one that worked with our existing setup.
A big thank you to:
@Kane York for his article on Discourse,
@Axel Fernandes’ article on medium his notes on downloading the necessary discourse files and
@Vivek Gite from nixCraft and his article on cyberciti on installing Docker on AWS Linux 2.