macOS 13 install PHP 8 and Apache config Apache Virtual Host All In One
macOS 13 install PHP 8 and Apache config Apache Virtual Host All In One
目的:搭建本地 PHP 网站用于对
XSS
/CSRF
等 Web 攻击安全问题进行验证
macOS 13 & PHP 8.x
# PHP was deprecated in macOS 11 and removed from macOS 12
$ brew install php
# PHP 8.1.13
$ php -v
PHP 8.1.13 (cli) (built: Nov 24 2022 20:36:17) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.13, Copyright (c) Zend Technologies
with Zend OPcache v8.1.13, Copyright (c), by Zend Technologies
https://formulae.brew.sh/formula/php#default
PHP 8.2
Apache & PHP
# Apache 2
$ apachectl -v
Server version: Apache/2.4.54 (Unix)
Server built: Sep 30 2022 02:51:01
$ sudo apachectl start
$ sudo apachectl stop
$ sudo apachectl restart
error
macOS 系统自带的 Apache 配置 PHP 后,会出现无法访问问题 ❌ (macOS 不支持 PHP 了,配置了会导致服务器报错)💩
macOS 13 修改 Apache 的配置文件 httpd.conf
添加 PHP 8 配置后,导致无法启动
# PHP 8.1 ❌ 报错
# LoadModule php_module /usr/local/opt/php/lib/httpd/modules/libphp.so
<IfModule dir_module>
# DirectoryIndex index.html ✅ 不报错,但是 index.php 会纯文本展示
DirectoryIndex index.html index.php
</IfModule>
# PHP 8.1 ❌ 报错
# <FilesMatch \.php$>
# SetHandler application/x-httpd-php
# </FilesMatch>
https://www.clloz.com/programming/backend/php/2021/12/22/macos-12-brew-install-httpd-php8/
solution
# 重新安装 Apache
$ brew install httpd
# web 目录
/usr/local/var/www
# 8080 => 80
/usr/local/etc/httpd/httpd.conf
# 8443 => 443
/usr/local/etc/httpd/extra/httpd-ssl.conf
#
$ brew services restart httpd
# 后台运行
$ /usr/local/opt/httpd/bin/httpd -D FOREGROUND
这将卸载启动守护进程并使 macOS 内置的 Apache 失去能力,
# 抓取 80 端口的进程
$ sudo lsof -i:80
# 启动 Apache httpd 服务
$ brew services start httpd
# 关闭
$ brew services stop httpd
# 重启
$ brew services restart httpd
phpinfo
<?php
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-08-01
* @modified
*
* @description PHP.info
* @augments
* @example
* @link http://192.168.1.12:8090/info.php
*
*/
// php info
phpinfo();
echo('$php_info');
// return 1
// $php_info = phpinfo();
// echo($php_info)
echo('https://www.cnblogs.com/xgqfrms/p/5492399.html')
?>
httpd.conf 配置文件路径
# macOS 系统自带的 Apache ❌ private 权限限制
$ cd /private/etc/apache2/
$ cat httpd.conf
$ ccode .
# brew 安装的 Apache ✅
$ cd /usr/local/etc/httpd/
$ cat httpd.conf
$ ccode .
http://localhost:8080/
http://localhost:8080/phpinfo.php
http://localhost:80/
http://localhost:80/phpinfo.php
https://wpbeaches.com/installing-configuring-apache-on-macos-using-homebrew/
https://www.git-tower.com/blog/apache-on-macos/
demos
XSS 测试
http://localhost/xss.php?args=测试字符串
http://localhost/xss.php?args=<script>alert("XSS 🐞")</script>
macOS 配置本地 hosts
/*
配置本地 hosts
✅
https://www.webhacker.com/xss.php?args=测试字符串
❌
https://www.webhacker.com/xss.php?args=<script>alert("XSS 🐞")</script>
*/
macOS 查看
hosts
文件
Finder
, Go to Folder Cmd + Shit + G
# mm & mbp
$ cd /private/etc/
$ cat /private/etc/hosts
$ code .
## mm & mbp
$ cd /etc/
$ cat /etc/hosts
$ code .
# Windows 刷新 hosts
$ ipconfig /flushdns
# macOS 13 刷新 hosts
$ sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
# Virtual Host
127.0.0.1 webhacker.com www.webhacker.com
https://www.freecodecamp.org/chinese/news/how-to-flush-dns-on-mac-macos-clear-dns-cache/
Apache 虚拟主机 / Apache VirtualHost
https://httpd.apache.org/docs/2.4/mod/core.html#virtualhost
https://www.cnblogs.com/jdwang-admin/p/7877804.html
Apache config
host
/usr/local/etc/httpd/httpd.conf
virtual host
/usr/local/etc/httpd/extra/httpd-vhosts.conf
# 配置本地域名
# 1. 域名后面不可以有 `/` 符号 ✅; 2. 同一个 ip 的域名,可以写在同一行上
127.0.0.1 webhacker.com www.webhacker.com
https://httpd.apache.org/docs/2.4/vhosts/
<VirtualHost *:8080>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/usr/local/opt/httpd/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "/usr/local/var/log/httpd/dummy-host.example.com-error_log"
CustomLog "/usr/local/var/log/httpd/dummy-host.example.com-access_log" common
</VirtualHost>
<VirtualHost *:8080>
ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/usr/local/opt/httpd/docs/dummy-host2.example.com"
ServerName dummy-host2.example.com
ErrorLog "/usr/local/var/log/httpd/dummy-host2.example.com-error_log"
CustomLog "/usr/local/var/log/httpd/dummy-host2.example.com-access_log" common
</VirtualHost>
PHP 8
## PHP 8.1
<VirtualHost *:80>
ServerAdmin webmaster@www.webhacker.com
# DocumentRoot "/Library/WebServer/Documents"
# DocumentRoot "/usr/local/var/www"
DocumentRoot "/usr/local/var/php8"
# DocumentRoot "/usr/local/opt/httpd/docs/php8"
# DocumentRoot "/usr/local/opt/httpd/docs/www.webhacker.com"
ServerName webhacker.com
ServerAlias www.webhacker.com
ErrorLog "/usr/local/var/log/httpd/www.webhacker.com-error_log"
CustomLog "/usr/local/var/log/httpd/www.webhacker.com-access_log" common
</VirtualHost>
Apache 虚拟主机 / Apache VirtualHost
$ cd /usr/local/opt/httpd
$ mkdir docs && cd docs && mkdir php8
# $ mkdir docs && cd docs && mkdir www.webhacker.com
$ cd /usr/local/var/log/httpd
$ touch www.webhacker.com-access_log
$ touch www.webhacker.com-error_log
https://httpd.apache.org/docs/2.4/mod/core.html#virtualhost
https://www.cnblogs.com/jdwang-admin/p/7877804.html
https://wpbeaches.com/installing-configuring-apache-on-macos-using-homebrew/
开启虚拟主机
# Virtual hosts
## PHP 8.1,开启虚拟主机
Include /usr/local/etc/httpd/extra/httpd-vhosts.conf
https://wpbeaches.com/set-up-virtual-hosts-on-macos-big-sur-11-in-apache/
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
refs
https://github.com/xgqfrms?tab=repositories&q=&type=&language=php&sort=
https://github.com/xgqfrms/php/issues/2
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16972365.html
未经授权禁止转载,违者必究!