This post is adapted from a step by step tutorial here. I’ve encountered a few problems following that guide to install discourse on my 1und1 server. The server is almost free for students and has a few constrains on the configuration. So I made some adjustments here to avoid the problems I met back then.
Overview
##Step 1: OS Configuration
here are a handful of prerequisites for running Discourse; this tutorial will assume a fresh Centos 6 VPS.
###Add Swap Space
If you’re running a VPS with less than 2 GB of RAM, you’ll need to enable swap on your VPS. If you do not do so, some build steps are likely to fail.
This tutorial has instructions on enabling swap for CentOS 6. For smaller VPS, add at least 1 GB of swap space.
###Create a local, non-root user
Most steps will be run as a non-root user with sudo access. This tutorial will run you through creating a user account and granting it sudo power.
###Create a non-root user that will run Discourse
You’ll also need a user account which will run Discourse. It’s best for this to be a different account from your own. discourse would be a good choice.
You can create the sudo user by opening the sudoers file with this command:
Adding the user’s name and the same permissions as root under the the user privilege specification will grant them the sudo privileges.
###Add the EPEL repository
EPEL stands for Extra Packages for Enterprise Linux, and it has some packages we’ll need to install that are not part of the base CentOS repositories.
Log in as your local user and run:
###Install required packages
These are pre-requisites for Discourse or its pre-requisites.
###Install and start Redis
Redis is an open-source key value data store used by Discourse.
##Step 2: Install ngnix
Ngnix is a lightweight web server and reverse proxy that will be used to reverse proxy connections to Discourse.
###Add repository
ngnix is not in the central CentOS repositories, so you will have to add their repository for yum to be able to install ngnix.
Create a text file /etc/yum.repos.d/nginx.repo:
Paste in this content:
Once that’s installed, you need to refresh the yum package list and then install ngnix.
##Step 3: Install PostgreSQL
Discourse uses PostgreSQL for its data backend. While the EPEL contains packages for PostgreSQL, it’s fairly outdated. This will install a newer package from PostgreSQL’s repository.
###Disable CentOS repository
First we’ll tell yum to not look at packages from the core CentOS repository. To do this, edit the file /etc/yum.repos.d/CentOS-Base.repo.
Add the line:
under the [base] and [updates] sections of this file.
###Install the PostgreSQL repository
Now we’ll register PostgreSQL’s repository with yum.
This will install the server and some development packages needed by Discourse, start the server, initialize the database, and set it to start on reboot.
###Grant permissions to users
Now tell PostgreSQL about our users and give them permission to access the database.
##Step 4: Install rvm and Ruby
Ruby and rvm will be installed in single-user context for the discourse user.
###Install rvm
Become the discourse user and install the stable branch of rvm:
rvm will add some environment setup to the login scripts for the discourse user; to make sure these are set it’s easiest to log out then back in.
###Verify packages rvm requires are installed
This will make sure the environment is set up correctly and rvm is ready to work. Again, as the discourse user, run:
This should return that no additional packages are required by rvm.
###Install ruby
As the discourse user, install a local ruby environment and bundler:
###Tweak ruby configuration
By default, the gem builder will not be able to find the support libraries and binaries for the system’s PostgreSQL server. In order for this gem to build, this will tell bundle to pass commandline arguments when it builds the PostgresSQL library.
Still as the discourse user, run:
##Step 5: Install and Configure Discourse
Now the system is ready to build and run Discourse.
###Build Discourse
As the discourse user, pull down the Discourse source using git. This will clone the Discourse git tree to a sub-folder discourse:
Once that has copied the source locally, build Discourse:
If you encounter problem in this step, try this:
###Configure Discourse
Copy the example configuration files so Discourse will find them:
You’ll need to edit database.yml. Change the host_names line to your server’s hostname.
###Create database
Return to the discourse directory, and as the discourse user run:
The precompile step can take several minutes without printing any output to the screen, so it may appear like the process has hung.
###Fix permissions
By default the webserver will not be able to access the discourse directory. To grant more broad permissions, run this as the discourse user:
Bluepill is a process monitoring tool that Discourse uses to monitor itself. Install and configure it to start on boot:
###Run Discourse
Discourse is now ready to start up. To start Discourse, run:
You should also configure Discourse to start at bootup by adding this to the discourse user’s crontab. Open up your crontab for editing:
And paste the following line into the editor that pops up:
Discourse is now running, but ngnix needs to be configured to forward requests to the running Discourse server before it will answer requests sent to your hostname.
##Step 7: Configure ngnix
Now configure ngnix to point to the Discourse instance.
Log back in and execute these commands as the local user, not as discourse.
###Base ngnix configuration
There’s a small change you’ll need to make to the configuration. Open the /etc/nginx/nginx.conf file:
Find the http section of the file and add the following line:
###Configure nginx for Discourse
Discourse comes with an example nginx configuration file. Copy the example Discourse ngnix configuration to where ngnix expects it:
There are a few changes that need to be made to this file. Open the
/etc/nginx/conf.d/discourse.conf’ file:
###Change server_name to your hostname.
Change the socket paths to reflect where Discourse is installed. They should look like this:
Change the base path in the file. Look for a line with a path /var/www/discourse/public and replace this with our correct path, /home/discourse/discourse/public.
Now stop httpd to free the 80 port and restart ngnix to pick up the configuration changes:
The stop command may fail if ngnix wasn’t currently running, but the start command should succeed.
auto start nginx after boot:
##Step 8: Use Discourse!
Discourse is now running and responding to requests at your hostname. The last thing you’ll need to do is set up an admin account to give you permission to administrate the Discourse instance.
###Create your Discourse user
Open your Discourse website by navigating to your hostname in your webbrowser. Create a user account for yourself and make note of the email address with which you sign up.
###Grant yourself administrative priveleges
Return back to your shell. Become the discourse user and bring up a rails console connected to the Discourse instance:
This will drop you into a Rails console where you can run commands. These commands will grant you administrative access; replace the email address with the email address you signed up with:
You can also set this user as the default contact, as well:
Type exit to quit the Rails console.
Verify you are now an administrator
Return back to your Discourse instance in your webbrowser. If you’re not logged in as the account you signed up for, log back in. You will now be able to configure your Discourse instance to your liking.
Congratulations! You’ve installed Discourse. Huzzah!
##Some useful commands for maintenance
###Start discourse
###Simple script to update
Error fetching message: Error connecting to Redis on localhost:6379 (Errno::ECONNREFUSED)
sudo service redis restart