Isaac Sloan - Setting Up Ubuntu 18.04 as Rails Server

Setting Up Ubuntu 18.04 as Rails Server

Step 1: Install Ubuntu

Step 2: Add a deploy user and optionally a user for yourself.

Assuming you're logged in as root add these users and login as "not root."

adduser deploy
adduser [yourname] #optionally
vim /etc/group
# add your users to the sudo group like so: 
vim /etc/ssh/sshd_config
# add to file or if they exist set the flags to no.
  ChallengeResponseAuthentication no
  PasswordAuthentication no
  UsePAM no
service ssh restart

su - deploy # or your username. The important thing is that you use a user other than root.

Step 3: Update and Install dependencies

Make sure that each line completes before running the next. Don't just copy the lines in all at once or it will use the beginning letters of one line as an answer to questions the first may ask.

sudo apt -y update && sudo apt-get -y upgrade
sudo apt -y install build-essential zlib1g-dev libssl-dev libreadline-dev libyaml-dev libcurl4-openssl-dev curl git-core python-software-properties vim libmagickwand-dev imagemagick libxml2 libxml2-dev libxslt1-dev

I usually install sshguard to help protect my server from being hacked.

sudo apt-get install sshguard

Step 4: Install ruby 2.1.5

Option 1: RVM
gpg --keyserver hkp:// --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
curl -L | sudo bash -s stable
sudo vim /etc/group
# Add your current user and deploy to the rvm group like so: rvm:x:101:isaac,deploy
# Logout and log back in.
rvm list known # finds the newest version of ruby.
rvm install ruby-2.1.5 #whatever the latests version of ruby is.
rvm ruby-2.1.5 --default
Option 2: Compile Ruby
tar -xvzf ruby-2.1.5.tar.gz
cd ruby-2.1.5/
sudo make install
sudo gem install bundler

Step 5: Install Node.js

sudo apt -y install nodejs npm

Step 6: Install your database

sudo apt -y install mariadb-server mariadb-client libmariadb-client-lgpl-dev

Step 7: Install your Web Server (Nginx and Passenger)

Setup a swapfile (Recommended)


swapon -s

If you don't see any swapfiles you should create one like so:

sudo fallocate -l 1G /swapfile
sudo chown root:root /swapfile 
sudo chmod 0600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo bash -c "echo '/swapfile       none    swap    sw      0       0 ' >> /etc/fstab"
Install Passenger
gem install passenger
sudo passenger-install-nginx-module # if you installed ruby with rvm use "rvmsudo"

The following lines will setup you nginx configs with sites-enabled and sites-available directories. This way you can keep you config a lot cleaner as you won't ever have to edit the main conf file.

sudo sed -i '1iuser deploy rvm;' /opt/nginx/conf/nginx.conf #use replace staff with rvm as group if installed.
sudo sed -i '36iinclude /opt/nginx/conf/sites-enabled/*;' /opt/nginx/conf/nginx.conf
sudo sed -i '36iserver_names_hash_bucket_size   64;' /opt/nginx/conf/nginx.conf
sudo mkdir /opt/nginx/conf/sites-enabled
sudo mkdir /opt/nginx/conf/sites-available

To setup the following commands for nginx:

sudo service nginx stop
sudo service nginx start
sudo service nginx restart


wget -O
sudo mv /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo /usr/sbin/update-rc.d -f nginx defaults

Step 8: Host your rails app

Clone your rails app to a directory owned by deploy. Then create a file in /opt/nginx/conf/sites-available/ called

sudo touch /opt/nginx/conf/sites-available/

Edit /opt/nginx/conf/sites-available/ with your favorite editor and add:

server {
  listen 80;
  return     301$request_uri;

server {
  client_max_body_size 40M;
  listen 80;
  root /path/to/your/railsapp/public;   # <--- be sure to point to 'public'!
  passenger_enabled on;

Then enabled your site by symlinking your conf file to sites-enabled and restarting nginx.

sudo ln -s /opt/nginx/conf/sites-available/ /opt/nginx/conf/sites-enabled/
sudo service nginx restart

All done. Enjoy!

October 04, 2018
comments powered by Disqus