---
title: "Running Discourse"
date: "2017-05-24"
category: "ruby"
slug: "running-discourse"
---

[Discourse](https://www.discourse.org/) is a popular forum software written in Ruby. Because Discourse relies on Docker, which is incompatible with the hosting platform, installation must be carried out manually. A [Pro package](https://apnscp.com/pricing) is recommended as each Discourse worker uses approximately 200 MB of memory.

## Getting Started

All installation steps are performed from the [[accessing-terminal]].

### 1. Clone the Repository

cd /var/www
git clone https://github.com/discourse/discourse.git
cd discourse

### 2. Set the Ruby Version

Verify the Ruby interpreter is at least 2.3.1. Use [[changing-ruby-versions]] if needed.

rvm list
# Ensure at least ruby-2.3.1 is available
rvm use 2.4.1

Optionally pin the Ruby version for this directory:

echo 2.4.1 > .ruby-version

### 3. Install Dependencies

gem install bundle
bundle install

**Note:** The build may fail on the pg gem. Configure the pg build path, then rerun `bundle install` ([[determining-platform-version]] uses PostgreSQL 9.6, v6.5 uses 9.4, v6 uses 9.1):

bundle config build.pg --with-pg-config=/usr/pgsql-9.6/bin/pg\_config

### 4. Set Up Redis

Configure [[running-redis]] for your account.

### 5. Create the PostgreSQL Database

Go to **Databases** > **PostgreSQL Manager** in the control panel. Set the connecting user's concurrent connection limit to _15_ (instead of the default 5), as Discourse uses connection pooling.

### 6. Create an Email User

Go to **User** > **Add User** and create a user with both incoming and outgoing email privileges enabled.

### 7. Configure Discourse

cp config/discourse\_defaults.conf config/discourse.conf

Edit `config/discourse.conf` and set these values to match your account:

- `db_pool`: set to `5`
- `developer_emails`: comma-delimited list of email addresses that receive admin rights upon registration
- `db_name`, `db_username`, `db_password`
- `db_host`: set to `127.0.0.1`
- `hostname`: your Discourse domain
- `smtp_address`: set to `localhost`
- `smtp_user_name`, `smtp_password`
- `smtp_openssl_verify_mode`: set to `none`
- `redis_port`

### 8. Run Database Migrations

RAILS\_ENV=production bundle exec rake db:migrate

If migration fails requiring the hstore or pg\_trgm extensions, open a ticket to request these extensions for your database. Alternatively, use [[scripting-with-beacon]] to call [sql\_add\_pgsql\_extension](http://docs.apnscp.com/class-Sql_Module.html#_add_pgsql_extension).

### 9. Precompile Assets

RAILS\_ENV=production bundle exec rake assets:precompile

### 10. Create Upload Directory

mkdir public/uploads

### 11. Start Sidekiq

Sidekiq handles background tasks including email delivery:

RAILS\_ENV=production rvm 2.4.1 do sidekiq -L /tmp/sidekiq.log -P /tmp/sidekiq.pid -q critical -q low -q default -d

### 12. Populate Initial Posts

RAILS\_ENV=production bundle exec rake posts:rebake

### 13. Configure Passenger

gem install --no-rdoc --no-ri passenger
passenger-config --ruby-command

Add the following to `public/.htaccess`:
- `RailsEnv production` (see [[htaccess-guide]])
- The `PassengerRuby` directive from the output above (see [[setting-rails-passenger]]), e.g. `PassengerRuby /home/apnscp/.rvm/gems/ruby-2.4.0/wrappers/ruby`

### 14. Connect to a Subdomain

Connect `discourse/public` to a [[creating-subdomain]] or addon domain.

![](kb-image:add-subdomain.png)
*Connecting Discourse to a subdomain named discourse.mydomain.com*

### 15. Complete Setup

Visit your Discourse forum and register using the email address specified in `developer_emails`. A confirmation email will be sent if SMTP is configured correctly and Sidekiq is running. Click the confirmation link and follow the setup wizard.

**Recommended:** Configure a CDN (see below) for significantly better performance.

## Adding a CDN

Without a CDN, Discourse serves all content through the application, creating significant overhead. A CDN caches and delivers static assets from a third-party edge network.

Any CDN works. This example uses Amazon [CloudFront](https://aws.amazon.com/cloudfront/) (50 GB free tier).

Assuming Discourse runs on _discourse.mydomain.com_ and the CDN will use _cdn.mydomain.com_:

### Configure CORS

Add to `public/.htaccess`:

Header set Access-Control-Allow-Origin "http://discourse.mydomain.com"

### Set Up CloudFront

1. Click **Create Distribution** and select **Web**
2. Set **Origin Domain Name** to _discourse.mydomain.com_
3. Set **Origin ID** to _cdn.mydomain.com_
4. Under **Alternative Domain Names (CNAMEs)**, enter _cdn.mydomain.com_
5. Under **Forward Headers**, select _Whitelist_, then add _Origin_ to the whitelist

### Create DNS Record

In the control panel, go to **DNS** > **DNS Manager** and create a CNAME record for `cdn.mydomain.com` pointing to your CloudFront domain name.

![](kb-image:domain-name.png)
*CloudFront domain field*

### Update Discourse Config

Edit `config/discourse.conf` and set **cdn\_url** to your CDN domain.

### Restart the Application

[[restarting-passenger-processes]]:

touch tmp/restart.txt
