Question

Running Rails & PHP on same Ubuntu 20.04

Posted January 12, 2021 408 views
Ruby on RailsNginxPHPUbuntu 20.04

I have a droplet which I have Ubuntu 20.04 (LTS) x64, Nginx, and passenger. Currently, I’m running a Rails app in it. I was wondering how and if I’m able to run Rails alongside PHP script on the same server.

What I’m looking for is to have Rails app in domain.com and the PHP as domain.com/php-here

Thanks in advance

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
5 answers

Hi @rubioli,

You’ll need to configure this in your domain’s Nginx configuration file. You can add something like the following to make your configuration work:

  location /php-folder {

    try_files $uri $uri/ /index.php;

    # process PHP here
    location ~* ^/(.*\.php)$ {
      # directives to process PHP
    }
  }

If you just want to proccess .php files in your main folder, you can just add the following

    # process PHP here
    location ~* ^/(.*\.php)$ {
      # directives to process PHP
    }

In your main location block.

Regards,
KFSys

Thanks a lot @KFSys do I need to add this is the same ngnix config file I have for my rails? and what do I need to add in the directives to process PHP?

Thanks in advance!

  • Both codes of block(depending which one you need) need to be added to the configuration file for the domain you wish to use in this case where your RailApp is.

With my user I did sudo vim /etc/nginx/sites-enabled/domainname

and I have:

server {
  listen 80;
  listen [::]:80;

  server_name domain.com;
  root /home/deploy/domain/current/public;

  passenger_enabled on;
  passenger_app_env production;

  location /cable {
    passenger_app_group_name lustsnaps_websocket;
    passenger_force_max_concurrent_requests_per_process 0;
  }

  # Allow uploads up to 100MB in size
  client_max_body_size 200m;

  location ~ ^/(assets|packs) {
    expires max;
    gzip_static on;
  }
    # process PHP here
   location /php {

    try_files $uri $uri/ /index.php;

    # process PHP here
    location ~* ^/(.*\.php)$ {
      # directives to process PHP
    }
  }
}

Is this what you mean?

I appreciate your help @KFSys.

I added only

location ~* ^/(.*\.php)$ {
      # directives to process PHP
    }

and in my public folder in rails I added a info.php file, and when I visit that, it just downloads the file and doesn’t show anything :/

I also did the following:

server {
  listen 80;
  listen [::]:80;

  server_name mydomain.com;

location / {
 root /home/deploy/mydomain/current/public;

  passenger_enabled on;
  passenger_app_env production;
}
  location /cable {
    passenger_app_group_name mydomain_websocket;
    passenger_force_max_concurrent_requests_per_process 0;
  }

  location ~ ^/(assets|packs) {
    expires max;
    gzip_static on;
  }

location /php/ {
 root /var/www/html/php;
  # Tested with this as well
  #root /home/deploy/php;

index index.php;
 passenger_enabled off;
}

location ~ \.php$ {
       try_files $uri =404;
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       fastcgi_pass unix:/var/run/php5-fpm.sock;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
}

And when I visit HTTP://mydomain.com/php/index.php I get a 404 page

Hi @rubioli,

Let’s edit that conf a little bit. In the locatio /php/ you should add the following line

try_files $uri $uri/ /index.php;

Additionally, you need to add the .php location where you are processing your files added to the above location block.

server {
  listen 80;
  listen [::]:80;

  server_name mydomain.com;

  location / {
    root /home/deploy/mydomain/current/public;

    passenger_enabled on;
    passenger_app_env production;
  }
  location /cable {
    passenger_app_group_name mydomain_websocket;
    passenger_force_max_concurrent_requests_per_process 0;
  }

  location ~ ^/(assets|packs) {
    expires max;
    gzip_static on;
  }

  location /php/ {
    root /var/www/html/php;
    # Tested with this as well
    #root /home/deploy/php;

    index index.php;
    passenger_enabled off;
    location ~ \.php$ {
       try_files $uri =404;
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       fastcgi_pass unix:/var/run/php5-fpm.sock;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
    }

  }

}
  • Thanks @KFSys. Appreciate your help! I tried and still no luck and getting 404 error page from Nginx.

    This is the conf file:

    server {
      listen 80;
      listen [::]:80;
    
      server_name mydomain.com;
    
      location / {
        root /home/deploy/mydomain/current/public;
    
        passenger_enabled on;
        passenger_app_env production;
      }
      location /cable {
        passenger_app_group_name mydomain_websocket;
        passenger_force_max_concurrent_requests_per_process 0;
      }
    
      location ~ ^/(assets|packs) {
        expires max;
        gzip_static on;
      }
    
      location /php/ {
        root /var/www/html/php;
        try_files $uri $uri/ /index.php;
        index index.php;
        passenger_enabled off;
        location ~ \.php$ {
           try_files $uri =404;
           fastcgi_split_path_info ^(.+\.php)(/.+)$;
           fastcgi_pass unix:/var/run/php5-fpm.sock;
           fastcgi_index index.php;
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           include fastcgi_params;
        }
    
      }
    
    }
    

    Not sure if I’m still doing something wrong and when Im doing ls in deploy@domain:/var/www/html/php, I see the index.php which has:

    <?php
    phpinfo();
    ?>
    

    and file permission is: -rw-r--r--

    and php -v gives PHP 7.4.3

    • Hi @rubioli,

      Hmm, that’s really strange. I’ll need to create a droplet and try to recreate the environment to be sure why it’s not working. I’ll test this out and see what’s stopping it.

      Despite that, if you manage to find the proper solution, please post it here.

      • thanks a lot @KFSys I appreciate it

        • Hi @rubioli,

          Sorry for the late reply. So I did create a testing Nginx droplet and I can confirm the above way which I posted

            location /php/ {
              try_files $uri $uri/ /index.php;
              index index.php;
              passenger_enabled off;
              location ~ \.php$ {
                 try_files $uri =404;
                 fastcgi_split_path_info ^(.+\.php)(/.+)$;
                 fastcgi_pass unix:/var/run/php5-fpm.sock;
                 fastcgi_index index.php;
                 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                 include fastcgi_params;
              }
          

          worked for me without a problem.