安装Nginx
执行如下命令
brew install nginx
结果如下:
$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
执行如下命令
brew upgrade nginx
结果如下:
$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文件
- 执行命令:
sudo cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf
找到目录下的php-fpm文件
/private/etc/php-fpm.conf找到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
- 找到 server 的 location 配置,给 index加一个index.php
location / {
root html;
index index.html index.htm index.php;
}
- 并打开 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 参数
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,输入
<?php phpinfo(); ?>
启动相关服务
brew services start nginx
sudo php-fpm
然后访问localhost:8083,看到 php 配置信息,就说明 ok 了
启动Nginx
brew services start nginx
重启Nginx
brew services restart nginx
测试
在浏览器中输入以下地址:
http://localhost:8083/