安装PHP
brew install php@7.4
echo 'export PATH="/opt/homebrew/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc
export LDFLAGS="-L/usr/local/opt/php@7.4/lib"
export CPPFLAGS="-I/usr/local/opt/php@7.4/include"
brew link --overwrite --force php@7.4
使用php -v
查看是否安装成功
配置Apache
- 根据个人习惯创建网站根目录
mkdir /Users/xzs/www
- 修改Apache配置文件
/etc/apache2/httpd.conf
,每个代码块代表着对应的修改部分
#Listen xx.xx.xx.xx:80
Listen 127.0.0.1:80
#添加下行内容
LoadModule php7_module /opt/homebrew/Cellar/php@7.4/7.4.30/lib/httpd/modules/libphp7.so (根据具体安装位置修改)
#ServerName www.example.com:80
ServerName localhost
#DocumentRoot "/Library/WebServer/Documents"
#<Directory "/Library/WebServer/Documents">
DocumentRoot "/Users/xzs/www"
<Directory "/Users/xzs/www">
# 添加下述内容
<FilesMatch .php$>
SetHandler application/x-httpd-php
</FilesMatch>
#<IfModule dir_module$>
# DirectoryIndex index.html
#</IfModule>
<IfModule dir_module$>
DirectoryIndex index.html index.php
</IfModule>
<Directory />
#AllowOverride none
# Require all denied
Require all granted
AllowOverride all
</Directory>
#Include /private/etc/apache2/extra/httpd-vhosts.conf
Include /private/etc/apache2/extra/httpd-vhosts.conf
- 配置codesign
详细步骤参考:https://scriptverse.academy/blog/codesign-certificate-mac.html
LoadModule php7_module /opt/homebrew/Cellar/php@7.4/7.4.30/lib/httpd/modules/libphp7.so "codesign"
- 配置vhost
详细步骤参考:https://blog.csdn.net/simplty/article/details/43021375
- 创建
.htaccess
文件
# 进入到网站根目录的上级目录
cd /Users/xzs
touch .htaccess
# 写入如下内容
Order allow,deny
Allow from all
Require all granted
- 添加可执行权限
# 确保网站根目录的上级目录具有可执行权限
cd /Users
chmod +x xzs
- 测试
cd /Users/xzs/www
echo "<?php phpinfo();?>" > index.php
8.安装php扩展,以soap为例
到php官网下载对应版本的源码包
tar zxvf php-7.4.30.tar.gz
./configure --enable-soap=shared --without-iconv --without-pear --disable-phar
make
cp modules/soap.so /opt/homebrew/Cellar/php@7.4/7.4.30/pecl/2019090/
后记
因为自带的Apache2配合PHP的环境经常出现一些莫名其妙的问题,比如修改php.ini
没有效果等等,目前采用通过docker搭建nginx+php的环境。
具体可参考:https://www.runoob.com/docker/docker-install-php.html