Deploy 2 Sites on same Apache server - ruby-on-rails

I need to deploy my Rails app to a server that is already serving a PHP site at its root. I can't touch the existing site, and I have to deploy my app at a sub-url or sub-domain, ie xx.xx.xx.xx/rails or rails.xx.xx.xx.xx.
The Apache config I normally use to deploy my app:
<VirtualHost *:80>
ServerName localhost
DocumentRoot "C:/RubyStack-3.2.5-0/projects/app_name/public"
<Directory "C:/RubyStack-3.2.5-0/projects/app_name/public">
Allow from all
Options -MultiViews
</Directory>
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://app_balancers%{REQUEST_URI} [P,QSA,L]
<Proxy balancer://app_balancers>
BalancerMember http://localhost:3001/
BalancerMember http://localhost:3002/
</Proxy>
# Support for far-futures expires header
<LocationMatch "^/assets/.*$">
Header unset ETag
FileETag None
# RFC says only cache for 1 year
ExpiresActive On
ExpiresDefault "access plus 1 year"
</LocationMatch>
</VirtualHost>
How could I change this configuration to serve that folder from a sub-url or subdomain? A RedirectMatch won't work because it would also route people away from the existing PHP site, right?

Easiest way would be to do name-based virtual hosting, using a subdomain as you suggested. So where you write:
<VirtualHost *:80>
ServerName localhost
</VirtualHost>
Simply enter your subdomain instead:
<VirtualHost *:80>
ServerName mysubdomain.mycompany.com
</VirtualHost>
Apache should then separate any requests to that subdomain from the 'main' virtualhost automatically.
edit:
Alternatively it is possible to mount your rails app to a subdirectory as well (assuming you're using Passenger.) Here's an example based on my own local staging environment. I got this working by following the instructions at the Phusion website
<VirtualHost *:80>
ServerName localhost
# We will mount our application under http://localhost/myapp
RailsBaseURI /myapp
# This can be anywhere on the system, I just happened to use /home/www
<Directory /home/www/myapp>
# Here you can add any directives necessary for your app, like for example..
SetEnv GEM_HOME /home/user/.rvm/gems/ree-1.8.7-2012.02
</Directory>
</VirtualHost>
Now you have to do one more thing, and that's make /home/www/myapp a link to the public dir of the actual application. So let's say you have the application in your own homedir you would have to type this:
cd /home/www
ln -s /home/myuser/myapp/public myapp
If you then type ls -l it should show:
lrwxrwxrwx 1 myuser myuser 18 Jun 10 16:41 devb -> /home/myuser/myapp/public
I think that should be it.

Related

Directory index forbidden by Options directive in ruby on rails

I Configured SSL for rails app running with thin server in CentOS linux environment with apache. When I try to open my site using "https" I am getting Apache error page and I checked apache error logs and got the following error message
Directory index forbidden by Options directive.
I made some changes in /etc/httpd/conf.d/welcome.conf as
"Options -Indexes" TO "Options +Indexes"
then I am getting directory structure in browser.
Please help me to solve the issue
Update:
<VirtualHost *:80>
ServerName XXXX
ServerAlias www.XXXX.com
DocumentRoot XXXX
RewriteEngine On
<Proxy balancer://thinservers>
BalancerMember http://127.0.0.1:3000
</Proxy>
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://thinservers%{REQUEST_URI} [P,QSA,L]
ProxyPass / balancer://thinservers/
ProxyPassReverse / balancer://thinservers/
ProxyPreserveHost on
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
# Custom log file locations
ErrorLog XXXX
CustomLog XXXX
Your included Apache configuration shows the HTTP virtual host as it's litening on port 80
<VirtualHost *:80>
HTTPS connects to a different virtual host defined on port 443 you will need to modify this virtualhost's configuration in order to change the way the HTTPS portion of your website behaves.

apache + passenger just listing files in public dir

<VirtualHost *:80>
# ... server name/aliases and such.
DocumentRoot /var/www/html
RackEnv staging
RackBaseURI /extapp/forms
<Directory /var/www/html/extapp/forms>
Options -MultiViews
PassengerRuby /home/pstn/.rbenv/versions/2.1.2/bin/ruby
</Directory>
</VirtualHost>
/var/www/html/extapp/forms is a symlink linking to the public folder of a rails 4 application, all permissions for the directory tree are correct (r+w).
Passenger boots, as per the entry in the logs:
[ 2014-09-08 14:07:17.5896 21559/7f8d77305720 agents/Watchdog/Main.cpp:728 ]: All Phusion Passenger agents started!
However, when I visit the appropriate path, all I get is a directory listing of the files residing in the public folder (i.e. robots.txt et. al.)
Try this
<Directory /var/www/html/extapp/forms>
Options -MultiViews
PassengerRuby /home/posten/.rbenv/versions/2.1.2/bin/ruby
PassengerResolveSymlinksInDocumentRoot on
</Directory>
Then restart the server.
My server config is
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerName mysite
RailsEnv development
DocumentRoot /var/www/mysite/public
<Directory /var/www/mysite/public>
Allow from all
Options -MultiViews
PassengerResolveSymlinksInDocumentRoot on
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/ruby-error.log
CustomLog ${APACHE_LOG_DIR}/ruby-access.log combined
# For most configuration files from conf-available/, which are
</VirtualHost>

Apache: how to config ip address without SeverName?

I want to set my own sever to run my ruby on rails project(with passenger plugin). I use Apache2 on Ubuntu11.04(sever version).
It's my first time to use Apache and I have read some documents.
All the docs ask me to set SeverName,such as Apache doc and ubuntu docs.
Unfortunately, I don't have a domain name, can I just set IP address and use IP address to access this sever?
If it's ok, how should I do?
It's the config sample given by passenger:
<VirtualHost *:80>
ServerName www.yourhost.com
DocumentRoot /somewhere/public
<Directory /somewhere/public>
AllowOverride all
Options -MultiViews
</Directory>
</VirtualHost>
Thanks!
Comment out the line:
# NameVirtualHost *:80
Comment out any <VirtualHost> blocks.
Find the line:
DocumentRoot "/var/www/html" # or whatever your config uses for the overall apache document root.
Change it to your rails application's root.
DocumentRoot "/somewhere/public"
Then add your other config settings to a <Directory> block.
<Directory /somewhere/public>
AllowOverride all
Options -MultiViews
</Directory>
I haven't tested this for sure, but you should be able to get to it by only visiting the IP address. Make sure you have Passenger installed, have run passenger-install-apache2-module, and have added the appropriate module loading lines to your apache config.
/etc/apache2/httpd.conf
<VirtualHost *:80>
ServerName example.com
DocumentRoot /home/urpc-name/RailsApps/anything/public
<Directory /home/webonise/RailsApps/anything/public>
RailsEnv development
AllowOverride all
Options -MultiViews
</Directory>
</VirtualHost>
In server name:select ur virtual name...u can choose anything
In document root and directory,give your rails project path
Select rails environment in RailsEnv
/etc/hosts
127.0.0.1 example.com
Put server name in virtual host-127.0.0.1 is the localhost
Then just go to your browser and type example.com
Hope this helps

Multiple domains, multiple vhosts, separate rails apps

I am trying to host multiple different rails apps on the same VPS using multiple domains. I am using Apache 2.2.17 on Ubuntu 10.10. For Apache, I have multiple vhost files so that I can enable and disable particular sites easily without needing to comment them out or delete them. In addition, I am also using mod_rewrite so that the multiple domains that go to the same rails app appear to go to the same URL so I don't take a hit with search engines as far as duplicate content.
I believe that my DNS is setup properly. For each of the domains, I have a www subdomain as well as some site-specific sub-domains, such as blogs, etc. The issue I am seeing is that Apache appears to matching the www subdomain immediately and not examining the additional URL behind it. Changing ServerAlias does nothing. For example, using my setup, if I enter davidheartsrachel.com, I properly reach my wedding website. However, if I use www.davidheartsrachel.com, I reach my other website, my software development business. The URL is not re-written; it stays as davidheartsrachel.com instead of afewguyscoding.com.
The only way I have been able to properly get it to work is to use mod_rewrite in the primary vhost file to redirect to the wedding website vhost file (you can see that I have that in the primary vhost file, but it is commented out for purposes of this question). This doesn't seem proper to me? Should I get another IP and do IP vhosts instead of name-based vhosts?
When I execute apachectl -S, I get the following:
VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80 is a NameVirtualHost
default server afewguyscoding.com (/etc/apache2/sites-enabled/afewguyscoding.com:1)
port 80 namevhost afewguyscoding.com (/etc/apache2/sites-enabled/afewguyscoding.com:1)
port 80 namevhost blog.afewguyscoding.com (/etc/apache2/sites-enabled/afewguyscoding.com:69)
port 80 namevhost lbbs.afewguyscoding.com (/etc/apache2/sites-enabled/afewguyscoding.com:84)
port 80 namevhost davidheartsrachel.com (/etc/apache2/sites-enabled/davidheartsrachel.com:1)
port 80 namevhost dhr.afewguyscoding.com (/etc/apache2/sites-enabled/davidheartsrachel_staging:1)
Syntax OK
I notice that afewguyscoding.com is the default site - however, doesn't it have to do a full string match to determine the proper site?
Primary site's vhost file
<VirtualHost *:80>
ServerAdmin david.stites#afewguyscoding.com
ServerName afewguyscoding.com
ServerAlias davidstites.com, 5280software.com, milehigh-software.com, milehighsoftware.org
ServerAlias www.5280software.com, www.milehigh-software.com, www.milehighsoftware.org, www.davidstites.com, www.afewguyscoding.com
# this tells rails that it will run in production mode
# this is for rails < 3.x
RailsEnv production
DocumentRoot /var/www/afewguyscoding/current/public
DirectoryIndex index.html
# custom log file locations
# possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel alert
ErrorLog /var/www/afewguyscoding/current/log/error.log
CustomLog /var/www/afewguyscoding/current/log/access.log combined
# allows compression of text based mime.types
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript text/css application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
FileETag None
RewriteEngine On
# check for maintenance file and redirect all requests
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{REQUEST_URI} !\.(css|jpg|png|gif)$
RewriteCond %{REQUEST_URI} !^/ws/
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /system/maintenance.html
#RewriteCond %{HTTP_HOST} ^www.davidheartsrachel.com$
#RewriteRule ^(.*)$ http://davidheartsrachel.com$1 [L]
RewriteCond %{HTTP_HOST} ^www.davidstites.com$
RewriteRule ^(.*)$ http://www.afewguyscoding.com$1 [R=301,L]
<Directory /var/www/afewguyscoding/current/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
# In case I ever need CGI
#ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
#<Directory "/usr/lib/cgi-bin">
# AllowOverride None
# Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
# Order allow,deny
# Allow from all
#</Directory>
# how we can restrict access to documents from the local subnet
#Order deny,allow
#Deny from all
#Allow from 127.0.0.0/255.0.0.0 ::1/128
<Location /blog>
PassengerEnabled off
</Location>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin info#afewguyscoding.com
ServerName blog.afewguyscoding.com
DocumentRoot /var/www/wpress
DirectoryIndex index.php
<Directory /var/www/afewguyscoding/current/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Wedding site's vhost file
<VirtualHost *:80>
ServerAdmin info#davidheartsrachel.com
ServerName davidheartsrachel.com
ServerAlias rachelanddavid.net, rachelanddavidstites.com, rachelanddavidwedding.com
ServerAlias www.davidheartsrachel.com, www.rachelanddavidstites.com, www.rachelanddavidwedding.com, www.rachelanddavid.net
# this tells rails that it will run in production mode
# this is for rails < 3.x
RailsEnv production
# this is for rails >= 3.x
RackEnv production
DocumentRoot /var/www/davidheartsrachel/current/public
DirectoryIndex index.html
# Custom log file locations
# Possible values include: debug, info, notice, warn, error, crit, alert and emerg,
LogLevel alert
ErrorLog /var/www/davidheartsrachel/current/log/error.log
CustomLog /var/www/davidheartsrachel/current/log/access.log combined
# Allows compression of text based mime types
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript text/css application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
FileETag None
RewriteEngine On
# Check for maintenance file and redirect all requests
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{REQUEST_URI} !\.(css|jpg|png|gif)$
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /system/maintenance.html [L]
RewriteCond %{HTTP_HOST} ^www.rachelanddavidwedding.com$
RewriteRule ^(.*)$ http://www.davidheartsrachel.com$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.rachelanddavidstites.com$
RewriteRule ^(.*)$ http://www.davidheartsrachel.com$1 [R=301,L]
# Static cache
RewriteCond %{REQUEST_METHOD} !^POST$
RewriteCond /var/www/davidheartsrachel/current/tmp/cache/static$1/index.html -f
RewriteRule ^(.*)$ /var/www/davidheartsrachel/current/tmp/cache/static$1/index.html [L]
<Directory /var/www/davidheartsrachel/current/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from All
</Directory>
</VirtualHost>
If you can think of any more information that might be helpful, please, ask me to post it.
Edit:
Let me try to clear this up. In the most essential form, my question is: why does going to davidheartsrachel.com takes you to www.afewguyscoding.com and davidheartsrachel.com takes you to davidheartsrachel.com based on my configuration above
On my servers I don't define a primary site in my main httpd.conf file. I do all the virtual hosting in separate application specific conf files.
The only thing I have in my main httpd.conf is the line NameVirtualHost *:80
Here's a gist of a way you could try it. I pulled all the wedding site configuration out of your main site configuration. I set it so davidstites.com, afewguyscoding.com, and www.davidstites.com all redirect to www.afewguyscoding.com. The other domains (milehigh et al) are unaffected. I'm not sure where you wanted those to go.
All the wedding related domains redirect to www.davidheartsrachel.com.
I did notice what I think might have been a mess up in your main site configuration. Toward the end where you set your directory permissions you were using the /var/www/afewguyscoding/current/public directory instead of the /var/www/wpress that is the DocumentRoot. The correction I made there is on line 74 of the first document in that gist.
The answer is that I was incorrectly separating the entries under ServerAlias with a comma thusly:
ServerAlias rachelanddavid.net, rachelanddavidstites.com, rachelanddavidwedding.com, www.davidheartsrachel.com, www.rachelanddavidstites.com, www.rachelanddavidwedding.com, www.rachelanddavid.net
It is supposed to be a space:
ServerAlias rachelanddavid.net rachelanddavidstites.com rachelanddavidwedding.com www.davidheartsrachel.com www.rachelanddavidstites.com www.rachelanddavidwedding.com www.rachelanddavid.net

First time deploying rails app, cant get passenger to work?

Used railsready, then passenger w/ apache2, finally copied my app to /home/myapp.
Inserted the passenger config lines into /etc/apache2/apache.conf at the bottom along with my virtual host settings:
The servers name is rails.
LoadModule passenger_module /home/myapp/.rvm/gems/ruby-1.9.2-p180/gems/passenger-3.0.7/ext/apache2/mod_passenger.so
PassengerRoot /home/myapp/.rvm/gems/ruby-1.9.2-p180/gems/passenger-3.0.7
PassengerRuby /home/myapp/.rvm/wrappers/ruby-1.9.2-p180/ruby
<VirtualHost *:80>
ServerName rails
DocumentRoot /home/myapp/public
</VirtualHost>
All I get is the apache "It Works" page when I open the IP address. Am I doing something wrong?
You declared a ServerName so you must use it to access this VirtualHost (and not the IP address as you mentioned). Connect to http://rails/
For this to work, the hostname rails must resolv to your server's IP. You can add it to you hosts file or use a real domain name configured to resolve to this IP.
Or you can change the DocumentRoot of the default VirtualHost of Apache, and then it will always respond with the Rails app.
Try:
<VirtualHost *:80>
ServerName rails
DocumentRoot /home/myapp/public
<Directory /var/www/robox/public>
AllowOverride all
Options -MultiViews
</Directory>
</VirtualHost>
Also, you might need to disable the default site. In Ubuntu you can do this with:
[sudo] a2dissite default
[sudo] service apache2 restart

Resources