简体   繁体   English

PHP 文件由浏览器下载,而不是由本地开发服务器 (MAMP) 处理

[英]PHP files are downloaded by browser instead of processed by local dev server (MAMP)

Everything was going great until I added AddHandler application/x-httpd-php5s .php to the .htaccess file in my local server's document root (which I change frequently depending on the site I'm working with).一切都很顺利,直到我将AddHandler application/x-httpd-php5s .php到本地服务器文档根目录中的 .htaccess 文件中(我经常根据我使用的站点更改该文件)。 Since I did that when I visit http://localhost:8888 my browser just downloads the index.php and it's not processed at all, just the raw code.因为我在访问http://localhost:8888时这样做了,所以我的浏览器只下载 index.php 并且根本没有处理它,只是原始代码。 Now I removed that line from the .htaccess file but I'm still having this problem .现在我从 .htaccess 文件中删除了该行,但我仍然遇到这个问题

I've found that if I add an alternative entry to my hosts file for 127.0.0.1 the new entry behaves like 'localhost' used to.我发现如果我为 127.0.0.1 的主机文件添加一个替代条目,新条目的行为就像过去的“localhost”。 But if I add the line above to my .htaccess it knocks out that new host as well.但是,如果我将上面的行添加到我的 .htaccess 中,它也会淘汰那个新主机。 I've tried reinstalling MAMP and clearing its caches and all the temporary files I could find.我试过重新安装MAMP并清除它的缓存和我能找到的所有临时文件。 I surfed through Apache's httpd.conf file all to no avail.我浏览了 Apache 的 httpd.conf 文件,但无济于事。

So, to be clear: http://localhost:8888 is experiencing the above problem.所以,要明确: http://localhost:8888遇到了上述问题。 If I add a new entry to my hosts file for 127.0.0.1, say 'goomba' and the above line is not in the root .htaccess (and has never been for that host/alias/whatever) then I can access http://goomba:8888 just fine.如果我为 127.0.0.1 在我的主机文件中添加一个新条目,例如“goomba”,并且上面的行不在根 .htaccess 中(并且从未用于该主机/别名/任何内容),那么我可以访问http:/ /goomba:8888就好了。 But if I do add that line to the .htaccess then I have to add yet another entry to my hosts file to get around it even if I remove that line from the the .htaccess file .但是,如果我确实将该行添加到 .htaccess 中,那么即使我从 .htaccess 文件中删除了该行,我也必须向我的主机文件中添加另一个条目以绕过它。

I'm fine with using a different 127.0.0.1 alias (host? what is that called?) but it's bugging me that this is still broken.我可以使用不同的 127.0.0.1 别名(主机?那叫什么?),但让我烦恼的是这仍然是坏的。

Just to be clear, I'm on Mac OS Leopard (but I'm not using the built in Apache setup, but MAMP).需要说明的是,我使用的是 Mac OS Leopard(但我没有使用内置的 Apache 设置,而是使用 MAMP)。

I've had a similar issue a couple times and renaming the file did not work for me.我遇到过几次类似的问题,重命名文件对我不起作用。 With OS X Lion I found the right configuration is this:使用 OS X Lion 我发现正确的配置是这样的:

<IfModule php5_module>
    AddType application/x-httpd-php .php
    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>
    AddType application/x-httpd-php-source .phps
    <IfModule dir_module>
        DirectoryIndex index.php index.html
    </IfModule>
</IfModule>

The magic that made it work for me is the SetHandler application/x-httpd-php part.让它为我工作的魔法是SetHandler application/x-httpd-php部分。

Of course edit the <IfModule php5_module> to your php version.当然,将<IfModule php5_module>编辑为您的 php 版本。

You are applying a mimetype where a handler should be (see documentation on handlers )您在处理程序应在的位置应用 mimetype(请参阅有关处理程序的文档

Try this instead:试试这个:

AddType application/x-httpd-php5 .php

EDIT: As you have indicated caching modules are loaded, you could read up on caching and htcacheclean (to clear the disk cache).编辑:正如您已经指出缓存模块已加载,您可以阅读缓存htcacheclean (以清除磁盘缓存)。 You can also temporarily use the CacheDisable directive.您还可以临时使用CacheDisable指令。 One other thing that you could also try is to rename the file that you have requested (eg index.php -> index.bak), request the file again in the browser (should now 404), then revert and try again.您还可以尝试的另一件事是重命名您请求的文件(例如 index.php -> index.bak),在浏览器中再次请求该文件(现在应该是 404),然后还原并重试。

Just note, make sure you don't have a htaccess file from your live environment accidentally downloaded with other files.请注意,请确保您的实时环境中没有与其他文件一起意外下载的 htaccess 文件。 Additionally, make sure you match your PHP version when editing htaccess.此外,请确保在编辑 htaccess 时匹配您的 PHP 版本。 Wrong version cause same issue-wrong settings.错误的版本会导致同样的问题——错误的设置。

Here is an example for running PHP7:下面是一个运行 PHP7 的例子:

application/x-httpd-ea-php71 .php .php7 .phtml应用程序/x-httpd-ea-php71 .php .php7 .phtml

I hope this info can help - it happened to me 8 years after ticket is created :)我希望这些信息能有所帮助 - 在创建票证 8 年后发生在我身上 :)

