macos - mac 設定的PHP Nginx 環境 存取php檔案出現下載而不是執行php文件

WBOY
發布: 2023-03-02 12:24:01
原創
2111 人瀏覽過

<code>vim /usr/local/etc/nginx/sites-available/default
</code>
登入後複製
登入後複製
<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>
登入後複製
登入後複製
<code>/usr/local/etc/nginx/nginx.conf
</code>
登入後複製
登入後複製
<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>
登入後複製
登入後複製

/usr/local/etc/nginx/sites-available/default 這個檔案配置的是80端口,為啥80端口不能訪問, 但是8080 可以訪問

80 連接埠存取都是出現404 錯誤找不到

回覆內容:

<code>vim /usr/local/etc/nginx/sites-available/default
</code>
登入後複製
登入後複製
<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>
登入後複製
登入後複製
<code>/usr/local/etc/nginx/nginx.conf
</code>
登入後複製
登入後複製
<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>
登入後複製
登入後複製

/usr/local/etc/nginx/sites-available/default 這個檔案配置的是80端口,為啥80端口不能訪問, 但是8080 可以訪問

80 連接埠存取都是出現404 錯誤找不到

首先你需要一個 php-fpm 來跑 php,然後還要把它配置到正確的位置

參考我的 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>
登入後複製

有問題可以搜尋關鍵字:nginx 連接 php-fpm
我提供的這種是 http 方式,你可以換用效率更高的 socket 方式,關鍵字:socket nginx php-fpm

  • 404錯誤你可以看看error.log日誌

  • 你有沒有發現php-fpm和nginx沒有結合?

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!