I wrote this guide for my students in WDI of General Assembly, so that they could build a community for all the graduates globally, namely GA Alumni. Thus, this guide is showing actual deployment steps in this website.
We decided to use Discourse
as the community platform, due to several reasons:
- We learn Ruby on Rails in the class,
Discourse
is a big project powered by Ruby on Rails. - We can learn how a Ruby on Rails project look like, which connects the students to the open source community.
- Students will be responsible in running this website, so this guide serve as a teaching material.
- Students will be fixing issues or working on new features for
Discourse
project, so that they could contribute back to the open source community.
There are some guides available on the Internet, look at reference section at the bottom. However, due to the many combinations of tools, I decided to write our own to ensure the students can go thru all the detailed steps. Also, I hope this will become a once and for all guide in our website.
GA Alumni is planned to serve the need for Hong Kong students first, and then promote it to Global. We will be using a cloud server, so that we can scale up easily according to need.
As a start, we will build a web server serving the static assets, and direct traffic to 4 rails instances, running as thin server. The deployment process is based on Capistrano.
The domain is registered in GoDaddy, and use its DNS service as well.
List of tools:
- GoDaddy for Domain
- rvm - ruby version manager
- ruby 2.1.1
- Thin Server for Rails
- Redis
- postgresql
- Capistrano - Deployment
- Domain (ga-alumni.com)
- Digital Ocean (512MB memory / 1 Core Processor / 20GB SSD Disk / 1TB Transfer)
- Ubuntu 14.04
- Discourse
- SSH keys
- Local Mac Machine
- Setup SSH keys (optional)
- Register Cloud Service at DigitalOcean
- Register domain at GoDaddy
- Setup SSH for Github
- Setup Capistrano for Discourse
- Run Capistrano
- Setup Nginx
In your local machine, turn on terminal and type ssh-keygen
, save it to the default location. It will create both the public and private keys. This helps in authenticating the cloud server securely, so that you need not to type password every time.
Assume you are saving the keys in default location, copy the public key into clipboard by typing pbcopy < ~/.ssh/id_rsa.pub
, then paste it into DigitalOcean.
Visit DigitalOcean to register for a Cloud Server.
Setup Billing method.
In DigitalOcean's SSH Keys
section, paste the public key, and give it a name, e.g. My MacBook Air
.
Create Droplet, pick the size, start with the basic one, and you could upgrade it easily later on. Set the Hostname
to the domain name ga-alumni.com
. Select the Region, the speed to visit the site is generally better if the region is near you. Select Image as Ubuntu 14.04 x64
. Select SSH Keys previously created, otherwise it will send you the root
access password by email. Only tick Enable VirtIO
in the Settings.
Visit GoDaddy to register for your favourite domain, in my case http://ga-alumni.com
Visit GoDaddy DNS Manager to modify DNS record. Select Edit Zone
under your domain.
Modify the A (Host)
record. Points your domain to the IP Address provided by DigitalOcean. Click Save
on top-right corner. Since it is .com domain, the update will effect almost immediately, otherwise wait upto several hours
Fork your project, clone in to local, then checkout to the latest stable tag by typing git checkout tags/v0.9.9.4
. We will now create a branch here by git checkout -b deploy
, the new branch is named deploy
.
Push the new branch to your repository by git push origin deploy
. We need this to do the deployment.
SSH into your server by ssh [email protected]
Instead of using root
user to setup the server, I highly recommend to create a user (e.g. ubuntu
) for doing the job. We will use this user to run the application as well. root
user should be used for the super administrator in urgent case.
root@www:~# adduser ubuntu
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for harry
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] Y
We will turn ubuntu
user to use the SSH key as well, so we need not type password everytime.
Exit the root
access, SSH into your server using ssh [email protected]
, copy the same public key created in local machine, paste it in ~/.ssh/authorized_keys
Start installing the cloud server under ubuntu
user:
sudo apt-get install build-essential openssl libreadline6 libreadline6-dev \
curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev \
sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev libgdbm-dev \
ncurses-dev automake libtool bison subversion pkg-config libffi-dev
To install RVM, open terminal and type in this command:
curl -L https://get.rvm.io | bash -s stable
After it is done installing, load RVM. You may first need to exit out of your shell session and start up a new one.
source ~/.rvm/scripts/rvm
Check version for rvm
rvm -v
rvm 1.25.25 (stable) by Wayne E. Seguin <[email protected]>, Michal Papis <[email protected]> [https://rvm.io/]
Install ruby 2.1.1, it takes some time
rvm install 2.1.1
After ruby is installed, pick this as the default version
rvm use 2.1.1 --default
Check the ruby version by,
ruby -v
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-linux]
Finally, we'll need bundler to install gems for rails project
gem install bundler --no-rdoc --no-ri
Discourse requires PostgreSQL and Redis to run, so let’s install those as well:
sudo apt-get install postgresql-9.1 postgresql-contrib-9.1 redis-server \
libxml2-dev libxslt-dev libpq-dev make g++
Note that postgresql-contrib-9.1 will install the hstore extension for PostgreSQL, which Discourse requires. You’ll need to create a postgres role and Discourse’s production database:
# Create your postgres role
sudo -u postgres createuser discourse -s -P
# Create discourse's production database (I named mine discourse_production)
createdb -U discourse discourse_production
Nginx is the web server, or sometimes Reverse Proxy server, which we will set before the Rails application.
sudo apt-get install nginx
This will install Nginx to folder /etc/nginx/
. Then setup the Nginx
to redirect traffic into Discourse
, we need a config file, types sudo vi /etc/nginx/sites-enabled/discourse
and save the config file.
We can test the correctness of Nginx config
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Then restart the Nginx web server
sudo service nginx restart
Until now, we have succesfully prepared the environment for running Discourse, it's time to deploy the application to the server.
The following part may contain some secrets that we don't want to share to the public, thus we will create a separate git branch. Type git checkout -b secret
Open the Gemfile, add these gems to the bottom
gem "rvm-capistrano"
group :development do
gem "capistrano", "2.15.4"
gem "capistrano-ext"
gem 'capistrano-unicorn', :require => false
gem "cape"
end
Then, create several files thin.yml
, config/deploy.rb
and config/deploy/production.rb
These files sets the environment on steps during the deployment process, once and for all. We will then try to run the following commands:
# Set up the server's directory structure for Capistrano
cap deploy:setup
# Seed the database you created earlier
cap db:seed
# Do the first deploy!
cap deploy:restart
Congratulations! You should now have Discourse running on your cloud server. You'll want to follow the Quick Start Guide to setup the Admin Panel.
In the cloud server ssh-keygen
- Use
master
branch ortag
? - Capistrano
502 Bad Gateway
403 Forbidden
- Restart your cloud server
- Postgresql Peer Authentication
Pending topicshar
- Costing
- Backup
Pending
Pending topics
Thin
toUnicorn
- Multiple server instances
Pending topics
- Mailgun
For postgresql, use this