MacOS Brew搭建PHP开发环境

  • MacOS Brew搭建PHP开发环境
    • HomeBrew 包管理器安装
    • 一、安装Nginx
    • 二、安装PHP
    • 三、安装MySQL
    • 四、配置Nginx
    • 五、安装Composer

MacOS Brew搭建PHP开发环境

HomeBrew 包管理器安装

参考链接:MacOS下安装homebrew包管理器

HomeBrew 相关命令简要说明:

brew update                        #更新brew可安装包,建议每周执行一下
brew search package-name           #搜索需要安装的包名,例如:brew search php
brew tap shivammathur/php          #挂载其他PHP扩展<github_user/repo>
brew tap                           #查看安装的扩展列表
brew install package-name          #安装包文件,例如:brew install php
brew uninstall package-name        #卸载包文件,例如:brew uninstall php
brew upgrade package-name          #升级包文件,例如:brew upgrade php
brew info package-name             #查看包相关信息,例如:brew info php
brew home package-name             #访问安装包官方网站
brew services list                 #查看系统通过 brew 安装的服务
brew services cleanup              #清除已卸载无用的启动配置文件
brew services restart package-name #重启服务,brew services restart php@7.2

一、安装Nginx

#搜索是否有nginx安装包
➜  ~ brew search nginx
==> Formulae
nginx   #搜到的包文件
#执行安装命令,会自动安装
➜  ~ brew install nginx
#自动安装过程省略……
➜  ~

生成目录及配置文件路径简要说明:

#根目录路径
/usr/local/var/www
#虚拟主机配置文件目录
/usr/local/etc/nginx/servers/
#nginx配置文件路径,默认端口是8080
/usr/local/etc/nginx/nginx.conf
#重启nginx命令
brew services restart nginx
#不需要在后台运行执行这条命令
/usr/local/opt/nginx/bin/nginx -g daemon off

nginx相关操作简要说明:

#测试配置是否有语法错误
sudo nginx -t#启动 nginx
sudo nginx#重新加载配置|重启|停止|退出 nginx
nginx -s reload|reopen|stop|quit#使用Mac的launchctl来启动|停止
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

测试nginx是否安装成功,因为默认配置文件监听的是8080端口,所以先对8080端口发起请求,在浏览器中输入127.0.0.1:8080,出现下图说明安装正确。

或者使用curl测试

➜  ~ curl -IL http://127.0.0.1:8080#输出结果如下
HTTP/1.1 200 OK
Server: nginx/1.21.6
Date: Wed, 09 Mar 2022 04:19:15 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 25 Jan 2022 15:04:02 GMT
Connection: keep-alive
ETag: "61f01162-xxx"
Accept-Ranges: bytes

如果要nginx 监听 80 端口的话,需要 root 权限执行

➜  ~ sudo chown root:wheel /usr/local/Cellar/nginx/1.21.6_1/bin/nginx
➜  ~ sudo chmod u+s /usr/local/Cellar/nginx/1.21.6_1/bin/nginx

二、安装PHP

➜  ~ brew search php #搜索php
==> Formulae
brew-php-switcher   php@7.2             phplint             pcp
php                 php@7.3             phpmd               pup
php-code-sniffer    php@7.4             phpmyadmin
php-cs-fixer        php@8.0             phpstan
php-cs-fixer@2      phpbrew             phpunit
➜  ~ brew install php@7.2  #安装php7.2
#自动安装过程省略……
➜  ~

安装目录简要说明:

#配置文件路径
/usr/local/etc/php/7.2/php.ini
/usr/local/etc/php/7.2/php-fpm.ini
#重启服务
brew services restart php@7.2
#不在后台运行
/usr/local/opt/php@7.2/sbin/php-fpm --nodaemonize

写入环境变量

#写入环境变量
echo 'export PATH="/usr/local/opt/php@7.2/bin:$PATH"' >> ~/.zshrc   #php
echo 'export PATH="/usr/local/opt/php@7.2/sbin:$PATH"' >> ~/.zshrc  #php-fpm
#刷新配置文件
source ~/.zshrc

