mac環(huán)境下安裝配置nginx+php

安裝Nginx

執(zhí)行如下命令

brew install nginx

結(jié)果如下:

$brew install nginx
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/nginx-1.12.0_1.sierra.bottl
Already downloaded: /Users/xxx/Library/Caches/Homebrew/nginx-1.12.0_1.sierra.bottle.tar.gz
==> Pouring nginx-1.12.0_1.sierra.bottle.tar.gz
Warning: This keg was marked linked already, continuing anyway
==> Using the sandbox
==> Caveats
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd start nginx now and restart at login:
  brew services start nginx
Or, if you don't want/need a background service you can just run:
  nginx
==> Summary
??  /usr/local/Cellar/nginx/1.12.0_1: 23 files, 1MB

升級Nginx

執(zhí)行如下命令

brew upgrade nginx

結(jié)果如下:

$brew upgrade nginx
==> Upgrading 1 outdated package, with result:
nginx 1.12.0_1
==> Upgrading nginx
==> Installing dependencies for nginx: pcre, openssl@1.1
==> Installing nginx dependency: pcre
==> Downloading https://homebrew.bintray.com/bottles/pcre-8.40.sierra.bottle.tar
######################################################################## 100.0%
==> Pouring pcre-8.40.sierra.bottle.tar.gz
==> Using the sandbox
??  /usr/local/Cellar/pcre/8.40: 204 files, 5.4MB
==> Installing nginx dependency: openssl@1.1
==> Downloading https://homebrew.bintray.com/bottles/openssl@1.1-1.1.0f.sierra.b
######################################################################## 100.0%
==> Pouring openssl@1.1-1.1.0f.sierra.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl@1.1/certs

and run
  /usr/local/opt/openssl@1.1/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have this software first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.bash_profile

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/openssl@1.1/lib
    CPPFLAGS: -I/usr/local/opt/openssl@1.1/include
For pkg-config to find this software you may need to set:
    PKG_CONFIG_PATH: /usr/local/opt/openssl@1.1/lib/pkgconfig

==> Summary
??  /usr/local/Cellar/openssl@1.1/1.1.0f: 6,421 files, 15.5MB
==> Installing nginx
==> Downloading https://homebrew.bintray.com/bottles/nginx-1.12.0_1.sierra.bottl
######################################################################## 100.0%
==> Pouring nginx-1.12.0_1.sierra.bottle.tar.gz
==> Caveats
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd start nginx now and restart at login:
  brew services start nginx
Or, if you don't want/need a background service you can just run:
  nginx
==> Summary
??  /usr/local/Cellar/nginx/1.12.0_1: 23 files, 1MB

修改php-fpm文件

  1. 執(zhí)行命令:
sudo cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf
  1. 找到目錄下的php-fpm文件
    /private/etc/php-fpm.conf

  2. 找到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 配置

  1. 打開 nginx.config 文件
/usr/local/etc/nginx/nginx.conf
  1. 找到 server 的 location 配置,給 index加一個index.php
location / {
    root   html;
    index  index.html index.htm index.php;
}
  1. 并打開 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;
}

修改 fastcgi_param 參數(shù)

fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

改為

fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

創(chuàng)建index.php

在 /usr/local/var/www 目錄下,刪除 index.html,創(chuàng)建 index.php,輸入

<?php phpinfo(); ?>

啟動相關(guān)服務(wù)

brew services start nginx
sudo php-fpm

然后訪問localhost:8083,看到 php 配置信息,就說明 ok 了

啟動Nginx

brew services start nginx

重啟Nginx

brew services restart nginx

測試

在瀏覽器中輸入以下地址:
http://localhost:8083/

參考資料

Mac下Nginx環(huán)境配置

http://www.cnblogs.com/cheemon/p/5638394.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容