I'm currently trying to get my server to work with both rails and php. Currently I have Apache2 installed with PHP (Default from the provider)

But I'm trying to get rails to work also. We use PHP on our main domain, and are going to have a subdomain where the rails application will run from.

There's no must for Apache2, but how can I get both rails and php to work on the same server?

UPDATE Failed to use apache and are now trying to get it done with nginx which I have used before. But my problem is to get PHP working. I have my rails app working, but I can't get the PHP to work. Currently I have this in my config file.

server {
    listen 80;
    server_name www.domain.no domain.no;

    location = / {
        root /var/www/vhosts/domain.com/httpdocs;
        index index.php index.html index.htm;

    location / {
        root   /var/www/vhosts/domain.com/httpdocs;
        index  index.php index.html index.htm;

    # pass the PHP scripts to FastCGI server listening on
    location ~ \.php$ {
        root   /var/www/vhosts/domain.com/httpdocs;
        fastcgi_index index.php;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param CONTENT_TYPE $content_type;
        fastcgi_param CONTENT_LENGTH $content_length;
        fastcgi_param SCRIPT_FILENAME /var/www/vhosts/domain.com/httpdocs/$fastcgi_script_name;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param DOCUMENT_URI $document_uri;
        fastcgi_param DOCUMENT_ROOT $document_root;
        fastcgi_param SERVER_PROTOCOL $server_protocol;
        fastcgi_param GATEWAY_INTERFACE CGI/1.1;
        fastcgi_param SERVER_SOFTWARE nginx;
        fastcgi_param REMOTE_ADDR $remote_addr;
        fastcgi_param REMOTE_PORT $remote_port;
        fastcgi_param SERVER_ADDR $server_addr;
        fastcgi_param SERVER_PORT $server_port;
        fastcgi_param SERVER_NAME $server_name;

But when I try to access a php file I just get redirected to www.domain.com//service

Any idea?

  • Really do not know what solved it, but suddenly it all just worked. Trying to load the same files, not restarted the server or anything. But I'm still happy :P – ThoKra Dec 18 '09 at 2:43

I'm running two production servers that serve both PHP and Rails sites. The easiest solution has been to run nginx on port 80. Nginx manages requests for Rails pages and static assets, then proxies any php requests to Apache which listens on a private port. This is easier than managing FCGI processes and it beats messing around with the myriad rewrite rules and PROXY_PASS settings that people suggest.

  • Upvoted. I just went through this mess recently, it's far easier to have nginx just proxy_pass off php stuff to apache. – Steve Klabnik Jan 13 '10 at 4:03

Just set it up to point to a virtual host using your subdomain. Instructions are here, dead simple.

<VirtualHost *:80>
    ServerName subdomain.domain.com
    DocumentRoot /railsapps/subapp/public
  • Tried and failed many times. Have a new approach now, updated the main post. – ThoKra Dec 18 '09 at 2:25

To get nginx passenger working with rails just add another server to your config:

passenger_root /usr/lib64/ruby/gems/1.8/gems/passenger-2.2.5; 
passenger_ruby /usr/bin/ruby; 

server {
    listen       80;
    server_name  SUBDOMAIN.DOMAIN.NO;
    #access_log  logs/host.access.log  main;

    passenger_enabled on;
    rails_spawn_method smart-lv2;

    rails_env    production;


    location ~* \.(ico|jpg|jpeg|gif|png|css|js)$ {
       expires      30d;

    error_page  404              /404.html;
    error_page  500 502 503 504  /50x.html;

    location = /50x.html {
        root   html;

location ~ \.html\.erb$ {
    deny  all;

location ~ /\.ht {
        deny  all;

UPD: regarding your php problem, try following set of params:

  fastcgi_param   DOCUMENT_ROOT   /var/www/vhosts/domain.com/httpdocs;
  fastcgi_param   SCRIPT_FILENAME /var/www/vhosts/domain.com/httpdocs$fastcgi_script_name;
  fastcgi_param   PATH_TRANSLATED /var/www/vhosts/domain.com/httpdocs$fastcgi_script_name;

  fastcgi_param   SCRIPT_NAME     $fastcgi_script_name;
  fastcgi_param   QUERY_STRING    $query_string;
  fastcgi_param   CONTENT_TYPE    $content_type;
  fastcgi_param   CONTENT_LENGTH  $content_length;
  fastcgi_param   REDIRECT_STATUS 200;
  fastcgi_param   SERVER_PROTOCOL $server_protocol;
  fastcgi_param   SERVER_NAME     $server_name;
  fastcgi_param   REQUEST_URI     $request_uri;
  fastcgi_param   REQUEST_METHOD  $request_method;
  fastcgi_param   REMOTE_USER     $remote_user;
  fastcgi_param   REMOTE_ADDR     $remote_addr;
  fastcgi_param   REMOTE_PORT     $remote_port; 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.