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.