how to map a Rails app to a certain URL path? - ruby-on-rails

Hey, Guys
I'm now learning starting up the Rails on my VPS server, Now I can visit my app rails my thin server by a 3000 port number, something like this http://mydomain:3000,
But I want to map this app to the url like http://mydomain/railsapp1, so when I add a railsapp2 for testing purpose, it won't mess up my railsapp1.
Should I add something in the thin configuration file? or I should use nginx?

Are you open to using Passenger (ModRails)? You could then use Nginx and setup your Rails apps under different subdirectories.
General information for installing Passenger in Nginx can be found here: http://www.modrails.com/install.html
You can see more information here on setting up Rails in subdirectories: http://www.modrails.com/documentation/Users%20guide%20Nginx.html#deploying_rails_to_sub_uri

You could just start railsapp2 on port 3001 if you want to have both running at the same time
Rails 2
script/server -p 3001
Rails 3
rails server -p 3001

Related

Load a rails page after foreman start

I'm new to web development. I'm trying to follow the instructions here to set up a local instance of the sharetribe.com website: https://github.com/sharetribe/sharetribe
I've followed all the steps under "Setting up the development environment" without any issues, but when I get to the end, I'm supposed to "Open a browser and go to the server URL (e.g. http://lvh.me:3000)"
At that URL, I get "This site can’t be reached." Is my URL supposed to be at a different port on localhost? How do I figure out where my site is at?
Foreman starts the server on 'http://0.0.0.0:5000' by default.
Instead of Foreman, you can also start the rails server with following command:
rails s -b 0.0.0.0 -p 5000
Figured it out! The IP address prints out in the terminal where you did the "foreman start" command after the line "Rails 5.1.1 application starting in development on"
Mine was http://0.0.0.0:5000 (not that it's 5000 not 3000), then after filling in the information, I was redirected to http://[my marketplace name].lvh.me:3000/ and I had to change that to 5000 to see the marketplace. Huzzah!

Running Rails on Unicorn via Pow, is it possible to know what port Unicorn is running on?

In my Ruby on Rails dev environment, I am starting Rails and Unicorn via Foreman in the typical way:
(Procfile:)
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
I am also running Pow. But not as a webserver. I'm just using Pow to direct http requests from mydomain.dev (port 80) to the port Unicorn is listening on.
You can do this by creating a pow file (mydomain.dev) containing the port number Unicorn is running on.
Given this setup, is it possible in my rails code to know what the port I started Unicorn on?
I'm only wanting to know this in my dev environment, it's not a production issue.
In my Rails code, I've tried a few different things, but none of them work:
Unicorn::Configurator::RACKUP[:port] - returned nothing
Rails::Server.new.options[:Port] - doesn't exist in Rails 4
Rack::Server.new.options[:Port] - returns default rack port (9292) not the one configured for this rack instance.
Is there a way to get the current rack instance from rails?
request.port - returns 80, which is the port that Pow is listening on. Pow is routing http traffic to Unicorn, which is on a different port.
None of these give me the port that Unicorn is running on.
Any ideas?
EDIT If you're wondering why I want to know this, it's because in my dev environment, I'm trying to dynamically create configuration files for Pow, so I can route http requests to Unicorn on the correct port.
If you're responding to a request in a controller or view, use the request object - this should work, although you say it does not:
request.port
If you're in an initialiser :
Rails::Server.new.options[:Port]
How to find the local port a rails instance is running on?
You should just be able to access it via ENV['PORT'], given it's value has been set to the $PORT environment variable.
I've sort of found a way to do this.
Create separate config files for Unicorn. unicorn.development.rb and unicorn.test.rb
Install the dotenv-rails gem
Inside my unicorn config files, do something like this:
# unicorn.development.rb:
require "dotenv"
Dotenv.load(
"./.env.local",
"./.env.development",
"./.env"
)
if ENV['UNICORN_PORT'].nil?
throw 'UNICORN_PORT not set in environment!'
end
worker_processes 3
timeout 30
preload_app true
listen ENV['UNICORN_PORT'], backlog: 64
... rest of unicorn config...
# unicorn.testing.rb:
require "dotenv"
Dotenv.load(
"./.env.local",
"./.env.testing",
"./.env"
)
if ENV['UNICORN_PORT'].nil?
throw 'UNICORN_PORT not set in environment!'
end
worker_processes 3
timeout 30
preload_app true
listen ENV['UNICORN_PORT'], backlog: 64
... rest of unicorn config...
In my .env.development and .env.testing environment files, set the UNICORN_PORT environment variable
Make sure you use the correct Unicorn config file to start the app. This can be done by using separate Procfiles for dev and testing.
# Procfile.dev
web: bundle exec unicorn -c ./config/unicorn.development.rb
# Procfile.testing
web: bundle exec unicorn -c ./config/unicorn.testing.rb
This appears to mostly work, but is not without it's issues...
Probably a bad idea, but whatever:
uport = `netstat -n --listening --tcp -p | grep unicorn | sed 's/.*:\([0-9]*\) .*/\1/'`

