1. 程式人生 > >php+mysql+nginx+phpmyadmin環境搭建

php+mysql+nginx+phpmyadmin環境搭建

nts home sem 鏈接 ipv4 1.5 不出 英文 由於

英文原文:http://blog.frd.mn/install-nginx-php-fpm-mysql-and-phpmyadmin-on-os-x-mavericks-using-homebrew/

參照博文作者: http://blog.liuweifeng.net/author/liuweifeng/

首先,為了不出現各種神奇的錯誤,請安裝 XCode。Yosemite 發布後,XCode 的最新版本是 6.1.0(6A1052d)。然後,不可忽略的一步,請在終端裏執行

xcode-select --install  

來安裝最新的 XCode 命令行工具(Xcode Command Line Tools)。

安裝 Homebrew

如果沒有安裝,請移步官網 →_→ Homebrew 官方網站

如果你已經安裝了 Homebrew,請在終端執行brew doctor檢查各種可能的沖突和問題。然後執行 brew update && brew upgrade 升級 Homebrew 自身和自帶的 formulas

安裝 PHP-FPM

Homebrew 的 formulas 裏默認木有 PHP-FPM,我們需要手動添加:

brew tap homebrew/php  

然後開始安裝:

brew search php     //choose php version
brew install --without-apache --with-fpm --with-mysql
php71

更改為新安裝的php,打開~/.bash_profile

export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"  //可以采取下面的方式

因為我們打算安裝 Nginx,Apache 就不需要了(貌似 OS X 自帶了 Apache?)。

如果你想在命令行裏使用 PHP 的話,你需要在你 shell 的 Profile 裏設置下 $PATH:

# If you use Bash    
echo ‘export PATH="$(brew --prefix homebrew/php/php71)/bin:$PATH"‘ >> ~/.bash_profile && . ~/.bash_profile  
# If you use ZSH
echo ‘export PATH="$(brew --prefix homebrew/php/php71)/bin:$PATH"‘ >> ~/.zshrc && . ~/.zshrc  

  設置開機啟動:

mkdir -p ~/Library/LaunchAgents  
cp /usr/local/Cellar/php71/7.1.5_17/homebrew.mxcl.php71.plist ~/Library/LaunchAgents/  

啟動 PHP-FPM:

launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php71.plist 

看下 PHP-FPM 是不是在監聽 9000 端口:

lsof -Pni4 | grep LISTEN | grep php  

輸出應該是這個樣子的:

php-fpm   78168 liuweifeng    7u  IPv4 0x9c66922b37bcbcb7      0t0  TCP 127.0.0.1:9000 (LISTEN)  
php-fpm   78170 liuweifeng    0u  IPv4 0x9c66922b37bcbcb7      0t0  TCP 127.0.0.1:9000 (LISTEN)  
php-fpm   92277 liuweifeng    0u  IPv4 0x9c66922b37bcbcb7      0t0  TCP 127.0.0.1:9000 (LISTEN)  
php-fpm   94154 liuweifeng    0u  IPv4 0x9c66922b37bcbcb7      0t0  TCP 127.0.0.1:9000 (LISTEN)  

安裝 MySQL

brew install mysql 

設置開機啟動:

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents 

MySQL 安全設置

mysql_secure_installation

可以設置 root 的密碼、移除匿名用戶、禁止 root 遠程登錄,等等。

安裝 phpMyAdmin

首先需要安裝 autoconf

brew install autoconf 

設置 $PHP_AUTOCONF

# If you use Bash
echo ‘PHP_AUTOCONF="‘$(which autoconf)‘"‘ >> ~/.bash_profile && . ~/.bash_profile  
# If you use ZSH
echo ‘PHP_AUTOCONF="‘$(which autoconf)‘"‘ >> ~/.zshrc && . ~/.zshrc 

開始安裝 phpMyAdmin :

brew install phpmyadmin  

安裝 Nginx

brew install nginx 

設置開機自動啟動

由於我們需要啟動 80 端口,所以需要使用 sudo 來運行 nginx

sudo cp -v /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/  
sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

啟動 Nginx

sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist 

關閉 Nginx

sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.nginx.plist  

更多配置

nginx.conf

創建配置文件中需要用到的目錄

mkdir -p /usr/local/etc/nginx/logs  
mkdir -p /usr/local/etc/nginx/sites-available  
mkdir -p /usr/local/etc/nginx/sites-enabled  
mkdir -p /usr/local/etc/nginx/conf.d  
mkdir -p /usr/local/etc/nginx/ssl  
sudo mkdir -p /var/www

sudo chown :staff /var/www  
sudo chmod 775 /var/www  

