Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

priyalaks's avatar

PHP file getting downloaded instead of executing in Mac M2

I've been following this tutorial to set up my Nginx and PHP, but no matter what I try, my PHP file gets downloaded instead of getting executed. I've done the following so far-

brew install nginx               //to install nginx in my mac m2
sudo nginx                       //to start nginx as sudo
sudo brew services restart nginx //when ever I need to restart nginx post configuration

This is how I've installed nginx server.

This is how my /opt/homebrew/etc/nginx/nginx.conf looks:

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
    }

    location ~ \.php$ {
      root html;
      include fastcgi.conf;
      fastcgi_pass 127.0.0.1:9074;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
    }

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    charset utf-8;
    include servers/*;
}

This is how specific config /opt/homebrew/etc/nginx/servers/mydomain.test.conf looks:

server {
    server_name mydomain.test;
    root /Users/hs/Developer/laravelProject/public;
    index index.php index.html;

    location / {
        try_files   $uri $uri/ /index.php?$query_string;
    }

    # Remove trailing slash to please routing system.
    # if (!-d $request_filename) {
    #        rewrite     ^/(.+)/$ / permanent;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9074;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

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

}

Further, the fastcgi_params file includes the following:

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_NAME        $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  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

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;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

Finally, I've also ensured php-fpm is running on port 9074

❯ sudo lsof -i -n -P|grep php-fpm
php-fpm   34969    hs    8u  IPv4 0x6c3f761f580c38d5      0t0    TCP 127.0.0.1:9074 (LISTEN)
php-fpm   34970    hs    9u  IPv4 0x6c3f761f580c38d5      0t0    TCP 127.0.0.1:9074 (LISTEN)
php-fpm   34971    hs    9u  IPv4 0x6c3f761f580c38d5      0t0    TCP 127.0.0.1:9074 (LISTEN)
php-fpm   50313    hs    9u  IPv4 0x6c3f761f580c38d5      0t0    TCP 127.0.0.1:9074 (LISTEN)

I believe I've tried almost all the possiblities to best of my knowledg to get this validated, but not sure where am going wong. I've also placed index.html with static content as "it works" and if i access the URL mydomain.test/index.html, it outputs correctly, with that I can be sure that its reading the right directory structure, but always downloads the php fil.

I tried following the most of the SO answers (most of them were for linux, but still related) and tried ensusing the required configurations for this (fastcgi_param, fastcgi.conf) are valid.

Expectation is to run the php seamlessly.

0 likes
1 reply
LaryAI's avatar
Level 58

It looks like the Nginx configuration is correct, but the issue might be with PHP-FPM. Try checking the PHP-FPM error logs to see if there are any errors.

You can also try restarting PHP-FPM and Nginx to see if that resolves the issue:

sudo brew services restart [email protected]
sudo brew services restart nginx

If that doesn't work, try changing the fastcgi_pass directive in the Nginx configuration to use a Unix socket instead of a TCP socket:

fastcgi_pass unix:/usr/local/var/run/php-fpm.sock;

Make sure to update the path to the PHP-FPM socket if it's different on your system.

After making any changes to the Nginx configuration, be sure to reload the configuration:

sudo nginx -s reload

Please or to participate in this conversation.