第一步:安装brew
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
如果mac没有git或git版本太老,系统会提示你安装或升级git后,才可以继续安装brew。
提示:git至少需要20GB的磁盘空间。
安装脚本过程记录:
...................................................................................................................................................
--> 脚本开始执行
未发现Git代理(属于正常状态)
开始 进入brew官方安装脚本 开始
下载官方install.sh文件,当前目录是: /Users/ziyin
Cloning into 'brew-install-ck'...
remote: Enumerating objects: 18, done.
remote: Counting objects: 100% (18/18), done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 18 (delta 0), reused 11 (delta 0), pack-reused 0
Receiving objects: 100% (18/18), 20.54 KiB | 1.14 MiB/s, done.
==> Checking for `sudo` access (which may request your password)...
==> This script will install:
/opt/homebrew/bin/brew
/opt/homebrew/share/doc/homebrew
/opt/homebrew/share/man/man1/brew.1
/opt/homebrew/share/zsh/site-functions/_brew
/opt/homebrew/etc/bash_completion.d/brew
/opt/homebrew
==> The following new directories will be created:
/opt/homebrew/bin
/opt/homebrew/etc
/opt/homebrew/include
/opt/homebrew/lib
/opt/homebrew/sbin
/opt/homebrew/share
/opt/homebrew/var
/opt/homebrew/opt
/opt/homebrew/share/zsh
/opt/homebrew/share/zsh/site-functions
/opt/homebrew/var/homebrew
/opt/homebrew/var/homebrew/linked
/opt/homebrew/Cellar
/opt/homebrew/Caskroom
/opt/homebrew/Frameworks
Press RETURN/ENTER 现在是brew官方安装提示,它需要你按回车键开始 other key to abort:
==> /usr/bin/sudo /usr/bin/install -d -o root -g wheel -m 0755 /opt/homebrew
==> /usr/bin/sudo /bin/mkdir -p /opt/homebrew/bin /opt/homebrew/etc /opt/homebrew/include /opt/homebrew/lib /opt/homebrew/sbin /opt/homebrew/share /opt/homebrew/var /opt/homebrew/opt /opt/homebrew/share/zsh /opt/homebrew/share/zsh/site-functions /opt/homebrew/var/homebrew /opt/homebrew/var/homebrew/linked /opt/homebrew/Cellar /opt/homebrew/Caskroom /opt/homebrew/Frameworks
==> /usr/bin/sudo /bin/chmod ug=rwx /opt/homebrew/bin /opt/homebrew/etc /opt/homebrew/include /opt/homebrew/lib /opt/homebrew/sbin /opt/homebrew/share /opt/homebrew/var /opt/homebrew/opt /opt/homebrew/share/zsh /opt/homebrew/share/zsh/site-functions /opt/homebrew/var/homebrew /opt/homebrew/var/homebrew/linked /opt/homebrew/Cellar /opt/homebrew/Caskroom /opt/homebrew/Frameworks
==> /usr/bin/sudo /bin/chmod go-w /opt/homebrew/share/zsh /opt/homebrew/share/zsh/site-functions
==> /usr/bin/sudo /usr/sbin/chown ziyin /opt/homebrew/bin /opt/homebrew/etc /opt/homebrew/include /opt/homebrew/lib /opt/homebrew/sbin /opt/homebrew/share /opt/homebrew/var /opt/homebrew/opt /opt/homebrew/share/zsh /opt/homebrew/share/zsh/site-functions /opt/homebrew/var/homebrew /opt/homebrew/var/homebrew/linked /opt/homebrew/Cellar /opt/homebrew/Caskroom /opt/homebrew/Frameworks
==> /usr/bin/sudo /usr/bin/chgrp admin /opt/homebrew/bin /opt/homebrew/etc /opt/homebrew/include /opt/homebrew/lib /opt/homebrew/sbin /opt/homebrew/share /opt/homebrew/var /opt/homebrew/opt /opt/homebrew/share/zsh /opt/homebrew/share/zsh/site-functions /opt/homebrew/var/homebrew /opt/homebrew/var/homebrew/linked /opt/homebrew/Cellar /opt/homebrew/Caskroom /opt/homebrew/Frameworks
==> /usr/bin/sudo /usr/sbin/chown -R ziyin:admin /opt/homebrew
==> /usr/bin/sudo /bin/mkdir -p /Users/ziyin/Library/Caches/Homebrew
==> /usr/bin/sudo /bin/chmod g+rwx /Users/ziyin/Library/Caches/Homebrew
==> /usr/bin/sudo /usr/sbin/chown -R ziyin /Users/ziyin/Library/Caches/Homebrew
==> Downloading and installing Homebrew...
remote: Enumerating objects: 276920, done.
remote: Counting objects: 100% (83296/83296), done.
......................................................................................................................................................
==> Resetting /opt/homebrew...
Reset branch 'stable'
==> Installation successful!
==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
Read the analytics documentation (and how to opt-out) here:
https://docs.brew.sh/Analytics
No analytics data has been sent yet (nor will any be during this install run).
==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew#donations
==> Next steps:
- Run these two commands in your terminal to add Homebrew to your PATH:
(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/ziyin/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
- Run brew help to get started
- Further documentation:
https://docs.brew.sh
此步骤成功
Password:
完成 退出brew官方安装脚本 完成
==> 配置国内镜像源HOMEBREW BOTTLE
此处如果显示Password表示需要再次输入开机密码,输入完后回车
sed: /Users/ziyin/.zprofile: No such file or directory
有些电脑xcode和git混乱,再运行一次,此处如果有error正常。
xcode-select: error: command line tools are already installed, use "Software Update" to install updates
Homebrew已经安装成功,接下来配置国内软件下载源。
请选择今后brew install的时候访问那个国内镜像,例如阿里巴巴,输入5回车。
1、中科大国内源
2、清华大学国内源
3、上海交通大学国内源
4、腾讯国内源
5、阿里巴巴国内源(推荐)
请输入序号: 2
你选择了清华大学国内源
环境变量写入->/Users/ziyin/.zprofile
此步骤成功
==> 安装完成,brew版本
Homebrew 4.3.13
Homebrew前期配置成功
==> Updating Homebrew...
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/bottles-portable-ruby/portable-ruby-3.3.4_1.arm64_big_sur.bottle.tar.gz
###################################################################### 100.0%
==> Pouring portable-ruby-3.3.4_1.arm64_big_sur.bottle.tar.gz
==> Homebrew collects anonymous analytics.
Read the analytics documentation (and how to opt-out) here:
https://docs.brew.sh/Analytics
No analytics have been recorded yet (nor will be during this `brew` run).
==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
https://github.com/Homebrew/brew#donations
Already up-to-date.
Homebrew自动安装程序运行完成
国内地址已经配置完成
之前步骤选了删除本机brew的话,桌面多出一个Old_Homebrew文件夹,可以删除。
初步介绍几个brew命令
查看版本:brew -v 更新brew版本:brew update
查找:brew search python(其中python替换为要查找的关键字)
安装:brew install python(其中python替换为要安装的名称)
本地软件库列表:brew ls
欢迎右键点击下方地址-打开链接 点个赞吧
https://zhuanlan.zhihu.com/p/111014448
如果遇到问题可以右键下面地址查看常见错误解决办法
https://gitee.com/cunkai/HomebrewCN/blob/master/error.md
brew官方地址:https://brew.sh/zh-cn/
安装成功 但还需要重启终端 或者 运行 source /Users/ziyin/.zprofile 否则国内地址无法生效
查看相关配置是否正确:
ziyin@bogon Caches % cat /Users/ziyin/.zprofile
export HOMEBREW_PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple #ckbrew
export HOMEBREW_API_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api #ckbrew
export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles #ckbrew
eval $(/opt/homebrew/bin/brew shellenv) #ckbrew
ziyin@bogon Caches %
ziyin@bogon Caches % source /Users/ziyin/.zprofile
ziyin@bogon Caches % brew update
==> Updating Homebrew...
Already up-to-date.
ziyin@bogon Caches %
第二步:安装和配置Nginx
ziyin@bogon Caches % brew install nginx
==> nginx
Docroot is: /opt/homebrew/var/www
The default port has been set in /opt/homebrew/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.
nginx will load all files in /opt/homebrew/etc/nginx/servers/.
To start nginx now and restart at login:
brew services start nginx
Or, if you don't want/need a background service you can just run:
/opt/homebrew/opt/nginx/bin/nginx -g daemon\ off\;
ziyin@bogon Caches %
ziyin@ziyin-Air ~ % brew services start nginx
==> Tapping homebrew/services
Cloning into '/opt/homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Enumerating objects: 3069, done.
remote: Counting objects: 100% (530/530), done.
remote: Compressing objects: 100% (195/195), done.
remote: Total 3069 (delta 349), reused 452 (delta 331), pack-reused 2539
Receiving objects: 100% (3069/3069), 859.92 KiB | 801.00 KiB/s, done.
Resolving deltas: 100% (1490/1490), done.
Tapped 1 command (47 files, 1MB).
==> Successfully started `nginx` (label: homebrew.mxcl.nginx)
ziyin@ziyin-Air ~ %
验证nginx服务:
ziyin@ziyin-MacBook-Air ~ % nginx -t //测试配置文件是否正确
nginx: the configuration file /opt/homebrew/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /opt/homebrew/etc/nginx/nginx.conf test is successful
ziyin@ziyin-MacBook-Air ~ % sudo nginx //启动nginx(想重启服务可以使用sudo nginx -s reload)
ziyin@ziyin-Air ~ % curl http://localhost:8080 //命令行访问网页测试
或者打开浏览器,输入http://localhost:8080/验证nginx是否正常访问。
==================Nginx 其它相关操作=================================
#启动nginx
$ sudo nginx
#重新加载|重启|退出
$ sudo nginx-s reload|reopen|quit
#brew操作,启动|停止|重启
$ brew services start|stop|restart nginx
============================================================
第三步:安装mysql
ziyin@ziyin-Air ~ % brew install mysql
==> mysql
Upgrading from MySQL <8.4 to MySQL >9.0 requires running MySQL 8.4 first:
- brew services stop mysql
- brew install mysql@8.4
- brew services start mysql@8.4
- brew services stop mysql@8.4
- brew services start mysql
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 -u root
To start mysql now and restart at login:
brew services start mysql
Or, if you don't want/need a background service you can just run:
/opt/homebrew/opt/mysql/bin/mysqld_safe --datadir\=/opt/homebrew/var/mysql
ziyin@ziyin-Air ~ %
验证MySQL:
ziyin@ziyin-Air ~ % brew services start mysql
Error: Formula `mysql` has not implemented #plist, #service or installed a locatable service file
ziyin@ziyin-Air ~ % ls -al /opt/homebrew/opt/mysql
lrwxr-xr-x 1 ziyin admin 21 8 8 10:22 /opt/homebrew/opt/mysql -> ../Cellar/mysql/9.0.1
ziyin@ziyin-Air ~ % brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!
Warning: Suspicious https://github.com/Homebrew/brew git origin remote found.
The current git origin is:
https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew
With a non-standard origin, Homebrew won't update properly.
You can solve this by setting the origin remote:
git -C "/opt/homebrew" remote set-url origin https://github.com/Homebrew/brew
Warning: You have unlinked kegs in your Cellar.
Leaving kegs unlinked can lead to build-trouble and cause formulae that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
mysql
Warning: Broken symlinks were found. Remove them with `brew cleanup`:
/opt/homebrew/opt/mysql
/opt/homebrew/opt/mysql@9.0
ziyin@ziyin-Air ~ %
以上错误是macOS老版本的一个bug,需要升级下macOS后,重新安装mysql即可解决
ziyin@ziyin-Air ~ % sudo softwareupdate -l //查看更新软件包
ziyin@ziyin-Air ~ % sudo softwareupdate -i -a。//安装所有更新软件包
ziyin@ziyin-MacBook-Air ~ % brew reinstall mysql //重新安装
ziyin@ziyin-MacBook-Air ~ % brew services start mysql //开启mysql服务成功
==> Successfully started `mysql` (label: homebrew.mxcl.mysql)
ziyin@ziyin-MacBook-Air ~ % mysql_secure_installation //MySQL安全设置mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.
Estimated strength of the password: 100 //设置密码强度
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y //修改 mysql的root账户的密码
New password:
Re-enter new password:
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y //确认密码设置
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y //删除匿名用户
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y //禁止mysql的root账户远程登录权限
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y //删除默认保留的test数据库
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y //重新加载权限表
Success.
All done!
ziyin@ziyin-MacBook-Air ~ %
至此,mysql数据库安装及配置完毕!
========================常用的mysql数据库维护操作======================
#启动|停止|重启 mysql服务
$ brew services start|stop|restart mysql
#本地登录MySQL
$ mysql-u用户名-p密码
#远程登录
$ mysql-hIP地址-u用户名-p密码-P端口号(默认3306)
===================================================================
第四步:安装php
注意:由于homebrew主库中没有PHP7.2 之前的版本,并且7.2,7.3版本也被标记成过时版本!
ziyin@ziyin-MacBook-Air ~ % brew search php //搜索PHP,会搜索出PHP相关的资源,然后根据自己需求选择安装
==> Formulae
brew-php-switcher php-code-sniffer php-cs-fixer@2 php@8.1 phpbrew phpmyadmin phpunit pop
php php-cs-fixer php@8.0 php@8.2 phpmd phpstan pup pcp
==> Casks
eclipse-php phpstorm phpwebstudy pop
ziyin@ziyin-MacBook-Air ~ % brew install php //安装PHP,不指定PHP版本时,默认安装最新版本
.........................
............
==> /opt/homebrew/Cellar/php/8.3.10/bin/pear update-channels
==> Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.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:
/opt/homebrew/etc/php/8.3/
To start php now and restart at login:
brew services start php
Or, if you don't want/need a background service you can just run:
/opt/homebrew/opt/php/sbin/php-fpm --nodaemonize
==> Summary
🍺 /opt/homebrew/Cellar/php/8.3.10: 524 files, 88.8MB
==> Running `brew cleanup php`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> No outdated dependents to upgrade!
==> Checking for dependents of upgraded formulae...
Disable this behaviour by setting HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Reinstalling 1 dependent with broken linkage from source:
mysql
Error: Could not rename mysql keg! Check/fix its permissions:
sudo chown -R ziyin /opt/homebrew/Cellar/mysql/9.0.1 //目录读取权限报错
ziyin@ziyin-MacBook-Air ~ %
ziyin@ziyin-MacBook-Air ~ % sudo chown -R ziyin /opt/homebrew/Cellar/mysql/9.0.1
Password:
ziyin@ziyin-MacBook-Air ~ %
ziyin@ziyin-MacBook-Air ~ % brew install php
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api/formula.jws.json
-=#=- # # #
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api/cask.jws.json
##O=-# #
Warning: php 8.3.10 is already installed and up-to-date.
To reinstall 8.3.10, run:
brew reinstall php
ziyin@ziyin-MacBook-Air ~ %
至此,php自动安装完成,默认安装的是最新版本8.3.10!
接着,开始配置php环境,即将PHP加入 $PATH 中:
ziyin@ziyin-MacBook-Air ~ % whereis php //找到php运行命令所在的位置
php: /opt/homebrew/bin/php /opt/homebrew/share/man/man1/php.1
ziyin@ziyin-MacBook-Air ~ %
===========================================================
#shell使用bash添加方式
$ vim ~/.bash_profile
export PATH="/opt/homebrew/bin:$PATH"
source ~/.bash_profile
# shell使用zsh添加方式
$ vim ~/.zprofile
export PATH="/opt/homebrew/bin:$PATH"
source ~/.zprofile
===========================================================
ziyin@ziyin-MacBook-Air ~ % php -v
PHP 8.3.10 (cli) (built: Aug 5 2024 00:56:36) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.10, Copyright (c) Zend Technologies
with Zend OPcache v8.3.10, Copyright (c), by Zend Technologies
ziyin@ziyin-MacBook-Air ~ %
最后,配置php-fpm.conf。php-fpm.conf是PHP-FPM(FastCGI Process Manager)的配置文件,用于控制PHP-FPM管理进程的相关参数。这个配置文件包含了工作子进程的数量、运行权限、监听端口、慢请求等设置。PHP-FPM是一个PHP FastCGI管理器,旨在将FastCGI进程管理整合进PHP包中,特别是对于PHP 5.3.3之前的版本,PHP-FPM是一个必要的补丁包,需要通过patch到PHP源代码中,并在编译安装PHP后才能使用。php-fpm.conf配置文件中的设置对于优化PHP性能和确保PHP应用程序的稳定运行至关重要,包括但不限于设置display_errors为off、error_log的路径、memory_limit等参数。此外,该配置文件中的所有相对路径都是相对于PHP的安装前缀设置的,确保了配置的一致性和可维护性。
FPM配置文件为php-fpm.conf,其语法类似 php.ini 。
其php手册上也有详细的讲解:http://php.net/manual/zh/install.fpm.configuration.php
ziyin@ziyin-MacBook-Air ~ % vim /opt/homebrew/etc/php/8.3/php-fpm.conf
#去掉第17行和第24行前面的分号
17 ;pid = run/php-fpm.pid
24 ;error_log = log/php-fpm.log
#启动
ziyin@ziyin-MacBook-Air ~ % brew services start php
==> Successfully started `php` (label: homebrew.mxcl.php)
ziyin@ziyin-MacBook-Air ~ %
#查看php-fpm是否启动成功
ziyin@ziyin-MacBook-Air ~ % lsof -Pni4 | grep LISTEN | grep php
php-fpm 23903 ziyin 9u IPv4 0xdcb33f2a9a7935d1 0t0 TCP 127.0.0.1:9000 (LISTEN)
php-fpm 23906 ziyin 10u IPv4 0xdcb33f2a9a7935d1 0t0 TCP 127.0.0.1:9000 (LISTEN)
php-fpm 23907 ziyin 10u IPv4 0xdcb33f2a9a7935d1 0t0 TCP 127.0.0.1:9000 (LISTEN)
ziyin@ziyin-MacBook-Air ~ %
至此php安装配置完成!
第五部:整合NGINX+PHP+MYSQL开发环境
修改nginx.conf配置文件
ziyin@ziyin-MacBook-Air ~ % vim /opt/homebrew/etc/nginx/nginx.conf
ziyin@ziyin-MacBook-Air ~ % grep -v ^$ /opt/homebrew/etc/nginx/nginx.conf | grep -v #
worker_processes 1;
error_log /opt/homebrew/var/log/nginx/error.log;
pid /opt/homebrew/var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /opt/homebrew/var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 8080;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include servers/*;
include vhosts/*;
}
ziyin@ziyin-MacBook-Air ~ %
修改配置说明:
1,监听的端口可以是8080或80(如果80没有被占用的话);
2,server_name即为主机域名,这里是localhost,可以在/etc/hosts里添加一个本地ip的域名指向,然后在nginx里添加该域名,即可通过浏览器输入域名地址访问;
3,nginx.conf有个备份文件,文件名nginx.conf.default,可以通过改文件覆盖掉当前运行的配置文件;
4,对应的access_log、error_log、pid的具体文件位置,需提前确认是否存在,否则nginx会报错,无法启动;
5,修改完nginx.conf保存退出,通过nginx -t来测试配置文件是否存在错误,如下:
ziyin@ziyin-MacBook-Air ~ % nginx -t
nginx: the configuration file /opt/homebrew/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /opt/homebrew/etc/nginx/nginx.conf test is successful
ziyin@ziyin-MacBook-Air ~ %
以上返回表示配置文件没有错误。
配置虚拟主机(通过修改vhosts文件实现):
创建虚拟机主机前,先创建一个存放虚拟主机对应网站的存放目录,例如test目录;
ziyin@ziyin-MacBook-Air ~ % mkdir -p /opt/homebrew/var/www/test
接着,创建存放虚拟主机配置文件的目录,例vhosts目录;
ziyin@ziyin-MacBook-Air ~ % mkdir -p /opt/homebrew/etc/nginx/vhosts
最后,创建虚拟机主机的配置文件;
ziyin@ziyin-MacBook-Air ~ % vim /opt/homebrew/etc/nginx/vhosts/www.test.com.conf
ziyin@ziyin-MacBook-Air ~ % cat /opt/homebrew/etc/nginx/vhosts/www.test.com.conf
server {
#监听端口
listen 8080;
#虚拟主机域名
server_name www.test.com;
#网站根目录
root /opt/homebrew/var/www/test;
#定义路径下默认访问的文件名
index index.php index.html;
location / {
try_files $uri $uri /index.php?$query_string;
#打开目录浏览功能,可以列出整个目录
#autoindex on;
}
access_log /opt/homebrew/var/log/nginx/www.test.com-access.log main;
error_log /opt/homebrew/var/log/nginx/www.test.com-error.log error;
#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
ziyin@ziyin-MacBook-Air ~ %
修改完毕后,测试并验证下配置文件是否有错误的地方,可以通过如下命令:
ziyin@ziyin-MacBook-Air ~ % nginx -t
nginx: the configuration file /opt/homebrew/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /opt/homebrew/etc/nginx/nginx.conf test is successful
ziyin@ziyin-MacBook-Air ~ %
以上反馈信息说明配置文件正确无误。
======================独立运行的二级栏目解析================================
以往MAC部署apache解析二级栏目,只需要在对应的目录下添加.htaccess文件,在文件中配置规则,而nginx不支持.htaccess,所以直接在对应的主机文件里配置
location /a {
index index.php index.html;
try_files$uri$uri/ /a/index.php?query_string;
}
========================================================================
重启相关的服务并测试是否生效:
ziyin@ziyin-MacBook-Air ~ % brew services restart nginx
Stopping `nginx`... (might take a while)
==> Successfully stopped `nginx` (label: homebrew.mxcl.nginx)
==> Successfully started `nginx` (label: homebrew.mxcl.nginx)
ziyin@ziyin-MacBook-Air ~ % brew services restart php
Stopping `php`... (might take a while)
==> Successfully stopped `php` (label: homebrew.mxcl.php)
==> Successfully started `php` (label: homebrew.mxcl.php
ziyin@ziyin-MacBook-Air ~ % brew services restart mysql
Stopping `mysql`... (might take a while)
==> Successfully stopped `mysql` (label: homebrew.mxcl.mysql)
==> Successfully started `mysql` (label: homebrew.mxcl.mysql)
ziyin@ziyin-MacBook-Air ~ %
开始测试php开发环境:
ziyin@ziyin-MacBook-Air ~ % vim /opt/homebrew/var/www/test/index.php
ziyin@ziyin-MacBook-Air ~ % cat /opt/homebrew/var/www/test/index.php
<?php
phpinfo();
?>
ziyin@ziyin-MacBook-Air ~ %
设置MAC OS当前用户开机自动启动PHP,MYSQL,NGINX服务:
% launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php.plist 2>/dev/null
% launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist 2>/dev/null
% launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist 2>/dev/null