Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP socket error Permission denied on Linux

Tags:

linux

php

sockets

I have installed php 5.4.13 on Linux 2.6.34.

I have make simple client/server page using socket but it did not work on it.

It give permission denied error

Below is my php code

if (false == ($socket = socket_create(AF_INET, SOCK_STREAM, 0)))  // create socket 
{
   $stringData= date("D M d, Y g:i A"). " socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "<br>";
   echo $stringData;
}
else
{
    $timeout = array('sec'=>5,'usec'=>500000);
    socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,$timeout);

    if(false==($result = socket_connect($socket, $host, $port)))
    {
        $stringData= date("D M d, Y g:i A"). " socket_connect() failed: reason: " . socket_strerror(socket_last_error()) . "<br>";
        echo $stringData;
        socket_close($socket);
    }
    else
    {
        $stringData= date("D M d, Y g:i A"). " Socket connected succefully <br>";
        echo $stringData;

        if(false==(socket_write($socket, $command, strlen($command))))
        {
            $stringData= date("D M d, Y g:i A"). " socket_write() failed: reason: " . socket_strerror(socket_last_error()) . "<br>";
            echo $stringData;
            socket_close($socket);
        }
        else
        {
            if(false===($cmd = socket_read ($socket, 65536)))
            {
                //10060 for windows and 11 for linux

              if(10060!=socket_last_error() && 11!=socket_last_error())
              {
                $stringData= date("D M d, Y g:i A"). " socket_read() failed: reason: " . socket_strerror(socket_last_error()) . "<br>";
                echo $stringData;
                socket_close($socket);

              }
              switch(socket_select($r = array($socket), $w = array($socket), $f = array($socket), 5))
              {
                       case 2:
                               $refused=1;  
                               break;
              }
              if($refused==1)
              {
                $stringData= date("D M d, Y g:i A"). " socket_read() failed: reason: Connection Refused <br>";
                $ourFileHandle = fopen(SOCKET_LOG, 'a');
                echo $stringData;
                socket_close($socket);

              }
            }
            else
            {
                echo "<pre>".html_entity_decode(print_r($cmd,true))."</pre>";
            }
        }
    }
}

Above code work fine on command prompt but it gives error Permission denied when page open from any browser.

Command of run php from terminal : /usr/local/rootfs/php5/bin/php /www/socket_client.php

like image 616
Hkachhia Avatar asked Nov 30 '25 19:11

Hkachhia


1 Answers

Check your php.ini settings for the parameter "safe_mode". It should be "Off".

Another problem could be caused by "selinux" that is blocking your mod_php (apache process) from connecting via socket. In this case:

# check if it's enabled:
/usr/sbin/sestatus -v
to add the connecting rule:
setsebool httpd_can_network_connect=1

If you want to disable it completely:

setenforce 0

Also, for debug reasons, disable any security module on apache/PHP. For example try to disable Suhosin if it's running.

like image 165
dAm2K Avatar answered Dec 03 '25 12:12

dAm2K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!