问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/12110334/deploy-2-sites-on-same-apache-server