在 macOX 系统上搭建 PHP 开发环境

标签: 编程学习 PHP学习

之前在自己电脑上开发都是使用的集成开发环境,最近把电脑系统升级到了最新版,发现很多集成环境各种问题,就不想折腾了,大家都知道 OX 系统是基于 UNIX 的,跟 Linux 也比较接近,所以就直接在电脑上配置一下环境吧,其实 OX 系统本身带的有很多服务,但是由于我们可能经常改配置什么的,所以不建议直接使用系统的,以免损害到系统,我这里使用的是 brew 安装的。

提醒

在mac上开发一定要装好 Xcode 和 命令行工具

xcode-select --install # 安装 Xcode Command Line Tools

Homebrew 使用mac的都应该用上了,没用过的建议直接搜~

PHP

我这里安装的是 PHP7.3

brew search php
brew install php@7.3

安装好之后修改环境变量

echo 'export PATH="/usr/local/opt/php@7.3/bin:$PATH"' >> ~/.bash_profile

echo 'export PATH="/usr/local/opt/php@7.3/sbin:$PATH"' >> ~/.bash_profile

source ~/.bash_profile

启动 PHP 服务

brew services start php@7.3

MySQL

我这里安装的是 MySQL 的 5.6 版本

brew search mysql
brew install mysql@5.6
echo 'export PATH="/usr/local/opt/mysql@5.6/bin:$PATH"' >> ~/.bash_profile

安装完成后,启动 MySQL

brew services start mysql@5.6

连接 MySQL 服务器,默认是没有密码的

mysql -u root -p

设置 root 密码、安全等级等参数

mysql_secure_installation

Nginx

Nginx 我就使用的最新版本了,直接安装

brew install nginx

启动服务

brew services start nginx

这里可能就会遇到问题了,发现 Nginx 无法启动,问题在于要监听端口需要管理员权限,添加之,这里添加的可能多了,适当使用

sudo chown root:wheel  /usr/local/opt/nginx/bin/nginx
sudo chmod u+s /usr/local/opt/nginx/bin/nginx
sudo chown root:wheel /usr/local/Cellar/nginx/1.19.8/bin/nginx
sudo chmod u+s /usr/local/Cellar/nginx/1.19.8/bin/nginx

配置虚拟主机,Nginx 的配置文件在/usr/local/etc/nginx/nginx.conf,查看最后会读取 /usr/local/etc/nginx/servers/ 这里的配置,那我们直接把虚拟主机配置在这个目录下

这里要注意用不到 rtmp 服务的话请把 rtmp 的配置注释掉。

我这里的配置是 Laravel 的简单版

server {
    listen 80;
    server_name flc.test;
    root /Users/admin/Desktop/flcc/public;
    rewrite . /index.php;
    location / {
        index index.php index.html index.htm;
        autoindex on;
    try_files $uri $uri/ /index.php?$query_string;
    }
    #proxy the php scripts to php-fpm
    location ~ \.php$ {
        include /usr/local/etc/nginx/fastcgi.conf;
        fastcgi_intercept_errors on;
        fastcgi_pass 127.0.0.1:9000;
    }
}

重启服务就可以访问了

brew services restart nginx
brew services list  #查看启动的服务列表

其实使用 brew 安装大部分操作都差不多,只是有细微的差别,后面我还安装了 Redis 等,大家可以自己试试。