Skip to content

Instantly share code, notes, and snippets.

@fire
Last active April 19, 2017 11:40
Show Gist options
  • Save fire/eb2f40aba48182a96a07 to your computer and use it in GitHub Desktop.
Save fire/eb2f40aba48182a96a07 to your computer and use it in GitHub Desktop.
Smartos install of discourse

Discourse Smartos Install Guide

Guide is working, but it's missing details on how to install pg_trgm extension. https://github.com/fire/pkgsrc-wip/tree/pg_trgm

Recommended Server Hardware

  • 2 GB of RAM
  • 2 GB of swap
  • 2 processor cores

With 2 GB of memory and dual cores, you can run two instances of the thin server (NUM_WEBS=2), and easily host anything but the largest of forums.

1 GB of memory, 3 GB of swap and a single core CPU are the minimums for a steady state, running Discourse forum – but it's simpler to just throw a bit more hardware at the problem if you can, particularly during the install.

sudo apt-get -y install build-essential libssl-dev libyaml-dev git libtool libxslt-dev libxml2-dev libpq-dev gawk curl pngcrush imagemagick python-software-properties
pkgin in build-essential git gawk curl pngcrush ImageMagick libtool libxslt libxml2 gawk curl

Install Redis

pkgin in redis
/usr/sbin/svcadm enable svc:/pkgsrc/redis:default

Install ruby

pkgin in ruby-2.1.1 ruby211-nokogiri ruby211-xslt ruby211-bundler ruby211-readline

Create discourse user and group

groupadd discourse
useradd \
-s /bin/false \
-c 'Discourse forum' \
-g discourse \
-d /home/discourse -m discourse

Install postgres

sudo pkgin postgres-93 postgresql93-datatypes
# TODO install pg_trgm extension 
# You have to compile the package from https://github.com/fire/pkgsrc-wip/tree/pg_trgm/postgresql93-trgm

Give Postgres database rights to discourse.

sudo -u postgres createuser -s discourse
# If you will be using password authentication on your database, only
# necessary if the database will be on a remote host
sudo -u postgres psql -c "alter user discourse password 'todayisagooddaytovi';" # Note choose your own password
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE discourse to discourse";
sudo -u postgres /opt/loca/bin/nano  /var/pgsql/data/pg_hba.conf
# Add "local   discourse    discourse    peer"
# before "local all   all   password"
sudo svcadm restart postgresql

Continue with Discourse installation:

Pull down the latest code.

Now would be a great time to consider forking, if want to work from your own copy of discourse.

If you don't need to customize your installation, and want less hassle upgrading clone from Discourse's repo.

sudo -u discourse /bin/bash
git clone git://github.com/discourse/discourse.git  tmp && mv tmp/.git . && rm -rf tmp && git reset --hard
git checkout latest-release

Install bundler

# Change Gemfile and Gemfile.lock to use tzinfo-data # May 31, 2014
bundle install --path vendor/bundle
bundle install --without test --deployment

Configure Discourse:

# Run these commands as the discourse user
cd /home/discourse/config
cp discourse_quickstart.conf discourse.conf
chmod 600 discourse.conf

Editing /home/discourse/config/discourse.conf:

Database/Hostname:

  • change database username/password if appropriate
  • change hostname to the name you'll use to access the Discourse site, e.g. "forum.example.com"

Redis:

  • no changes if this is the only application using redis, but have a look

E-mail:

  • browse through all the settings and be sure to add your mail server SMTP settings so outgoing mail can be sent (we recommend Mandrill)
  • If your users will come from "internal" private unroutable IPs like 10.x.x.x or 192.168.x.x please see this topic.

Initialize the database:

# Run these commands as the discourse user
# The database name here should match the production one in database.yml
cd /home/discourse
createdb discourse
RUBY_GC_MALLOC_LIMIT=90000000 RAILS_ENV=production bundle exec rake db:migrate
RUBY_GC_MALLOC_LIMIT=90000000 RAILS_ENV=production bundle exec rake assets:precompile

Not english? Set the default language as appropriate:

Run these commands as the discourse user

