mac os nginx php mysql

最近mac Air重做了系统后,想配置lnmp环境,但是搜索了很多页面都以失败告终,在这里特别感谢http://www.zhoujiping.com/archives/2016/01/mnmp.html,他给我提供了很多的帮助。当然还有其它很多的朋友,这里就不细描,但这个列的确实比较详细。

准备工作

  1. 进入终端,键入gcc,如没装xcode命令行工具,点击安装即可。

  2. ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"安装HomeBrew。(HomeBrew详细用法见官网

安装nginx

  1. brew install nginx

  2. nginx -v(看到nginx版本安装)

  3. sudo nginx(启动nginx)

安装php56

  • 安装php ,php-fpm

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版本


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 /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

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 
    原文作者:starsfun
    原文地址: https://segmentfault.com/a/1190000004925044
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