Mac 安裝 Nginx、PHP
原文地址:
Mac 安裝 Nginx、PHP
因工作需要的關係原本在 MacBook Air 上使用 MAMP 的因不是 Pro 版只能使用一個 Virtual Host 所以更改為安裝在系統內
先利用 Homebrew 來安裝 Nginx
Homebrew 是什麼?
homebrew 是在 OS X 不可缺少的套件管理器,與 Linux 下的 apt-get 或 yum
安裝 Homebrew
打開終端機將下面的指令貼上即可:/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
安裝完後即可使用 brew search/install/uninstall/update FORMULA 來做操作
安裝 Nginx
brew install nginx
安裝完後可用以下指令操作
nginx -t 測試 Nginx 設定有無錯誤sudo nginx 開啟 Nginxnginx -s reload|reopen|stop|quit 重新加載配置|重新啟用|停止|退出 Nginx
開機自動啟用
ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
監聽 80 port
sudo chown root:wheel /usr/local/Cellar/nginx/1.10.2_1/bin/nginx
sudo chmod u+s /usr/local/Cellar/nginx/1.10.2_1/bin/nginx
設定配置
依照 ubuntu 的擺放設定來處理
mkdir -p /usr/local/var/logs/nginx
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
Nginx 全域設定
vi /usr/local/etc/nginx/nginx.conf
worker_processes 1;
error_log /usr/local/var/log/nginx/error.log debug;
pid /usr/local/var/run/nginx.pid;
events {
worker_connections 256;
}
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" $host $request_time $upstream_response_time $scheme '
'$cookie_evalogin';
access_log /usr/local/var/log/access.log main;
sendfile on;
keepalive_timeout 65;
port_in_redirect off;
include /usr/local/etc/nginx/sites-enabled/*;
}
Nginx php-fpm 設定
vi /usr/local/etc/nginx/conf.d/php-fpm
location ~ \.php$ {
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_intercept_errors on;
include /usr/local/etc/nginx/fastcgi.conf;
}
Virtual Site 設定
在 usr/local/etc/nginx/sites-enable 目錄下,一個文件對應一個 Virtual Site,設定 Web Server 目錄為 /var/www
sudo mkdir -p /var/www
sudo chown :staff /var/www
sudo chmod 775 /var/www
vi /var/www/info.php
vi /var/www/index.html
vi /var/www/403.html
vi /var/www/404.html
Default Site
vi /usr/local/etc/nginx/sites-enabled/default
server {
listen 80;
server_name localhost;
root /var/www/;
access_log /usr/local/var/log/nginx/default.access.log main;
location / {
index index.html index.htm index.php;
autoindex on;
include /usr/local/etc/nginx/conf.d/php-fpm;
}
location = /info {
allow 127.0.0.1;
deny all;
rewrite (.*) /.info.php;
}
error_page 404 /404.html;
error_page 403 /403.html;
}
Default SSL Site
vi /usr/local/etc/nginx/sites-enabled/default-ssl
server {
listen 443;
server_name localhost;
root /var/www/;
access_log /usr/local/var/log/nginx/default-ssl.access.log main;
ssl on;
ssl_certificate ssl/ssl.crt;
ssl_certificate_key ssl/ssl.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
include /usr/local/etc/nginx/conf.d/php-fpm;
}
location = /info {
allow 127.0.0.1;
deny all;
rewrite (.*) /.info.php;
}
error_page 404 /404.html;
error_page 403 /403.html;
}
安裝 PHP 5.6
使用 php-fpm
加入官方 Source
brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/php
安裝 PHP
brew install php56 --with-debug --with-homebrew-curl --with-homebrew-libxslt --with-imap
安裝完後會有一些提示說明,因 Mac 有自帶 PHP,所以要修改系統路徑,優先使用 brew 所安裝的版本echo 'export PATH="$(brew --prefix homebrew/php/php56)/bin:$PATH"' >> ~/.bashrc
安裝 PHP Extension
可使用 brew search php56 來查詢有那些 Extension 可以使用brew install php56-gearman php56-msgpack php56-memcache php56-memcached php56-mongo php56-redis php56-zookeeper php56-xdebug
測試 PHP
php -v
若出現 Extension 啟動錯誤可以使用 brew reinstall Extension --build-from-souce重新安裝如
Cannot load Xdebug - it was built with configuration API220131226,NTS, whereas running engine is API220131226,NTS,debug
PHP Warning: PHP Startup: igbinary: Unable to initialize module
Module compiled with build ID=API20131226,NTS
PHP compiled with build ID=API20131226,NTS,debug
These options need to match
in Unknown on line 0
就需要重裝 php56-xdebug 及 php56-igbinarybrew reinstall php56-xdebug --build-from-source php56-igbinary --build-from-source
啟動 php-fpm
測試 php-fpm
php-fpm -v
啟動 php-fpm
sudo php-fpm -D
關閉 php-fpm
sudo killall php-fpm
查看 php-fpm 狀態
lsof -Pni4 | grep LISTEN | grep php
設定開機啟動 php-fpm
ln -sfv /usr/local/opt/php56/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
啟動 php-fpm 可能會遇到的情況
錯誤訊息
ERROR: failed to open configuration file '/private/etc/php-fpm.conf': No such file or directory
ERROR: failed to load configuration file '/private/etc/php-fpm.conf'
ERROR: FPM initialization failed
解決方式sudo cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf
錯誤訊息
ERROR: failed to open error_log (/usr/var/log/php-fpm.log): No such file or directory (2)
ERROR: failed to post process the configuration
ERROR: FPM initialization failed
解決方式
sudo vi /private/etc/php-fpm.conf
#在 `error_log` 下面添加:
error_log = /usr/local/var/log/php-fpm.log
pid = /usr/local/var/run/php-fpm.pid
參考
http://youyusan.github.io/2016/01/30/php-nginx-in-mac/
http://avnpc.com/pages/install-lnmp-on-osx
https://hinine.com/create-and-deploy-a-self-signed-ssl-certificate-to-nginx/
http://aevit.xyz/2015/12/16/MAC-NMP/
https://github.com/Homebrew/homebrew-php/issues/3101
https://segmentfault.com/a/1190000005090828
浙公网安备 33010602011771号