(MAC)PHP Apache 安装与配置

简介: (MAC)PHP Apache 安装与配置

MAC 默认是自带 Apache 服务器的,但是我们需要后天的去使用配置一下它。


基础命令

// 开启
sudo apachectl -k start
// 关闭
sudo apachectl -k stop
// 重启
sudo apachectl -k restart
// 开启
sudo apachectl start
// 关闭
sudo apachectl stop
// 重启
sudo apachectl restart
// mac hosts 文件目录
/private/etc/hosts
// 配置文件路径
/etc/apache2/httpd.conf
// 备份配置文件
sudo cp httpd.conf httpd.conf.backup
// 还原配置(其实就是用备份文件覆盖)
sudo cp httpd.conf.backup httpd.conf
// 虚拟机配置路径
/etc/apache2/extra/httpd-vhosts.conf
// php.ini 路径
/etc/php.ini
// 将 php.ini.default 拷贝为 php.ini
sudo cp php.ini.default php.ini


  • httpd.conf:为 Apache 服务器的配置文件。
  • php.ini:为 PHP 语言的配置文件。
  • 上面启动参数里面有带 -k 跟没带 -k 的启动两种方式,可以看看 Apache起步命令加-k参数和不加的区别,但是有一点需要注意,如果你是用 -k 的启动那么你后面的停止重启都需要用带 -k 命令,否则会不生效,停止或者重启不了,使用不带 -k 的启动也是一样的道理。
  • 我这边是新机器,之前也没有对 Apache 服务器做过处理的,所以我们先开机就来启动一下 Apache:
sudo apachectl -k start

运行之后结果:

dengzemiaodeMacBook-Pro:~ dengzemiao$ sudo apachectl -k start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using dengzemiaodeMacBook-Pro.local. Set the 'ServerName' directive globally to suppress this message
dengzemiaodeMacBook-Pro:~ dengzemiao$ 

咋们先不管警告啥的,因为咋们是新机器肯定啥都没有的,启动之后浏览器打开IP地址:http://localhost 或 http://127.0.0.1,如果浏览器输出 It works!,那就是启动成功了。

看来还是启动成功的!既然启动成功了!那么我们可以继续下一步…


修改 Apache 的配置文件

  • 修改前先备份配置文件 httpd.conf(使用上面的终端命令),然后用 sublime text、vscode、文本编辑等编辑器打开 /etc/apache2 下的 httpd.conf 文件。
  • Windows 上面是需要配置 Apache 路径的,Mac上则不需要这个步骤。
  • 插入 Indexes 字段,开发阶段使用,线上服务器则需要去掉

按 Command+F 搜索关键字 "FollowSymLinks",在 Options 和 FollowSymLinks 之间插入单词 Indexes。


Indexs 配置的作用是如果不存在 Index.html 文件的时候, 将该目录下的文件树列出来


# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks Multiviews
    MultiviewsMatch Any
    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    AllowOverride None
    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

没有加入 Indexes 的打开 http://127.0.0.1 效果:

加入 Indexes 的打开 http://127.0.0.1 效果:

  • 修改默认服务器地址

搜索关键字 "ServerName",如果不修改,就是 http://127.0.0.1

# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
#ServerName www.example.com:80 -》 默认配置就是 127.0.0.1
# 现在我们需要自定义一个 ServerName
ServerName 127.0.0.1


  • 打开 php

搜索关键字 "php7_module"(根据版本不同,数字也可能是php3456等),默认是被注释的,需要手动打开。


#LoadModule php7_module libexec/apache2/libphp7.so -》默认是被注释的
LoadModule php7_module libexec/apache2/libphp7.so
  • 设置本地资源文件的根目录

一般在用户根目录 /Users/电脑名字 下创建文件夹 Sites 作为存放本地资源的根目录。(直接用 /Users/xxxx 根目录的原因:为了避免在 DesktopDocuments等目录下出现的子文件夹需要逐级授权问题)

