当前位置:网站首页 > PHP开发 > 正文

mac 安装php环境

MacOS下安装homebrew包管理器

一、安装PHP

#搜索PHP,会搜索出PHP相关的资源,然后根据自己需求选择安装 $ brew search php #安装PHP,不指定PHP版本时,默认安装最新版本 $ brew install php 

程序会自动安装,安装完成后,将PHP加入 $PATH 中

#shell使用bash添加方式 $ vim ~/.bash_profile export PATH="/usr/local/sbin:$PATH" source ~/.bash_profile # shell使用zsh添加方式 $ vim ~/.zshrc export PATH="/usr/local/sbin:$PATH" source ~/.zshrc 

配置php-fpm.conf

vim /usr/local/etc/php/8.1/php-fpm.conf #去掉第17行和第24行前面的分号 17 ;pid = run/php-fpm.pid 24 ;error_log = log/php-fpm.log 

PHP相关操作

#启动|停止|重启 $ brew services start|stop|restart php 

查看php-fpm是否启动成功

$ lsof -Pni4 | grep LISTEN | grep php #启动成功输出 php-fpm 80788 yamol 8u IPv4 0x38dc2fee9c1e7791 0t0 TCP 127.0.0.1:9000 (LISTEN) php-fpm 80794 yamol 9u IPv4 0x38dc2fee9c1e7791 0t0 TCP 127.0.0.1:9000 (LISTEN) php-fpm 80795 yamol 9u IPv4 0x38dc2fee9c1e7791 0t0 TCP 127.0.0.1:9000 (LISTEN) 

二、安装nginx

#安装nginx $ brew install nginx #使用80端口的话,需要将nginx加入root组当中 $ sudo cp -v /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/ $ sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist #启动nginx服务 $ brew services start nginx #测试nginx是否安装成功 $ curl -IL http://127.0.0.1:8080 

安装成功结果

HTTP/1.1 200 OK
Server: nginx/1.21.4
Date: Thu, 16 Dec 2021 02:08:07 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 02 Nov 2021 14:49:28 GMT
Connection: keep-alive
ETag: “61814ff8-267”
Accept-Ranges: bytes

Nginx 其它相关操作

#启动nginx $ sudo nginx #重新加载|重启|退出 $ sudo nginx -s reload|reopen|quit #brew操作,启动|停止|重启 $ brew services start|stop|restart nginx 

三、安装MySQL

#搜索mysql,查看包含哪些版本 $ brew search mysql #安装mysql,不指定版本号,默认安装最新 $ brew install mysql 

MySQL安全设置mysql_secure_installation

#执行mysql_secure_installation $ mysql_secure_installation Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD COMPONENT 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 component? #是否验证密码强度,输入y|Y验证,输入其他不验证 Press y|Y for Yes, any other key for No: #设置root登录密码 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|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|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|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|Y重载,输入其他键不重载,本人选择重载 Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y Success. All done! 

测试数据库是否安装成功,如下显示即为安装成功

mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 15 Server version: 8.0.27 Homebrew Copyright (c) 2000, 2021, 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相关操作

#启动|停止|重启 mysql服务 $ brew services start|stop|restart mysql #本地登录MySQL $ mysql -u用户名 -p密码 #远程登录 $ mysql -h IP地址 -u用户名 -p密码 -P端口号(默认3306) 

四、整合Nginx+PHP+MySQL

1.创建配置文件的文件夹

$ mkdir -p /usr/local/etc/nginx/logs $ mkdir -p /usr/local/etc/nginx/conf.d $ mkdir -p /usr/local/etc/nginx/ssl $ sudo chown :staff /usr/local/var/www $ sudo chmod 775 /usr/local/var/www 

2.修改nginx配置文件

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

nginx.conf 内容如下:

worker_processes 1;

error_log       /usr/local/etc/nginx/logs/error.log debug;
pid             /usr/local/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 /usr/local/etc/nginx/logs/access.log main;

     sendfile   on;

     keepalive_timeout 65;

     index index.html index.php;

     include /usr/local/etc/nginx/servers/*;
}

3.创建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; } 

4.创建站点配置文件

$ vim /usr/local/etc/nginx/servers/default.conf 

输入以下内容

server {
        listen          8080;
        server_name     localhost;
        root            /usr/local/var/www/;
        charset utf-8;

        access_log      /usr/local/etc/nginx/logs/default.access.log main;
        error_log       /usr/local/var/log/nginx/default.error.log;

        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 $ brew services restart mysql 

至此整合完毕!

测试

#创建info.php $ vim /usr/local/var/www/info.php #输入以下内容 <?php phpinfo(); ?> #保存 

浏览器访问http://localhost:8080/info.php
正常展示如下面表示配置成功
image.png

五、遇见的报错整理

1.在mac上启动nginx时会报一个警告

nginx: [warn] 1024 worker_connections exceed open file resource limit: 256 

解决方案:

$ ulimit -n 1024 

2.nginx: [error] invalid PID number “” in “/var/run/nginx/nginx.pid”

➜ run sudo nginx -s reload Password: nginx: [error] invalid PID number "" in "/usr/local/var/run/nginx.pid" #解决方案:重新指向配置文件即可 ➜ run sudo nginx -c /usr/local/etc/nginx/nginx.conf nginx: [warn] 1024 worker_connections exceed open file resource limit: 256 ➜ run sudo nginx -s reload 

3.error 2002 (hy000): can’t connect to local mysql server through socket ‘/tmp/mysql.sock’ (xxx)

#第一步,先关闭 mysql 服务,使用brew 启动的mysql服务,使用下面命令关闭 $ brew services stop mysql #如果是使用 mysql.server start 命令启动的mysql,则使用如下命令关闭 $ mysql.server stop #第二步,依次输入下面命令 $ pkill mysql $ rm -rf /usr/local/var/mysql/ #注意:此操作会删除数据库的所有数据,请提前备份。 $ brew postinstall mysql $ brew services restart mysql $ mysql -uroot 
到此这篇mac 安装php环境的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • PHP开发API接口的实例代码_php api接口开发实例2024-10-30 23:12:00
  • PHP从入门到精通—PHP开发入门-PHP概述、PHP开发环境搭建、PHP开发环境搭建、第一个PHP程序、PHP开发流程2024-10-30 23:12:00
  • eclipse搭建php开发环境2024-10-30 23:12:00
  • mac php开发环境搭建2024-10-30 23:12:00
  • 常用PHP开发工具都有哪些(2021整理)2024-10-30 23:12:00
  • Mac上使用phpstudy+vscode配置PHP开发环境2024-10-30 23:12:00
  • mac使用docker安装php开发环境_mac 安装docker2024-10-30 23:12:00
  • 如何提升PHP开发技能:从初学者到高级开发者的进阶之路2024-10-30 23:12:00
  • eclipse创建php2024-10-30 23:12:00
  • 如何提升PHP技能:从初学者到高级开发者的实用指南2024-10-30 23:12:00
  • 全屏图片