Friday, October 3, 2014

My Attempt of Installing Varnish with Nginx + WP Backend in Ubuntu 14.04

This is my attempt of installing Varnish before a Nginx web server hosting multiple Wordpress sites in Ubuntu 14.04.

Prerequisites

Before the installation, the server already had:
Ubuntu 14.04 with Nginx and Wordpress installed and configured.


First Step

The Varnish package can be obtained in repo.varnish-cache.org/ubuntu/

This is what I do to get the source:

$ sudo curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -

Then, I added the Varnish package into list of sources:

$ sudo nano /etc/apt/sources.list

Inside the file, I added this line:

deb http://repo.varnish-cache.org/ubuntu/ trusty varnish-3.0

Then, I install them:

$ sudo apt-get update
$ sudo apt-get install varnish libvarnish-dev


Step two - Configure Varnish

Now, I configure Varnish to listen to port 80 (web):

$ sudo nano /etc/default/varnish

I find this block of configurations and modify it to listen to port 80:

DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"

Then, I modify the default.vcl to look for webserver content:

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

backend mysite2 {
    .host = "127.0.0.1";
    .port = "8800";
}

backend mysite3 {
    .host = "127.0.0.1";
    .port = "4000";
}

sub vcl_recv {
        if (req.http.host ~ "mycoolwebsite1.com") {
                set req.backend = mysite2;
        } else if (req.http.host ~ "notthatcoolwebsite.com") {
                set req.backend = mysite3;
        } else {
                set req.backend = default;
        }

        if (!(req.url ~ "wp-(login|admin)")) {
                unset req.http.cookie;
        }
}

sub vcl_fetch {
        if (!(req.url ~ "wp-(login|admin)")) {
                unset beresp.http.set-cookie;
        }
}

My Varnish installation is configured to listen to backend in the localhost with port 8080, 8800 and 4000 respectively.

The requests will be redirected accordingly, note that Wordpress cookies in login and admin are not cached.

Then, I configure my Nginx webserver for it to work correctly.


Step Three - Configure Nginx

I have already have a Nginx webserver running, now I have to configure it.

Firstly, I edit the vhost file:

$ sudo nano /etc/nginx/sites-enabled/blog-farms

I added this to my server blocks:

server {
    # listen [::]:80 ipv6only=off;
    listen 127.0.0.1:8080 ipv6only=off;
    server_name mainwebsite.com *.mainwebsite.com;

 ... blah blah blah ...

Then, I added configurations for other backend as well... :)

