文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Mac安装PHP开发环境

2023-09-06 10:06

关注

MacOS下安装homebrew包管理器

一、安装PHP

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

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

#shell使用bash添加方式$ vim ~/.bash_profileexport PATH="/usr/local/sbin:$PATH"source ~/.bash_profile# shell使用zsh添加方式$ vim ~/.zshrcexport 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.pid24 ;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 passwordsand improve security. It checks the strength of passwordand allows the users to set only those passwords which aresecure 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 havea user account created for them. This is intended only fortesting, and to make the installation go a bit smoother.You should remove them before moving into a productionenvironment.#是否删除匿名用户,输入y|Y删除,输入其他键不删除,本人选择删除Remove anonymous users? (Press y|Y for Yes, any other key for No) : ySuccess.Normally, root should only be allowed to connect from'localhost'. This ensures that someone cannot guess atthe root password from the network.#是否禁止远程登录,输入y|Y禁止,输入其他键不禁止,本人选择禁止Disallow root login remotely? (Press y|Y for Yes, any other key for No) : ySuccess.By default, MySQL comes with a database named 'test' thatanyone can access. This is also intended only for testing,and should be removed before moving into a productionenvironment.#是否删除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 changesmade so far will take effect immediately.#是否重载权限表,输入y|Y重载,输入其他键不重载,本人选择重载Reload privilege tables now? (Press y|Y for Yes, any other key for No) : ySuccess.All done!

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

mysql -uroot -pEnter password:Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 15Server version: 8.0.27 HomebrewCopyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.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

创建配置文件的文件夹

$ 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

修改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/*;}

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

创建站点配置文件

$ 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#输入以下内容#保存

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

五、遇见的报错整理

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

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

解决方案:

$ ulimit -n 1024

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

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

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

来源地址:https://blog.csdn.net/wangyaodong915/article/details/123587481

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