Perhaps you want application/x-httpd-php5 instead of application/x-httpd-php5s ?也许你想要application/x-httpd-php5而不是application/x-httpd-php5s (Note the lack of an s at the end.) (请注意末尾缺少s 。)

in my case deleting or comment out "AddHandler php56-cgi .php" in my root's involved htacces files solved it在我的情况下,删除或注释掉我的根涉及的 htacces 文件中的“AddHandler php56-cgi .php”解决了它

Best最好的事物

If someone has again this kind of issue, do something most important firstly.如果有人再次遇到这种问题,请先做一些最重要的事情。 I mean, use private navigation (without cache).我的意思是,使用私人导航(无缓存)。 I wasted my time because of this.因为这个,我浪费了我的时间。

GLHF GLHF

For the same issue, i removed the '5' AddType application/x-httpd-php .php .htm .html对于同样的问题,我删除了“5”AddType 应用程序/x-httpd-php .php .htm .html

its working fine!它的工作正常! try尝试

I actually had a very similar issue.我实际上有一个非常相似的问题。 All of my php files were downloading when I tried to test if php and apache were working together.当我尝试测试 php 和 apache 是否一起工作时,我的所有 php 文件都在下载。 It turns out they weren't working together.原来他们没有一起工作。

I had to uninstall php, I would recomend the same course of action and then reinstalling php just using the zip file download on php.net, instead of installing it with MAMP.我不得不卸载 php,我会推荐相同的操作过程,然后仅使用 php.net 上的 zip 文件下载重新安装 php,而不是使用 MAMP 安装它。 I think my problem was that I had used the php installer.我认为我的问题是我使用了 php 安装程序。 I do not recomend using that.我不建议使用它。

This website helped me a lot, I was having an issue with apache not starting and while that is not your issue, this website solved both the apache not starting and the downloading of php files issue and even though you are on a mac it may help you as well http://forums.phpfreaks.com/topic/185771-problem-starting-apache-2214-after-installing-php-5212该网站对我帮助很大,我遇到了 apache 无法启动的问题,虽然这不是您的问题,但该网站解决了 apache 无法启动和 php 文件下载问题,即使您使用的是 mac,它也可能有所帮助你也是http://forums.phpfreaks.com/topic/185771-problem-starting-apache-2214-after-installing-php-5212

Hope everything works!希望一切正常! Good Luck!祝你好运!

First check if your apache server is running.首先检查您的 apache 服务器是否正在运行。 Start->Run->cmd and then execute command:开始->运行->cmd然后执行命令:

netstat -abn netstat -abn

Lookup the result for line like this:像这样查找行的结果:

TCP 0.0.0.0:8888 0.0.0.0:0 LISTENING 600 [apache.exe] TCP 0.0.0.0:8888 0.0.0.0:0 聆听 600 [apache.exe]

If you cant find anything listening on port 8888 ( no 0.0.0.0:8888 line) then your apache is failing to start.如果您在端口 8888(没有 0.0.0.0:8888 行)上找不到任何侦听内容,那么您的 apache 无法启动。 To find out why it cant start you should find apache log directory and examine the error.log (may be you have updated your php resently?).要找出它无法启动的原因,您应该找到 apache 日志目录并检查 error.log(可能是您最近更新了 php?)。 If you find 0.0.0.0:80 listening line but some other software is listening there (do you have IIS running?) then you should remove / reconfigure that softure to free port 80. If you have apache listening on port 80 but still cant open your site and you cant figure out what is causing the problem via examining apache log files then it my be database problem.如果您发现 0.0.0.0:80 侦听线路但其他一些软件正在侦听那里(您是否正在运行 IIS?)那么您应该删除/重新配置该软件以释放端口 80。如果您有 apache 侦听端口 80 但仍然无法打开您的网站,您无法通过检查 apache 日志文件找出导致问题的原因,然后是数据库问题。 Check if your mysql is running and listening using same command but you should be looking for检查您的 mysql 是否正在运行并使用相同的命令进行侦听,但您应该寻找

TCP 0.0.0.0:3306 0.0.0.0:0 LISTENING [mysqld-nt.exe] TCP 0.0.0.0:3306 0.0.0.0:0 侦听 [mysqld-nt.exe]

If you cant find such line then your mysql server is not running - check mysql log files for errors.如果你找不到这样的行,那么你的 mysql 服务器没有运行 - 检查 mysql 日志文件是否有错误。 If both servers are running and you cant get any output in your browser then check your firewall and antivirus - they may block your requests.如果两个服务器都在运行并且您无法在浏览器中获得任何输出,请检查您的防火墙和防病毒软件 - 它们可能会阻止您的请求。 Hope this helps ;)希望这可以帮助 ;)

I had the same issue and it was because inside this folder you have .htaccess file hidden with some custom code, for me was because I copy from my running website server.我遇到了同样的问题,这是因为在这个文件夹中你有 .htaccess 文件隐藏了一些自定义代码,对我来说是因为我从我正在运行的网站服务器复制。 Try to rename the file and you will see your project.尝试重命名文件,您将看到您的项目。 Then customise the file in your needs.然后根据您的需要自定义文件。

Just remove the comment删除评论就好了

<IfDefine PHP>
   LoadModule php_module        modules/libphp.so
</IfDefine>

in etc/extra/httpd-xampp.conf在 etc/extra/httpd-xampp.conf

就我而言,在克隆 repo 并尝试设置项目后,我不得不运行composer update来安装所有依赖项,然后打开页面而不是下载文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM