Home > Backend Development > PHP Tutorial > macos - PHP Nginx environment configured on mac. When accessing php files, downloading occurs instead of executing php files.

macos - PHP Nginx environment configured on mac. When accessing php files, downloading occurs instead of executing php files.

WBOY
Release: 2023-03-02 12:24:01
Original
2121 people have browsed it

<code>vim /usr/local/etc/nginx/sites-available/default
</code>
Copy after login
Copy after login
<code>server {
    listen       80;
    server_name  localhost;
    root         /var/www/;

    access_log  /usr/local/var/logs/nginx/default.access.log  main;

    location / {
        index  index.html index.htm index.php;
        autoindex   on;
        include     /usr/local/etc/nginx/conf.d/php-fpm;
    }

    location = /info {
        allow   127.0.0.1;
        deny    all;
        rewrite (.*) /.info.php;
    }

    error_page  404     /404.html;
    error_page  403     /403.html;
}
</code>
Copy after login
Copy after login
<code>/usr/local/etc/nginx/nginx.conf
</code>
Copy after login
Copy after login
<code>worker_processes  1;

error_log   /usr/local/var/logs/nginx/error.log debug;


pid        /usr/local/var/run/nginx.pid;


events {
    worker_connections  256;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /usr/local/var/logs/access.log  main;

    sendfile        on;
    keepalive_timeout  65;
    port_in_redirect off;

    include /usr/local/etc/nginx/sites-enabled/*;
}

</code>
Copy after login
Copy after login

/usr/local/etc/nginx/sites-available/default This file configures port 80. Why can’t port 80 be accessed, but can access port 8080?

When accessing port 80, a 404 error cannot be found

Reply content:

<code>vim /usr/local/etc/nginx/sites-available/default
</code>
Copy after login
Copy after login
<code>server {
    listen       80;
    server_name  localhost;
    root         /var/www/;

    access_log  /usr/local/var/logs/nginx/default.access.log  main;

    location / {
        index  index.html index.htm index.php;
        autoindex   on;
        include     /usr/local/etc/nginx/conf.d/php-fpm;
    }

    location = /info {
        allow   127.0.0.1;
        deny    all;
        rewrite (.*) /.info.php;
    }

    error_page  404     /404.html;
    error_page  403     /403.html;
}
</code>
Copy after login
Copy after login
<code>/usr/local/etc/nginx/nginx.conf
</code>
Copy after login
Copy after login
<code>worker_processes  1;

error_log   /usr/local/var/logs/nginx/error.log debug;


pid        /usr/local/var/run/nginx.pid;


events {
    worker_connections  256;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /usr/local/var/logs/access.log  main;

    sendfile        on;
    keepalive_timeout  65;
    port_in_redirect off;

    include /usr/local/etc/nginx/sites-enabled/*;
}

</code>
Copy after login
Copy after login

/usr/local/etc/nginx/sites-available/default This file configures port 80. Why can’t port 80 be accessed, but can access port 8080?

When accessing port 80, a 404 error cannot be found

First you need a php-fpm to run php, and then configure it to the correct location

Refer to my conf:

<code>server {
  listen 80;

  server_name xxxxxx;

  index index.php;

  root /usr/www/web-sites/xxxxxx;

  error_log /var/log/nginx/xxxxxx.xxx.error.log;
  access_log /var/log/nginx/xxxxxx.xxx.access.log;

  location / {
      try_files $uri /index.php$is_args$args;
  }

  location ~ ^/.+\.php(/|$) {
    fastcgi_pass php_fpm:9000;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }

  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {
    expires 1d;
  }
}</code>
Copy after login

If you have any questions, you can search for the keyword:

nginx connects to php-fpm

The one I provide is the http method. You can switch to the more efficient socket method. Keyword: socket nginx php-fpm

    404 error you can check the error.log log
  • Have you found that php-fpm and nginx are not combined?
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template