PHP / Ubuntu permissions error - QxcbConnection: Failed to connect to interrupt display

I am using php script on my apache / ubuntu server to call a bash script that launches an application that takes 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 run the bash file directly, it starts too.

But when I run the PHP file in the browser, it does not work, and I get the following error in the apache error log:

 QXcbConnection: Could not connect to display Aborted 

I understand that Apache / php works as the user of "www-data" (used by "whoami" for verification), and that is why I have sudo in my exec. I changed and remade permissions for both users to no avail. When I run the php file from the terminal as the user "www-data", it does not throw an error, but does nothing but display random echo tags at the beginning and end of the script to debug it.

I am a beginner novice, so any help is appreciated.

+5
source share
2 answers

Well, I finally managed to solve this problem.

The problem is not with permissions, but with environment variables.

I had to include the following line in my bash script

 export DISPLAY=':0.0' 

Please note that setting a variable in the terminal and running the script does not work. The string must 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 happens in the case of Apache / PHP, where the script runs as "www-data".

+4
source

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 
+2
source

Source: https://habr.com/ru/post/1269151/


All Articles