You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx配置PHP支持:已有example.com配置文件需添加PHP解析

Enable PHP Support for Your Nginx Server (example.com)

Got it, let's get your Nginx setup to handle PHP requests properly. Here's a step-by-step guide tailored to your existing configuration:

1. Install PHP-FPM First

Nginx doesn't process PHP directly—it relies on PHP-FPM (FastCGI Process Manager) to handle PHP execution. Install it based on your Linux distribution:

  • For Ubuntu/Debian systems:

    sudo apt update && sudo apt install php-fpm
    

    (If you need a specific PHP version like 8.2, use php8.2-fpm instead of php-fpm)

  • For CentOS/RHEL/Rocky Linux systems:

    sudo dnf install php-fpm
    

    Again, specify a version if needed (e.g., php8.2-fpm)

2. Update Your Nginx Configuration File

Open your config file at /etc/nginx/conf.d/example.com and adjust it to add PHP handling. I'll fix root inheritance issues and add the necessary PHP location block to your existing setup:

server {
    listen 80;
    server_name example.com;

    # Move root to server level so all locations inherit this path
    root /home/emad/website;
    # Add index.php to the default index file list
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404; # Properly handle static file requests
    }

    # Add this block to process PHP files
    location ~ \.php$ {
        try_files $uri =404; # Prevent passing non-existent PHP files to PHP-FPM (security best practice)
        fastcgi_pass unix:/run/php/php-fpm.sock; # Path varies by PHP version/distribution
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location /nrm {
        rewrite ^/nrm/(.*)$ /$1 break;
        proxy_pass http://127.0.0.1:4646;
    }

    location /files/khu {
        rewrite ^/files/khu/(.*)$ /$1 break;
        proxy_pass http://127.0.0.1:8000;
    }

    location /files {
        rewrite ^/files/(.*)$ /files/$1 break; # Fixed your incomplete rewrite rule
        # If this is for static files, add try_files to handle requests properly:
        # try_files $uri =404;
    }
}

Quick Notes on the PHP Block:

  • The fastcgi_pass path: On Ubuntu/Debian for PHP 8.2, this would be /run/php/php8.2-fpm.sock; on CentOS/RHEL, it might be /var/run/php-fpm/www.sock. You can verify the correct path in your PHP-FPM config at /etc/php-fpm.d/www.conf (look for the listen directive).

3. Validate Config and Restart Services

First, check if your Nginx config has syntax errors:

sudo nginx -t

If you see "test is successful", restart Nginx and PHP-FPM to apply changes:

  • Ubuntu/Debian:

    sudo systemctl restart nginx
    sudo systemctl restart php-fpm # Use php8.2-fpm if you installed a specific version
    
  • CentOS/RHEL:

    sudo systemctl restart nginx
    sudo systemctl restart php-fpm
    

4. Test PHP Functionality

Create a test PHP file in your website root (/home/emad/website/info.php) with this content:

<?php phpinfo(); ?>

Visit http://example.com/info.php in your browser—you should see the PHP info page if everything works. Remember to delete this file after testing (it exposes sensitive server details!).


内容的提问来源于stack exchange,提问作者Emad Helmi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:34:41