移除默認的 nginx.conf(其實在 /usr/local/etc/nginx/nginx.conf.default 還有個備份,你可以看一眼,如果你想看的話。。。),然後使用 curl從 Github 上下載我的自定義版本:

rm /usr/local/etc/nginx/nginx.conf  
curl -L https://gist.github.com/frdmn/7853158/raw/nginx.conf -o /usr/local/etc/nginx/nginx.conf  

這是一個盡可能簡單和輕量級的配置文件。

加載 PHP-FPM

從 Github 下載我的自定義配置文件:

curl -L https://gist.github.com/frdmn/7853158/raw/php-fpm -o /usr/local/etc/nginx/conf.d/php-fpm 

創建默認的虛擬主機

curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_default -o /usr/local/etc/nginx/sites-available/default  
curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_default-ssl -o /usr/local/etc/nginx/sites-available/default-ssl  
curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_phpmyadmin -o /usr/local/etc/nginx/sites-available/phpmyadmin  

使用 Git 從 Github 上克隆我的實例虛擬主機配置(包括默認的 404、403頁面,和一個 phpinfo() rewrite)。

git clone http://git.frd.mn/frdmn/nginx-virtual-host.git /var/www  
rm -rf /var/www/.git  

克隆完記得把 .git 目錄刪除掉,這樣就不會被 Git 記錄了。

譯者註:其實吧,我趕腳這個 404 和 403 挺醜的。。。

設置 SSL

創建需要的目錄

mkdir -p /usr/local/etc/nginx/ssl

創建 4096bit RSA keys 和自簽名證書:

openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=localhost" -keyout /usr/local/etc/nginx/ssl/localhost.key -out /usr/local/etc/nginx/ssl/localhost.crt  
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=phpmyadmin" -keyout /usr/local/etc/nginx/ssl/phpmyadmin.key -out /usr/local/etc/nginx/ssl/phpmyadmin.crt  

啟用虛擬主機

創建軟鏈接:

ln -sfv /usr/local/etc/nginx/sites-available/default /usr/local/etc/nginx/sites-enabled/default  
ln -sfv /usr/local/etc/nginx/sites-available/default-ssl /usr/local/etc/nginx/sites-enabled/default-ssl  
ln -sfv /usr/local/etc/nginx/sites-available/phpmyadmin /usr/local/etc/nginx/sites-enabled/phpmyadmin 

再次啟動 Nginx:

sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist 

最終測試

基本上完事了,點擊下面的鏈接來感受下吧:

  • http://localhost → "Nginx works" page
  • http://localhost/info → phpinfo()
  • http://localhost/nope → " Not Found" page
  • https://localhost:443 → "Nginx works" page (SSL)
  • https://localhost:443/info → phpinfo() (SSL)
  • https://localhost:443/nope → "Not Found" page (SSL)
  • https://localhost:306 → phpMyAdmin (SSL)

服務配置

以後你肯定會關閉或者重啟上面設置的某個服務的,所以你可能需要設置一些別名。(需要修改bash_aliases中的php版本)

# If you use Bash
curl -L https://gist.github.com/frdmn/7853158/raw/bash_aliases -o /tmp/.bash_aliases  
cat /tmp/.bash_aliases >> ~/.bash_aliases  
echo "source ~/.bash_aliases" >> ~/.bash_profile  
# If you use ZSH
curl -L https://gist.github.com/frdmn/7853158/raw/bash_aliases -o /tmp/.zshrc  
cat /tmp/.bash_aliases >> ~/.zshrc  
echo "source ~/.bash_aliases" >> ~/.zshrc  

再打開一個終端窗口,來使上面的別名生效:

source ~/.bash_profile  
# or
source ~/.zshrc  

現在你可以使用別名而不是 launchctl 和那一長串參數了~

Nginx

你可以這樣來啟動、關閉、重啟 Nginx:

nginx.start  
nginx.stop  
nginx.restart  

快速 tail 訪問日誌和錯誤日誌:

nginx.logs.access  
nginx.logs.default.access  
nginx.logs.phpmyadmin.access  
nginx.logs.default-ssl.access  
nginx.logs.error  
nginx.logs.phpmyadmin.error  

檢查配置:

sudo nginx -t 

PHP-FPM

啟動、關閉、重啟 PHP-FPM:

php-fpm.start  
php-fpm.stop  
php-fpm.restart 

檢查配置:

php-fpm -t

MySQL

啟動、關閉、重啟 MySQL:

mysql.start  
mysql.stop  
mysql.restart  

php+mysql+nginx+phpmyadmin環境搭建