简体   繁体   English

Php套接字,Apache和CentOS 6.3给出了权限被拒绝

[英]Php sockets, Apache and CentOS 6.3 gives Permission denied

What I am trying to do is running a simple PHP script that checks if a game server is online and gets some info from it. 我想要做的是运行一个简单的PHP脚本,检查游戏服务器是否在线并从中获取一些信息。 I am running exactly this same script on a local box with WAMP Server where i just uncommented php_openssl.dll and php_sockets.dll and - voila - it was working as expected. 我在WAMP服务器的本地机器上运行完全相同的脚本,我只是取消注释php_openssl.dll和php_sockets.dll,并且 - 瞧 - 它按预期工作。

But then came our production environment! 但后来我们的生产环境! I usually work with Debian, but our host decided to install CentOS on our dedicated server because the NIC was failing in Debian, and it's been nothing but trouble since. 我通常和Debian一起工​​作,但是我们的主机决定在我们的专用服务器上安装CentOS,因为NIC在Debian中失败了,而且这只是麻烦了。

I overcame a few issues, and am left with this problem: how do I fix PHP sockets? 我克服了一些问题,并留下了这个问题:如何修复PHP套接字? I read that I needed php-common, so I installed this with: 我读到我需要php-common,所以我安装了这个:

# yum install php-common

Then i checked phpinfo() and I got this 然后我检查了phpinfo() ,我得到了这个

'./configure'  '--with-openssl' '--enable-sockets' ...

So this is looking good if you ask me, both openssl and sockets are installed and should be working, however this is not the case. 所以如果你问我这个看起来很好,openssl和socket都已安装并且应该正常工作,但事实并非如此。 I found this script here on Stack Overflow: 我在Stack Overflow上找到了这个脚本:

<?php
//just in case
if (!extension_loaded('sockets')) {
die('The sockets extension is not loaded.');
}
echo '<p><strong>Establishing connection...</strong></p>';
$socket = socket_create(AF_INET,SOCK_STREAM,0);
if (!socket_connect($socket, "stackoverflow.com", 80))
{
die('Socket error : '.socket_strerror(socket_last_error()));
}

echo '<p><strong>Connection successful!</strong></p>';

$request = join("\n",array(
"GET / HTTP/1.1",
"Connection: close",
"Host: stackoverflow.com",
"User-Agent: Mozilla/5.0 (Windows NT 6.1)",
"Accept: text/html,*/*;q=0.8",
""));
socket_write($socket,$request,strlen($request));

$response = socket_read($socket,2048);


echo "<p><strong>This is the received data : </strong></p>";
echo '<pre>'.htmlentities($response).'</pre>';

socket_close($socket);

What this outputs surprises me completely: 这个输出让我完全惊讶:

Establishing connection...

Connection successful!

This is the received data :

HTTP/1.0 408 Request Time-out
Cache-Control: no-cache
Connection: close
Content-Type: text/html

<html><body><h1>408 Request Time-out</h1>
Your browser didn't send a complete request in time.
</body></html>

But still, my script is not functioning ? 但是,我的剧本还没有运作? :( I have disabled iptables with :(我已经禁用了iptables

# service iptables stop

just to be sure it's not a firewall issue. 只是为了确保它不是防火墙问题。 What happens is the following, when I open the page instead of a game servers name I get this: 会发生以下情况,当我打开页面而不是游戏服务器名称时,我得到了这个:

Warning: socket_connect(): unable to connect [13]: Permission denied in /var/www/test.php on line 9 <<< 警告:socket_connect():无法连接[13]:第9行的/var/www/test.php中的权限被拒绝<<<

This is the code I'm using, it's open source from xPaw, so I take no credit for it: 这是我正在使用的代码,它是xPaw的开源代码,所以我不相信它:

<?php
function queryserver( $IP, $Port = 25565, $Timeout = 2 )
{
$Socket = Socket_Create( AF_INET, SOCK_STREAM, SOL_TCP );
Socket_Set_Option( $Socket, SOL_SOCKET, SO_SNDTIMEO, array( 'sec' => (int)$Timeout, 'usec' => 0 ) );
Socket_Set_Option( $Socket, SOL_SOCKET, SO_RCVTIMEO, array( 'sec' => (int)$Timeout, 'usec' => 0 ) );
if( $Socket === FALSE || Socket_Connect( $Socket, $IP, (int)$Port ) === FALSE )
{
return FALSE;
}
Socket_Send( $Socket, "\xFE\x01", 2, 0 );
$Len = @Socket_Recv( $Socket, $Data, 256, 0 );
Socket_Close( $Socket );
if( $Len < 4 || $Data[ 0 ] !== "\xFF" )
{
return FALSE;
}
$Data = SubStr( $Data, 3 );
$Data = iconv( 'UTF-16BE', 'UTF-8', $Data );

if( $Data[ 1 ] === "\xA7" && $Data[ 2 ] === "\x31" )
{
$Data = Explode( "\x00", $Data );
return Array(
'HostName'   => $Data[ 3 ],
'Players'    => IntVal( $Data[ 4 ] ),
'MaxPlayers' => IntVal( $Data[ 5 ] ),
'Protocol'   => IntVal( $Data[ 1 ] ),
'Version'    => $Data[ 2 ]
);
}
$Data = Explode( "\xA7", $Data );
return Array(
'HostName'   => SubStr( $Data[ 0 ], 0, -1 ),
'Players'    => isset( $Data[ 1 ] ) ? IntVal( $Data[ 1 ] ) : 0,
'MaxPlayers' => isset( $Data[ 2 ] ) ? IntVal( $Data[ 2 ] ) : 0,
'Protocol'   => 0,
'Version'    => '1.3'
);
}

$test = queryserver('SERVERADDRESSHERE'); // (i tried multiple, which are all working @wamp)
$echo = $test['HostName'] . "<<< <br />";
echo $echo;

在linuxbox中尝试此命令(以root身份)。

setsebool -P httpd_can_network_connect 1

I had similar issues, so I figured I would drop an answer here also for the next person that runs into this. 我有类似的问题,所以我想我也会在这里找到答案的下一个人。 A way of going about it, without disabling/limiting SELinux, is to change the policy surrounding your httpd. 在不禁用/限制SELinux的情况下实现它的方法是更改​​httpd周围的策略。

Hope this saves 2 days for the next person reading this. 希望这为下一个阅读此内容的人节省2天。 lol 大声笑

chcon -R -t httpd_sys_content_t /var/www
chcon -R -t httpd_sys_content_rw_t /var/www/html

(Posted on behalf of the OP). (代表OP发布)。

I found the answer. 我找到了答案。 Seems SELinux is blocking the socket connections. 似乎SELinux正在阻止套接字连接。 Disabled SE Linux with '# setenforce Permissive' and it works now. 使用'#setenforce Permissive'禁用SE Linux,现在可以使用了。 Just in case anybody is experiencing the same strangeness. 以防任何人遇到同样的陌生感。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM