PHP 中的 Freetds SQL Server 连接错误:权限?
我有一个 PHP 脚本,可以连接到远程 SQL Server。 我可以以 root 身份从命令行调用 freetds 命令: tsql –H 主机名 –U 用户名 它连接得很好,我可以运行查询。
我在 /var/www/html/axis/public/test.php 中有一个 PHP 脚本 当我: sudo –u apache –s (将用户从 root 更改为 apache),然后从命令行运行我的 PHP 脚本时,它会连接并运行查询,效果非常好。 但是,当从 Web 浏览器执行相同的脚本时,它会失败 - 浏览器返回数据库连接错误。 Web 浏览器能够连接它应该连接的任何地方,并呈现所有不需要 SQL 连接的网页。
apache 是运行 httpd 的用户。我已通过以下方式确认了这一点: ps aux | grep apache
关于为什么 apache 可以从命令行很好地执行 PHP 脚本,但当浏览器尝试连接到相同的脚本时,它会失败吗?
谢谢, 德里克
I have a PHP script that makes a connection to a remote SQL Server.
From the command line as root I can call the freetds command:
tsql –H hostname –U username
and it connects just great and I can run queries.
I have a PHP script in /var/www/html/axis/public/test.php
When I: sudo –u apache –s (change the user from root to apache) and then run my PHP script from the command line, it connects and runs the queries just great.
But when executing the same script from the web browser, it fails - the browser returns a DB Connection error. The web browser is able to connect everywhere it is supposed to and renders all the web pages that don't need an SQL connection.
apache is the user that is running httpd. I’ve confirmed this via: ps aux | grep apache
Any ideas as to why apache can execute the PHP script fine from the command line, but when the browser attempts to connect to the very same script, it fails?
Thanks,
Derrick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 FreeTDS 常见问题解答:
http://www.freetds.org/faq.html#php
您还可以发布您的连接代码以及您到底遇到了什么错误吗?
As per the FreeTDS FAQ:
http://www.freetds.org/faq.html#php
Also can you post your connection code and what error's you're getting exactly?
所以问题出在 SELinux 上。 Web 服务器被禁止连接到网络外部(连接到 SQL Server)。我必须运行以下命令:
/usr/sbin/setsebool -P httpd_can_network_connect_db 1
一旦完成,一切都很好。这个答案是在 Daniel Fazekas 的帮助下提供的。
感谢大家和我一起看这个问题。
So the problem was with SELinux. The web server was forbidden from connecting outside the network (to the SQL Server). I had to run this command:
/usr/sbin/setsebool -P httpd_can_network_connect_db 1
Once that was done, all was well. This answer was provided with help from Daniel Fazekas.
Thanks everyone for taking a look at this issue with me.