然后在配置文件中搜索关键字 "DocumentRoot",将 DocumentRootDirectory 后面的默认路径替换为上面的自定义路径。

# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
# /Library/WebServer/Documents -》 默认根目录
# /Users/dengzemiao/Sites -》 自定义根目录
DocumentRoot "/Users/dengzemiao/Sites"
<Directory "/Users/dengzemiao/Sites">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks Multiviews
    MultiviewsMatch Any
    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    AllowOverride None
    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

我在自定义目录里面创建一个自定义 index2.html(里面其他文件是我从默认路径那边拷贝过来的,这些都是没用的文件只是测试用一下):



c9cf7d89ec22c363b37f53feea64ff5f_3e3ae170a133660268b49c214597d102.png

然后在启动 `Apache` 之后访问路径:http://127.0.0.1/index2.html

b1eebef6084eb3922b7ce9ac46c8608e_e73f6c67026484b2dedcaf6e04a730f3.png


注意了:以后开发写的php文件就是在这个 Sites 文件夹中进行创建文件或者项目开发,因为只有这里面你的php文件才会被执行成功,在其他地方则会显示源码不会被执行成功。

8ef256ca790c333cc70b159471319679_3cd57ea9be4ada51ac13cd325792ba2b.png



cd /etc
sudo cp php.ini.default php.ini

(这一步好像可以不用配置,我忘了之前的操作了) 并在 httpd.conf 中添加配置:

// 只需要添加路径即可,不需要带文件名
PHPIniDir /private/etc
  • 启动Apache服务
sudo apachectl -k start

启动完成后,用浏览器访问 127.0.0.1,可以看到放置在本地资源根目录下的文件。


IP(127.0.0.1)也可以换成你电脑的IP地址,这样在同一局域网的设备也可以访问服务器的内容。


用完 Apache 要手动关闭,否则会一直占用内存。


到这里为止 Apache 也就配置完成了!!!


下面是一些 Apache 细节检测设置功能介绍,可选择性了解。

检测配置文件是否有问题

如果配置文件不小心改错或者写错东西

<IfDefine SERVER_APP_HAS_DEFAULT_PORTS>
    Listen 8080
</IfDefine>
<IfDefine !SERVER_APP_HAS_DEFAULT_PORTS>
    Listen 80
</IfDefine>
不小心改成
<IfDefine SERVER_APP_HAS_DEFAULT_PORTS>
    Listen 8080
</IfDefine>
<IfDefine !SERVER_APP_HAS_DEFAULT_PORTS>
    Listenss1 80
</IfDefine>

可以通过命令行

sudo apachectl -t

来检测配置文件是否有问题,上面改错之后会输出


dengzemiaodeMacBook-Pro:etc dengzemiao$ sudo apachectl -t
AH00526: Syntax error on line 56 of /private/etc/apache2/httpd.conf:
Invalid command 'Listenss1', perhaps misspelled or defined by a module not included in the server configuration
dengzemiaodeMacBook-Pro:etc dengzemiao$


如果没有问题,则会输出:

dengzemiaodeMacBook-Pro:etc dengzemiao$ sudo apachectl -t
Syntax OK

PHP Apache - 多站点虚拟主机配置


