安装Homebrew
在 macOS 上安装 PHP 运行环境和 Nginx 服务器通常可以通过以下步骤完成。我们将使用 Homebrew 这个包管理器来简化安装过程。
打开终端并运行以下命令安装 Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
如遇到以下报错:
error: RPC failed; curl 92 HTTP/2 stream 5 was not closed cleanly: CANCEL (err 8)
error: 1338 bytes of body are still expected
fetch-pack: unexpected disconnect while reading sideband packet
fatal: early EOF
fatal: fetch-pack: invalid index-pack output
Failed during: /usr/bin/git fetch --force origin
可更换仓库地址,添加到环境变量中并再次运行安装命令:
export HOMEBREW_BREW_GIT_REMOTE=https://github.com/Homebrew/brew.git
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
安装PHP8
brew install php
等待安装完成后,执行
php --version
显示版本信息即为成功:
PHP 8.3.1 (cli) (built: Dec 20 2023 12:44:38) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.1, Copyright (c) Zend Technologies
with Zend OPcache v8.3.1, Copyright (c), by Zend Technologies
安装nginx
brew install nginx
等待安装完成后,执行以下命令启动nginx:
brew install nginx
** 编辑nginx配置文件**
cd /usr/local/etc/nginx
vim nginx.conf
** 注意:配置文件内容主要在http下面添加一个server部分**
server {
listen 8080;
server_name localhost;
# 指定thinkphp8项目的 public 目录
root /Users/<myName>/Documents/workspace/tp8/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
# 添加thinkphp的伪静态重写路由
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
}
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000; # PHP-FPM 的地址和端口
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_log /usr/local/var/log/nginx/error.log;
access_log /usr/local/var/log/nginx/access.log;
}
配置好端口、路径等信息后,执行以下命令重载配置文件:
# 校验配置文件正确性
sudo nginx -t
# 重载配置文件
udo nginx -s reload
# 或者运行brew命令重启nginx
brew services restart nginx
** 启动php-fpm**
brew services start php@8.3
# 注意,安装的php版本为8.3.1,但启动命令只能是@8.3
## 安装Composer
```bash
curl -sS https://getcomposer.org/installer | php
# 安装到全局
mv composer.phar /usr/local/bin/composer
安装thinkphp8
** 切换到工程目录 /Users/<myName>/Documents/workspace/ **
cd /Users/<myName>/Documents/workspace
composer create-project topthink/think tp8
等待安装完成,前面nginx配置了相关项目路径和端口,浏览器输入http://localhost:8080/即可打开测试
也可以运行以下命令指定端口测试:
# 指定端口80运行
php think run -p 80
浏览器打开http://localhost即可测试









网友评论