使用 .htaccess 进行 PHP 设置时出现 500 内部服务器错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10928328/
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
500 Internal Server Error when using .htaccess for PHP Settings
提问by Debiprasad
When I am using .htaccessfor the following PHP settings, I am getting 500 Internal Server Errorwhile accessing the website.
当我使用.htaccess以下 PHP 设置时,我500 Internal Server Error在访问网站时遇到了问题。
the code in the .htaccessfile:
.htaccess文件中的代码:
php_flag display_errors off
php_flag log_errors on
The file permission for the .htaccessfile is 644
该文件的文件权限为.htaccess644
I know that code above is correct. But when it showed me 500 Internal Server Error, I tried different code (most probably wrong) too, but nothing worked. The different code tried are:
我知道上面的代码是正确的。但是当它显示给我时500 Internal Server Error,我也尝试了不同的代码(很可能是错误的),但没有任何效果。尝试的不同代码是:
php_value display_errors off
php_value log_errors on
and
和
php_value display_errors 0
php_value log_errors 1
What can be the cause of 500 Internal Server Error?
可能是什么原因500 Internal Server Error?
After learning from the comments on this question, I found that PHP settings on .htaccessdoes not work with FastCGI. So, to change PHP settings, I need to modify the php.inior I need to do it in the php code. Is there any alternate way, when I don't have access to modify php.inifile and I don't want to individually modify all the PHP files?
从关于这个问题的评论中了解到,我发现 PHP 设置对.htaccessFastCGI 不起作用。因此,要更改 PHP 设置,我需要修改php.ini或者我需要在 php 代码中进行。当我无权修改php.ini文件并且不想单独修改所有 PHP 文件时,是否有其他替代方法?
回答by Harald Brinkhof
Like the comments above said: you need to run your php module as Dynamic Shared Objectto make it work, as described in the Apache PHP Request Hanlding Documentation
就像上面的评论所说:您需要将 php 模块作为动态共享对象运行才能使其工作,如Apache PHP 请求处理文档中所述
DSO considerations:
libphp provides Apache directives such as php_$value and php_admin_$value. DSO is the only option where these directives will be valid inside .htaccess filesor httpd.conf. When these directives are compiled with the concurrent DSO patch, they should be named php4_$value and php5_$value instead.
DSO注意事项:
libphp 提供了 Apache 指令,例如 php_$value 和 php_admin_$value。DSO 是这些指令在 .htaccess 文件或 httpd.conf 中有效的唯一选项。当这些指令与并发 DSO 补丁一起编译时,它们应该被命名为 php4_$value 和 php5_$value。
cgi, fcgi, suphp it will not work.
cgi, fcgi, suphp 它不会工作。
回答by Gerard ONeill
As you've discovered, modifying .htaccessonly works with an apache module, where it would add to the directives that apache can understand. This wont work with cgi and others.
正如您所发现的,修改.htaccess仅适用于 apache 模块,它将添加到 apache 可以理解的指令中。这不适用于 cgi 和其他人。
The php way is to modify the php.ini for the entire site, or modify it on a per directory or per user basis.
php的方式是修改整个站点的php.ini,或者按目录或按用户修改。
For php5.5 (not sure about other versions) modify the .user.inifile in which ever directory you want these settings to apply to. Obviously setting this at the root directory for the site would make it apply to the entire site.
对于 php5.5(不确定其他版本),请修改.user.ini您希望这些设置应用到的目录中的文件。显然,在站点的根目录中设置它会使其适用于整个站点。
Go here for information on .user.ini: http://php.net/manual/en/configuration.file.per-user.php
转到此处获取有关 .user.ini 的信息:http://php.net/manual/en/configuration.file.per-user.php
Go here to check the list of directives and where they can be changed: http://php.net/manual/en/ini.list.php
去这里检查指令列表以及它们可以在哪里更改:http: //php.net/manual/en/ini.list.php
Lastly, to check on your file (probably mentioned in the comments), add a phpinfo.php (the name doesn't matter) file containing
最后,要检查您的文件(可能在评论中提到),添加一个 phpinfo.php(名称无关紧要)文件,其中包含
<?php phpinfo() ?>
to the directory so you can see what the settings are for php scripts that run in that directory.
到该目录,以便您可以查看在该目录中运行的 php 脚本的设置。
回答by Rob Stocki
I know it has been a while since this question was asked but if your .htaccess doesn't contain errors it could be the comments that are causing your headache.
我知道这个问题已经有一段时间了,但如果您的 .htaccess 不包含错误,则可能是导致您头痛的评论。
I recently upgraded WHM and suddenly I was getting 500 Internal Server Errors on some pages. After going through the logs I could see that the .htaccess file was the issue and I solved the problem since they were being caused by the comments.
我最近升级了 WHM,突然在某些页面上出现了 500 个内部服务器错误。在查看日志后,我可以看到 .htaccess 文件是问题所在,我解决了这个问题,因为它们是由评论引起的。
Here's what cleared the problem up for me.
这就是为我解决问题的原因。
Previously, I had directives in the .htaccess file that looked like this:
以前,我在 .htaccess 文件中有如下指令:
Order Deny,Allow
Deny from 52.87.112.125 # whattheme.com
Deny from 192.163.217.239 # scanwp.net
I started getting the 500 Internal Server Error after the WHM upgrade (likely apache was included in the upgrade).
在 WHM 升级后,我开始收到 500 Internal Server Error(升级中可能包含 apache)。
Suddenly, apache didn't like comments mid line in the directives. I modified the .htaccess file for the code above to look like this:
突然间,apache 不喜欢指令中的注释。我修改了上面代码的 .htaccess 文件,如下所示:
Order Deny,Allow
# whattheme.com
Deny from 52.87.112.125
# scanwp.net
Deny from 192.163.217.239
Problem Solved.
问题解决了。
This may not be your issue, but after a hunt, this solution worked for me. Good luck.
这可能不是你的问题,但经过一番寻找,这个解决方案对我有用。祝你好运。

