最近mac Air重作了系統後,想配置lnmp環境,可是搜索了不少頁面都以失敗了結,在這裏特別感謝http://www.zhoujiping.com/archives/2016/01/mnmp.html
,他給我提供了不少的幫助。固然還有其它不少的朋友,這裏就不細描,但這個列的確實比較詳細。php
進入終端,鍵入gcc
,如沒裝xcode命令行工具,點擊安裝便可。html
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
安裝HomeBrew。(HomeBrew詳細用法見官網)mysql
brew install nginx
nginx
nginx -v
(看到nginx版本安裝)git
sudo nginx
(啓動nginx)github
安裝php ,php-fpmsql
brew tap homebrew/dupes brew tap homebrew/versions brew tap homebrew/php brew install php56 \ --without-snmp \ --without-apache \ --with-debug \ --with-fpm \ --with-intl \ --with-homebrew-curl \ --with-homebrew-libxslt \ --with-homebrew-openssl \ --with-imap \ --with-mysql \ --with-tidy
添加系統環境變量PATH來替代自帶PHP版本apache
echo 'export PATH="$(brew --prefix php56)/bin:$PATH"' >> ~/.bash_profile echo 'export PATH="$(brew --prefix php56)/sbin:$PATH"' >> ~/.bash_profile echo 'export PATH="/usr/local/bin:/usr/local/sbin:$PATH"' >> ~/.bash_profile source ~/.bash_profile
修改php-fpm配置文件vim
vim /usr/local/etc/php/5.6/php-fpm.conf
找到;pid = run/php-fpm.pid,去掉註釋(去掉前面的;),而後測試下php-fpm後端
php-fpm -t
調試php-fpm代碼
php-fpm -D
啓動php-fpm
lsof -Pni4 | grep LISTEN | grep php
運行監聽9000端口
ln -sfv /usr/local/opt/php56/*.plist ~/Library/LaunchAgents launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
開機啓動
Nginx自己不會對PHP進行解析,終端對PHP頁面的請求將會被Nginx交給FastCGI進程監聽的IP地址及端口(這就是爲何咱們啓動php-fpm時,要查看下9000端口是否被監聽的緣由),由php-fpm做爲動態解析服務器處理,最後將處理結果再返回給nginx。其實,Nginx就是一個反向代理服務器。Nginx經過反向代理功能將動態請求轉向後端php-fpm,從而實現對PHP的解析支持,這就是Nginx實現PHP動態解析的原理。因此如今咱們要作的就是讓nginx和php-fpm創建關係。如何創建關係呢? 主要是在nginx.conf文件中加入下面這樣的代碼。
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; }
可是爲了方便管理之後新建的網站,咱們不會把全部的配置都放置在nginx.conf中,咱們來規劃下:
mkdir -p /usr/local/var/logs/nginx 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全局配置
vim /usr/local/etc/nginx/nginx.conf
輸入內容
worker_processes 1; error_log /usr/local/var/logs/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/logs/access.log main; sendfile on; keepalive_timeout 65; port_in_redirect off; include /usr/local/etc/nginx/sites-enabled/*.conf; }
把一些可複用配置獨立出來放在/usr/local/etc/nginx/conf.d下,好比fastcgi的設置
vim /usr/local/etc/nginx/conf.d/php-fpm.conf
輸入內容
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; }
之後要增長新域名,只要在/usr/local/etc/nginx/sites-enabled/目錄下能夠一個文件對應一個域名的配置,咱們試着來創建一個默認網站,默認網站的根目錄放在/var/www/default下面,在/var/www/中創建default文件夾,並在default中創建info.php,在其中輸入內容
mkdir -p /var/www/default vim /var/www/default/info.php
而後在/usr/local/etc/nginx/sites-enabled/下面創建個配置文件default.conf
vim /usr/local/etc/nginx/sites-enabled/default.conf
輸入
server { listen 8080; server_name localhost; root /var/www/default; location / { index index.html index.htm index.php; include /usr/local/etc/nginx/conf.d/php-fpm.conf; } }
restart nginx*
sudo nginx -s reload