Oct 30, 2024 3 min read

Discourse: Self-Hosting Made Simple

Discourse: Self-Hosting Made Simple
Table of Contents

Discourse is a powerful, open-source forum platform designed for modern communities. It’s an excellent choice for self-hosting due to its robust feature set, high customizability, and full data ownership. In this guide, we’ll explore how to deploy, configure, secure, and manage Discourse to maximize its potential for your community.

Installing Discourse

πŸ“¦ Docker/Docker Compose Setup

Discourse officially recommends Docker for deployment. Start by creating a docker-compose.yml file tailored to Discourse. This setup includes necessary port mappings, volumes, and environment variables.


version: "3.8"

services:

discourse:

image: discourse/base:2.0.20230920-0045

container_name: discourse

restart: always

ports:

- "80:80"

- "443:443"

volumes:

- ./data:/var/www/discourse/data

- ./logs:/var/www/discourse/logs

environment:

DISCOURSE_HOSTNAME: "yourdomain.com"

DISCOURSE_DEVELOPER_EMAILS: "[email protected]"

DISCOURSE_SMTP_ADDRESS: "smtp.yourprovider.com"

DISCOURSE_SMTP_PORT: "587"

DISCOURSE_SMTP_USER_NAME: "smtpuser"

DISCOURSE_SMTP_PASSWORD: "smtppassword"

Run the following commands to deploy Discourse with Docker Compose:


docker-compose up -d

docker-compose logs -f

πŸš€ Manual Installation

For a non-Docker setup, follow these steps to install Discourse on a Linux server:

Install dependencies:


sudo apt update

sudo apt install -y git curl apt-transport-https ca-certificates software-properties-common build-essential libssl-dev libyaml-dev libreadline-dev zlib1g-dev

Clone the Discourse repository and set up the application:


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

cd /var/www/discourse

bundle install --deployment --without test development

RAILS_ENV=production bundle exec rake db:create db:migrate assets:precompile

Start the Discourse application:


RAILS_ENV=production bundle exec puma -C config/puma.rb

Configuring Nginx as a Reverse Proxy

🌐 Nginx Configuration

Set up Nginx as a reverse proxy to route traffic to the Discourse application. Create a new configuration file, e.g., /etc/nginx/sites-available/discourse, with the following content:


server {

listen 80;

server_name yourdomain.com;

location / {

proxy_pass http://127.0.0.1:3000;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

}

Activate the configuration and restart Nginx:


sudo ln -s /etc/nginx/sites-available/discourse /etc/nginx/sites-enabled/

sudo nginx -t

sudo systemctl reload nginx

πŸ”’ SSL/TLS Setup

Secure your site with a Let's Encrypt certificate using Certbot:


sudo apt install certbot python3-certbot-nginx

sudo certbot --nginx -d yourdomain.com

sudo systemctl reload nginx

Automate certificate renewal:


sudo crontab -e

0 0 * * * certbot renew --quiet

Logging and Debugging Discourse

πŸ—ƒοΈ Enabling Debug Logs

Enable verbose logging for debugging by adding the following line to the app.yml file (for Docker installs) or discourse.conf (manual install):


DISCOURSE_LOG_LEVEL: debug

πŸ“„ Viewing Logs

Access logs via Docker:


docker logs -f discourse

For manual installations, check logs directly:


tail -f /var/www/discourse/log/production.log

πŸ› οΈ Troubleshooting Common Issues

Example: If Discourse fails to start due to database connection issues, check the database logs:


docker exec -it discourse_db psql -U postgres -c '\l'

πŸ“€ Exporting Logs

Send logs to an ELK Stack using Filebeat. Example Filebeat config snippet:


filebeat.inputs:

- type: log

enabled: true

paths:

- /var/www/discourse/log/*.log

Backup and Restore

πŸ—‚οΈ File-Based Backups

Create a snapshot of Discourse’s configuration and data:


tar -cvzf discourse_backup.tar.gz /var/www/discourse

πŸ”„ Database Backups

Export and restore the database:


docker exec -it discourse_db pg_dump -U postgres discourse > backup.sql

docker exec -i discourse_db psql -U postgres discourse < backup.sql

πŸ“… Automated Backup Scripts

Automate backups using a cron job. Example script:


#!/bin/bash

tar -czvf /backups/discourse_$(date +%F).tar.gz /var/www/discourse

Add this to crontab:


0 2 * * * /path/to/backup_script.sh

Updating and Upgrading Discourse

⬆️ Updating Docker Images

Pull the latest version and redeploy:


docker-compose pull

docker-compose down

docker-compose up -d

πŸ› οΈ Manual Updates

For manual installs, update the repository and recompile assets:


cd /var/www/discourse

git pull

bundle install --deployment --without test development

RAILS_ENV=production bundle exec rake assets:precompile

πŸ” Checking for Updates

Check for updates via the command line:


docker-compose exec discourse /var/www/discourse/script/check_update

Leveraging Discourse’s Unique Features

πŸ”§ Enabling APIs

Activate the API and create an API key via the Discourse admin panel. Use the key with the API:


curl -X GET "https://yourdomain.com/posts.json" \

-H "Api-Key: your-api-key" \

-H "Api-Username: your-username"

🌟 Advanced Configurations

Example: Enable SSO (Single Sign-On) by editing discourse.conf:


enable_sso: true

sso_url: https://sso.yourdomain.com

Restart Discourse to apply changes:


docker-compose restart

Wrapping Up

This guide covered all essential steps to deploy, configure, manage, and customize Discourse for a self-hosted environment. With its flexibility, robust API, and rich feature set, Discourse is a powerful platform for building vibrant online communities. Start implementing these steps today to take full control of your forum and unlock its full potential!

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Selfhosted Ninja.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.