Mac安装PHP开发环境

article/2025/10/23 6:00:40

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_installationSecuring 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 HomebrewCopyright (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
#输入以下内容
<?phpphpinfo();
?>
#保存

浏览器访问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

http://chatgpt.dhexx.cn/article/bmUrVAkR.shtml

相关文章

如何在VSCode配置PHP开发环境(详细版)

这篇博文是当初笔者上课需要配置XAMPP&#xff0c;整理出来配置方法&#xff0c;错漏之处没有认真核对&#xff0c;给造成麻烦的同学道个歉。 以下有两场修正之处。第一处&#xff0c;XAMPP国内下载地址改成了XAMPP中文网最新版本下载链接&#xff0c;给之前误下p2p的同学再次道…

Windows上搭建PHP开发环境

Windows上搭建PHP开发环境 前言运行环境&安装软件数据库 mysql 安装服务器 nginx 安装PHP 安装redis 配置phpMyAdmin 安装PhpStorm 安装结语 前言 作为一个不太全面发展的客户端程序员&#xff0c;基本没怎么写过服务器&#xff0c;就是以前闲暇时候写过点php和go&#xf…

php开发环境

本人用的是phpstudy&#xff0c;很方便 开启三个功能 FTP Sql数据库 以及最重要的Apach 同时Apach能配置本机的虚拟主机&#xff0c;以及访问的项目路径&#xff0c;本人觉得还是很方便的 管理数据库的工具&#xff0c;个人用的是Navicat 至于破解&#xff0c;可以网上找找看…

搭建PHP开发环境(Apache+PHP+MySQL)

简介&#xff1a;总结在Windows环境搭建PHP开发环境&#xff08;ApachePHPMySQL&#xff09; 安装Apache 1、下载Apache&#xff0c;地址&#xff1a;http://www.apachelounge.com/download/&#xff0c;如下图&#xff1a; 2、解压压缩包&#xff0c;最好放到磁盘根目录&…

PHP笔记(一):开发环境配置

本文记录如何在Windows系统配置简单的PHP开发环境。 目录 PHP环境部署下载安装XAMPP配置XAMPP Vscode 配置PHP配置PHP 可执行文件安装插件 PHP代码示例 PHP环境部署 PHP适用于Web应用程序开发&#xff0c;这里介绍使用XAMPP来配置开发环境&#xff0c;它是一个免费的Apache发行…

Linux中的insmod和mknod命令

一、insmod命令详解 insmod命令用于将给定的模块加载到内核中。Linux有许多功能是通过模块的方式&#xff0c;在需要时才载入kernel。如此可使kernel较为精简&#xff0c;进而提高效率&#xff0c;以及保有较大的弹性。这类可载入的模块&#xff0c;通常是设备驱动程序。 注意…

mknod命令的使用

1、mknod命令 在Linux系统下&#xff0c;mknod命令可用于系统下字符设备文件和块设备文件的创建。 &#xff08;1&#xff09;命令语法 mknod&#xff08;选项&#xff09;&#xff08;参数&#xff09; &#xff08;2&#xff09;常用选项说明 -Z&#xff1a;设置安全的上下文…

mknod创建一个设备节点

mknod是make node的缩写&#xff0c;就是创建一个节点&#xff08;设备文件又叫做设备节点&#xff09;。在linux系统中&#xff0c;一个节点代表一个文件&#xff0c;创建一个文件最主要的根本工作就是分配一个新的节点&#xff08;注意&#xff0c;这是存在于磁盘上的节点&am…

QtableView点击滑动设计

效果 头文件&#xff1a; #ifndef CustomScroll_H #define CustomScroll_H#include <QObject> #include <QWidget> #include <QTimer> #include <QTableView> #include <QPropertyAnimation> #include <QDateTime>class CustomScroll :…

QTableView样式表实战

1.案例 隔行颜色、选中颜色、标题样式、表格内容样式、表格边角样式 2.qss代码 /*设置表格整体样式*/ QTableView{outline:none; /*设置选中单元格不显示虚线框*/border:0px solid #FFFFFF;background-color:#FFFFFF; } /*设置表格标题样式*/ QHeaderView::section {padding:…

QT(3)-QTableView

QTableView 0 相关文章1 说明2 常用函数2.1 clearSpans2.2 setSpan2.3 columnAt2.4 rowAt2.5 columnSpan2.6 rowSpan2.7 columnViewportPosition2.8 rowViewportPosition2.9 列宽、行高2.9.1 columnWidth2.9.2 rowHeight2.9.3 setColumnWidth2.9.4 setRowHeight2.9.5 resizeCol…

QTableView复选框

QTableView复选框&#xff0c;支持未选中、部分选中、全选三种状态 参考博文&#xff08;若有侵权&#xff0c;联系删除&#xff09; https://blog.csdn.net/liang19890820/article/details/50718340https://blog.csdn.net/qq_44257811/article/details/120266599 效果图&am…

Qt QTableView QStandardItemModel用法

QStandardItemModel 是标准的以项数据&#xff08;item data&#xff09;为基础的标准数据模型类&#xff0c;通常与 QTableView 组合成 Model/View 结构&#xff0c;实现通用的二维数据的管理功能。 本节介绍 QStandardltemModel 的使用&#xff0c;主要用到以下 3 个类&#…

pyqt QTableView详细用法

QTableView是表格视图&#xff0c;可以像excel一样的显示数据&#xff0c;如下图&#xff1a; 详细代码&#xff1a; from PyQt5.QtWidgets import * from PyQt5.QtGui import * import sysclass TableView(QWidget):def __init__(self, argNone):super(TableView, self).__in…

QT QTableView(基于QStandardItemModel用法)操作详解

文章目录 前言关于QStandardItemModel上下翻页实例代码讲解 前言 前面详细介绍过QTableWidget的用法&#xff0c;今天想一次性把QTable系列写完&#xff0c;也就是QTabelview的使用&#xff0c;我发现使用QTableWidget如果频繁的clean&#xff0c;然后又set&#xff0c;会很卡…

Qt QTableView样式设置

Qt QTableView样式设置 显示效果 QSS设置 选中设置&#xff1a; 1 2 3 4 5 QTableView::item:selected { background:rgb(0,0,100); color:white; } 表头设置&#xff1a; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 QHeaderView::section:first { b…

Qt QTableView详解

一.常用接口 1.设置model&#xff0c;添加model数据 model new QStandardItemModel(this);model->setHorizontalHeaderLabels(QStringList()<<"姓名"<<"性别"<<"年龄");QStandardItem *item00 new QStandardItem("…

QTableView常用设置

QTableView的设置比较复杂&#xff0c;做项目的时候总是搞忘&#xff0c;以本文记录一下它的样式、布局等设置方式。 目录 1、设置item布局1.1、平均铺满1.2、最后一项铺满 2、交替背景色2.1、行交替2.2、项交替 3、去掉选中虚线4、去掉网格线5、设置选中方式5.1、选中单个项目…

qt中Qtableview的用法

QTableView常用于实现数据的表格显示。下面我们如何按步骤实现学生信息表格&#xff1a; 一 添加表头 //准备数据模型 QStandardItemModel *student_model new QStandardItemModel(); student_model->setHorizontalHeaderItem(0, new QStandardItem(QObject::tr(&…

Qt深入浅出(十五)QTableView

QTableView ​ 表格视图控件QTableView&#xff0c;需要和QStandardItemModel, 配套使用&#xff0c;这套框架是基于MVC设计模式设计的&#xff0c;M(Model)是QStandardItemModel数据模型&#xff0c;不能单独显示出来。V(view)是指QTableView视图&#xff0c;要来显示数据模型…