Bilal Abbasi
Bilal Abbasi

Reputation: 31

Deployment of PHP and react js app on nginx

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

Upvotes: 0

Views: 1888

Answers (1)

CodeBird
CodeBird

Reputation: 502

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;

}

Upvotes: 1

Related Questions