cd /home/discourse
RAILS_ENV=production bundle exec rails c
SiteSetting.default_locale = 'fr'

Not sure if your locale is supported? Check at the rails console:

LocaleSiteSetting.values
 => ["cs", "da", "de", "en", "es", "fr", "id", "it", "nb_NO", "nl", "pt", "ru", "sv", "zh_CN", "zh_TW"]

nginx setup

pkgin in nginx -y

Copy the nginx config:

mkdir -p /opt/local/etc/nginx/sites-enabled
mkdir -p /opt/local/etc/nginx/sites-available
cp /home/discourse/config/nginx.sample.conf /etc/nginx/sites-available/discourse.conf
ln -s /opt/local/etc/nginx/sites-available/discourse.conf nginx/sites-enabled/
Edit nginx.conf and add the include line after http {

vim /opt/local/etc/nginx/nginx.conf
#add this line after http {
include /opt/local/etc/nginx/sites-enabled/*.conf;
Enable nginx:

svcadm enable nginx

Look at /home/discourse/config/nginx.global.conf
Look at /home/discourse/config/nginx.sample.conf

Change the paths in nginx.sample.conf and change the name of the config to subdomain.example.com.

Figure out how to disable default site

Edit /etc/local/nginx/sites-enabled/discourse.conf

  • edit server_name. Example: server_name cain.discourse.org test.cain.discourse.org;
  • change socket count depending on your NUM_WEB count
  • change socket paths if Discourse is installed to a different location
  • modify root location if Discourse is installed to a different location

Reload nginx by running

svcadm enable nginx

Generate unicorn smf configurations.

su
nano rails-discourse.xml # Copy bottom smf configuration
svccfg import rails-discourse.xml
svcadm enable rails-discourse
tail -f -n 50 /var/svc/log/site-rails-discourse:default.log
sudo su - discourse
gem install unicorn --user-install
# Add gem directory to path  

Log rotation setup

# Disabled for now - log rotation isn't *quite* complete
#0 0 * * * /usr/sbin/logrotate /var/www/discourse/config/logrotate.conf

Email setup

IMPORTANT: Discourse relies heavily on email. If your email configuration is not correct, you will effectively have a broken forum. Please, head over to our Mail Setup Guide to find out more information on how to properly setup emails.

Congratulations! You've got Discourse installed and running!

Administrator account

Now make yourself an administrator account. Browse to your Discourse instance and create an account by logging in normally, then run the commands:

 # Run these commands as the discourse user
 sudo -u discourse /bin/bash && cd ~
 RAILS_ENV=production bundle exec rails c

 # Administratorize yourself:
 # (in rails console)
 > me = User.find_by_username_or_email('myemailaddress@me.com')
 > me.activate # use this in case you haven't configured your mail server and therefore can't receive the activation mail.
 > me.admin = true
 > me.save

 # Mark yourself as the 'system user':
 # (in rails console)
 > SiteSetting.site_contact_username = me.username

Site localization

Custom assets such as images should be placed somewhere under:

 /home/discourse/public/

For example, create a local directory and place it into:

 /home/discourse/public/uploads/local/michael.png

The corresponding site setting is:

 logo_small_url: /uploads/local/michael.png

Updating Discourse

 sudo svcadm disable rails-discourse
 # Back up your install
 sudo -u discourse /bin/bash
 DATESTAMP=$(TZ=UTC date +%F-%T)
 pg_dump --no-owner --clean discourse | gzip -c > ~/discourse-db-$DATESTAMP.sql.gz
 tar cfz ~/discourse-dir-$DATESTAMP.tar.gz -C /home/discourse
 # get the latest Discourse code
 cd /home/discourse
 git checkout master
 git pull
 git fetch --tags
 # To run on the latest numbered release instead of bleeding-edge:
 #git checkout latest-release
 #
 # Follow the section below titled:
 # "Check sample configuration files for new settings"
 bundle install --without test --deployment
 RUBY_GC_MALLOC_LIMIT=90000000 RAILS_ENV=production bundle exec rake db:migrate
 RUBY_GC_MALLOC_LIMIT=90000000 RAILS_ENV=production bundle exec rake assets:precompile
 sudo svcadm enable rails-discourse

Check sample configuration files for new settings

Check the sample configuration files provided in the repo with the ones being used for additional recommended settings and merge those in:

 # Run these commands as the discourse user
 cd /home/discourse
 diff -u config/discourse_quickstart.conf config/discourse.conf

Example 2

 $ diff -u config/nginx.sample.conf /etc/nginx/conf.d/discourse.conf
 --- config/nginx.sample.conf	2013-07-15 17:38:06.521507000 +0000
 +++ /etc/nginx/conf.d/discourse.conf	2013-07-15 17:52:46.649507024 +0000
 @@ -12,17 +12,18 @@
    gzip_min_length 1000;
    gzip_types application/json text/css application/x-javascript;

 -  server_name enter.your.web.hostname.here;
 +  server_name webtier.discourse.org;

    sendfile on;

    keepalive_timeout 65;
 -  client_max_body_size 2m;
    location / {
      root /home/discourse/discourse/public;

This change reflects a change in placeholder information plus (importantly) adding the client_max_body_size 2m; directive to the nginx configuration. This change should also be made to your production file.

<?xml version="1.0"?>
<!DOCTYPE service_bundle SYSTEM "/usr/share/lib/xml/dtd/service_bundle.dtd.1">
<service_bundle type="manifest" name="rails-discourse">
<service name="site/rails-discourse" type="service" version="1">
<create_default_instance enabled="true"/>
<single_instance/>
<dependency name="network" grouping="require_all" restart_on="refresh" type="service">
<service_fmri value="svc:/milestone/network:default"/>
</dependency>
<dependency name="filesystem" grouping="require_all" restart_on="refresh" type="service">
<service_fmri value="svc:/system/filesystem/local"/>
</dependency>
<dependency name="nginx" grouping="require_all" restart_on="refresh"
type="service">
<service_fmri value="svc:/pkgsrc/nginx:default"/>
</dependency>
<dependency name="postgresql" grouping="require_all"
restart_on="refresh" type="service">
<service_fmri value="svc:/pkgsrc/postgresql:default"/>
</dependency>
<dependency name="redis" grouping="require_all"
restart_on="refresh" type="service">
<service_fmri value="svc:/pkgsrc/redis:default"/>
</dependency>
<method_context working_directory="/home/discourse">
<method_credential user="discourse" group="discourse" privileges='basic,net_privaddr' />
<method_environment>
<envvar name="PATH" value="/home/discourse/bin:/opt/local/bin/:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/home/discourse/.gem/ruby/2.1.1/bin"/>
<envvar name="HOME" value="/home/discourse"/>
<envvar name="RAILS_ENV" value="production"/>
<envvar name="RAILS_ROOT" value="/home/discourse"/>
<envvar name="UNICORN_WORKERS" value="3"/>
<envvar name="UNICORN_SIDEKIQS" value="1"/>
<envvar name="RUBY_GC_HEAP_INIT_SLOTS" value="800000"/>
<envvar name="RUBY_GC_MALLOC_LIMIT" value="40000000"/>
</method_environment>
</method_context>
<exec_method
type="method"
name="start"
exec="/opt/local/bin/bundle exec unicorn -E production -c config/unicorn.conf.rb"
timeout_seconds="60"/>
<exec_method
type="method"
name="stop"
exec=":kill"
timeout_seconds="60"/>
<property_group name="startd" type="framework">
<propval name="duration" type="astring" value="child"/>
<propval name="ignore_error" type="astring" value="core,signal"/>
</property_group>
<property_group name="application" type="application">
</property_group>
<stability value="Evolving"/>
<template>
<common_name>
<loctext xml:lang="C">Discourse forum service</loctext>
</common_name>
</template>
</service>
</service_bundle>
@siepkes
Copy link

siepkes commented Feb 20, 2017

# TODO install pg_trgm extension 

No need to manually compile pg_trgm. That extension is in the postgresql93-contrib package of pkgsrc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment