安裝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啟動(dòng)關(guān)閉命令:
#測試配置是否有語法錯(cuò)誤
nginx -t
#打開 nginx
sudo nginx
#重新加載配置|重啟|停止|退出 nginx
nginx -s reload|reopen|stop|quit
好了,來跑下nginx
sudo nginx
到這里為止,我們已經(jīng)安裝完了mysql php nginx, 在安裝php的時(shí)候添加了--with-mysql, 所以php操作mysql是沒有問題的,現(xiàn)在我們就要配置nginx,讓它監(jiān)聽php-fpm的進(jìn)程,這樣當(dāng)用戶打開瀏覽器訪問的時(shí)候,身為反向代理的nignx就能把東西讓php去執(zhí)行了。
接下來,我們要配置nginx.conf文件,創(chuàng)建一個(gè)php-fpm文件(監(jiān)聽php-fpm), 還要約定下將nginx.pid文件,log日志,以及以后我們要配置的站點(diǎn).conf的路徑,我們的路徑約定還是按照brew默認(rèn)的目錄來設(shè)置,如下:
# nginx.conf,已經(jīng)被創(chuàng)建好了,我們一會(huì)要更改下
/usr/local/etc/nginx/nginx.conf
# php-fpm,這個(gè)我們就放在和nginx.conf一樣的路徑下吧,這個(gè)要我們自己創(chuàng)建
/usr/local/etc/nginx/php-fpm
# 日志文件放在/usr/local/var/log/nginx中,默認(rèn)已經(jīng)有了access.log和error.log文件了
/usr/local/var/log/nginx/
# nginx.pid文件,放在/usr/local/var/run/下面,和php-fpm.pid放一堆
/usr/local/var/run/
# 以后要配置的站點(diǎn).conf, 我們就放在/usr/local/etc/nginx/servers/下面,這個(gè)servers文件夾本身就存在的
/usr/local/etc/nginx/servers/
# 站點(diǎn)的根目錄,也就用brew給我們?cè)O(shè)置的吧
/usr/local/var/www/
我不知道大家是怎么來定義路徑的,我的智商不咋的,記性非常的差,一般我都用brew安裝軟件,所以用brew默認(rèn)的路徑,我能方便的找到我的文件。
下面我們先來修改nginx.conf, 用vim打開,把下面的信息覆蓋nginx.conf, vim /usr/local/etc/nginx/nginx.conf, 如果你不習(xí)慣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, 下面就能配置站點(diǎn)了,先到/usr/local/var/www目錄下建立站點(diǎn)根目錄,就叫做default吧,然后在里面建立個(gè)info.php,內(nèi)容就放phpinfo()函數(shù)就行.
mkdir /usr/local/var/www/default
vi /usr/local/var/www/default/info.php #輸入 <?php phpinfo();
最后,去創(chuàng)建站點(diǎn).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服務(wù)
sudo nginx # 已經(jīng)開啟的用sudo nginx -s reload 重啟下
瀏覽器打開http://localhost/info.php測試下
別名
上面的php-fpm關(guān)閉和重啟的命令有點(diǎn)長,我們給它做個(gè)別名,在你的個(gè)人主目錄下先建立一個(gè).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)境結(jié)束
以上安裝出現(xiàn)的問題記錄:
- 使用Curl出現(xiàn)的502錯(cuò)誤
做有贊接口的時(shí)候發(fā)現(xiàn)出現(xiàn)了502錯(cuò)誤,用下面的測試代碼也可以測試:
$curl = curl_init();
// 剛開始抓取了https://github.com,但是頁面彈框,后來改用抓取支付寶首頁測試
curl_setopt($curl, CURLOPT_URL, 'https://www.alipay.com');
// 設(shè)置header
curl_setopt($curl, CURLOPT_HEADER, 1);
// 設(shè)置cURL 參數(shù),要求結(jié)果保存到字符串中還是輸出到屏幕上
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// 運(yùn)行cURL,請(qǐng)求網(wǎng)頁數(shù)據(jù)
$data = curl_exec($curl);
// 關(guān)閉cURL請(qǐng)求
curl_close($curl);
// 打印出抓取的測試數(shù)據(jù)
var_dump($data);
產(chǎn)生問題的原因:brew安裝curl時(shí)默認(rèn)沒有帶上--with-openssl
解決方法:
# 先刪除curl
brew uninstall curl
# 重新安裝curl,帶上--with-openssl
brew install curl --with-openssl
然后重啟下php-fpm