How to create dynamic subdomain rails - ruby-on-rails

How can i create subdomain for each user that registers on my site? For example userone.mysite.com and usertwo.mysite.com.
In php it can be done using apache virtual host, but i can't figure out how to do the same in Ruby on Rails. Here is how it can be done in apache
<VirtualHost *:80>
ServerName www.mysite.com
ServerAlias mysite.com *.mysite.com
DocumentRoot /www/domain
</VirtualHost>
I went through many blogs but couldn't find the solution.
Please Advice.

<VirtualHost *:80>
ServerName mysite.com
ServerAlias *.my_site.com
# !!! Be sure to point DocumentRoot to 'public'!
DocumentRoot /var/www/html/my_site
<Directory /var/www/html/my_site>
# This relaxes Apache security settings.
AllowOverride all
# MultiViews must be turned off.
Options -MultiViews
# Uncomment this if you're on Apache >= 2.4:
Require all granted
</Directory>
</VirtualHost>
and do not forgot change cname * entry to your domain in DNS

Addition to Dinesh Saini answer - you also need to update Rails configs accordingly. For example, if you need deep subdomains you should change
config.action_dispatch.tld_length in staging.rb and you should re-check routes.rb.
Live example: I had to implement shop displaying by subdomain - example URL
my-shop.shop.testapp.com.So what I did except server config changes
constraints (lambda { |req| req.subdomains[1] == 'shop' }) do
get '/', to: 'shopes#show', as: :shop
end
In controller to find resource
Shop.find_by(id: request.subdomains[0])
I also set
config.action_dispatch.tld_length = 2
I did this for staging env because it has such URL staging.testapp.com so I need another subdomain level. I think for you it's only good to check that it's has 1.

Related

Running Rails+Passenger+Devise from a subdirectory?

I have a server A which proxies all traffic on /rails to server B.
So I setup this virtual host, and most things work...okay. link_to is broken and generates urls to /users as opposed to /rails/users, but I can work around that.
If I set config.action_controller.relative_url_root to /rails then my routes work okay EXCEPT all the devise routes. They point to the bare URL. How do I properly configure server B to understand that its running in a subdirectory and generate links and routes correctly?
<VirtualHost *:80>
ServerName http://ec2-url.compute-1.amazonaws.com/
SetEnv RDS_HOSTNAME "mydb..."
SetEnv RAILS_RELATIVE_URL_ROOT "/rails"
DocumentRoot /home/ubuntu/myapp/public
RailsEnv staging
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/app.log combined
PassengerLogLevel 3
<Directory "/home/ubuntu/myapp/public">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
Options -MultiViews
Require all granted
</Directory>
</VirtualHost>
I am using Rails 4.
In your environment files, add a config for OmniAuth.config.full_host.
OmniAuth.config.full_host = 'http://myfullurl/subdir'
Now, in application_controller.rb, add this method:
def after_sign_in_path_for(resource_or_scope)
path = super(resource_or_scope)
"#{OmniAuth.config.full_host}#{path}"
end
Would be great if you share your routes.rb, but I think the easy way to change devise routes is to put something like the following in routes.rb
scope '/rails' do
devise_for :users
end

URL routing on Bitnami Google Compute Ruby on Rails

I am attempting to deploy the sample rails app: https://github.com/railstutorial/sample_app_rails_4 to the Google Compute Engine using Bitnami. I have followed the wiki instructions to do so from: https://wiki.bitnami.com/Infrastructure_Stacks/BitNami_Ruby_Stack#How_can_I_deploy_my_Rails_application.3f
But upon attempting to access the url for the site, it still does not direct to the Rails app
as you can see here: http://www.1234-ok-go.com (a domain I have purchased) it simply goes to the base Ruby Stack success page.
Even though I have setup the httpd-vhosts.conf to specifically target the Rails App:
<VirtualHost *:80>
ServerName 1234-ok-go.com
DocumentRoot "/opt/bitnami/apps/myapp/htdocs/public"
Include "/opt/bitnami/apps/myapp/conf/httpd-app.conf"
</VirtualHost>
<VirtualHost *:443>
ServerName 1234-ok-go.com
DocumentRoot "/opt/bitnami/apps/myapp/htdocs/public"
Include "/opt/bitnami/apps/myapp/conf/httpd-app.conf"
</VirtualHost>
How can this issue be alleviated and route the URL to my Rails app?
Bitnami developer here.
After you had created your httpd-vhosts.conf (the one you pasted) file under installdir/apps/myapp/conf/ you have to append this line to /installdir/apache2/conf/bitnami/bitnami-apps-prefix.conf
Include "/installdir/apps/myapp/conf/httpd-vhosts.conf"
And then create this file: /opt/bitnami/apps/myapp/conf/httpd-app.conf to look something like this:
<Directory /installdir/apps/myapp/htdocs/public>
Options +FollowSymLinks
AllowOverride None
<IfVersion < 2.3 >
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.3>
Require all granted
</IfVersion>
PassengerEnabled on
PassengerAppRoot "/installdir/apps/myapp/htdocs"
</Directory>
And finally, restart apache:
<installdir>/ctlscript.sh restart apache

