2017-07-14 123 views
0

我在我的专用根服务器上运行nginx,并有多个WordPress的网站。nginx不执行php文件

此外我想通过PHP脚本更新我的私人IP。为此,我创建了这个PHP文件(myscript.php):

<?php  
    $pwort = 'mypassword'; 
    $port = ':80'; 

    $dyntxt = "my_IP.txt"; 
    $pworttest = $_GET["pass"]; 
    $IP = $_GET["meineip"]; 

    if (file_exists($dyntxt)){ 
     if($pworttest==$pwort) { 
      $a = fopen("$dyntxt", "w"); 
      $dynamicip = $_SERVER["REMOTE_ADDR"]; 
      fwrite($a, $IP); 
      fclose($a); 
     } 
     else { 
      $a = fopen("$dyntxt", "r+"); 
      $dynamicip = fread($a,filesize($dyntxt)); 
      fclose($a); 

      $url="http://".$dynamicip."".$port; 
      header("Location: $url"); 
     } 
    } 
?> 

这里是我的配置为dyn.myserver.co上nginx的:

server { 
    listen 80; 
    listen [::]:80; 

    root /var/www/dyn; 
    index index.php index.html index.htm; 

    server_name dyn.myserver.co; 

    location/{ 
     try_files $uri/ /index.php?$args; 
    } 
    error_page 404 /404.html; 
    error_page 500 502 503 504 /50x.html; 
    location = /50x.html { 
     root /var/www/dyn; 
    } 

    location ~ \.php$ { 
     try_files $uri =404; 
     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
     fastcgi_pass unix:/var/run/php5-fpm.sock; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     include fastcgi_params; 
    } 
} 

这是写在MYIP的IP。通过cronjob将txt复制到DNS服务器。这是行得通的。 例如:在我的apache服务器上,我调用了dyndns URL(http://dyn.myserver.co/myscript.php),并被重定向到填充在myIP.txt中的IP。在nginx上它只显示了myscript.php的内容。

服务PHP5-FPM状态给出:

[email protected]:/etc/php5/fpm/pool.d# service php5-fpm status 
● php5-fpm.service - The PHP FastCGI Process Manager 
    Loaded: loaded (/lib/systemd/system/php5-fpm.service; enabled) 
    Active: active (running) since Tue 2017-07-11 16:08:54 CEST; 3 days ago 
    Process: 495 ExecStartPre=/usr/lib/php5/php5-fpm-checkconf (code=exited, status=0/SUCCESS) 
Main PID: 613 (php5-fpm) 
    Status: "Processes active: 0, idle: 3, Requests: 3294, slow: 0, Traffic: 0req/sec" 
    CGroup: /system.slice/php5-fpm.service 
      ├─ 613 php-fpm: master process (/etc/php5/fpm/php-fpm.conf) 
      ├─ 677 php-fpm: pool www 
      ├─ 1486 php-fpm: pool www 
      └─23693 php-fpm: pool www 

Jul 11 16:08:52 host01 systemd[1]: Starting The PHP FastCGI Process Manager... 
Jul 11 16:08:54 host01 systemd[1]: Started The PHP FastCGI Process Manager. 

我认为这是与nginx的配置有问题。有谁能够帮助我?

+0

'my_IP.txt'与您的PHP脚本位于同一个文件夹中吗?如果没有,它会给出空白页面,而不是重定向,因为'file_exists($ dyntxt)'是'false' – StefansArya

回答

0

将这个现场可用的文件夹

server { 
    listen 80; 
    #listen [::]:80; 

    root /var/www/dyn; 
    index index.php index.html index.htm; 

    server_name dyn.myserver.co; 

    error_page 404 /404.html; 
    error_page 500 502 503 504 /50x.html; 

    location/{ 
     try_files $uri $uri/ =404; 
    } 

    location ~ \.php$ { 
     try_files $uri =404; 
     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
     fastcgi_pass unix:/var/run/php5-fpm.sock; 
     fastcgi_index index.php; 
     include fastcgi_params; 
    } 
} 

然后,你必须在文件链接到启用站点文件夹

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com

,并重新启动nginx的服务

+0

我只是把这个配置放入ln -s并重新启动nginx。现在调用dyn.myserver.co/myscript.php给出404 Not Found ...任何想法? – tynie

+0

你可以进入'/ run/php'并告诉我是否存在'php5-fpm.sock'吗? – StefansArya

+0

我编辑了配置,尝试粘贴并重新启动nginx ..并查看发生了什么 – StefansArya

0

我解决了这个问题...

这就是我的scipt:

<?  
    $pwort = 'mypassword'; 
    $port = ':80'; 

    $dyntxt = "my_IP.txt"; 
    $pworttest = $_GET["pass"]; 
    $IP = $_GET["meineip"]; 

    if (file_exists($dyntxt)){if($pworttest==$pwort) { $a = fopen("$dyntxt", "w"); 
    $dynamicip = $_SERVER["REMOTE_ADDR"]; 
    fwrite($a, $IP); 
    fclose($a); } 
    else { $a = fopen("$dyntxt", "r+"); 
    $dynamicip = fread($a,filesize($dyntxt)); 
    fclose($a); 

    $url="http://".$dynamicip."".$port; 
    header("Location: $url");} } 
?> 

但它必须是:

<?php  
    $pwort = 'mypassword'; 
    $port = ':80'; 

    $dyntxt = "my_IP.txt"; 
    $pworttest = $_GET["pass"]; 
    $IP = $_GET["meineip"]; 

    if (file_exists($dyntxt)){if($pworttest==$pwort) { $a = fopen("$dyntxt", "w"); 
    $dynamicip = $_SERVER["REMOTE_ADDR"]; 
    fwrite($a, $IP); 
    fclose($a); } 
    else { $a = fopen("$dyntxt", "r+"); 
    $dynamicip = fread($a,filesize($dyntxt)); 
    fclose($a); 

    $url="http://".$dynamicip."".$port; 
    header("Location: $url");} } 
?> 

非常感谢@StefansArya你的努力!

+0

嗨,请把它放在你的问题中,通过编辑它..有人downvote你的问题,并没有得到任何帮助.. – StefansArya

+0

我编辑你的代码几个小时前,但看起来像它不能达到你http: //i63.tinypic.com/zx1744.jpg – StefansArya