Relocate Rails /tmp/pids/server.pid file to system /tmp/pids/

Is there a proper way to relocate server pids from ~/[RAILS APP]/tmp/pids to the system /tmp/pids folder? The reason is to create functions to stop and restart rails server by a simple function. Or is there an alternative, preferable, approach?
Try starting the server with this:
bundle exec rails s -p 3000 -P [NOT RAILS]/tmp/pids/server.pid
Credit: Rails Update to 3.2.11 breaks running multiple servers

how to change http://www.myexample.com:3000 to http://wwww.myexample.com in ruby?

I am new to ruby, can any one help me in explaining "how to change http://www.myexample.com:3000 to http://wwww.myexample.com in ruby?"
require 'uri'
uri = URI.parse('http://www.myexample.com:3000')
uri.port = nil
uri.host.sub!('www', 'wwww')
uri.to_s # => http://wwww.myexample.com
Depends on what web server you're using, but in general, try adding a -p 80 option to the command line when starting up the web server.
For example, with the Thin web server, you'd run: thin -p 80 start.
Assuming your using webrick, pass --port 80 on the command line when running your server
port 3000 is the rails server default port. You can avoid this 3000 by getting your rails app behind apache/ passenger (normally the production setup)
By that way apache and passenger will do the routings and you can view your app at
http://www.example.com
Following will be helpful
passenger - http://www.modrails.com/
rails casts - http://railscasts.com/episodes/122-passenger-in-development
hope this helps
cheers
sameera

Why would running a Rails app as a WEBrick server work, but installing it as a Mongrel service would not?

Yet another newbie RoR question from me.
I started banging my head against a wall last night when I simply could not get my Rails app to display in my browser after installing it as a Mongrel service.
I installed it using a command like this (from the app's root directory):
mongrel_rails service::install -N MyAppName -e development -p 3000
This set up the Windows service and everything seemed to be just fine. I could start/stop the service and saw no errors in the logs. Then navigating to localhost:3000 in my browser, I was greeted with a variety of errors, none Rails-specific (all along the lines of "Could not connect to server" or the like). Consulting the log at this point revealed no obvious problems.
I could not for the life of me figure out how to get this to work. So, out of exasperation, I tried simply running the app on WEBrick instead:
ruby script/server webrick -p 3000
When I did this, my app ran perfectly! Opening my browser to localhost:3000 now displayed my front page as expected.
I should note that I have used Mongrel successfully for other apps on my local machine.
So what app-specific characteristics could be responsible for WEBrick working where Mongrel doesn't?
Just some ideas to try:
Add -c param with full path to application:
-c "C:\xxx\yyy\zzz"
Check if system-wide PATH environment variable contains ruby bin directory - maybe just user's PATH is set.
Switch service to run as your user.

Resources