server {
    # listen [::]:80 ipv6only=off;
    listen 127.0.0.1:8800 ipv6only=off;
    server_name mycoolwebsite1.com *.mycoolwebsite1.com;

 ... another server config ...

Another one:

server {
    # listen [::]:80 ipv6only=off;
    listen 127.0.0.1:4000 ipv6only=off;
    server_name notthatcoolwebsite.com *.notthatcoolwebsite.com;

 ... yet another server config ...

Then, I restart all 'da servers!

$ sudo service nginx configtest
$ sudo service nginx restart
$ sudo service varnish restart

Now it is up and running!

I can check the Varnish stats by typing this:

$ varnishstat

This will show:

$ varnishstat
0+00:22:49
Hitrate ratio:        1        1        1
Hitrate avg:     0.0000   0.0000   0.0000

           1         0.00         0.00 client_conn - Client connections accepted
           1         0.00         0.00 client_req - Client requests received
           1         0.00         0.00 cache_miss - Cache misses
           1         0.00         0.00 backend_conn - Backend conn. success
           1         0.00         0.00 backend_recycle - Backend conn. recycles
           1         0.00         0.00 fetch_chunked - Fetch chunked

... a long long page ( ◕ ω ◕ ) ...

That's it!

Thanks for reading! (If there is any visitor around.)

Friday, March 7, 2014

Migrate from RVM to Rbenv and install Ruby on Rails in Ubuntu Saucy 13.10

This blog post shows how to migrate from RVM to Rbenv, then install the latest Ruby on Rails version. (By the time of writing, it was Ruby 2.1.1 and Rails 4.1.0.rc1...)

Removing RVM

$ rvm implode
Are you SURE you wish for rvm to implode?
This will recursively remove /home/joseph/.rvm and other rvm traces?
(anything other than 'yes' will cancel) > yes
Removing rvm-shipped binaries (rvm-prompt, rvm, rvm-sudo rvm-shell and rvm-auto-ruby)
Removing rvm wrappers in /home/joseph/.rvm/bin
Hai! Removing /home/joseph/.rvm
/home/joseph/.rvm has been removed.

Note you may need to manually remove /etc/rvmrc and ~/.rvmrc if they exist still.
Please check all .bashrc .bash_profile .profile and .zshrc for RVM source lines and delete or comment out if this was a Per-User installation.
Also make sure to remove `rvm` group if this was a system installation.
Finally it might help to relogin / restart if you want to have fresh environment (like for installing RVM again).


If you cancel it, you will receive the following message:

Psychologist intervened, cancelling implosion, crisis avoided :)

After that, please remove this line in bash profile:

PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting

For Ubuntu 13.10, the bash profile is saved in the .bashrc file, you can open it using any text editor:

$ nano ~/.bashrc


Installing Rbenv

Make sure you have all the dependencies ready. If not, make sure you install using

$ sudo apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev libgdbm-dev ncurses-dev automake libtool bison subversion pkg-config libffi-dev

(Some of these may not be needed... correct me if I am wrong)

Then, download a copy of the rbenv official repository by running 'git clone' command:

$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv

Steps to install:

$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc

Then, download the ruby-build repository into your machine into ~/.rbenv/plugins folder.

~/.rbenv/plugins$ git clone git://github.com/sstephenson/ruby-build.git
Cloning into 'ruby-build'...
remote: Reusing existing pack: 3077, done.
remote: Total 3077 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3077/3077), 510.82 KiB | 286.00 KiB/s, done.
Resolving deltas: 100% (1404/1404), done.
Checking connectivity... done
~/.rbenv/plugins$

Now it works!

$ rbenv
rbenv 0.4.0-89-g14bc162
Usage: rbenv <command> [<args>]

Some useful rbenv commands are:
   commands    List all available rbenv commands
   local       Set or show the local application-specific Ruby version
   global      Set or show the global Ruby version
   shell       Set or show the shell-specific Ruby version
   install     Install a Ruby version using ruby-build
   uninstall   Uninstall a specific Ruby version
   rehash      Rehash rbenv shims (run this after installing executables)
   version     Show the current Ruby version and its origin
   versions    List all Ruby versions available to rbenv
   which       Display the full path to an executable
   whence      List all Ruby versions that contain the given executable

