当前位置:   article > 正文

macOS搭建PHP开发环境(brew安装nginx、mysql 和多版本php,并配置多个php同时运行的环境)_mac搭建php开发环境

mac搭建php开发环境

macOS搭建php开发环境

1.安装brew

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

2.安装Nginx

brew install nginx

3.安装mysql

brew install mysql

4.安装php

由于homebrew主库中没有PHP7.2 之前的版本,并且7.2,7.3版本也被标记成过时版本;所以需要先挂在第三方的扩展,具体操作如下:

  1. brew tap shivammathur/php
  2. brew search php

php5.6

brew install shivammathur/php/php@5.6

php7.3

brew install shivammathur/php/php@7.3

php7.4

brew install shivammathur/php/php@7.4

php8.2

默认新版8以上直接安装

brew install php

5.修改php设置

sudo vim /usr/local/etc/php/5.6/php-fpm.conf  下的:

注意:5.6版本的配置文件路径和其他版本不一样
listen = 127.0.0.1:9000
改为
listen = 127.0.0.1:9056

sudo vim /usr/local/etc/php/7.3/php-fpm.d/www.conf  下的:
listen = 127.0.0.1:9000
改为
listen = 127.0.0.1:9073

sudo vim /usr/local/etc/php/7.4/php-fpm.d/www.conf  下的:
listen = 127.0.0.1:9000
改为
listen = 127.0.0.1:9074

sudo vim /usr/local/etc/php/8.2/php-fpm.d/www.conf  下的:
listen = 127.0.0.1:9000
改为
listen = 127.0.0.1:9082

6.修改nginx配置

sudo vim /usr/local/etc/nginx/nginx.conf

改为如下:

  1. #user nobody;
  2. worker_processes 1;
  3. error_log /var/logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. #gzip on;
  22. include servers/*;
  23. }

配置站点test1:

cd /usr/local/etc/nginx/servers 下新建站点配置文件如:
sudo vim test1.conf
内容如下:

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. # 配置项目路径
  5. root /Users/xxx/xxx/php/2023/test1;
  6. #access_log logs/host.access.log main;
  7. location / {
  8. index index.html index.htm index.php;
  9. if (!-e $request_filename) {
  10. rewrite ^(.*)$ /index.php?s=/$1 last;
  11. break;
  12. }
  13. }
  14. #error_page 404 /404.html;
  15. # redirect server error pages to the static page /50x.html
  16. #
  17. error_page 500 502 503 504 /50x.html;
  18. location = /50x.html {
  19. root html;
  20. }
  21. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  22. location ~ \.php$ {
  23. # 9056上面设置的监听端口,加载php5.6
  24. fastcgi_pass 127.0.0.1:9056;
  25. fastcgi_index index.php;
  26. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  27. include fastcgi_params;
  28. }
  29. }

配置站点test2:

cd /usr/local/etc/nginx/servers 下新建站点配置文件如:
sudo vim test2.conf
内容如下:

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. # 配置项目路径
  5. root /Users/kbq/workspace/php/2023/test2;
  6. #access_log logs/host.access.log main;
  7. location / {
  8. index index.html index.htm index.php;
  9. if (!-e $request_filename) {
  10. rewrite ^(.*)$ /index.php?s=/$1 last;
  11. break;
  12. }
  13. }
  14. #error_page 404 /404.html;
  15. # redirect server error pages to the static page /50x.html
  16. #
  17. error_page 500 502 503 504 /50x.html;
  18. location = /50x.html {
  19. root html;
  20. }
  21. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  22. location ~ \.php$ {
  23. # 9074上面设置的监听端口,加载php7.4
  24. fastcgi_pass 127.0.0.1:9074;
  25. fastcgi_index index.php;
  26. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  27. include fastcgi_params;
  28. }
  29. }

检查配置

nginx -t
如果报文件权限问题:sudo chmod -R 777 /var/logs

设置php-fpm开机自启

未验证成功

/usr/local/opt/php@5.6 下的homebrew.php@5.6.service
/usr/local/opt/php@7.3 下的homebrew.php@7.3.service

/usr/local/opt/php@7.4 下的homebrew.php@7.4.service
/usr/local/opt/php@8.2 下的homebrew.php@8.2.service
/usr/local/opt/php 下的homebrew.php.service

复制到

/Library/LaunchAgents

最后重启系统 

手动启动php-fpm

 brew services start php@5.6

 brew services start php@7.3

 brew services start php@7.4

 brew services start php@8.2

验证是否启动成功

lsof -i :9056

lsof -i :9073

lsof -i :9074

lsof -i :9082

终端切换php版本

解除之前版本链接:brew unlink php

 增加新版本链接:

brew link --overwrite php@5.6

brew link --overwrite php@7.3

brew link --overwrite php@7.4

brew link --overwrite php@8.2

 将php加入环境变量

将如下加入 /Users/xxx/.bash_profile

  1. export PATH=${PATH}:/usr/local/opt/php@5.6/bin
  2. export PATH=${PATH}:/usr/local/opt/php@5.6/sbin
  3. alias php56="/usr/local/opt/php@5.6/bin/php"
  4. export PATH=${PATH}:/usr/local/opt/php@7.3/bin
  5. export PATH=${PATH}:/usr/local/opt/php@7.3/sbin
  6. alias php73="/usr/local/opt/php@7.3/bin/php"
  7. export PATH=${PATH}:/usr/local/opt/php@7.4/bin
  8. export PATH=${PATH}:/usr/local/opt/php@7.4/sbin
  9. alias php74="/usr/local/opt/php@7.4/bin/php"
  10. export PATH=${PATH}:/usr/local/opt/php@8.2/bin
  11. export PATH=${PATH}:/usr/local/opt/php@8.2/sbin
  12. alias php82="/usr/local/opt/php@8.2/bin/php"

使配置生效 source .bash_profile

校验配置是否成功,终端输入

php56 -v

php73 -v

php74 -v

php82 -v

显示如下类似内容,则为成功

  1. PHP 8.2.12 (cli) (built: Nov 6 2023 02:54:37) (NTS)
  2. Copyright (c) The PHP Group
  3. Zend Engine v4.2.12, Copyright (c) Zend Technologies
  4. with Zend OPcache v8.2.12, Copyright (c), by Zend Technologies

安装composer

  1. php56 -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
  2. php56 -r "if (hash_file('sha384', 'composer-setup.php') === 'e21205b207c3ff031906575712edab6f13eb0b361f2085f1f1237b7126d785e826a450292b6cfd1d64d92e6563bbde02') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
  3. php56 composer-setup.php
  4. php56 -r "unlink('composer-setup.php');"
  5. sudo mv composer.phar /usr/local/bin/composer56

检验是否成功 

  1. ~/ composer74
  2. Failed loading /usr/local/Cellar/php/7.3.3/lib/php/pecl/20180731/xdebug.so: dlopen(/usr/local/Cellar/php/7.3.3/lib/php/pecl/20180731/xdebug.so, 0x0009): tried: '/usr/local/Cellar/php/7.3.3/lib/php/pecl/20180731/xdebug.so' (no such file)
  3. ______
  4. / ____/___ ____ ___ ____ ____ ________ _____
  5. / / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
  6. / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
  7. \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
  8. /_/
  9. Composer version 2.6.5 2023-10-06 10:11:52

其他版本安装,将php56改为php73、php74、php82,将composer56改为composer73、composer74、composer82

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号