mac OSX, nginx 解析PHP

原文路徑:http://youyusan.github.io/2016/01/30/php-nginx-in-mac/

測試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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章