首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

全新 Mac 打造 PHP 開發環境完整教學 (Homebrew + Nginx + PHP-FPM + MySQL)

     編輯:PHP 綜合知識

系統準備

  • macOS 10.9+ (Mavericks 或更新)
  • 安裝 Xcode Command Line Tools:xcode-select --install

Homebrew 套件管理器

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew doctor  # 檢查環境

核心開發工具

brew install git wget tmux cmake openssl imagemagick graphicsmagick gearman geoip readline autoconf autojump zsh-completions sshfs
brew install macvim --with-override-system-vim

Oh My Zsh + iTerm2(推薦終端環境)

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
chsh -s /bin/zsh
brew cask install iterm2

Homebrew Cask 安裝 GUI 應用

brew tap homebrew/cask
brew cask install alfred appcleaner firefox google-chrome phpstorm sublime-text sequel-pro sketch mplayerx

資料庫與快取服務

MySQL

brew install mysql
brew services start mysql
mysql_secure_installation  # 設定 root 密碼、移除匿名用戶等

Redis / Memcached

brew install redis memcached
brew services start redis
brew services start memcached

PHP 環境 (以 PHP 8.2 為例)

brew tap shivammathur/php
brew install shivammathur/php/php@8.2 --with-fpm --with-gd --with-imap --with-intl --with-mysql --with-pgsql --with-redis --with-xdebug
brew link --force --overwrite php@8.2

# 設定 PATH
echo 'export PATH="/opt/homebrew/opt/php@8.2/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/php@8.2/sbin:$PATH"' >> ~/.zshrc
source ~/.zshrc

安裝常用擴充套件

pecl install redis swoole mongodb imagick
# 或透過 brew
brew install php@8.2-redis php@8.2-swoole php@8.2-imagick

PHP-FPM 設定與啟動

# 編輯 /opt/homebrew/etc/php/8.2/php-fpm.d/www.conf
# listen = 127.0.0.1:9000
brew services start php@8.2

Nginx 設定

brew install nginx
sudo mkdir -p /var/www
sudo chown $(whoami):staff /var/www
sudo chmod 775 /var/www
# /opt/homebrew/etc/nginx/nginx.conf 關鍵片段
worker_processes auto;
error_log /opt/homebrew/var/log/nginx/error.log;

events { worker_connections 1024; }

http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;

    # PHP-FPM 代理
    upstream php-fpm { server 127.0.0.1:9000; }

    server {
        listen 80;
        server_name localhost;
        root /var/www/public;

        index index.php index.html;

        location / {
            try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
            fastcgi_pass php-fpm;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
}
brew services start nginx

Composer

brew install composer

常用別名 (加入 ~/.zshrc)

alias php-start="brew services start php@8.2"
alias php-stop="brew services stop php@8.2"
alias php-restart="brew services restart php@8.2"
alias nginx-start="brew services start nginx"
alias nginx-restart="brew services restart nginx"
alias mysql-start="brew services start mysql"
alias redis-start="brew services start redis"

驗證環境

php -v
nginx -t
mysql -V
redis-cli ping
composer --version

專案部署建議

  • 程式碼放置 /var/www/your-project,Nginx root 指向 public/。
  • 使用 .env 管理環境變數,composer install --no-dev --optimize-autoloader 部署。
  • SSL 憑證可用 mkcert 產生本地信任憑證。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved