Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP/Ubuntu - QxcbConnection: Could not connect to display aborted

I am using a php script on my apache/ubuntu server to call a bash script that triggers an application taking a python script as an argument (IDAPro).

PHP Code

chdir('/var/www/dashboard/team/static/sql');
$output = exec('sudo -u rohan ./start.sh');

Now, the above code works fine if I run the PHP file from the terminal - but only if I run it as the root user. Needless to say, if I execute the bash file directly it runs too.

But when I run the PHP file on the browser, it doesn't work and I get the following error in the apache error log:

QXcbConnection: Could not connect to display
Aborted

I understand that Apache/php runs as 'www-data' user (used the 'whoami' to verify), and that is why I have the sudo in my exec. I have tweaked and tinkered the permissions for both users to no avail. When I run the php file from the terminal as the 'www-data' user, it throws no error but does not do anything except display the random echo tags I at the start and end of the script to debug it.

I am a linux novice, so any help is greatly appreciated.

like image 248
Rohan Sood Avatar asked Jun 23 '17 11:06

Rohan Sood


2 Answers

Okay, I finally managed to solve it.

The issue is not with the permissions, but it is with the environment variables.

I had to include the following line in my bash script

export DISPLAY=':0.0'

Note that setting the variable in the terminal and running the script does not work. The line needs to be inside the script.

I assume this is because the DISPLAY variable is not set if you run the script as any user other than root, which is what happens in case of Apache/PHP where the script is executed as the 'www-data' user.

like image 137
Rohan Sood Avatar answered Nov 05 '22 09:11

Rohan Sood


perhaps you could use something like the following at the top of your script:

if [ "$(id -un)" != "rohan" ]; then
    exec sudo -u rohan $0 "$@"
fi

export XAUTHORITY=/home/rohan/.Xauthority
export DISPLAY=:0
like image 27
João Dantas Avatar answered Nov 05 '22 11:11

João Dantas