Configure Apache and php on Mac

(The following steps, I personally test and use it)

In the Mac environment, the Apache (Apache server) and PHP that come with the system itself are pre-installed. Let's step by step to operate the merger of Apache and PHP:

The first step: (can be omitted, just understand that there is apache on the mac):: Used to view the pre-installed Apache and PHP versions of the system:

(1)打开终端,输入命令:sudo apachectl -version

write picture description here

(2)输入命令:sudo php -v

write picture description here

Step 2: Modify the Apache configuration file

(1) 前往Finder,(一般快捷键为:command+shift+G),输入地址:/private/etc/apache2 

write picture description here

2)找到名为 httpd.conf 的文件,选择TextEdit打开.
    (因为此文件为locked不允许被修改.所以,我们可以将其复制到桌面上,修改后将原文件删掉,再将修改后的文件移到原文件的位置)

write picture description here

3)打开后使用搜索关键字“php” (一般快捷键为command+F),搜索结果如图所示:

write picture description here

(4)删除这行文本最前面的那个“#”号,删除后保存一下,关闭文档,用其替换原文档

write picture description here

Step 3: Generate a PHP configuration file

1)前往Finder,(一般快捷键为:command+shift+G),输入地址:/private/etc   只需到etc目录下,在这个目录下找到一个名为 php.ini.default 的文件. 将这个文件复制一下,仍然拷贝到当前目录下,然后将新拷贝的文件重命名为 php.ini,注意:只改文件名,内容不需要修改.修改后,如图所示:

write picture description here

Step 4: Restart the Apache service

(1)打开终端,输入命令: sudo apachectl restart

(Open http://localhost at this point : the webpage will show that it works!)

Step 5: Test run the PHP file

1)前往Finder,(一般快捷键为:command+shift+G),输入地址:/Library/WebServer(需要强调的是这个文件夹是服务器的根路径文件夹,默认情况下我们是没有读写权限的,所以我们需开启读写权限,这样也方便以后的开发工具对这里的文件进行读写。)具体操作步骤见图:

Show introduction ->
write picture description here
click the small lock in the lower right corner -> enter the password
write picture description here

(2) 在 Documents 文件夹下(再次强调这就是服务器的根路径!)创建一个名为 index.php 的文件(名字随便起,为了测试),在里面写如下php的代码,保存一下:
<?php

  phpinfo();

?>

write picture description here

3)打开浏览器访问我们刚才创建的文件,在地址栏输入:http://localhost/index.php, 如图所示,则成功:

write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325532382&siteId=291194637