Mac搭建php+nginx

安装Homebrew

此处不做简述 可以阅读这片文章。很棒!
brew.idayer.com/

安装nginx

//下载nginx
brew install nginx

//查看是否安装成功
nginx -v
可能遇到的错误:
//此处注意运行下面命令,可能会报错nginx: [error] open() "/usr/local/var/run/nginx.pid" failed 

nginx -s reload  //是因为nginx服务还没有启动

//开启nginx服务
brew services start nginx
//这一段是来自下载完成nginx之后官方给的提示。多看一下,记不住的copy起来看
    1.docroot 为:/usr/local/var/www
    2.默认端口已在 /usr/local/etc/nginx/nginx.conf 中设置为 8080,以便nginx 可以在没有 sudo 的情况下运行.
    3.nginx 将加载 /usr/local/etc/nginx/servers/ 中的所有文件。
    4.现在启动 nginx 并在登录时重新启动:brew services start nginx
    5.或者,如果您不想要/不需要后台服务,您可以运行:nginx

安装PHP

Tips: Mac中自带php(可以运行 php -v 查看),因此安装完PHP后需要添加系统环境变量 PATH 来替代自带 PHP 版本。

//搜索现有php版本
brew search php

//下载自己需要的版本
brew install php@7.3

下载完成后会给出提示,php加环境变量,文件存放路径的命令行等等(看不明白请自行去翻译,不给你们翻译了:smile:)。以下给出的提示句中 PHP添加环境变量 时有一个坑(在这段提示之后有解释)。

To enable PHP in Apache add the following to httpd.conf and restart Apache:

 LoadModule php7_module /usr/local/opt/php@7.3/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.3/

php@7.3 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.3 first in your PATH, run:

 echo 'export PATH="/usr/local/opt/php@7.3/bin:$PATH"' >> ~/.zshrc

 echo 'export PATH="/usr/local/opt/php@7.3/sbin:$PATH"' >> ~/.zshrc

For compilers to find php@7.3 you may need to set:

 export LDFLAGS="-L/usr/local/opt/php@7.3/lib"

 export CPPFLAGS="-I/usr/local/opt/php@7.3/include"

To have launchd start php@7.3 now and restart at login:

 brew services start php@7.3

Or, if you don't want/need a background service you can just run:

 php-fpm
这里讲一下上面给出的提示中将PHP加入环境变量时遇到的问题

 echo 'export PATH="/usr/local/opt/php@7.3/bin:$PATH"' >> ~/.zshrc
 echo 'export PATH="/usr/local/opt/php@7.3/sbin:$PATH"' >> ~/.zshrc
//这两条添加环境变量的命令行可以换掉Mac自带的php版本,但是这两条命令行后一定要执行

source ~/.zshrc

//source命令(从 C Shell 而来)是bash shell的内置命令。点命令,就是个点符号,(从Bourne Shell而来)是source的另一名称。
//同样的,当前脚本中配置的变量也将作为脚本的环境,source(或点)命令通常用于重新执行刚修改的初始化文档,如 .bash_profile 和 .profile 等等。

完成之后再次运行 php -v 之后的提示就与安装之前运行的php -v提示不一样了。说明替换成功

自己开发遇到的问题,随手记录。持续更新中…
本作品采用《CC 协议》,转载必须注明作者和本文链接
Reality
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
讨论数量: 2

我用docke塔建多环境, 本地安装个nginx 想用哪个版本配置相关端口即可,没办法公司三个版本的项目都有

file

3年前 评论
Hesunfly 3年前
飞飞鱼 (楼主) 3年前
飞飞鱼 (楼主) 3年前

写得不错,同样折腾过brew,感觉还是 docker 更加适合开发,博主有时间可以看下docker的环境搭建 附上个人整合的dockerfiles

3年前 评论
飞飞鱼 (楼主) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!