安裝 Homebrew
Homebrew 作為 macOS 不可或缺的套件管理器,用來安裝、升級以及解除安裝常用的軟體。在命令列中執行以下命令即可安裝:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
安裝後可以修改 Homebrew 源,國外源一直不是很給力,這裡我們將 Homebrew 的 git 遠端倉庫改為中國科學技術大學開源軟體映象:
cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc
安裝 PHP 7.4
安裝 PHP7.4.* 來代替系統自帶的 PHP7.3:
brew install php
啟動 php 服務:
brew services start php
替換系統自帶的 php-fpm:
echo 'export PATH="/usr/local/opt/php/sbin:$PATH"' >> ~/.zshrc
source ~/.zshrc
檢視版本資訊:
php -v
php-fpm -v
安裝 MySQL
推薦 MySQL 8.0 作為資料庫伺服器:
brew install mysql
當然,你也可以選擇安裝 PostgreSQL 或者 MariaDB。
安裝完成後,啟動 MySQL:
brew services start mysql
進入 MySQL 伺服器:
mysql -u root -p
設定 root 密碼、安全等級等引數:
mysql_secure_installation
按照步驟提示一步一步來即可。
安裝 Redis
安裝 redis 伺服器:
brew install redis
安裝完成後,啟動 Redis:
brew services start redis
使用 redis 客戶端:
redis-cli
安裝 nginx
這裡我們選擇 nginx 代替系統自帶的 Apache,作為我們的 Web 伺服器:
brew install nginx
啟動 nginx 服務:
brew services start nginx
檢視已安裝的 brew services:
brew services list
檢視nginx資訊:
brew info nginx
配置 nginx.conf 檔案
透過以下命令可以檢視 nginx.conf 檔案的位置:
nginx -h
輸出:
nginx version: nginx/1.17.3
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.17.3_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 /usr/local/etc/nginx/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 檔案:
vim /home/www/php-project/index.php
寫入內容:
phpinfo();
重啟 nginx:
brew services restart nginx
開啟瀏覽器,訪問http://localhost:8099,即可訪問到關於 PHP 配置的資訊。
原配置檔案/usr/local/etc/nginx/nignx.config
server {
listen 8081;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html; #這裡要刪去
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
————————————————
修改為:
server {
listen 8081;
server_name localhost;
root /Users/fjh1997/upload; #這裡要注意
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
index index.php;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
需要注意的是,務必刪去 location ~ .php$ {裡面的root項,同時修改/scripts f a s t c g i s c r i p t n a m e ; 為 fastcgi_script_name;為 fastcgiscriptname;為document_root$fastcgi_script_name;,並在location外層新增root項。如本例為/Users/fjh1997/upload;
預設專案位置:/usr/local/var/www
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://mirrors.aliyun.com/composer/
安裝 PHP 擴充套件
以 php-redis 擴充套件為例,有下載原始碼包來進行安裝或者 pecl install 安裝:
wget https://pecl.php.net/get/redis-5.1.0.tgz # 下載原始碼包
tar -zxvf redis-5.1.0.tgz # 解壓
cd redis-5.1.0 # 進入目錄
phpize # 生成編譯配置
./configure # 編譯配置檢測
make # 編譯
make install # 安裝
擴充套件安裝完成後,我們還需最後一步,修改php.ini檔案,並重啟 PHP 服務:
vi /usr/local/etc/php/7.4/php.ini # 追加 extension=redis.so
brew services restart php
php -m |grep redis
或者使用 pecl 安裝:
pecl install redis
本作品採用《CC 協議》,轉載必須註明作者和本文連結