Skip to content

Instantly share code, notes, and snippets.

@overdrivemachines
Last active July 18, 2023 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save overdrivemachines/b473beead565423d4e0171f3eb71fc7a to your computer and use it in GitHub Desktop.
Save overdrivemachines/b473beead565423d4e0171f3eb71fc7a to your computer and use it in GitHub Desktop.
Set up new Ubuntu Computer

Setup Ubuntu 22.04 for Ruby on Rails

Upgrade Ubuntu

sudo apt-get update && sudo apt-get upgrade -y

Use local time

timedatectl set-local-rtc 1 --adjust-system-clock

Install Software

List of software that will be installed:

  • Google Chrome Beta:
  • Sublime Text
  • Filezilla
  • Gnome Tweak
  • Multimedia Codecs
  • Ubuntu Cleaner
  • Rar, p7zip
wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" | sudo tee /etc/apt/sources.list.d/google-chrome.list
sudo apt update

wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key add -
sudo apt install apt-transport-https
echo "deb https://download.sublimetext.com/ apt/stable/" | sudo tee /etc/apt/sources.list.d/sublime-text.list
sudo apt update

sudo add-apt-repository ppa:gerardpuig/ppa
sudo apt-get update

sudo apt install -y google-chrome-beta sublime-text filezilla gnome-tweaks ubuntu-restricted-extras ubuntu-cleaner rar unrar p7zip-full p7zip-rar

sudo snap install vlc

Install Rails

sudo apt install curl
curl -sL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

sudo apt-get update
sudo apt-get install -y git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev nodejs yarn

gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

\curl -sSL https://get.rvm.io | bash

Enable "Run command as a login shell" in Terminal preferences then exit Terminal and open it again:

rvm install 3.2.2
rvm --default use 3.2.2
echo 'gem: --no-document' >> ~/.gemrc
gem update --system
gem install bundler
gem install rails
gem update

git config --global color.ui true
git config --global user.name "Dipen Chauhan"
git config --global user.email "get.dipen@gmail.com"
ssh-keygen -t rsa -b 4096 -C "get.dipen@gmail.com"
cat ~/.ssh/id_rsa.pub

Paste above output to: https://github.com/settings/ssh

ssh -T git@github.com

Cleanup

To clean partial packages, unused dependencies and apt-cache:

sudo apt-get autoclean
sudo apt-get autoremove
sudo apt-get clean

Settings

timedatectl set-local-rtc 1 --adjust-system-clock

New Project

In terminal

rails new myapp
cd myapp

Rails stores secrets in config/credentials.yml.enc. It uses config/master.key to encrypt the credentials file. credentials.ymc.enc is stored on Github but master.key file is not. To edit, in terminal:

EDITOR=subl
bin/rails credentials:edit

Deployment Server Only

Create a User and enable easy login

adduser deploy
adduser deploy sudo
exit
ssh-copy-id root@1.2.3.4
ssh-copy-id deploy@1.2.3.4
  • Install Rails as described above
  • Install and Setup Git as described above

Install NGINX & Passenger:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger focal main > /etc/apt/sources.list.d/passenger.list'
sudo apt-get update
sudo apt-get install -y nginx-extras libnginx-mod-http-passenger
if [ ! -f /etc/nginx/modules-enabled/50-mod-http-passenger.conf ]; then sudo ln -s /usr/share/nginx/modules-available/mod-http-passenger.load /etc/nginx/modules-enabled/50-mod-http-passenger.conf ; fi
sudo ls /etc/nginx/conf.d/mod-http-passenger.conf

Get output of

which ruby

and paste it in file:

sudo nano /etc/nginx/conf.d/mod-http-passenger.conf

right after it says passenger_ruby.

Start NGINX

sudo service nginx start

Add Config file for App

sudo rm /etc/nginx/sites-enabled/default
sudo nano /etc/nginx/sites-enabled/myapp

Paste the following:

server {
  listen 80;
  listen [::]:80;

  server_name _;
  root /var/www/myapp/code/public;

  passenger_enabled on;
  passenger_app_env production;

  location /cable {
    passenger_app_group_name myapp_websocket;
    passenger_force_max_concurrent_requests_per_process 0;
  }

  # Allow uploads up to 100MB in size
  client_max_body_size 100m;

  location ~ ^/(assets|packs) {
    expires max;
    gzip_static on;
  }
}

Reload NGINX

sudo service nginx reload

Download App from Github:

cd /var
sudo mkdir www
cd www
sudo mkdir myapp
chown deploy: /var/www/myapp
cd myapp
git clone git@github.com:overdrivemachines/myapp.git code
cd code
bundle config set --local deployment 'true'
bundle config set --local without 'development test'
bundle install
chmod 700 config db
chmod 600 config/database.yml
# TODO: chmod for config/master.key and config/credentials.yml.enc

Make sure to copy master.key file from the dev computer to the production server.

bundle exec rake assets:precompile db:migrate RAILS_ENV=production
sudo service nginx restart

Log Files

They are located at:

  • Access Log: /var/log/nginx/access.log
  • Error Log: /var/log/nginx/error.log

Install PostgreSQL

sudo apt-get install postgresql postgresql-contrib libpq-dev

Create Databse User deploy:

sudo su - postgres
createuser --pwprompt deploy

Create Database:

createdb -O deploy myapp

Connect to the Database:

psql -U deploy -W -h 127.0.0.1 -d myapp

Create a MySQL Database

sudo apt-get install mysql-server mysql-client libmysqlclient-dev
sudo mysql_secure_installation
# Open the MySQL CLI to create the user and database
mysql -u root -p

If the above gives you an error, exectute the following and replace 'Password!' with your password:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Password!';

SSL Certificate

Install Certbot

sudo snap install core; sudo snap refresh core
sudo apt remove certbot
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

Allowing HTTPS Through the Firewall

sudo ufw allow OpenSSH
sudo ufw delete allow 'Nginx HTTP'
sudo ufw allow 'Nginx Full'
ufw enable
sudo ufw status
systemctl status nginx

Obtain SSL Certificate

sudo certbot --nginx -d example.com -d www.example.com

Certbot Autorenewal

sudo systemctl status snap.certbot.renew.service
sudo certbot renew --dry-run

References

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