Skip to content

Instantly share code, notes, and snippets.

@jtadeulopes
Last active March 29, 2024 10:23
Show Gist options
  • Save jtadeulopes/7271019 to your computer and use it in GitHub Desktop.
Save jtadeulopes/7271019 to your computer and use it in GitHub Desktop.
Server setup with ubuntu, nginx and puma for rails app.

Update and upgrade the system

sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade && sudo apt-get autoremove
sudo reboot

Configure timezone

sudo dpkg-reconfigure tzdata
sudo apt-get install -y ntp
sudo ntpdate ntp.ubuntu.com
date

Create new user

sudo adduser ubuntu
sudo adduser ubuntu sudo

Edit /etc/sudoers

%sudo ALL=(ALL:ALL) ALL
ubuntu ALL=NOPASSWD: ALL

Environment variables

Edit /etc/profile

export PATH=/opt/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export RAILS_ENV=production
export LC_ALL=en_US.UTF-8

Edit /etc/environment

PATH=/opt/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
RAILS_ENV=production
LC_ALL=en_US.UTF-8

Edit /etc/sudoers

Defaults secure_path="/opt/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

Some libraries for Mysql, ImageMagick, Node, etc

sudo apt-get install -y libmagickwand-dev libmysqlclient15-dev nodejs npm imagemagick libpq-dev htop curl

Ruby

sudo apt-get install -y libyaml-dev libssl-dev libreadline-dev libxml2-dev libxslt1-dev libffi-dev build-essential

Compiled:

wget http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.4.tar.gz
tar xvfz ruby-2.1.4.tar.gz
cd ruby-2.1.4/
./configure --prefix=/opt/ruby --disable-install-rdoc
make
sudo make install
sudo gem install bundler

Package:

wget -q -O - http://apt.hellobits.com/hellobits.key | sudo apt-key add -
echo 'deb [arch=amd64] http://apt.hellobits.com/ trusty main' | sudo tee /etc/apt/sources.list.d/hellobits.list
sudo apt-get update
sudo apt-get install -y ruby-2.3
sudo gem install bundler

GIT

sudo apt-get install -y git

VIM

Syntax highlighting for Nginx in VIM

sudo mkdir /etc/vim/syntax
sudo curl http://www.vim.org/scripts/download_script.php?src_id=14376 -o /etc/vim/syntax/nginx.vim
cd /etc/vim
Download de the file https://gist.github.com/jtadeulopes/8978872

For more detail, see http://zduck.com/2012/syntax-highlighting-for-nginx-in-vim

Global settings

Edit /etc/vim/vimrc and add the lines below

set number
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab

OpenSSL

cd /opt
sudo wget http://www.openssl.org/source/openssl-1.0.2g.tar.gz
sudo tar xvfz openssl-1.0.2g.tar.gz
sudo mv openssl-1.0.2g openssl
cd openssl/
sudo ./config
sudo make
sudo make install

Nginx

wget http://nginx.org/download/nginx-1.9.9.tar.gz
tar xvfz nginx-1.9.9.tar.gz
cd nginx-1.9.9/
./configure --prefix=/opt/nginx --with-http_ssl_module --with-openssl=/opt/openssl
sudo make
sudo make install

Nginx settings

cd /opt/nginx/conf
sudo rm nginx.conf
Download the file https://gist.github.com/jtadeulopes/7237669

sudo mkdir /var/log/nginx

sudo mkdir sites-enabled
cd sites-enabled
Download the file https://gist.github.com/jtadeulopes/7237731
Or with SSL support https://gist.github.com/jtadeulopes/9189120

Upstart script

cd /etc/init
Download the file https://gist.github.com/jtadeulopes/7237115

Now, you can run:

sudo (start|restart|stop) nginx

Deploy

Add user

sudo adduser deploy

SSH keys

sudo -iu deploy
ssh-keygen -t rsa

Add authorized keys

cd
vim .ssh/authorized_keys

Copy and paste public keys
exit

Create folder to deploy the app

sudo mkdir /var/www
sudo chown deploy:deploy -c -R /var/www

Capistrano

Capfile https://gist.github.com/jtadeulopes/7249948

deploy.rb https://gist.github.com/jtadeulopes/7249921

Puma Upstart script

For each project, you must create the puma upstart file

cd /etc/init
Download the file https://gist.github.com/jtadeulopes/7254953

With the upstart for each project, you can run:

sudo (start|restart|stop) project

Puma config

cd my/project/path/config
Download the file https://gist.github.com/jtadeulopes/7265503

Monit

sudo apt-get install -y monit

For each project, you must create the monit confg file

cd /etc/monit/conf.d
Download the file https://gist.github.com/jtadeulopes/7270279

Edit /etc/monit/monitrc

set daemon 60

Restart monit

sudo /etc/init.d/monit restart

Bower

sudo ln -s /usr/bin/nodejs /usr/bin/node
sudo npm install -g bower
@wellington1993
Copy link

wellington1993 commented Jan 8, 2018

Legal!

@fakefarm
Copy link

fakefarm commented Feb 6, 2018

This is great work! I noticed you have a couple options for installing Ruby. Might consider adding a third version, using rbenv.

git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

rbenv install 2.5.0
rbenv global 2.5.0
ruby -v

I'm in the process of trying to figure out how to use the new Rails 5.2 credentials instead of secrets. If I figure it out, I'll post a comment if no else beats me to it 😄

Dave

@chricloud9
Copy link

Did you figure it out @fakefarm?

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