你如何让 php 在 Mac OS X 上工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6790568/
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-08-26 01:15:21  来源:igfitidea点击:

How do you get php working on Mac OS X?

phpmacososx-lion

提问by AJA

I have recently updated to Lion and enabled Web Sharing in the System Preferences but I am unable to get php working.

我最近更新到 Lion 并在系统偏好设置中启用了网络共享,但我无法让 php 工作。

I added an info file to the web root directory and it outputs the file as text.

我在 web 根目录中添加了一个 info 文件,它将文件输出为文本。

info.php
with the content
<?php phpinfo(); ?>

回答by Matt Gibson

(Edit: This method appears to work fine for 10.9 (Mavericks), 10.10 (Yosemite) and 10.11 (El Capitan), so I figured I'd mention that for any new influx of slightly frustrated OS X updaters :D )

(编辑:此方法似乎适用于 10.9(小牛队)、10.10(优胜美地)和 10.11(El Capitan),所以我想我会提到任何新涌入的略有沮丧的 OS X 更新程序:D)

Edit your /etc/apache2/httpd.conf and make sure the line:

编辑您的 /etc/apache2/httpd.conf 并确保该行:

LoadModule php5_module libexec/apache2/libphp5.so

...exists. I think it's commented out by default in the standard OS X config, but from what I remember, you just need to uncomment it, then re-start Apache:

……存在。我认为它在标准 OS X 配置中默认已被注释掉,但据我所知,您只需要取消注释,然后重新启动 Apache:

sudo apachectl restart

And you should be good to go.

你应该很高兴去。

回答by Ronaldt

UPDATE:Please note that this was written for OS X pre-(High) Sierra. If you run OSX 10.12 or newer, please follow this more than excellent guide by Andy Miller: macOS 10.15 Catalina Apache Setup: Multiple PHP Versions

更新:请注意,这是为 OS X pre-(High) Sierra 编写的。如果您运行 OSX 10.12 或更新版本,请遵循 Andy Miller 的优秀指南:macOS 10.15 Catalina Apache Setup:Multiple PHP Versions



I too like to use things that are basically already there. I don't see why anyone would use MAMP or AMPPS (or any other packed 3rd party out-of-box webserver app) when Mac OS X comes with apache and PHP by default.

我也喜欢使用基本上已经存在的东西。当 Mac OS X 默认附带 apache 和 PHP 时,我不明白为什么有人会使用 MAMP 或 AMPPS(或任何其他打包的 3rd 方开箱即用的网络服务器应用程序)。

Took me a couple of tries to get it working, so here is basically what did it for me and hopefully it'll help you guys save a little time.

我试了几次才让它工作,所以这基本上是为我做的,希望它能帮助你们节省一点时间。

Like Matt Gibson said, start terminal and type: (sudo requires your root password)

就像 Matt Gibson 所说的,启动终端并输入:(sudo 需要您的 root 密码)

sudo nano /etc/apache2/httpd.conf

Then uncomment this line by removing the '#' in front of it (ctrl+v can be used as page-down)

然后通过删除它前面的“#”来取消注释这一行(ctrl+v 可以用作向下翻页)

LoadModule php5_module libexec/apache2/libphp5.so

To make sure you can include files etc in PHP, scroll to "User _www" (in my case) and change that to: (where "yourusername" is the user you login with)

为了确保您可以在 PHP 中包含文件等,滚动到“用户 _www”(在我的情况下)并将其更改为:(其中“您的用户名”是您登录的用户)

User yourusername

You can leave the group as-is, "Group _www" by default on a fresh OS X Mountain Lion install.

在全新的 OS X Mountain Lion 安装中,您可以按原样保留该组,默认情况下为“Group _www”。

On default apache only looks for index.html, so search for "DirectoryIndex index.html" and change that to: (adding index.html at the end is optional of course)

默认 apache 只查找 index.html,因此搜索“DirectoryIndex index.html”并将其更改为:(当然在最后添加 index.html 是可选的)

DirectoryIndex index.php index.html index.htm

Exit and save by pressing ctrl+x (and confirm with "y")

按 ctrl+x 退出并保存(并用“y”确认)

Then restart apache:

然后重启apache:

sudo apachectl restart

My phpinfo(); returned with a PHP Version 5.3.15

我的 phpinfo(); 返回 PHP 版本 5.3.15

==================

==================

Since I find it useful to have my local sites in my user dir, I created a directory /Users/yourusername/Sites (which isn't there on default anymore in Mountain Lion).

因为我发现在我的用户目录中拥有我的本地站点很有用,所以我创建了一个目录 /Users/yourusername/Sites(它在 Mountain Lion 中不再默认存在)。

Again, edit httpd.conf via "sudo nano /etc/apache2/httpd.conf" and ...

再次,通过“sudo nano /etc/apache2/httpd.conf”编辑 httpd.conf 并...

Scroll down to "DocumentRoot" and change it to: (where "yourusername" is the username you login with)

向下滚动到“Doc​​umentRoot”并将其更改为:(其中“yourusername”是您登录时使用的用户名)

DocumentRoot "/Users/yourusername/Sites/"

Scroll to where it says "# This should be changed to whatever you set DocumentRoot to." and change the next line to: (where "yourusername" is the username you login with)

滚动到“#This should be changed to what you set DocumentRoot to.”的地方。并将下一行更改为:(其中“您的用户名”是您登录时使用的用户名)

<Directory "/Users/yourusername/Sites/">

Then exit and save by pressing ctrl+x (and confirm with "y")

然后按 ctrl+x 退出并保存(并用“y”确认)

Restart apache.

重启阿帕奇。

回答by cwallenpoole

I have one word for you. MAMP.

我对你说一句话。马普

回答by ydk2

Try it phpbrew

试试phpbrew

It's ease build, install tool for PHP, any version.

它很容易构建,安装任何版本的 PHP 工具。

回答by Harman Gill

For the latest version of mac os i.e Mojave 10.14, follow the steps below to activate PHP server:

对于最新版本的 mac os ie Mojave 10.14,请按照以下步骤激活 PHP 服务器:

  1. Open terminal. Press command+spacebar, type terminal and press enter.

  2. Enter command $ sudo nano /etc/apache2/httpd.confto edit httpd.conf file in nano, which is an inbuilt file editor in macOS.

  3. Enter the password.

  4. Now search loadmoduleby pressing control+w.

  5. Find file named #LoadModule php7_module libexec/apache2/libphp7.so

  6. Uncomment the file by removing the # symbol present at the very beginning of the module.

  7. Now exit the editor by pressing control+x, press y to save the changes and press enter to confirm changes to the httpd.conf file.

  8. Now restart the apache server by entering the command

    $ sudo apachectl restart

  9. PHP server should be running now running.

  1. 打开终端。按命令+空格键,输入终端并按回车键。

  2. $ sudo nano /etc/apache2/httpd.conf在nano中输入命令编辑httpd.conf文件,nano是macOS内置的文件编辑器。

  3. 输入密码。

  4. 现在按 control+w搜索loadmodule

  5. 查找名为 #LoadModule php7_module libexec/apache2/libphp7.so 的文件

  6. 通过删除模块开头的 # 符号来取消对文件的注释。

  7. 现在按 control+x 退出编辑器,按 y 保存更改,然后按 enter 确认对 httpd.conf 文件的更改。

  8. 现在通过输入命令重新启动apache服务器

    $ sudo apachectl restart

  9. PHP 服务器现在应该正在运行。

Now you can check the working of your server by opening the PHP file from your default server directory or you can also change the directory as well.

现在,您可以通过从默认服务器目录中打开 PHP 文件来检查服务器的工作情况,或者您也可以更改该目录。

回答by TrickyJ

After upgrading the MAC OSX to 10.14.5 My Apache config file was new and all my previous settings were replaced with default settings.

将 MAC OSX 升级到 10.14.5 后,我的 Apache 配置文件是新的,我以前的所有设置都替换为默认设置。

I was not able to open any PHP files in my browser. I followed the above steps and it resolved the issue.

我无法在浏览器中打开任何 PHP 文件。我按照上述步骤操作并解决了问题。

Note- In OSX 10.14.5 there is Php7.1.23

注意- 在 OSX 10.14.5 中有 Php7.1.23

Steps I followed -

我遵循的步骤 -

  1. Edit the httpd.conf file located under /etc/apache2/httpd.conf
  2. This below given live was commented. I uncommented it by removing the # sign and saved the httpd.conf file and restarted the apache service.
  1. 编辑位于 /etc/apache2/httpd.conf 下的 httpd.conf 文件
  2. 下面给出了现场评论。我通过删除 # 符号取消注释并保存 httpd.conf 文件并重新启动 apache 服务。

LoadModule php7_module libexec/apache2/libphp7.so

LoadModule php7_module libexec/apache2/libphp7.so

  1. sudo apachectl restart
  1. 须藤apachectl重启

Thank you for the solution, appreciated your help friends.

感谢您的解决方案,感谢您的帮助朋友。

Regards,

问候,

Vicky Jadhav (India - pune)

Vicky Jadhav(印度 - 浦那)