当前位置:   article > 正文

【MacOS12安装PHP8.0,1/2】MacOS12安装PHP7.4、PHP8.0、composer、Nginx_mac下载php环境

mac下载php环境

MacOS12 Monterey已经不自带PHP了,所以手动安装PHP。

这里我们用brew来安装所有用的到扩展,PHP版本荐PHP8.0 。

1. 安装brew(国内源):

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

顺升级brew,这里主要是确保国内brew源和全球同步:

brew update

--------------------------------------------

2-1:安装PHP8.0:

brew install php@8.0

(也可安装最新版PHP:brew install php)

2-2:链路PHP8.0

  1. fyonecon@DJhanwudi ~ % echo 'export PATH="/usr/local/opt/php@8.0/bin:$PATH"' >> ~/.zshrc
  2. fyonecon@DJhanwudi ~ % echo 'export PATH="/usr/local/opt/php@8.0/sbin:$PATH"' >> ~/.zshrc
  3. fyonecon@DJhanwudi ~ % export LDFLAGS="-L/usr/local/opt/php@8.0/lib"
  4. fyonecon@DJhanwudi ~ % export CPPFLAGS="-I/usr/local/opt/php@8.0/include"
  5. fyonecon@DJhanwudi ~ % brew services start php@8.0

查看php版本 

-----------------------------

2023-08更新:

brew install php@7.4 安装会报错:Error: php@7.4 has been disabled because it is a versioned formula

这是因为brew库已经移除了过时的php7.4版本。需要手动用第三方库安装php7.4

// 将第三方仓库加入

# brew brew tap shivammathur/php

// 安装PHP

# brew install shivammathur/php/php@7.4

// 链路php7.4

# echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
# echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc
# export LDFLAGS="-L/usr/local/opt/php@7.4/lib"
# export CPPFLAGS="-I/usr/local/opt/php@7.4/include"
# brew services start php@7.4

// 重启电脑,再用 php -v查看当前版本

-------------------------------

3. 全局安装composer:

  1. fyonecon@DJhanwudi ~ % php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
  2. fyonecon@DJhanwudi ~ % php composer-setup.php
  3. All settings correct for using Composer
  4. Downloading...
  5. Composer (version 2.1.5) successfully installed to: /Users/fyonecon/composer.phar
  6. Use it: php composer.phar
  7. fyonecon@DJhanwudi ~ % php -r "unlink('composer-setup.php');"
  8. fyonecon@DJhanwudi ~ % sudo mv composer.phar /usr/local/bin/composer
  9. Password:
  10. fyonecon@DJhanwudi ~ % composer selfupdate
  11. You are already using the latest available Composer version 2.1.5 (stable channel).
  12. fyonecon@DJhanwudi ~ %

4. 安装nginx:

brew install nginx

查看nginx版本:

nginx -v

启动nginx:

brew services start nginx

成功访问如下网址代表成功:

http://127.0.0.1/

其他命令:

停止:brew services stop nginx

重启:brew services restart nginx

5. 配置nginx.conf:

文件在:/usr/local/etc/nginx/nginx.conf ,你可以直接把如下代码全部替换到nginx.conf:

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log 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. server {
  23. listen 80;
  24. server_name _;
  25. #charset koi8-r;
  26. #access_log logs/host.access.log main;
  27. location / {
  28. root html;
  29. index index.php index.html;
  30. }
  31. #error_page 404 /404.html;
  32. # redirect server error pages to the static page /50x.html
  33. #
  34. error_page 500 502 503 504 /50x.html;
  35. location = /50x.html {
  36. root html;
  37. }
  38. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  39. location ~ \.php$ {
  40. root html;
  41. fastcgi_pass 127.0.0.1:9000;
  42. fastcgi_index index.php;
  43. #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  44. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  45. include fastcgi_params;
  46. }
  47. location ~* .(env|toml|yaml|log)$ {
  48. return 404;
  49. }
  50. }
  51. server
  52. {
  53. listen 8500;
  54. server_name _;
  55. location / {
  56. proxy_pass http://127.0.0.1:9500;
  57. index index.html ;
  58. }
  59. location ~* .(env|toml|yaml|log)$ {
  60. return 404;
  61. }
  62. }
  63. # another virtual host using mix of IP-, name-, and port-based configuration
  64. #
  65. #server {
  66. # listen 8000;
  67. # listen somename:8080;
  68. # server_name somename alias another.alias;
  69. # location / {
  70. # root html;
  71. # index index.html index.htm;
  72. # }
  73. #}
  74. # HTTPS server
  75. #
  76. #server {
  77. # listen 443 ssl http2;
  78. # server_name localhost;
  79. # ssl_certificate cert.pem;
  80. # ssl_certificate_key cert.key;
  81. # ssl_session_cache shared:SSL:1m;
  82. # ssl_session_timeout 5m;
  83. # ssl_ciphers HIGH:!aNULL:!MD5;
  84. # ssl_prefer_server_ciphers on;
  85. # location / {
  86. # root html;
  87. # index index.php index.html;
  88. # }
  89. #}
  90. include host/*.conf;
  91. }

需要重启nginx:

brew services restart nginx

6. 验证PHP文件运行:

代码默认存放目录:

/usr/local/var/www

设置一个index.php文件,并填入如下内容:

<?php phpinfo(); ?>

在成功浏览器访问:  

-

完成!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/848463
推荐阅读
相关标签
  

闽ICP备14008679号