Skip to content

Instantly share code, notes, and snippets.

@PelagicDev
Forked from jtadeulopes/server.md
Last active June 25, 2017 03:01
Show Gist options
  • Save PelagicDev/9d5afe3b7c646cafb12b2d80a2753e6d to your computer and use it in GitHub Desktop.
Save PelagicDev/9d5afe3b7c646cafb12b2d80a2753e6d 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 ntpd ntp.ubuntu.com
date

Create new user

sudo adduser deploy
sudo adduser deploy sudo

Edit /etc/sudoers

%sudo ALL=(ALL:ALL) ALL
deploy 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 nodejs npm imagemagick libpq-dev htop curl

Ruby

sudo apt-get update
sudo apt-get install curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev

From Rbenv:

cd
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.3.1
rbenv global 2.3.1
ruby -v

echo 'gem: --no-document' >> ~/.gemrc

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

Nginx

Install Nginx core

sudo apt-get update
sudo apt-get install nginx

Set SSH port to custom port:

sudo vi /etc/ssh/sshd_config

Allow web traffic & access via custom SSH port:

sudo ufw allow 'Nginx HTTP'
sudo ufw allow [SSH_PORT]

Nginx settings (Not modified)

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

Add SystemD script

TODO: the below is for Upstart; add SystemD info here:
cd /etc/init
Download the file https://gist.github.com/jtadeulopes/7237115

Now, you can run:

sudo (start|restart|stop) nginx

PostgreSQL

Install Latest version

sudo add-apt-repository "deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main"
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-9.6

Add app user & password

Redis

Use this guide to install Redis

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment