最近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