Mac搭建php开发环境
所属分类:php | 发布于 2023-06-20 11:32:45
Mac的php开发环境主要是apache和php这两个软件的安装以及配置。
Mac内置Apache使用
截止到macOS Ventura13.4,macOS还是自带了apache,所以对Apache直接使用即可
Apache根目录:/Library/WebServer/Documents
httpd.conf:/private/etc/apache2/httpd.conf
// 启动Apache服务
sudo apachectl start
// 重启Apache服务
sudo apachectl restart
// 停止Apache服务
sudo apachectl stop
// 验证配置文件是否正确
apachectl configtest
// 查看Apache版本
httpd -v
使用Brew安装Apache
macOS 12去掉内置的php后,使用brew安装php,无法和系统自带的apache配合使用,会报出签名错误: No code signing authority for module at...
解决办法有两个:1、卸载系统自带的apache,使用brew安装apache;2、给brew安装的php添加签名。
这里采用自己给libphp.so添加签名的方式,参考文档:https://blog.csdn.net/weixin_41767649/article/details/127557253
经过实践,就算使用自签名的php版本,也不能很好的和内置的Apache配合使用,我这里主要遇到的问题貌似是权限问题,莫名其妙的错误,所以才有了下面的使用Brew安装Apache的经历。
又没几个小时,权限问题好像知道是怎么回事了,具体看下面的使用brew安装的时候设置用户名和用户组的那段。
停止内置apache并移除自动加载脚本
$ sudo apachectl stop
$ sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null
使用brew命令安装apache
brew list
brew search httpd
brew install httpd
安装完成后的提示信息
DocumentRoot is /opt/homebrew/var/www.The default ports have been set in /opt/homebrew/etc/httpd/httpd.conf to 8080 and in/opt/homebrew/etc/httpd/extra/httpd-ssl.conf to 8443 so that httpd can run without sudo.To start httpd now and restart at login:brew services start httpdOr, if you don't want/need a background service you can just run:/opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND==> Summary???? /opt/homebrew/Cellar/httpd/2.4.57: 1,663 files, 32MB==> Running `brew cleanup httpd`...Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
重点信息已经用红色标注出来了。
几个路径
- 实际安装目录:/opt/homebrew/Cellar/httpd/2.4.57
- 映射目录:/opt/homebrew/opt/httpd
- 配置文件目录:/opt/homebrew/etc/httpd
- 默认Document Root:/opt/homebrew/var/www
执行脚本
// 启动Apache服务
sudo brew services start httpd
// 重启Apache服务
sudo brew services restart httpd
// 停止Apache服务
sudo brew services stop httpd
// 验证配置文件是否正确
apachectl configtest
// 查看Apache版本
httpd -v
虚拟主机提示权限不足
在httpd.conf中默认的配置是这样的
<IfModule unixd_module>
#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User _www
Group _www
</IfModule>
经过查找资料,最终得出可以这样设置
Setup User and Group for Apache: As you have configured the Apache document root to your home directory. You will face issues with the permissions because, by default, Apache runs as the user daemon and group daemon (Maybe username and group are to _www). For the personal systems, You can change these to match your user account (replace user_name with your real username), with a group of staff.
意思因为我们一般是个人用户,所以用户直接设置成自己的用户名,用户组设置为staff
User user_name
Group staff
使用apachectl脚本
brew安装的apachectl脚本在:/opt/homebrew/opt/httpd/bin/apachectl
可以用这个脚本来替代系统内置的脚本。
PHP安装
php的安装见这篇文章:Macos安装php多个版本
需要注意的是Intel版本和苹果自研芯片版本的安装路径不同。
又遇到了一个新的问题:
#PHP was deprecated in macOS 11 and removed from macOS 12
配置
配置文件
php.ini:/opt/homebrew/etc/php/8.1/
设置php环境变量
echo 'export PATH="/opt/homebrew/opt/php@8.1/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/php@8.1/sbin:$PATH"' >> ~/.zshrc
加载php模块
LoadModule php_module /opt/homebrew/opt/php@8.1/lib/httpd/modules/libphp.so php-ca-81
配置其它信息
ServerName 127.0.0.1:80
<IfModule php_module>
<FilesMatch .php$>
SetHandler application/x-httpd-php
</FilesMatch>
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
</IfModule>
去掉Include /private/etc/apache2/extra/httpd-vhosts.conf的注释
配置虚拟主机
<VirtualHost *:80>
ServerName abc.net
ServerAlias www.abc.net
DocumentRoot "/Users/alan/projects/php/abc.net/php/public/"
<Directory "/Users/alan/projects/php/abc.net/php/">
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
DirectoryIndex index.php index.html
require all granted
</Directory>
</VirtualHost>