Deploying Rails applications with Passenger and Nginx to VPS (EC2) - ruby-on-rails

I've developed a couple of applications which I'm ready to deploy. To do so, I've configured Capistrano and I'm already able to run cap deploy, which runs properly. However, I'm totally lost as to how to continue from here. My setup is EC2 + Rails 3.2 + Ruby 1.9.3 + Passenger + Nginx (the one Passenger installs first time you try to start it) + Capistrano.
Until now, I just ran passenger start on my app root folder, which would start passenger on port 3000, and I would start the second app on port 3001. Now, what I need is to have this 2 apps under 2 different domains, say www.domain1.com and www.domain2.com.
How am I supposed to start the servers now? I can go to the respective current folders that Capistrano created and run something like passenger start -e production -p 3001 -d and it starts running as a daemon, but, shouldn't capistrano take care of this? All I see is that, on each deploy, it touches the restart.txt file and that forces a "soft restart", which is not enough (as far as I know) if you've changed gems. Shouldn't Capistrano be starting and stopping Passenger, not me?
How do I run the 2 apps on 2 domains? As far as I know, you can't point a domain to a port, and all I've managed to do now is to run 1 of the applications by running Passenger on port 80 with rvmsudo, but of course this only works for 1 application. After searching a bit I've found something about Nginx Virtual Servers. How do you do this? I mean, I have never touched anything specific to Nginx, just Passenger! Am I supposed to forget about Passenger and deal with Nginx as a service? How?
Thanks in advance!

I believe to start servers there's a specific cap command to start servers, but i don't know much about capistrano, just played with it a little bit before.
As for the second part, this is where nginx takes part, nginx will handle forwarding each domain to the specific port, using proxy_pass, take a look at this example
server {
server_name: example1.com;
proxy_pass: http://127.0.0.1:3000;
}
server {
server_name: example2.com;
proxy_pass: http://127.0.0.1:3001;
}

Related

Domain setting using ruby rails

I am planning to have a web application.
To do so, I studied about ruby and ruby on rails. I am using linux server from amazon clouding system.
I bought a domain from godday, and I put the IP address on DNS setting. When I run 'rails s' command, I can connect to the wep page through port 3000 in such a way that domain.com:3000. However, I cannot directly connect to domain.com. How can I my domain works without port 3000?
And Do I have to run 'rails s' every time to make the wep page work? Actually I tried to use 'rails s &' to make it run in background. But it fails. How can I make the server run even though I am not connected to the linux server?
Thank you!
usually you use rails s just in development. there are quite a few ruby web servers you can choose from for your production environment: puma, passenger or unicorn to name a few.
of course all of them have their own tutorials how to set them up. for starters, i'd go with with passenger because it's integrated with nginx and apache and easily set up.
You need to specify a port, if you don't see the port it can be either 80 (http) or 443 (https).
rails server -p 80
On linux you have to be root to bind to port less than 1000, so just append sudo in front.

Deploying Rails/Passenger via nginx

I'm having a hard time figuring out where I'm going wrong in trying to deploy a Rails app via nginx. Rails is accessible via site.com:3000 (after starting it with rails server), and site.com:80 displays the standard nginx "working, but further configuration required" page. I've spent a few hours trawling the documentation trying to figure our how to get my Rails app accessible at :80 rather than :3000, but to no avail.
I think it's most likely that I'm misunderstanding how nginx, Passenger, and Rails work together, and have therefore configured my nginx.conf incorrectly (one page I found implied that I shouldn't both be using nginx and running rails server). Any and all help is hugely appreciated.
Possibly relevant version numbers:
Rails 4.1.4
Ruby 2.1.2p95
CentOS 6.5
nginx 1.6.0
nginx.conf partial: http://pastebin.com/A3JD09pr
I'm new at this, so it turned out that a couple things were up:
I needed to put export rvmsudo_secure_path=1 in my .bashrc instead of just running it once, following up with source ~/.bashrc in the terminal. This allowed me to run "rvmsudo" commands to start on port 80 rather than the default 3000.
I had both nginx and Rails competing for port 80, so I had to stop nginx's static page server to allow that. Simple as nginx stop.

Restart only a single Rails app running on Passenger / nginx on VPS hosting many passenger apps

I am running multiple rails apps and a sinatra app under the same domain on a VPS using nginx and passenger. When I deploy code I need to restart the application process for the app that got updated. Right now I'm running service nginx stop followed by service nginx start thereby restarting all the passenger processes. It seems silly to have to restart nginx instead of a just the target passenger process. Is there a way to do such a thing?
Here's my nginx.conf file: https://gist.github.com/srt32/8535548. Thanks.
Goto the root of your Rails application and touch a tmp/restart.txt file.
touch /webapps/mycook/tmp/restart.txt
Remove restart.txt once application is restarted, not mandatory though.
Reference - http://www.modrails.com/documentation/Users%20guide%20Nginx.html#_redeploying_restarting_the_ruby_on_rails_application

How to stop rails nginx-passenger application?

I use the passenger spawned by nginx. There are many other rails applications on the server that uses passenger (each has own virtual host in nginx).
I can restart the Rails/Nginx/Passenger application like this:
touch tmp/restart.txt
How I can stop it?
This doesn't work:
touch tmp/stop.txt
touch tmp/shutdown.txt
Method 1
Remove your app's virtual host entry and restart Nginx. Phusion Passenger will no longer serve it.
Method 2
In case you want to keep your app's virtual host entry, but not actually run the app.
Set the following option and restart Nginx:
passenger_min_instances 0;
Phusion Passenger will now shut down your app if it hasn't seen traffic for a while (~10 minutes). It'll be started again if traffic comes in for that app.
With passenger_min_instances 0, you can also kill the application processes manually. Look up the PIDs with passenger-status, then run kill <PID>.
Passenger is built to auto-run at all times.
As a workaround, you can stop the webserver entirely, or stop serving the particular app that you don't want to be served by removing the virtual host entry for it.
You could also uninstall passenger if you're really desperate

Why it won't apply changes when using Nginx?

I've just switched from Apache + Passenger to Nginx + Unicorn
I used to command /etc/init.d/httpd restart after I added change.
Then all the changes used to be applied to Rails Application.
But with Nginx, even if I command service nginx restart, changes won't be applied:(
Why? and How can I fix this problem?
You need to restart unicorn itself. See http://unicorn.bogomips.org/SIGNALS.html
With Apache+Passenger, when you restart Apache, it restarts Passenger. Unicorn is it's own server however and needs to be restarted/reloaded itself.

Resources