在 macOS High Sierra 10.13 搭建 PHP 開發環境

2017 年 9 月 26 日,蘋果公司正式發佈了新一代 macOS,版本爲 High Sierra (11.13)。
macOS High Sierra 預裝了 Ruby(2.3.3)、PHP(7.1.7)、Perl(5.18.2)、Python(2.7.10) 等經常使用的腳本語言,以及 Apache(2.4.27) Web 服務器。php

如下是個人 MNMP(macOS-nginx-MySQL-PHP)的安裝過程。html

本教程用使用了三處代替:mysql

  • 使用 iTerm2 代替了系統自帶的命令行終端
  • 使用 nginx 代替了系統自帶的 Apache
  • 使用 PHP7.2 代替了系統自帶的 PHP7.1

安裝 iTerm2

推薦 iTerm2,iTerm2 功能強大,能夠替代系統默認的命令行終端。下載解壓後,將 iTerm2 直接拖入"應用程序"目錄。nginx

安裝 PhpStorm

推薦 JetBrains PhpStorm 做爲集成開發工具。git

安裝 Xcode

Xcode 是蘋果出品的包含一系列工具及庫的開發套件。經過 AppStore 安裝最新版本的 Xcode(9.0)。咱們通常不會用 Xcode 來開發後端項目。但這一步也是必須的,由於 Xcode 會附帶安裝一些如 Git 等必要的軟件。github

安裝 Command Line Tools for Xcode

這一步會幫你安裝許多常見的基於 Unix 的工具。Xcode 命令行工具做爲 Xcode 的一部分,包含了 GCC 編譯器。在命令行中執行如下命令便可安裝:redis

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

當 Xcode 和 Xcode Command Line Tools 安裝完成後,你須要啓動 Xcode,並點擊贊成接受許可協議,而後關閉 Xcode 就能夠了。這一步驟也是必須的,不然 Xcode 包含的一系列開發工具都將不可用。sql

安裝 Homebrew

Homebrew 做爲 macOS 不可或缺的套件管理器,用來安裝、升級以及卸載經常使用的軟件。在命令行中執行如下命令便可安裝:數據庫

# 使用系統自帶的 ruby 安裝 Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

安裝後能夠修改 Homebrew 源,國外源一直不是很給力,這裏咱們將 Homebrew 的 git 遠程倉庫改成中國科學技術大學開源軟件鏡像macos

cd "$(brew --repo)"
git remote set-url origin git://mirrors.ustc.edu.cn/brew.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://github.com/Homebrew/homebrew-core.git
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

安裝一些必要的工具包

brew install wget
brew install autoconf
brew install openssl

安裝 nginx

這裏咱們選擇 nginx 代替系統自帶的 Apache,做爲咱們的 Web 服務器:

brew install nginx

安裝完成後,nginx 的一些經常使用命令:

sudo nginx # 啓動 nginx 服務
nginx -h # nginx 幫助信息
sudo nginx -s stop|quit|reopen|reload # 中止|退出|重啓|重載 nginx 服務

安裝 MySQL

推薦 MySQL 做爲數據庫服務器:

brew install mysql

固然,你也能夠選擇安裝 PostgreSQL 或者 MariaDB。

安裝完成後,啓動 MySQL:

mysql.server start

進入 MySQL 服務器:

mysql -u root -p

安裝 Redis

推薦 Redis 做爲 noSQL 數據庫服務器:

brew install redis

安裝完成後,啓動 Redis:

redis-server

安裝 PHP 7.2

安裝 PHP7.2 來代替系統自帶的 PHP7.1:

brew tap homebrew/dupes  
brew tap homebrew/versions  
brew tap homebrew/homebrew-php
brew install php72
echo export PATH="$(brew --prefix homebrew/php/php72)/bin:$PATH" >> ~/.bash_profile # 代替系統自帶的 php
source ~/.bash_profile

驗證 PHP 以及 php-fpm 版本:

php -v
php-fpm -v

啓動 php-fpm:

sudo killall php-fpm && sudo php-fpm

根據我的的實際開發場景,降版本安裝 php5.* 也是用相似方法,即把 php72 替換爲 php56

配置 nginx.conf 文件

經過如下命令能夠查看 nginx.conf 文件的位置:

nginx -h

輸出:

nginx version: nginx/1.12.1
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/Cellar/nginx/1.12.1/)
  -c filename   : set configuration file (default: /usr/local/etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

打開配置文件:

vi /usr/local/etc/nginx/nginx.conf

在文件末尾能夠看到:

include servers/*;

它將同目錄下的servers目錄裏的文件都包含了進來,由此,咱們能夠在servers文件裏建立開發項目的配置信息:

cd servers/
vi test.conf

將如下配置信息,寫入 test.conf文件中:

server {
    listen 8099;
    server_name localhost;
    root /home/www/php_project;
    rewrite . /index.php;
    location / {
        index index.php index.html index.htm;
        autoindex on;
    }

    #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;
    }
}

在上述的/home/www/php_project的目錄下,咱們建立一個 index.php 文件:

vi /home/www/php_project/index.php

寫入內容:

<?php
phpinfo();

重啓 nginx:

sudo nginx -s stop && sudo nginx

打開瀏覽器,訪問localhost:8099。能夠看到關於 PHP 配置的信息。

安裝 Composer

Composer 是 PHP 用來管理依賴(dependency)關係的工具。你能夠在本身的項目中聲明所依賴的外部工具庫(libraries),Composer 會幫你安裝這些依賴的庫文件。

安裝並啓動國內鏡像服務

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
composer config -g repo.packagist composer https://packagist.phpcomposer.com # 改成國內源

安裝 PHP 擴展

不推薦用 pecl 的方式安裝 PHP 擴展。以 php-redis 擴展爲例,下載源碼包來進行安裝:

wget https://pecl.php.net/get/redis-3.1.3.tgz # 下載源碼包
tar -zxvf redis-3.1.3.tgz # 解壓
cd redis-3.1.3
phpize # 生成編譯配置                 
./configure # 編譯配置檢測
make # 編譯
make install # 安裝

擴展安裝完成後,咱們還需最後一步,修改php.ini文件,並重啓 PHP-FPM:

# 追加 extension=redis.so
vi /usr/local/etc/php/7.2/php.ini

# 重啓 php-fpm
sudo killall php-fpm && sudo php-fpm -D

# 查看是否安裝成功
php -m |grep redis
相關文章
相關標籤/搜索