0

I am trying to run reactjs application and PHP application together on Nginx at my ubuntu 18.04 server. Reactjs application runs well, but I am unable to run PHP application. I have added two server blocks for both the applications but unable to run PHP app which is actually my api/service for react js app.

Any help in this regard would be much appreciated.

Here is my nginx configuration for react js application:

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/html/qurancom-reactjs;        
        index index.html index.htm index.nginx-debian.html;
        server_name 3.16.130.108;
location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://localhost:3000;
    proxy_set_header Host $http_host;
    proxy_cache_bypass $http_upgrade;
    proxy_redirect off;
                                                                                                                                                                                          }

and here are my php app nginx server block configurations:

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

        root /var/www/html/quran-app-services/api;
        index index.php index.html index.htm index.nginx-debian.html;

        server_name 3.16.130.108;

        location / {
                try_files $uri $uri/ =404;
        }
}

Thanks

  • You need to setup `php-fpm` and have `fastcgi_pass`, `fastcgi_index` and `fastcgi_params` setup properly. See [similar question](https://stackoverflow.com/questions/15423500/nginx-showing-blank-php-pages). – Koala Yeung Sep 20 '19 at 09:53
  • @KoalaYeung thanks for you response, I have tried this solution but it doesnot works, I am actualy trying to run both applications react and php, react site is working fine, but unable to run php as when I try to hit http://my-IP/index.php it downloads it. – Bilal Abbasi Sep 20 '19 at 10:18
  • Are they on different domain name? – Koala Yeung Sep 20 '19 at 10:24
  • I am not using domain, both are on same IP but I also tried with different ports but issue not resolved, I tried to run PHP on 8080 but same result – Bilal Abbasi Sep 20 '19 at 10:33
  • 1. You have to have `fastcgi_pass`, `fastcgi_index` and `fastcgi_params` to connect to the php-fpm. 2. Both your `proxy pass` and `fastcgi_pass` setttings should be in the same configuration block. 3. It is impossible for both to be the directory index of the root path. Either one need to be in a subpath like `/my-php` or `/my-reactjs` so the other one is the default page. – Koala Yeung Sep 20 '19 at 10:38
  • Thanks I will try this and will let you know, thanks for your time much appreciated – Bilal Abbasi Sep 20 '19 at 10:58

1 Answers1

1

Both your configs have listen ports as 80. Also, both react and PHP configs are for /.

Try setting different listen port for PHP, and change the location to /api.

Also, try adding error logs and access logs to server configs so that you can investigate when the server returns 500 or 405 errors

 error_log /var/log/nginx/laravel-app-error.log;
 access_log /var/log/nginx/laravel-app-access.log;

Here is a working server config for a Laravel project that you can refer to

server {
    listen     90;
    server_name <server ip or hostname>;
    charset utf-8;
    root /var/www/html/laravel/public;
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.php index.html index.htm;
    # Always serve index.html for any request
    location /api {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }

    error_log /var/log/nginx/laravel-app-error.log;
    access_log /var/log/nginx/laravel-app-access.log;

}
CodeBird
  • 387
  • 1
  • 8