安裝Nginx
和前面一樣先brew search nginx查找nginx, 看下信息brew info nginx
然后安裝brew install nginx
安裝信息記錄下,用的到:
? ~ brew install nginx
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
gitless
==> Updated Formulae
fzf gammu
==> Downloading https://homebrew.bintray.com/bottles/nginx-1.10.2.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring nginx-1.10.2.sierra.bottle.tar.gz
==> 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
Nginx啟動關閉命令:
#測試配置是否有語法錯誤
nginx -t
#打開 nginx
sudo nginx
#重新加載配置|重啟|停止|退出 nginx
nginx -s reload|reopen|stop|quit
好了噪叙,來跑下nginx
sudo nginx
到這里為止剧包,我們已經(jīng)安裝完了mysql php nginx, 在安裝php的時候添加了--with-mysql, 所以php操作mysql是沒有問題的,現(xiàn)在我們就要配置nginx,讓它監(jiān)聽php-fpm的進程,這樣當用戶打開瀏覽器訪問的時候舰褪,身為反向代理的nignx就能把東西讓php去執(zhí)行了急黎。
接下來,我們要配置nginx.conf文件琳猫,創(chuàng)建一個php-fpm文件(監(jiān)聽php-fpm), 還要約定下將nginx.pid文件伟叛,log日志,以及以后我們要配置的站點.conf的路徑脐嫂,我們的路徑約定還是按照brew默認的目錄來設置,如下:
# nginx.conf,已經(jīng)被創(chuàng)建好了统刮,我們一會要更改下
/usr/local/etc/nginx/nginx.conf
# php-fpm,這個我們就放在和nginx.conf一樣的路徑下吧,這個要我們自己創(chuàng)建
/usr/local/etc/nginx/php-fpm
# 日志文件放在/usr/local/var/log/nginx中账千,默認已經(jīng)有了access.log和error.log文件了
/usr/local/var/log/nginx/
# nginx.pid文件,放在/usr/local/var/run/下面侥蒙,和php-fpm.pid放一堆
/usr/local/var/run/
# 以后要配置的站點.conf, 我們就放在/usr/local/etc/nginx/servers/下面,這個servers文件夾本身就存在的
/usr/local/etc/nginx/servers/
# 站點的根目錄,也就用brew給我們設置的吧
/usr/local/var/www/
我不知道大家是怎么來定義路徑的匀奏,我的智商不咋的鞭衩,記性非常的差,一般我都用brew安裝軟件,所以用brew默認的路徑论衍,我能方便的找到我的文件瑞佩。
下面我們先來修改nginx.conf, 用vim打開,把下面的信息覆蓋nginx.conf, vim /usr/local/etc/nginx/nginx.conf, 如果你不習慣vim, 那就用sublime打開吧subl /usr/local/etc/nginx/nginx.conf
worker_processes 1;
error_log /usr/local/var/log/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"';
access_log /usr/local/var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
port_in_redirect off;
include /usr/local/etc/nginx/servers/*;
}
接下來坯台,將下面的信息放入到php-fpm文件中炬丸,vim /usr/local/etc/nginx/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;
}
ok, 下面就能配置站點了,先到/usr/local/var/www目錄下建立站點根目錄蜒蕾,就叫做default吧稠炬,然后在里面建立個info.php,內(nèi)容就放phpinfo()函數(shù)就行.
mkdir /usr/local/var/www/default
vi /usr/local/var/www/default/info.php #輸入 <?php phpinfo();
最后,去創(chuàng)建站點.conf文件
/usr/local/etc/nginx/servers/default.conf
輸入下面的內(nèi)容
server {
listen 80;
server_name localhost;
root /usr/local/var/www/default;
access_log /usr/local/var/log/nginx/default.access.log main;
location / {
index index.html index.htm index.php;
autoindex on;
include /usr/local/etc/nginx/php-fpm;
}
error_page 404 /404.html;
error_page 403 /403.html;
}
測試下配置文件
? servers sudo nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
然后開啟php-fpm,已經(jīng)開啟的就不用開了
php-fpm -D
開啟nginx服務
sudo nginx # 已經(jīng)開啟的用sudo nginx -s reload 重啟下
瀏覽器打開http://localhost/info.php測試下
別名
上面的php-fpm關閉和重啟的命令有點長咪啡,我們給它做個別名酸纲,在你的個人主目錄下先建立一個.aliases文件,然后放入下面的內(nèi)容
alias fpm.start='php-fpm -D'
alias fpm.stop='kill -INT `cat /usr/local/var/run/php-fpm.pid`'
alias fpm.restart='kill -USR2 `cat /usr/local/var/run/php-fpm.pid`'
alias fpm.status='lsof -Pni4 | grep LISTEN | grep php-fpm'
然后
echo 'source ~/.aliases' >> ~/.zshrc
source ~/.zshrc
安裝MNMP (Mac + Nginx + Mysql + Php) 開發(fā)環(huán)境結束
以上安裝出現(xiàn)的問題記錄:
- 使用Curl出現(xiàn)的502錯誤
做有贊接口的時候發(fā)現(xiàn)出現(xiàn)了502錯誤瑟匆,用下面的測試代碼也可以測試:
$curl = curl_init();
// 剛開始抓取了https://github.com,但是頁面彈框闽坡,后來改用抓取支付寶首頁測試
curl_setopt($curl, CURLOPT_URL, 'https://www.alipay.com');
// 設置header
curl_setopt($curl, CURLOPT_HEADER, 1);
// 設置cURL 參數(shù),要求結果保存到字符串中還是輸出到屏幕上
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// 運行cURL愁溜,請求網(wǎng)頁數(shù)據(jù)
$data = curl_exec($curl);
// 關閉cURL請求
curl_close($curl);
// 打印出抓取的測試數(shù)據(jù)
var_dump($data);
產(chǎn)生問題的原因:brew安裝curl時默認沒有帶上--with-openssl
解決方法:
# 先刪除curl
brew uninstall curl
# 重新安裝curl,帶上--with-openssl
brew install curl --with-openssl
然后重啟下php-fpm