Skip to content

Instantly share code, notes, and snippets.

@pakMann
Last active April 14, 2023 16:28
Show Gist options
  • Select an option

  • Save pakMann/f10e45c7b948491bb7e2 to your computer and use it in GitHub Desktop.

Select an option

Save pakMann/f10e45c7b948491bb7e2 to your computer and use it in GitHub Desktop.
Ubuntu 14.04 | Mina | rbenv | PostgreSQL | redis | puma | logrotate
/path/to/log/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
copytruncate
su [user] [group]
}
# if not have SSH key
#
root@[hostname]:~# ssh-keygen -t rsa
# installing libs
root@[hostname]:~# apt-get update
root@[hostname]:~# apt-get install sudo nano xclip
root@[hostname]:~# sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 40976EAF437D05B5
root@[hostname]:~# sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32
root@[hostname]:~# apt-get update
root@[hostname]:~# apt-get upgrade -y
root@[hostname]:~# apt-get install -y git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libffi-dev libyaml-dev libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties nodejs postgresql postgresql-contrib libpq-dev nginx imagemagick jpegoptim optipng
# create new user
#
root@[hostname]:~# adduser deployer
root@[hostname]:~# addgroup deployer sudo
root@[hostname]:~# su deployer
deployer@[hostname]:/root$ cd
$ ssh-copy-id -i ~/.ssh/id_rsa.pub deployer@[ipaddress]
root@[hostname]:~# visudo
# at the bottom of file add this line:
# ------------------------------------
# deployer ALL=NOPASSWD: ALL
# disable root login
#
root@[hostname]:~# nano /etc/ssh/sshd_config
# edit following lines
# --------------------
# Port 4444
# PermitRootLogin no
root@[hostname]:~# service ssh restart
# install rbenv, ruby-build, bundler
#
deployer@[hostname]:~$ git clone git://github.com/sstephenson/rbenv.git .rbenv
deployer@[hostname]:~$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
deployer@[hostname]:~$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
deployer@[hostname]:~$ exec $SHELL
deployer@[hostname]:~$ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
deployer@[hostname]:~$ echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
deployer@[hostname]:~$ exec $SHELL
deployer@[hostname]:~$ rbenv install 2.6.5
deployer@[hostname]:~$ rbenv global 2.6.5
deployer@[hostname]:~$ echo "gem: --no-document" > ~/.gemrc
deployer@[hostname]:~$ gem install bundler
# when using above method, if you want to upgrade ruby-build list, use following steps:
deployer@[hostname]:~$ cd .rbenv/plugins/ruby-build && git pull
# create db & dbuser
#
deployer@[hostname]:~$ sudo -u postgres psql postgres
postgres=# CREATE USER [dbuser] WITH PASSWORD '[dbpassword]';
postgres=# CREATE DATABASE [dbname];
postgres=# GRANT ALL PRIVILEGES ON DATABASE [dbname] TO [dbuser];
postgres=# \q
# generate ssh key for deployer user
#
deployer@[hostname]:~$ ssh-keygen -t rsa -C "deployer@[ipaddress]"
# add bitbucket to known hosts
#
ssh -T git@bitbucket.org
# install redis-server
#
deployer@[hostname]:~$ sudo apt-get install tcl8.6
deployer@[hostname]:~$ wget http://download.redis.io/releases/redis-stable.tar.gz
deployer@[hostname]:~$ tar xzf redis-stable.tar.gz
deployer@[hostname]:~$ cd redis-stable
deployer@[hostname]:~$ make
deployer@[hostname]:~$ make test
deployer@[hostname]:~$ sudo make install
deployer@[hostname]:~$ cd utils
deployer@[hostname]:~$ sudo ./install_server.sh
deployer@[hostname]:~$ sudo service redis_6379 start
deployer@[hostname]:~$ sudo update-rc.d redis_6379 defaults
upstream [env] {
server unix:/home/deployer/apps/[appname]/shared/tmp/sockets/puma.sock;
}
server {
listen 80 default_server;
client_max_body_size 4G;
keepalive_timeout 10;
error_page 500 502 504 /500.html;
error_page 503 @503;
server_name [domains here];
root /home/deployer/apps/[appname]/current/public;
try_files $uri/index.html $uri @[env];
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
if (-f $request_filename) {
break;
}
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
if (!-f $request_filename) {
proxy_pass http://[env];
break;
}
}
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
location @503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
}
$ scp -P <port> bin/* deployer@[hostname]:/home/deployer/apps/<appname>/shared/bin/
$ scp -P <port> config/boot.rb deployer@<hostname>:/home/deployer/apps/<appname>/shared/config/
# gem 'mina'
# gem 'mina-puma', require: false
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/rbenv'
require 'mina/puma'
case ENV['to']
when 'staging'
set :domain, '[ipaddress]'
set :branch, 'staging'
else
set :domain, '[ipaddress]'
set :branch, 'production'
end
set :deploy_to, '/home/deployer/apps/[appname]'
set :repository, '[gitrepo]'
# set :branch, 'master'
set :rails_env, ENV['to']
set :shared_paths, ['config/database.yml', 'config/puma.rb', 'log', '.env', 'bin', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'public/uploads']
set :user, 'deployer' # Username in the server to SSH to.
set :port, '4444'
task :environment do
invoke :'rbenv:load'
end
task setup: :environment do
queue! %[mkdir -p "#{deploy_to}/#{shared_path}/log"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/log"]
queue! %[mkdir -p "#{deploy_to}/#{shared_path}/config"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/config"]
queue! %(mkdir -p "#{deploy_to}/#{shared_path}/tmp/sockets")
queue! %(chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/tmp/sockets")
queue! %(mkdir -p "#{deploy_to}/#{shared_path}/tmp/cache")
queue! %(chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/tmp/cache")
queue! %(mkdir -p "#{deploy_to}/#{shared_path}/tmp/pids")
queue! %(chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/tmp/pids")
queue! %(mkdir -p "#{deploy_to}/#{shared_path}/public/uploads")
queue! %(chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/public/uploads")
queue! %(mkdir -p "#{deploy_to}/#{shared_path}/vendor/bundle")
queue! %(chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/vendor/bundle")
queue! %(mkdir -p "#{deploy_to}/#{shared_path}/public/system")
queue! %(chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/public/system")
queue! %[touch "#{deploy_to}/#{shared_path}/config/database.yml"]
queue! %[touch "#{deploy_to}/#{shared_path}/.env"]
queue! %[touch "#{deploy_to}/#{shared_path}/config/puma.rb"]
queue! %(sudo ln -nfs "#{deploy_to}/#{current_path}/#{ENV['to']}.conf" "/etc/nginx/sites-enabled/#{ENV['to']}.conf")
queue! %(sudo service nginx reload)
end
task deploy: :environment do
deploy do
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
invoke :'bundle:install'
invoke :'rails:db_migrate'
invoke :'rails:assets_precompile'
invoke :'deploy:cleanup'
to :launch do
# invoke :'puma:phased_restart'
invoke :'puma:restart'
end
end
end
@pakMann
Copy link
Copy Markdown
Author

pakMann commented Jul 27, 2017

@pakMann
Copy link
Copy Markdown
Author

pakMann commented Jul 28, 2017

@prabukey
Copy link
Copy Markdown

location ~* ^.+.(?:jpg|jpeg|gif|png|ico|cur|gz|swf|zip|rar|doc|xls|exe|pdf|ppt|txt|tar|mp4|webm|ogv)$ {
root /mnt/d/work/work/logicom/public/;
expires 30d;
access_log off;
add_header Cache-Control "public";
gzip_static on;
break;
}

location ~ ^/(assets)/ {
gzip_static on;
}

@prabukey
Copy link
Copy Markdown

prabukey commented Aug 1, 2017

gzip_types text/plain
text/css
application/json
application/x-javascript
text/xml application/xml
application/xml+rss
text/javascript
application/vnd.ms-fontobject
application/x-font-ttf
font/opentype
image/svg+xml
image/x-icon
image/png
image/tiff
image/vnd.wap.wbmp
image/x-icon
image/x-jng
image/x-ms-bmp
image/svg+xml
image/webp;

@prabukey
Copy link
Copy Markdown

prabukey commented Aug 1, 2017

location ~* ^.+.(?:jpg|jpeg|gif|png|ico|cur|gz|swf|zip|rar|doc|xls|exe|pdf|ppt|txt|tar|mp4|webm|ogv)$ {
#root /mnt/d/work/work/logicom/public/;
expires max;
access_log off;
add_header Pragma public;

  add_header Cache-Control "public";
  etag on;
  break;

}

location ~* .(?:css|js)$ {
expires 1y;
access_log off;
add_header Cache-Control "public";
gzip_static on;
}

location ~ ^/(assets)/ {
gzip_static on;
}

location ~ ^/(images)/ {
image_filter_jpeg_quality 65;
image_filter_buffer 20M;
image_filter_interlace on;
#image_filter_webp_quality 80;
}

location ~ ^/(uploads)/ {
image_filter_jpeg_quality 65;
image_filter_buffer 20M;
image_filter_interlace on;
#image_filter_webp_quality 80;
}

@pakMann
Copy link
Copy Markdown
Author

pakMann commented Oct 15, 2019

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