Mac 使用 brew 安裝 nginx 配置 php
一.安裝
查找 brew search nginx
可用版本
使用 brew install nginx
安裝nginx
二.安裝完成后brew會輸出關於nginx的配置信息
根目錄
#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/.
使用brew方式啟動nginx
#To have launchd start nginx now and restart at login:
# brew services start nginx
使用更簡便的方式啟動nginx
#Or, if you don't want/need a background service you can just run:
# nginx
三.訪問 localhost:8080
Nginx 默認 8080 端口,這時已經可以訪問了:
localhost:8080
會有一個默認歡迎界面,到此如果打開成功,那么Nginx安裝成功了。
四.修改Nginx配置
1. 打開 nginx.config 文件
vim /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;
5. 創建 index.php在 /usr/local/var/www 目錄下,刪除 index.html,創建 index.php,輸入
<?php phpinfo(); ?>
6. 啟動Nginx和PHP
sudo nginx
sudo php-fpm
然后訪問 localhost:8080,看到 php 配置信息,就說明 ok 了
7. 其他相關命令
重載配置文件
sudo nginx -s reload
停止 nginx 服務器
sudo nginx -s stop