Passenger on bitnami - can't access app root, all other routes working

I created a bitnami instance on AWS, and uploaded my rails application at -
/home/bitnami/my_app
I added the following to my virtual hosts -
<VirtualHost *:80>
ServerAdmin test#test.com
DocumentRoot "/home/bitnami/my_app/public"
<Directory /home/bitnami/my_app/public>
Allow from all
Options -MultiViews
</Directory>
RailsEnv production
ServerName www.my_app.com
ErrorLog "logs/my_app.com-error_log"
CustomLog "logs/my_app.com-access_log" common
</VirtualHost>
Inside the application routes.rb, I have -
root :to => "pages#home"
I can access all the routes in the application, i.e. if I go to www.my_app.com/signin, it works.
However, trying to access root - i.e. www.my_app.com gives 404 error.
What am I missing? Thanks in advance for your help.
Have you enabled Passenger in the Apache configuration? The guide below may help:
http://wiki.bitnami.com/Components/Phusion_Passenger

Virtual host with Passenger

Rails app already works correctly on mydomain.com with Apache+Passenger. In addition, I'm going to deploy non-Rails app(wordpress) on blog.mydomain.com. So I modified httpd.conf like
PassengerEnabled off
<VirtualHost *:80>
ServerName mydomain.com
DocumentRoot /var/www/railsapp/public
PassengerEnabled on
</VirtualHost>
<VirtualHost *:80>
ServerName blog.mydomain.com
DocumentRoot /var/www/blog
</VirtualHost>
But this doesn't work. blog.mydomain.com also shows Rails app. How can I divide them?
Try adding this to the top of the conf file:
NameVirtualHost *:80
You might also try swapping the order so the more specific one is first, but look at the section called 'Using the ServerPath Directive' on this page. It indicates that with older http/1.0 clients, you may not have enough information to route it correctly.

Passenger: RailsBaseURI case sensitive?

I used Passenger to deploy a RoR app to a sub URI on my domain. The problem I'm facing is that the sub URI seems to be case sensitive. Navigating to http://mydomain.com/RailsApp resolves fine. However, if I go to http://mydomain.com/railsapp, http://mydomain.com/railsApp, or any other variation, I get a 404 error. How can these requests using different casings get resolved correctly?
Here is my Apache configuration file:
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias www.mydomain.com
DocumentRoot /www/mydomain/public
<Directory "/www/mydomain/public">
RailsEnv "production"
Order allow,deny
Allow from all
</Directory>
RailsBaseURI /RailsApp
<Directory "/www/RailsApp/public">
RailsEnv "development"
Options -MultiViews
</Directory>
</VirtualHost>
Any help is much appreciated. Thanks!
You could look into using mod_rewrite and matching it case insensitive there.
Some links to get you started :)
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
http://httpd.apache.org/docs/2.2/misc/rewriteguide.html
Thanks ba for pointing me in the right direction.
I did some research and found the mod_speling module. This not only makes the URL case-insensitive but also checks for spelling errors.
To enable mod_speling:
sudo /usr/sbin/a2enmod speling
sudo /etc/init.d/apache2 force-reload
sudo /etc/init.d/apache2 restart
To use mod_speling, include the directive CheckSpelling on in your virtual host section:
<VirtualHost *:80>
CheckSpelling on
...
</VirtualHost>

Resources