Skip to content

Instantly share code, notes, and snippets.

@1v
Forked from ancap/gist:7385992
Last active June 6, 2017 01:27
Show Gist options
  • Save 1v/10272479 to your computer and use it in GitHub Desktop.
Save 1v/10272479 to your computer and use it in GitHub Desktop.
Ruby on Rails installation on Ubuntu 12.04 LTS + Nginx + Puma

Ruby on Rails installation on Ubuntu 12.04 LTS + Nginx + Puma

Adding new user

Create new user:

sudo useradd -G sudo -m -s /bin/bash luna

blog.lunarlogic.io

Change password:

sudo passwd luna

Login:

su - luna

Installing Ruby, Ruby on Rails and Puma

Install cURL:

sudo apt-get install curl

Install RVM:

curl -L https://get.rvm.io | bash -s stable --ruby --autolibs=enable --auto-dotfiles

Logs. Latest stable Ruby will be automaticly installed. To start using RVM you need to run:

source /home/luna/.rvm/scripts/rvm

Check:

$ ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]

If Ruby not available then run:

rvm install ruby

Logs.

For upgrade run:

rvm upgrade ruby

Logs.

If ruby still not available, then run:

$ rvm list

rvm rubies

=* ruby-2.1.1 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

If you get similar result, then run:

$ echo $PATH
/home/luna/.rvm/gems/ruby-2.1.1/bin:/home/luna/.rvm/gems/ruby-2.1.1@global/bin:/home/luna/.rvm/rubies/ruby-2.1.1/bin:/home/luna/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

If there is no /home/luna/.rvm/gems/ruby-2.1.1/bin:/home/luna/.rvm/gems/ruby-2.1.1@global/bin:/home/luna/.rvm/rubies/ruby-2.1.1/bin then you don't have ruby alias. This command should fix it:

$ rvm fix-permissions
RVM reloaded!
Fixing user for luna - this might take few long minutes...
Finished fixing.

Or:

rvm use 2.1.2

And if still not:

rvm alias create default 2.1.2
gem install rails -V

Most of the time will take:

Installing ri documentation for rails-4.0.1

Check:

$ rails -v
Rails 4.0.1

Create folders for all projects:

mkdir /home/luna/www

For "example project":

mkdir /home/luna/www/myapp

Browse to myapp:

cd /home/luna/www/myapp

Create Rails files:

rails new . -d mysql

Edit Gemfile:

nano Gemfile

Add to the bottom:

gem 'puma'

And run:

bundle install

to install missing packages (gems).

Rails required JavaScript runtime:

sudo apt-get install nodejs

source

Open config/secrets.yml and replace

production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

with

production:
  secret_key_base: rand(36**128).to_s(36)

Installing and configuring Nginx

All commands from this link. The NGINX version in the Ubuntu repo is quite old (ie. 1.2.6), you could install newer version by adding the official nginx.org repo:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys ABF5BD827BD9BF62

Edit /etc/apt/sources.list:

sudo nano /etc/apt/sources.list

Add to the bottom:

deb http://nginx.org/packages/ubuntu/ precise nginx

Make sure Nginx uninstalled:

sudo apt-get purge nginx*

Then install:

sudo apt-get update
sudo apt-get install nginx

Check:

$ sudo nginx -V
nginx version: nginx/1.4.3
TLS SNI support enabled
configure arguments: 
--prefix=/etc/nginx 
--sbin-path=/usr/sbin/nginx 
--conf-path=/etc/nginx/nginx.conf 
--error-log-path=/var/log/nginx/error.log 
--http-log-path=/var/log/nginx/access.log 
--pid-path=/var/run/nginx.pid 
--lock-path=/var/run/nginx.lock 
--http-client-body-temp-path=/var/cache/nginx/client_temp 
--http-proxy-temp-path=/var/cache/nginx/proxy_temp 
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp 
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp 
--http-scgi-temp-path=/var/cache/nginx/scgi_temp 
--user=nginx 
--group=nginx 
--with-http_ssl_module 
--with-http_realip_module 
--with-http_addition_module 
--with-http_sub_module 
--with-http_dav_module 
--with-http_flv_module 
--with-http_mp4_module 
--with-http_gunzip_module 
--with-http_gzip_static_module
--with-http_random_index_module 
--with-http_secure_link_module 
--with-http_stub_status_module 
--with-mail 
--with-mail_ssl_module 
--with-file-aio 
--with-http_spdy_module 
--with-cc-opt='-g -O2 -fstack-protector 
--param=ssp-buffer-size=4 -Wformat -Wformat-security -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2'
--with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,--as-needed' --with-ipv6

Gzip module

Open Nginx global config:

sudo nano /etc/nginx/nginx.conf

Uncomment line (for gzip compression):

gzip  on;

Then start service:

sudo service nginx start

Open in browser http://192.168.1.43/ (change to your server IP) there should be Nginx congrats page.

Open new config file:

sudo nano /etc/nginx/conf.d/myapp.conf

And paste this:

upstream myapp {
  server              unix:///tmp/myapp.sock;
}

server {
  listen              *:80;
  server_name         site.com;

  access_log          /var/log/nginx/my_app-access.log;

  location /favicon.ico {
    root              /home/luna/www/public/favicon.ico;
    gzip_static       on;
    expires           max;
    add_header        Cache-Control public;
  }

  location / {
    root              /home/luna/www/myapp/public;
    try_files         $uri @app;
    gzip_static       on;
    expires           max;
    add_header        Cache-Control public;
  }

  location @app {
    proxy_pass        http://myapp;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Forwarded-Proto $scheme;
    proxy_set_header  Host $http_host;
    proxy_redirect    off;
    proxy_next_upstream error timeout invalid_header http_502;
    proxy_max_temp_file_size 0;
  }
}

source

Save and exit.

Restart Nginx:

sudo service nginx restart

Make sure you are still in /home/luna/www/myapp/ folder and run:

bundle exec puma -e development -b unix:///tmp/myapp.sock

Output should be:

Puma starting in single mode...
* Version 2.6.0, codename: Pantsuit Party
* Min threads: 0, max threads: 16
* Environment: development
* Listening on unix:///tmp/myapp.sock
Use Ctrl-C to stop

Add to your desktop's hosts file line:

192.168.1.43 site.com

Open in browser http://site.com/ there should be Rails congrats page, and in console should be new line:

* Listening on unix:///tmp/myapp.sock
Use Ctrl-C to stop
192.168.1.36 - - [10/Nov/2013 15:18:30] "GET / HTTP/1.0" 200 - 0.2573

If you see 403 error page, then something wronge with Nginx config file, if you see 502 error then something wronge with Rails or Ruby.

In the end, if all working, run app as daemon:

bundle exec puma -e development -db unix:///tmp/myapp.sock

Show all daemons:

ps aux | grep puma

Output:

luna      2729  0.8  5.2 406332 53036 ?        Sl   15:36   0:00 /home/luna/.rvm/gems/ruby-2.0.0-p247/bin/puma                                                                                                                
luna      2737  0.0  0.0  10636   916 pts/0    S+   15:37   0:00 grep puma

Kill process:

sudo kill -s SIGTERM 2729

If not working:

sudo kill -s SIGKILL 2729

Restart process:

sudo kill -s SIGUSR2 2729

To generate assets for production:

rake assets:precompile --trace RAILS_ENV=production

More info about assets.

Then run production:

bundle exec puma -e production -b unix:///tmp/myapp.sock

In page source code you will see:

<link data-turbolinks-track="true" href="/assets/application-58c7c0e35a67f189e19b8c485930e614.css" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/assets/application-67fb624685eb5c3c2d8351122ac368a1.js"></script>

Check if gzipped files are requested https://gist.github.com/ancap/7412407.

See also

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