修改php-fpm.conf配置文件

➜  ~ vim /usr/local/etc/php/7.2/php-fpm.conf#去掉第17行和第24行前面的分号
17 ;pid = run/php-fpm.pid  #pid 文件就会自动产生在 /usr/local/var/run/php-fpm.pid
24 ;error_log = log/php-fpm.log

PHP相关操作简要说明:

#测试php-fpm配置
php-fpm -t
php-fpm -c /usr/local/etc/php/7.2/php.ini -y /usr/local/etc/php/7.2/php-fpm.conf -t#启动php-fpm
php-fpm -D
php-fpm -c /usr/local/etc/php/7.1/php.ini -y /usr/local/etc/php/7.1/php-fpm.conf -D#关闭php-fpm
kill -INT `cat /usr/local/var/run/php-fpm.pid`#重启php-fpm
kill -USR2 `cat /usr/local/var/run/php-fpm.pid`#启动|停止|重启 PHP
brew services start|stop|restart php

启动php后,使用以下命令监测php-fpm是否启动成功,监听9000端口:

➜  ~ lsof -Pni4 | grep LISTEN | grep php
#启动成功应当有以下类似输出
php-fpm   12424 yamol    8u  IPv4 0x982f86ac641367c1      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   12430 yamol    9u  IPv4 0x982f86ac641367c1      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   12431 yamol    9u  IPv4 0x982f86ac641367c1      0t0  TCP 127.0.0.1:9000 (LISTEN)

设置PHP-FPM 开机启动:

➜  ~ ln -sfv /usr/local/opt/php@7.2/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php@7.2.plist

三、安装MySQL

➜  ~ brew search mysql   #查询mysql
==> Formulae
automysqlbackup         mysql-client            mysql-sandbox           mysql@5.7
mysql                   mysql-client@5.7        mysql-search-replace    mysqltuner
mysql++                 mysql-connector-c++     mysql@5.6               qt-mysql
#homebrew下载的默认是最新版的 mysql8.0+,在 brew search mysql 选择你要安装的。因为 mysql8.0 + 和 mysql5.7 的区别还是有点大,目前还是用 5.7 的版本
➜  ~ brew install mysql@5.7    #安装mysql5.7版本
#自动安装过程省略……
➜  ~

配置环境变量

#加入环境变量
echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc
#刷新配置文件
source ~/.zshrc

MySQL的安全安装,使用以下命令,可以更改root密码、删除匿名用户、关闭远程连接等

➜  ~ mysql_secure_installation   #执行安全安装命令Securing the MySQL server deployment.Connecting to MySQL using a blank password.VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?Press y|Y for Yes, any other key for No: n  #是否开启密码强度验证,根据自己搭建环境选择,本人没有开启
Please set the password for root here.New password: #输入新密码Re-enter new password:    #输入重复新密码
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.#是否删除匿名用户,输入y
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.#是否禁止远程登录,输入y
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
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.#是否删除test数据库,输入y
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y- 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.#是否重载权限表数据,输入y
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.All done!

启动mysql服务

➜  ~ brew services restart mysql@5.7
==> Successfully started `mysql@5.7` (label: homebrew.mxcl.mysql@5.7)

访问mysql,能进入如下界面,说明安装成功

➜  ~ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.37 HomebrewCopyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>

设置mysql 开机启动:

➜  ~ ln -sfv /usr/local/opt/mysql@5.7/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

四、配置Nginx

  1. 创建需要用到的目录:
#创建nginx日志目录
➜  ~ mkdir -p /usr/local/var/logs/nginx
#配置文件目录
➜  ~ mkdir -p /usr/local/etc/nginx/conf.d
#ssl配置文件目录
➜  ~ mkdir -p /usr/local/etc/nginx/ssl
#配置项目存放目录
➜  ~ sudo mkdir -p /var/www
➜  ~ sudo chown :staff /var/www
➜  ~ sudo chmod 775 /var/www
  1. 修改nginx.conf配置文件
➜  ~ vim /usr/local/etc/nginx/nginx.conf

nginx.conf内容替换如下

user root admin;
worker_processes  1;error_log   /usr/local/var/logs/nginx/error.log debug;pid           /usr/local/var/run/nginx.pid;events {worker_connections  256;
}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  /usr/local/var/logs/access.log  main;sendfile        on;keepalive_timeout  65;port_in_redirect off;include /usr/local/etc/nginx/servers/*;
}
  1. 创建 nginx php-fpm配置文件
➜  ~ vim /usr/local/etc/nginx/conf.d/php-fpm

输入以下内容

location ~ \.php$ {try_files  $uri = 404;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_intercept_errors on;include /usr/local/etc/nginx/fastcgi.conf;
}
  1. 创建必要的文件到/var/www/default目录
➜  ~ vi /var/www/default/info.php
➜  ~ vi /var/www/default/index.html
➜  ~ vi /var/www/default/403.html
➜  ~ vi /var/www/default/404.html
  1. 创建默认虚拟主机配置文件
➜  ~ vim /usr/local/etc/nginx/servers/default

输入以下内容

server {listen       8080;server_name  localhost;root         /var/www/default/;access_log  /usr/local/var/logs/nginx/default.access.log  main;location / {index  index.html index.htm index.php;autoindex   on;include     /usr/local/etc/nginx/conf.d/php-fpm;}location = /info {allow   127.0.0.1;deny    all;rewrite (.*) /.info.php;}error_page  404     /404.html;error_page  403     /403.html;
}
  1. 创建ssl默认虚拟主机(该步骤非必须

安装SSL

➜  ~ cd /usr/local/etc/nginx/ssl
➜  ~ openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=localhost" -keyout /usr/local/etc/nginx/ssl/localhost.key -out /usr/local/etc/nginx/ssl/localhost.crt

创建ssl虚拟主机配置文件

➜  ~ vim /usr/local/etc/nginx/servers/default-ssl

输入以下内容

server {listen       443;server_name  localhost;root       /var/www/default/;access_log  /usr/local/var/logs/nginx/default-ssl.access.log  main;ssl                  on;ssl_certificate      ssl/localhost.crt;ssl_certificate_key  ssl/localhost.key;ssl_session_timeout  5m;ssl_protocols  SSLv2 SSLv3 TLSv1;ssl_ciphers  HIGH:!aNULL:!MD5;ssl_prefer_server_ciphers   on;location / {include   /usr/local/etc/nginx/conf.d/php-fpm;}location = /info {allow   127.0.0.1;deny    all;rewrite (.*) /.info.php;}error_page  404     /404.html;error_page  403     /403.html;
}

重启所有服务

➜  ~ brew services restart nginx
➜  ~ brew services restart php@7.2
➜  ~ brew services restart mysql@5.7

至此整合完毕!

测试

#编辑info.php
$ vi /var/www/default/info.php
#输入以下内容
<?phpphpinfo();
?>
#保存

访问http://localhost:8080/info.php

五、安装Composer

➜  ~ brew search composer
==> Formulae
composer                                 kompose==> Casks
code-composer-studio                     composercat
➜  ~ brew install composer
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/composer-2.
######################################################################## 100.0%
==> Pouring composer-2.2.7.monterey.bottle.tar.gz
												

MacOS Brew搭建PHP开发环境相关推荐

  1. flutter安装_在macOS上搭建Flutter开发环境

    flutter中文网 1. 去flutter官网下载其最新可用的安装包 下载官网 2. 解压缩到想放置的文件夹 该文件夹较大,且以后不需要动,所以可以根据自己的偏好放置在合适的位置 3. 将flutt ...

  2. Flutter开发之搭建Flutter开发环境(三)

    回首demo.之前的工程突然,无法iOS上运行了.重装了最新的Xcode.然后VS Code 终端运行 flutter packages get 再打开Xcode 设置开发证书.再在iOS真机或者模拟 ...

  3. macOS下GDAL Java开发环境搭建

    文章目录 macOS下GDAL Java开发环境搭建 GDAL源码编译安装 Maven安装本地JAR 使用Java版GDAL示例 版权声明:本文为博主原创文章,转载请注明原文出处! 写作时间:2020 ...

  4. macOS中SpaceVim搭建java开发环境

    安装JDK11 过程略,可参考: https://blog.csdn.net/lxyoucan/article/details/111120215 macOS安装支持Python 3的neovim 为 ...

  5. mac搭建python开发环境_Mac打造python2 python3开发环境

    最新版的MacOS Sierra默认带的python环境仍然为2.7.10,本文将介绍使用Brew安装python3.5并做简单的配置,打造python2,python3共存的开发环境 直接尝试bre ...

  6. 从零开始vim搭建Java开发环境[视频]

    视频教程 从零开始vim搭建Java开发环境 全屏观看 前言 起初我仅仅是想在手机上搭建一套能开发Java的vim环境.<玩转手机中的linux系统termux并搭建java开发环境> 玩 ...

  7. Mac搭建Java开发环境最佳指南

    一.前言 作为一名Java开发工程师,入门以及在公司的工作环境都是使用的Windows,有一家公司的办公电脑环境用的是ubuntu系统,还没有体验用Mac来开发是怎么样的感受呢. 个人电脑还是刚上大学 ...

  8. Mac系统搭建PHP开发环境

    mac上的PHP开发环境搭建方式有很多, brew, docker, mamp等, 这里使用最近新出的工具PhpWebStudy来搭建mac上的php开发环境 安装 使用brew安装: brew in ...

  9. 从零开始vim搭建Java开发环境之coc.nvim 篇

    前言 vim之美妙我就不过多介绍了,懂的自然懂.之前我已经有一篇文章介绍如何使用SpaceVim来搭建Java开发环境. 传送门:<从零开始vim搭建Java开发环境[视频]> 最近使用c ...

最新文章

  1. Flutter 初学者的简单例子充分解释
  2. 210312阶段三通过sqlite3源码安装sqlite3
  3. async框架源码研究
  4. kafka中LEO和HW
  5. github 创始人_GitHub联合创始人Scott Chacon的视频采访,探讨代码之外的未来
  6. 专科转行学java_大专女生想转行做IT,应该先学哪一块?
  7. eclipse远程调试liunx下的tomcat
  8. matlab对数画图命令,MATLAB命令画图
  9. 以太网误码率测试软件,最完美的PAM4标准通信数据测试方案大全
  10. android下利用高德地图获取经纬度等定位信息
  11. Neo4j使用记录--APOC和GDS的安装【实践】
  12. UVM Primer Ch2 A Conventional Testbench for the TinyALU
  13. 10万微商被骗100亿,最大微商集团被爆涉嫌传销
  14. 微信小程序自定义组件使用canvas绘图,无法绘制以及fail canvas is empty问题
  15. 一个PHP程序员的职业生涯技术提升阶梯规划方案
  16. vscode用tensorboard报错 We failed to start a TensorBoard session due to the following error: Command fa
  17. OJ每日一练——雇佣兵
  18. 控制人力成本量大策略+六种方案
  19. Steamcommunity302使用教程介绍
  20. ansible UI管理工具awx安装实践

热门文章

  1. 又半年,技术的探险(2009.7)
  2. 微信扫一扫调用失败解决方案
  3. springboot+Jcrop实现图片裁剪(模仿邮箱注册上传头像)
  4. C语言学习(一.C语言概述)
  5. kodi没有中文设置_kodi播放器如何设置为中文界面-kodi播放器设置中文的方法 - 河东软件园...
  6. IT创业失败案例解析 - 第一篇
  7. FPKM值基因表达量的计算、基因ID转gene symbol的例子
  8. webRTC混音流程
  9. python自然语言处理实战核心技术与算法——HMM模型代码详解
  10. 现代软件工程 第一周作业