Wednesday, January 5, 2022

[FIXED] How to make a server block configuration file to run a CakePHP app on Nginx server?

Issue

Since I'm new to DigitalOcean and to Nginx server I don't have an idea what I am doing exactly

Having said that here's issue I'm getting

I've put the CakeApp folder inside /var/www/html folder and also I added an info.php page into webroot folder of my CakeApp to check if I can access it

When I go to http://my_ip/CakeApp it redirects to http://my_ip/CakeApp/login page and give a 404, but when I access the info.php file by going to http://my_ip/CakeApp/info.php it works and returns the PHP info page

here is the sever block file

server {
    listen   80;
    listen   [::]:80;
    server_name app.cake.com;
    return 301 http://app.cake.com$request_uri;

    root   /var/www/html/CakeApp/public/webroot;
    index  index.php;

    location /CakeApp/webroot {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_intercept_errors on;
        fastcgi_param SCRIPT_FILENAME 
        $document_root$fastcgi_script_name;
}

So I want to get the login page when I go to http://my_ip/CakeApp


Solution

Found a fix.

server {
        listen 80;
        root /var/www/html/CakeApp/webroot;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name example.com;

        location / {
                rewrite ^(.+)$ $1 break;
                try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }
}


Answered By - itsChathurangaJ

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.