Day 6.5 - Configuring Nginx to Serve Rails Apps
This post is a part of a mini-series!
Day 6
1) Installing link
2) Test a Rails App! link
3) Configuring Webserver (You Are Here)
4) Configuring MySql link
ref: http://wiki.rubyonrails.org/deployment/nginx-thin
Ok, let's assume that you've setup nginx on your computer, and installed rails, and even have PHP configured maybe. Now you want to work with rails via nginx. Well, that means you need to install 'thin' to be the ruby server. We'll do that in this blog post.
Prerequisites/assumptions:
1) nginx installed
2) ruby, gem, and rails are installed
3) You have a debian based system (I hear ubuntu is similar enough)
:: Install Thin
Thin is a gem, so it's easy to download the latest version onto your system.
Now have thin autoboot at startup:
Now create a configuration script that will... tell thin how many processes to spawn, where your app is, etc.
Here's the "key: that I referenced:
Here's my own actual implimentation:
That command will create a new yml file visible from [inline]cat /etc/thin/myapp.yml[/inline].
Then start those thin processes which handle the guts of rails:
:: Configure nginx (yes, again...)
To your nginx configuration, once again, you must make changes, these:
(this content should be involved in your /usr/local/nginx/conf/nginx.conf file)
Now my only problem is, how do I get those thin processes to auto start at boot? To be concluded...
Ok, once you've got that setup, you should be able to make a new rails project in [inline]/home/kentos/dev/rails/mt[/inline] (it's based on the 'root' directive you give in your nginx.conf
Then navigate to [inline]http://localhost[/inline] and you should see your newly created and functional rails app. If not, you went wrong somewhere, plz share a comment about that.
Clarifications:
I think it's important to point out how all of these technologies are working together in order to "just work."
To see a rails app in a web browser, your server needs, at bare minimum:
1) A web server (duh, right?)
2) Ruby (duh, right?)
3) A ruby server (thin is like our... PHP for ruby. Get that? I'm not totally sure, but that's how I think about thin, it allows the magic of rails to work.)
4) Gem (duh) and the gem named rails (duh)
OK, so when you've got all that going, and you navigate to localhost and see a beautiful starter page, you might wonder the following questions:
A. How does it know what rails project to show?
- nginx.conf
- HTTP Host header (eg. example.com vs example2.com can direct elsewhere, even if same IP)
B. How does it know what ruby server to use?
- nginx.conf
C. How does it know what ruby version to use?
-Thin (whatever ruby environment was active at the time of [inline]thin install[/inline]) (untested, brb)
C. How does it know what rails version to use
-Gemfile
D. How does it know what database system to use?
-Gemfile
-database.yml
So you can see that 3 different technologies are configured, ever so delicately, together to bring your end user a slick rails experience.
Day 6
1) Installing link
2) Test a Rails App! link
3) Configuring Webserver (You Are Here)
4) Configuring MySql link
ref: http://wiki.rubyonrails.org/deployment/nginx-thin
Ok, let's assume that you've setup nginx on your computer, and installed rails, and even have PHP configured maybe. Now you want to work with rails via nginx. Well, that means you need to install 'thin' to be the ruby server. We'll do that in this blog post.
Prerequisites/assumptions:
1) nginx installed
2) ruby, gem, and rails are installed
3) You have a debian based system (I hear ubuntu is similar enough)
:: Install Thin
Thin is a gem, so it's easy to download the latest version onto your system.
Code:
$ rvm 1.9.2 ~~ thin is ok to install in 1.9.2, but webBrick won't work... $ gem install thin $ thin install
Now have thin autoboot at startup:
Code:
$ /usr/sbin/update-rc.d -f thin defaults
Now create a configuration script that will... tell thin how many processes to spawn, where your app is, etc.
Here's the "key: that I referenced:
Code:
>> KEY >> sudo thin config -C /etc/thin/<config-name>.yml -c <rails-app-root-path> --servers <number-of-threads> -e <environment>
Code:
$ thin config -C /etc/thin/myapp.yml -c /home/kentos/dev/rails/mt --servers 5 --socket /tmp/thin.myapp.sock -e development
Then start those thin processes which handle the guts of rails:
Code:
$ /etc/init.d/thin start
:: Configure nginx (yes, again...)
To your nginx configuration, once again, you must make changes, these:
- Add information about your thin processes
- change the root location to your app
- add a bunch of declairations
- add a weird chuck of if statements
(this content should be involved in your /usr/local/nginx/conf/nginx.conf file)
Code:
user nginx; worker_processes 5; error_log /var/log/nginx.error.log; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] $request ' '"$status" $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx.access.log main; sendfile on; keepalive_timeout 65; upstream thin_cluster { # NEW ADDITION! server unix:/tmp/thin.myapp.0.sock; # The names of these depends on what server unix:/tmp/thin.myapp.1.sock; # You specify after '--socket' server unix:/tmp/thin.myapp.2.sock; # When you run 'thin config' server unix:/tmp/thin.myapp.3.sock; # And the output you get when you run server unix:/tmp/thin.myapp.4.sock; # '/etc/init.d/thin start' } server { listen 80; server_name www.myserver.com; root /home/kentos/dev/rails/mt/public; # CHANGE LOCATION! location / { proxy_set_header X-Real-IP $remote_addr; # NEW ADDITIONS! proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect false; if (-f $request_filename/index.html) { # NEW ADDITIONS! rewrite (.*) $1/index.html break; } if (-f $request_filename.html) { rewrite (.*) $1.html break; } if (!-f $request_filename) { proxy_pass http://thin_cluster; break; } #index index... # COMMENT THE INDEX LINE OUT I GUESS } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
Now my only problem is, how do I get those thin processes to auto start at boot? To be concluded...
Ok, once you've got that setup, you should be able to make a new rails project in [inline]/home/kentos/dev/rails/mt[/inline] (it's based on the 'root' directive you give in your nginx.conf
Code:
$ cd /home/kentos/dev/rails $ rails new mt $ /etc/init.d/thin restart
Clarifications:
I think it's important to point out how all of these technologies are working together in order to "just work."
To see a rails app in a web browser, your server needs, at bare minimum:
1) A web server (duh, right?)
2) Ruby (duh, right?)
3) A ruby server (thin is like our... PHP for ruby. Get that? I'm not totally sure, but that's how I think about thin, it allows the magic of rails to work.)
4) Gem (duh) and the gem named rails (duh)
OK, so when you've got all that going, and you navigate to localhost and see a beautiful starter page, you might wonder the following questions:
A. How does it know what rails project to show?
- nginx.conf
- HTTP Host header (eg. example.com vs example2.com can direct elsewhere, even if same IP)
B. How does it know what ruby server to use?
- nginx.conf
C. How does it know what ruby version to use?
-Thin (whatever ruby environment was active at the time of [inline]thin install[/inline]) (untested, brb)
C. How does it know what rails version to use
-Gemfile
D. How does it know what database system to use?
-Gemfile
-database.yml
So you can see that 3 different technologies are configured, ever so delicately, together to bring your end user a slick rails experience.
Total Comments 0