bash PHP/Ubuntu - QxcbConnection:无法连接以显示中止

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44720732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:14:00  来源:igfitidea点击:

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

phplinuxbashapacheubuntu

提问by Rohan Sood

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).

我在我的 apache/ubuntu 服务器上使用一个 php 脚本来调用一个 bash 脚本,该脚本触发一个将 python 脚本作为参数(IDAPro)的应用程序。

PHP Code

PHP代码

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.

现在,如果我从终端运行 PHP 文件,上面的代码可以正常工作 - 但前提是我以 root 用户身份运行它。不用说,如果我直接执行 bash 文件,它也会运行。

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:

但是当我在浏览器上运行 PHP 文件时,它不起作用,我在 apache 错误日志中收到以下错误:

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.

我知道 Apache/php 以“www-data”用户身份运行(使用“whoami”进行验证),这就是为什么我的 exec 中有 sudo 的原因。我已经调整并修改了两个用户的权限,但无济于事。当我以“www-data”用户的身份从终端运行 php 文件时,它不会引发任何错误,但除了在脚本的开头和结尾显示随机回显标签 I 以进行调试外,不会执行任何操作。

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

我是 linux 新手,因此非常感谢任何帮助。

采纳答案by Rohan Sood

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

我必须在我的 bash 脚本中包含以下行

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.

我认为这是因为如果您以 root 以外的任何用户身份运行脚本,则不会设置 DISPLAY 变量,这在 Apache/PHP 的情况下会发生,其中脚本以“www-data”用户身份执行。

回答by Jo?o Dantas

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

也许您可以在脚本顶部使用以下内容:

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

export XAUTHORITY=/home/rohan/.Xauthority
export DISPLAY=:0