20190613 很久不再使用 apache 服务器,最近试试我的本机上的 apache2,发现又不能用了!
我希望在本机调试 php 程序,所以需要 apache 支持 php
为了调试方便,我需要直接列举虚拟主机上的所有文件夹,通过鼠标点击选择网站目录和文件,操作简单!反正也不会拿他当服务器使用!
先找到我自己以前的操作记录
https://my.oschina.net/u/1440971/blog/824415
mac 系统升级之后,经常会发生一些变化,但是,以前的操作总是可以参考的!
参考资料:Apache 官网
http://httpd.apache.org/docs/2.4/vhosts/
*** 本次 mac PS 版本:mac OS Mojave 10.14.5
1、准备工作
-
直接浏览器上输入 127.0.0.1
It works!
说明缺省虚拟主机配置是正确的!
输入 127.0.0.1/info.php ,结果一片空白! -
通过 Finder ,设置我的虚拟目录权限,全部可读写
** 缺省虚拟主机目录 DocumentRoot “/Library/WebServer/Documents”
** 使用次数不多,本次不再单独建立自己的虚拟主机目录了 -
打开 Terminal 终端,查看php 和 apache2 信息
$ php -vPHP 7.1.23 (cli) (built: Feb 22 2019 22:19:32) ( NTS )
Copyright © 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright © 1998-2018 Zend Technologies$ httpd -v
Server version: Apache/2.4.34 (Unix)
Server built: Feb 22 2019 20:20:11
$ which httpd
/usr/sbin/httpd
$ httpd -S
在这里可以看到 DocumentRoot 、ErrorLog 等信息 AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using dhbm-on-mac20180816.local. Set the 'ServerName' directive globally to suppress this message VirtualHost configuration: ServerRoot: "/usr" Main DocumentRoot: "/Library/WebServer/Documents" Main ErrorLog: "/private/var/log/apache2/error_log" Mutex default: dir="/private/var/run/" mechanism=default Mutex mpm-accept: using_defaults PidFile: "/private/var/run/httpd.pid" Define: DUMP_VHOSTS Define: DUMP_RUN_CFG User: name="_www" id=70 not_used Group: name="_www" id=70 not_used
2、修改配置文件
-
打开 php 支持
sudo vim /etc/apache2/httpd.conf
找到以下 libphp7.so 模块地方,去掉前面的注释 ## by wzh 20190613 enable php7 LoadModule php7_module libexec/apache2/libphp7.so
-
放开虚拟目录权限
sudo vim /etc/apache2/httpd.conf
找到 < Directory /> 的地方,放开 AllowOverride 和 Require 限制<Directory /> # by wzh 20190613 # AllowOverride none # Require all denied allow from all AllowOverride All Require all granted
同样找到
DocumentRoot “/Library/WebServer/Documents”
<Directory “/Library/WebServer/Documents”>
放开限制
# by wzh 20190613 AllowOverride None
#
# Controls who can get stuff from this server.
#
Require all granted
# add by wzh 20190613 none ->all
Options All
AllowOverride All
Allow from all
- 测试 php
curl 127.0.0.1/info.php
curl localhost/info.php
浏览器测试
http://localhost/info.php
http://127.0.0.1/info.php
4. 测试虚拟目录
http://localhost/
http://127.0.0.1/
*** mac OS 升级经常会造成 apache2 配置的一些变化,但是,感觉每次都会变的越来越简单!比起以前折腾过的两次,本次是最省事的一次!
*** 主要就是:1、加载 php 模块 2、放开目录权限 3、放开虚拟目录权限
*** 也许有些变化在上一次折腾的时候已经做过了!
3、新建本地虚拟目录和网站
-
增加一个虚拟目录
sudo vim /etc/apache2/httpd.conf** 本次为了简化,直接在缺省虚拟目录下新建子目录test123,作为测试网站站点的目录
** 每增加一个站点,就要先增加一个目录(我这里建立了 2 个)# add by wzh 20190613 new dir for other website:test123.wzh # for vhost test DocumentRoot "/Library/WebServer/Documents/test123" <Directory "/Library/WebServer/Documents/test123"> Require all granted # by wzh 20190613 none ->all Options All AllowOverride All Allow from all </Directory> # add by wzh 20190613 new dir for other website : tp123.wzh # for ThinkPHP test on 8080 port DocumentRoot "/Library/WebServer/Documents/tp123" <Directory "/Library/WebServer/Documents/tp123"> Require all granted # by wzh 20190613 none ->all Options All AllowOverride All Allow from all </Directory>
-
增加一个虚拟站点
$ sudo vim /etc/apache2/extra/httpd-vhosts.conf
*** 在文件最后增加以下 2 个测试站点# add by wzh 20190613 <VirtualHost *:80> ServerAdmin [email protected] DocumentRoot "/Library/WebServer/Documents/test123/" ServerName test123.wzh ErrorLog "/private/var/log/apache2/test123-error_log" CustomLog "/private/var/log/apache2/test123-access_log" common </VirtualHost> # add by wzh 20190613 <VirtualHost *:80> ServerAdmin [email protected] DocumentRoot "/Library/WebServer/Documents/test123/" ServerName test123.wzh ErrorLog "/private/var/log/apache2/test123-error_log" CustomLog "/private/var/log/apache2/test123-access_log" common </VirtualHost> # add by wzh 20190613 <VirtualHost *:8080> ServerAdmin [email protected] DocumentRoot "/Library/WebServer/Documents/tp123/" ServerName tp123.wzh ErrorLog "/private/var/log/apache2/tp123-error_log" CustomLog "/private/var/log/apache2/tp123-access_log" common </VirtualHost>
3.增加一条本地映射
** 仅供本地测试用,外网是找不到这 2 个域名的
$ sudo vim /etc/hosts
在最后增加 2 条记录
# add by wzh 20190623
127.0.0.1 test123.wzh
127.0.0.1 tp123.wzh
-
增加一个本地 8080 端口 listen
$ sudo vim /etc/apache2/extra/httpd-vhosts.conf** 如果不用80 端口之外的其他端口,可以忽略这一步
** 找到以下位置,增加 2 条自己的 listen#Listen 12.34.56.78:80 <IfDefine SERVER_APP_HAS_DEFAULT_PORTS> Listen 8080 </IfDefine> <IfDefine !SERVER_APP_HAS_DEFAULT_PORTS> Listen 80 </IfDefine> # add by wzh 20190613 Listen 127.0.0.1:80 Listen 127.0.0.1:8080
-
重启生效
$ sudo apachectl restart
4、测试一下
** 为了简化,先去关闭mac OS 防火墙
** mac OS 防火墙 从以前的 ipfw 改到现在的 pf
** 目前为止我也不知道怎么开放指定端口
ping tp123.wzh
ping test123.wzh
$ curl test123.wzh
$ curl test123.wzh/index.php
$ curl test123.wzh:8080
$ curl test123.wzh:8080/index.php
** 如果发现错误 curl: (7) Failed to connect to tp123.wzh port 8080: Connection refused
** 请参照上 一步:增加一个本地 8080 端口 listen
$ curl tp123.wzh
$ curl tp123.wzh/index.php
$ curl tp123.wzh:8080
$ curl tp123.wzh:8080/index.php
浏览器测试
http://test123.wzh/
http://test123.wzh/index.php
http://tp123.wzh/
http://tp123.wzh/index.php
http://test123.wzh:8080/
http://test123.wzh:8080/index.php
以上结果仅贴图一个为例
5、配置多虚拟目录、域名支持
参考
https://www.cnblogs.com/maowenqiang/p/5120767.html
写得非常清楚,这里再抄写一次
cd /etc/apache2
sudo vim httpd.conf
# by wzh 20190628 启用apache的虚拟主机功能
# LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
我这里没有打开这个!直接所有域名网站都放在 httpd-vhosts.conf 里面
# Virtual hosts
# by wzh 20190628 多域名支持
Include /private/etc/apache2/extra/httpd-vhosts.conf
cd /etc/apache2/extra
sudo vim httpd-vhosts.conf
加上新创建的虚拟目录对应的域名
# add by wzh 20190628 tp6.wzh
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/Users/dhbm/php20190628"
ServerName tp6.wzh
ErrorLog "/private/var/log/apache2/tp6--error_log"
CustomLog "/private/var/log/apache2/tp6-access_log" common
</VirtualHost>
cd /etc
sudo vim hosts
加上刚刚创建的域名和虚拟主机
# add by wzh 20190628
127.0.0.1 tp6.wzh
重启 apache2 生效
sudo apachectl restart
测试一下
http://tp6.wzh/tp/public/
6、错误处理
-
配置了多个虚拟目录之后,总是全部指向最后一个
按照以上 5、配置多虚拟目录、域名支持
cd /etc/apache2
sudo vim httpd.conf
放开以下 2 处 的 # 注释# by wzh 20190628 启用apache的虚拟主机功能 # LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so # Virtual hosts # by wzh 20190628 多域名支持 Include /private/etc/apache2/extra/httpd-vhosts.conf
这样的话,所有的网站都会到 /etc/apache2/extra/httpd-vhosts.conf 里面去查找,按照 ServerName 对应响应的虚拟目录
-
配置了vhost 支持之后,localhost 和 127.0.0.1 反而打不开了
ping 127.0.0. 和 ping localhost 都不通,因为 mac 开着防火墙?
测试一下 apache 语法
httpd -t
显示以下的错误AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using dhbm-on-mac20180816.local. Set the 'ServerName' directive globally to suppress this message
百度以下,找到这个博客 https://blog.csdn.net/weixin_43245095/article/details/89556756
按照他的建议修改
sudo vim /etc/apache2/httpd.conf
找到 ServerName 修改如下
# add by wzh 20190629
# 解决 httpd -t 错误
ServerName 127.0.0.1:80
restart 之后,再次 测试
httpd -t
Syntax OK
-
查看 apache 状态
sudo apachectl status 显示如下提示Go to http://localhost:80/server-status in the web browser of your choice.
Note that mod_status must be enabled for this to work.
参考 https://blog.csdn.net/wangkepermit/article/details/72954995
需要开启status功能
我的理解:
找到以下 mod_status.so
LoadModule status_module libexec/apache2/mod_status.so
另外 httpd-info 的地方,缺省是注释的
# Real-time info on requests and configuration
#Include /private/etc/apache2/extra/httpd-info.conf
可能涉及到安全性问题,目前暂时不需要,所以,这里保持不动!
记录一下简单命令
启动、停止 apache
sudo apachectl start
sudo apachectl stop
sudo apachectl restart
sudo apachectl status
检查 httpd 语法
httpd -t
查看apache 加载了那些模块
sudo apachectl -M
或 sudo apachectl -t -D DUMP_MODULES