相关文章
|
4天前
|
Ubuntu PHP
Ubuntu下使用apt为Apache2编译PHP7.1
以上就是在Ubuntu系统下,使用apt为Apache2编译PHP7.1的过程。希望这个过程对你有所帮助,如果你在执行过程中遇到任何问题,都可以在网上找到相关的解决方案。
38 25
|
10天前
|
Ubuntu PHP Apache
在Ubuntu系统中为apt的apache2编译PHP 7.1的方法
以上就是在Ubuntu系统中为apt的apache2编译PHP 7.1的方法。希望这个指南能帮助你成功编译PHP 7.1,并在你的Apache服务器上运行PHP应用。
53 28
|
7天前
|
关系型数据库 MySQL Linux
查看Linux、Apache、MySQL、PHP版本的技巧
以上就是查看Linux、Apache、MySQL、PHP版本信息的方法。希望这些信息能帮助你更好地理解和使用你的LAMP技术栈。
54 17
|
1月前
|
Ubuntu Linux Shell
Ubuntu gnome WhiteSur-gtk-theme类mac主题正确安装和卸载方式
通过这个过程,用户不仅可以定制自己的桌面外观,还可以学习到更多关于 Linux 系统管理的知识,从而更好地掌握系统配置和主题管理的技巧。
112 12
|
1月前
|
监控 Shell Linux
Android调试终极指南:ADB安装+多设备连接+ANR日志抓取全流程解析,覆盖环境变量配置/多设备调试/ANR日志分析全流程,附Win/Mac/Linux三平台解决方案
ADB(Android Debug Bridge)是安卓开发中的重要工具,用于连接电脑与安卓设备,实现文件传输、应用管理、日志抓取等功能。本文介绍了 ADB 的基本概念、安装配置及常用命令。包括:1) 基本命令如 `adb version` 和 `adb devices`;2) 权限操作如 `adb root` 和 `adb shell`;3) APK 操作如安装、卸载应用;4) 文件传输如 `adb push` 和 `adb pull`;5) 日志记录如 `adb logcat`;6) 系统信息获取如屏幕截图和录屏。通过这些功能,用户可高效调试和管理安卓设备。
|
5月前
|
开发工具 git 开发者
「Mac畅玩鸿蒙与硬件3」鸿蒙开发环境配置篇3 - DevEco Studio插件安装与配置
本篇将专注于如何在 DevEco Studio 中安装和配置必要的插件,以增强开发功能和提升效率。通过正确配置插件,开发流程能够得到简化,开发体验也会更加顺畅。
251 1
「Mac畅玩鸿蒙与硬件3」鸿蒙开发环境配置篇3 - DevEco Studio插件安装与配置
|
5月前
|
开发工具 iOS开发 开发者
「Mac畅玩鸿蒙与硬件2」鸿蒙开发环境配置篇2 - 在Mac上安装DevEco Studio
本篇将专注于如何在 Mac 上安装鸿蒙开发工具 DevEco Studio,确保开发环境能够顺利搭建。完成安装后,可以正式开始鸿蒙应用的开发工作。
268 1
「Mac畅玩鸿蒙与硬件2」鸿蒙开发环境配置篇2 - 在Mac上安装DevEco Studio
|
5月前
|
安全 PHP 开发者
php中配置variables_order详解
`variables_order` 是 PHP 配置中的一个关键指令,它决定了不同来源的变量被导入到全局变量空间的顺序。正确配置 `variables_order` 不仅可以确保变量的正确处理和覆盖顺序,还能提高应用程序的安全性。开发者应根据具体应用的需求,合理配置 `variables_order`,确保应用的稳定和安全运行。
73 5
|
5月前
|
监控 PHP Apache
优化 PHP-FPM 参数配置:实现服务器性能提升
优化PHP-FPM的参数配置可以显著提高服务器的性能和稳定性。通过合理设置 `pm.max_children`、`pm.start_servers`、`pm.min_spare_servers`、`pm.max_spare_servers`和 `pm.max_requests`等参数,并结合监控和调优措施,可以有效应对高并发和负载波动,确保Web应用程序的高效运行。希望本文提供的优化建议和配置示例能够帮助您实现服务器性能的提升。
240 3
|
7月前
|
iOS开发 MacOS Windows
Mac air使用Boot Camp安装win10 ,拷贝 Windows 文件时出错
Mac air使用Boot Camp安装win10 ,拷贝 Windows 文件时出错

热门文章

最新文章

推荐镜像

更多
下一篇
oss创建bucket