目的:通过brew安装并配置nginx+php+mysql环境,并能正常运行一个ThinkPHP5项目。
环境:macOS Catalina 10.15.7,已安装brew。
php
PHP选择7.4版本。
brew install php@7.4
成功的话最后输出如下
To enable PHP in Apache add the following to httpd.conf and restart Apache:
LoadModule php7_module /usr/local/opt/php@7.4/lib/httpd/modules/libphp7.so
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
Finally, check DirectoryIndex includes index.php
DirectoryIndex index.php index.html
The php.ini and php-fpm.ini file can be found in:
/usr/local/etc/php/7.4/
php@7.4 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.
If you need to have php@7.4 first in your PATH, run:
echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc
For compilers to find php@7.4 you may need to set:
export LDFLAGS="-L/usr/local/opt/php@7.4/lib"
export CPPFLAGS="-I/usr/local/opt/php@7.4/include"
To restart php@7.4 after an upgrade:
brew services restart php@7.4
Or, if you don't want/need a background service you can just run:
/usr/local/opt/php@7.4/sbin/php-fpm --nodaemonize
上面的几个export
加上后,再source ~/.zshrc
,这样就可以不使用默认的php 7.3了。
扩展通过pecl
安装,如
pecl install redis
配置文件位于/usr/local/etc/php
nginx
brew install nginx
配置文件位于/usr/local/etc/nginx
,如果要启用日志,需要在/usr/local/opt/nginx/
下新建logs
目录,或者使用其它的绝对路径。
配置文件示例,支持php和ThinkPHP的重写规则。
server {
listen 80;
server_name localhost;
#charset koi8-r;
access_log logs/host.access.log main;
root /Users/rhonin/Downloads/www/public;
index index.php index.html index.htm;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /Users/rhonin/Downloads/www/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
}
}
需要注意的是SCRIPT_FILENAME $document_root$fastcgi_script_name;
,默认的是/scripts
,会报错"Primary script unknown"
mysql
brew install mysql
安装的是MySQL 8,完成后按提示重置root密码。如果要安装5.7版本,请使用命令
brew install mysql@5.7
安装完后输出
We've installed your MySQL database without a root password. To secure it run:
mysql_secure_installation
MySQL is configured to only allow connections from localhost by default
To connect run:
mysql -uroot
mysql@5.7 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.
If you need to have mysql@5.7 first in your PATH, run:
echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc
For compilers to find mysql@5.7 you may need to set:
export LDFLAGS="-L/usr/local/opt/mysql@5.7/lib"
export CPPFLAGS="-I/usr/local/opt/mysql@5.7/include"
To restart mysql@5.7 after an upgrade:
brew services restart mysql@5.7
Or, if you don't want/need a background service you can just run:
/usr/local/opt/mysql@5.7/bin/mysqld_safe --datadir=/usr/local/var/mysql
对于大多数项目来说,需要修改下配置文件/usr/local/etc/my.cnf
,加上
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
默认的sql_mode是有ONLY_FULL_GROUP_BY
的限制,所以要拿掉它。