Skip to content

安装开发环境 - macOS

检测苹果电脑的CPU型号

苹果电脑目前有 2 种类型的 CPU,Apple Silicon CPU (ARM)Intel CPU (x86) 。 不同型号CPU的苹果电脑,安装步骤略有不同。新款的苹果电脑都是基于 Apple Silicon CPU 。

可通过以下命令查看当前电脑的CPU型号:

bash
uname -m

根据输出的结果判断:

  • 如果输出为 arm64,表示你的电脑是使用 Apple Silicon(例如 M1、M2、M3...)CPU。
  • 如果输出为 x86_64,表示你的电脑是使用 Intel CPU。

安装 Homebrew

Homebrew 是 macOS 下流行的包管理软件。Homebrew 默认从 GitHub 拉取安装软件包文件,中国大陆地区推荐从清华大学开源软件镜像站 的 Homebrew 镜像站拉取。

下面以使用清华大学开源软件镜像源为例,安装 Homebrew。

设置环境变量:

bash
export HOMEBREW_INSTALL_FROM_API=1
export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"

安装Homebrew:

bash
git clone --depth=1 https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install.git brew-install
/bin/bash brew-install/install.sh
rm -rf brew-install
bash
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc
eval "$(/opt/homebrew/bin/brew shellenv)"

echo 'export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"' >> ~/.zshrc
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zshrc
echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> ~/.zshrc
echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> ~/.zshrc
source ~/.zshrc
bash
(echo; echo 'eval "$(/usr/local/bin/brew shellenv)"') >>  ~/.zshrc
eval "$(/usr/local/bin/brew shellenv)"

echo 'export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"' >> ~/.zshrc
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zshrc
echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> ~/.zshrc
echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> ~/.zshrc
source ~/.zshrc

安装 PHP

添加 PHP 软件源:

brew tap shivammathur/php

安装 PHP 7.4:

bash
brew install shivammathur/php/php@7.4

链接为系统默认 PHP 版本:

brew link --overwrite --force shivammathur/php/php@7.4

配置环境变量:

bash
echo 'export PATH="/opt/homebrew/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc
source ~/.zshrc
bash
echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc
source ~/.zshrc

启动 PHP-FPM 服务:

bash
brew services start shivammathur/php/php@7.4

提示

可以通过以下命令重启、停止 PHP-FPM 服务:

bash
# 重启
brew services restart shivammathur/php/php@7.4 

# 停止
brew services stop shivammathur/php/php@7.4

验证 PHP-FPM 服务是否启动成功:

bash
ps aux | grep php-fpm

如存在相关 PHP-FPM 进程,则表明启动成功。

PHP 默认配置文件路径:

bash
php.ini: /opt/homebrew/etc/php/7.4/php.ini
fpm: /opt/homebrew/etc/php/7.4/php-fpm.d/www.conf
bash
php.ini: /usr/local/etc/php/7.4/php.ini
fpm: /usr/local/etc/php/7.4/php-fpm.d/www.conf

安装 Nginx

创建目录:

bash
sudo mkdir -p /var/www
sudo chown root:staff /var/www
sudo chmod g+w /var/www
sudo mkdir -p /var/log/nginx

安装:

bash
brew install nginx

Nginx 安装完毕后的默认配置:

Web根目录:/opt/homebrew/var/www
Nginx默认端口:8080
Nginx Server配置目录:/opt/homebrew/etc/nginx/servers/
Nginx 日志目录:/opt/homebrew/var/log/nginx
Web根目录:/usr/local/var/www
Nginx默认端口:8080
Nginx Server配置目录:/usr/local/etc/nginx/servers/
Nginx 日志目录:/usr/local/var/log/nginx

修改配置文件:

/opt/homebrew/etc/nginx/nginx.conf
/usr/local/etc/nginx/nginx.conf
conf
user  nobody;
worker_processes  1;

error_log  /var/log/nginx/error.log info;
pid        /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  /var/log/nginx/access.log  main;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;
        root         /var/www;
        charset utf-8;

        location / {
            index index.html index.htm index.php;
        }

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            include        fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        }
    }

    include servers/*;
}

以上配置的调整主要为了跟 Ubuntu 的配置风格保持一致:

  1. 监听端口8080,改为80;
  2. 调整 localhostroot 目录到 /var/www 下;
  3. 指定 error_log , access_log 日志目录到 /var/log/nginx 目录下;
  4. 开启 localhost.php 文件结尾的解析;

启动 Nginx 服务:

bash
sudo brew services start nginx

注意

由于系统限制,使用低于1024端口的应用程序需以root权限启动。所以启动80端口的Nginx需通过sudo来启动。

验证 Nginx 服务启动是否成功:

bash
echo "Hello, World." > /var/www/index.html
echo "<?php \nphpinfo();" > /var/www/phpinfo.php

在浏览器中打开http://localhost,出现Hello, World.表示 Nginx 启动成功。 在浏览器中打开http://localhost/phpinfo.php,出现 PHP 配置相关信息,表示 PHP 配置成功。

如启动失败,可通过查看/var/log/nginx/error.log日志文件来获得错误信息。

安装 MySQL

bash
brew install mysql@8.0

设置环境变量:

bash
echo 'export PATH="/opt/homebrew/opt/mysql@8.0/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
bash
echo 'export PATH="/usr/local/opt/mysql@8.0/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

启动 MySQL 服务:

bash
brew services start mysql@8.0

Mysql的root密码默认为,可以通过:mysql -uroot命令进入mysql。

修改root用户的登陆验证模式:

进入数据库:mysql -uroot,执行:

sql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '';
FLUSH PRIVILEGES;
exit;

安装 phpMyAdmin

phpMyAdmin 是一款方便易用的 MySql 数据库管理软件。可以在 官网 下载最新版的 phpMyAdmin。

下载后,将 phpMyAdmin 解压到:/var/www/phpmyadmin目录下,创建配置文件config.inc.php,内容如下:

php
<?php

$i = 0;

/* Server: localhost [1] */
$i++;
$cfg['Servers'][$i]['verbose'] = '127.0.0.1';
$cfg['Servers'][$i]['host'] = '127.0.0.1';
$cfg['Servers'][$i]['port'] = '';
$cfg['Servers'][$i]['socket'] = '';
$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = '';
$cfg['Servers'][$i]['AllowNoPassword'] = true;

在打开浏览器打开 http://localhost/phpmyadmin/ ,即可进入 phpMyAdmin。

安装前端工具链

我们是用 Volta 来管理前端工具链。参考:Volta: JavaScript工具链管理

EduSoho 前端编译使用 NodeJS 16 版本。安装完毕后,中国大陆地区可配置 NPM 镜像,加速软件包的安装 :

bash
npm config set registry https://registry.npmmirror.com