Mac下Nginx环境配置

作者: 苏大发 | 来源:发表于2017-09-19 11:54 被阅读1048次

    环境信息:

    Mac OS X 10.11.1
    Homebrew 0.9.5

    正文

    一、安装 Nginx

    终端执行:

    brew search nginx
    brew install nginx
    

    当前版本 1.6.2
    安装完以后,可以在终端输出的信息里看到一些配置路径:
    /usr/local/etc/nginx/nginx.conf (配置文件路径)
    /usr/local/var/www (服务器默认路径)
    /usr/local/Cellar/nginx/1.6.2 (貌似是安装路径)

    二、访问localhost:8080

    Nginx 默认8080端口,这时已经可以访问了:
    localhost:8080
    会有一个默认欢迎界面。

    三、修改 php-fpm 文件

    1.执行命令:

    sudo cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf
    

    2.找到目录下的 php-fpm 文件
    /private/etc/php-fpm.conf
    3.找到32行的 error_log ,改为(正行替换,注意 ‘;’ 和空格):
    error_log = /usr/local/var/log/php-fpm.log
    否则 php-fpm 时会报错:
    ERROR: failed to open error_log (/usr/var/log/php-fpm.log): No such file or directory (2)

    四、修改 Nginx 配置

    打开 nginx.config 文件
    /usr/local/etc/nginx/nginx.conf
    2.找到 server 的 location 配置,给 index 加一个 index.php

    location / {
    root  html;
    index  index.html index.htm index.php;
    }
    

    3.并打开 server 下被注释的 location ~.php$(即删除代码前面的 ‘#’),如下:

    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;
    }
    

    4.并修改 fastcgi_param 参数
    fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    改为
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    五、创建 index.php

    在 /usr/local/var/www 目录下,删除 index.html,创建 index.php,输入

    六、启动相关服务
    sudo nginx
    sudo php-fpm
    

    然后访问localhost:8080,看到 php 配置信息,就说明 ok 了

    七、其他命令

    修改 nginx.conf 后,重载配置文件
    sudo nginx -s reload
    停止 nginx 服务器
    sudo nginx -s stop
    停止 php-fpm可以直接在 Activity Monitor 中停止。也可以使用脚本来停。

    八、可能出现的问题

    1.访问 index.php 报 403 Forbidden.查看(四.2)步骤中,是否在 index 后添加 index.php。
    2.访问 index.php 报 File not found.查看(四.4)中,fastcgi_param 参数是否修改。

    注:Nginx运行PHP需要用到php-fpm

    测试php-fpm

    php-fpm -v
    

    附:测试Mac自带的php-fpm命令:/usr/bin/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
    

    可能出现的问题一

    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
    

    安装nginx
    基础安装
    安装命令:

    brew install nginx
    

    启动关闭命令:

    测试配置是否有语法错误

    nginx -t
    

    打开 nginx

    sudo nginx
    

    重新加载配置|重启|停止|退出 nginx

    nginx -s reload|reopen|stop|quit
    

    开机启动:

    ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
    

    nginx监听80端口:

    sudo chown root:wheel /usr/local/Cellar/nginx/1.6.0_1/bin/nginx
    sudo chmod u+s /usr/local/Cellar/nginx/1.6.0_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全局配置

    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/*;
    }
    

    设置nginx php-fpm配置文件

    vim /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;
    }
    

    在/usr/local/etc/nginx/sites-enabled目录下,一个文件对应一个域名的配置,我们设置web服务器目录是/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

    vim /usr/local/etc/nginx/sites-available/default
    

    输入以下配置

    server {
    listen      80;
    server_name  localhost;
    root        /var/www/;
    access_log  /usr/local/var/logs/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;
    }
    

    相关文章

      网友评论

        本文标题:Mac下Nginx环境配置

        本文链接:https://www.haomeiwen.com/subject/khswsxtx.html