See `rbenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/sstephenson/rbenv#readme

If it is not working on your Ubuntu machine, you may need to restart terminal without login shell (which works with RVM). Then, you should be able to run 'rbenv' command.

Install Ruby

$ rbenv install 2.1.1
Downloading ruby-2.1.1.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/e57fdbb8ed56e70c43f39c79da1654b2
Installing ruby-2.1.1...
Installed ruby-2.1.1 to /home/joseph/.rbenv/versions/2.1.1

$

If you can't run 'rbenv install', you may need to install it using './install.sh' in ruby-build folder.

To set the Ruby version newly installed as global.

$ rbenv global 2.1.1
$ rbenv rehash

Restart your terminal (or open a new one :p)

$ ruby -v
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-linux]
$

Now it works!

If you have local version of Ruby installed, it may show the local Ruby version after rehash.

Install Rails

$ gem install rails -v 4.1.0.rc1 --no-ri --no-rdoc
Fetching: atomic-1.1.15.gem (100%)
Building native extensions.  This could take a while...
Successfully installed atomic-1.1.15
Fetching: thread_safe-0.2.0.gem (100%)
Successfully installed thread_safe-0.2.0
Fetching: minitest-5.3.0.gem (100%)
Successfully installed minitest-5.3.0
Fetching: tzinfo-1.1.0.gem (100%)
Successfully installed tzinfo-1.1.0
Fetching: i18n-0.6.9.gem (100%)
Successfully installed i18n-0.6.9
Fetching: activesupport-4.1.0.rc1.gem (100%)
Successfully installed activesupport-4.1.0.rc1
Fetching: erubis-2.7.0.gem (100%)
Successfully installed erubis-2.7.0
Fetching: builder-3.2.2.gem (100%)
Successfully installed builder-3.2.2
Fetching: actionview-4.1.0.rc1.gem (100%)
Successfully installed actionview-4.1.0.rc1
Fetching: rack-1.5.2.gem (100%)
Successfully installed rack-1.5.2
Fetching: rack-test-0.6.2.gem (100%)
Successfully installed rack-test-0.6.2
Fetching: actionpack-4.1.0.rc1.gem (100%)
Successfully installed actionpack-4.1.0.rc1
Fetching: activemodel-4.1.0.rc1.gem (100%)
Successfully installed activemodel-4.1.0.rc1
Fetching: arel-5.0.0.gem (100%)
Successfully installed arel-5.0.0
Fetching: activerecord-4.1.0.rc1.gem (100%)
Successfully installed activerecord-4.1.0.rc1
Fetching: mime-types-1.25.1.gem (100%)
Successfully installed mime-types-1.25.1
Fetching: polyglot-0.3.4.gem (100%)
Successfully installed polyglot-0.3.4
Fetching: treetop-1.4.15.gem (100%)
Successfully installed treetop-1.4.15
Fetching: mail-2.5.4.gem (100%)
Successfully installed mail-2.5.4
Fetching: actionmailer-4.1.0.rc1.gem (100%)
Successfully installed actionmailer-4.1.0.rc1
Fetching: thor-0.18.1.gem (100%)
Successfully installed thor-0.18.1
Fetching: railties-4.1.0.rc1.gem (100%)
Successfully installed railties-4.1.0.rc1
Fetching: bundler-1.6.0.rc.gem (100%)
Successfully installed bundler-1.6.0.rc
Fetching: tilt-1.4.1.gem (100%)
Successfully installed tilt-1.4.1
Fetching: multi_json-1.8.4.gem (100%)
Successfully installed multi_json-1.8.4
Fetching: hike-1.2.3.gem (100%)
Successfully installed hike-1.2.3
Fetching: sprockets-2.11.0.gem (100%)
Successfully installed sprockets-2.11.0
Fetching: sprockets-rails-2.0.1.gem (100%)
Successfully installed sprockets-rails-2.0.1
Fetching: rails-4.1.0.rc1.gem (100%)
Successfully installed rails-4.1.0.rc1
29 gems installed
$

Note: if you don't install Rails with --no-ri --no-rdoc options, you may face the following conflicts:

rdoc's executable "rdoc" conflicts with /home/joseph/.rbenv/versions/2.1.1/bin/rdoc
Overwrite the executable? [yN]  y
rdoc's executable "ri" conflicts with /home/joseph/.rbenv/versions/2.1.1/bin/ri
Overwrite the executable? [yN]  y
Depending on your version of ruby, you may need to install ruby rdoc/ri data:

<= 1.8.6 : unsupported
 = 1.8.7 : gem install rdoc-data; rdoc-data --install
 = 1.9.1 : gem install rdoc-data; rdoc-data --install
>= 1.9.2 : nothing to do! Yay!

I will just overwrite it.

Well done! Now you have migrated from RVM to rbenv, and have Ruby 2.1.1 and Rails 4.1.0.rc1 installed!

Thank you for reading! :D