mac安裝nginx+php

參考連接

nginx

安裝命令php

brew install nginx

執行結果:
html

nginx 默認監聽 8080 端口,啓動成功以後,在瀏覽器訪問 http://localhost:8080 能夠看到 nginx 的歡迎界面。nginx

默認服務路徑,項目根目錄segmentfault

/usr/local/var/www

配置文件路徑瀏覽器

nginx安裝好後,只有對應的默認配置文件。socket

/usr/local/etc/nginx/nginx.conf.default

須要手動拷貝一份爲nginx.conf。php-fpm

執行命令spa

sudo cp /usr/local/etc/nginx/nginx.conf.default  /usr/local/etc/nginx/nginx.conf

這樣就有默認配置文件了。日誌

/usr/local/etc/nginx/nginx.conf

!!!服務啓動命令!!!code

brew services start nginx
sudo nginx    #啓動nginx服務
sudo nginx -s reload    #從新載入配置文件
sudo nginx -s stop    #中止nginx服務

!!!注意注意注意!!!

nginx 已經跑起來了,可是目前還不能解析 php,咱們須要利用php-fpm解析。php-fpm 和 php 一塊兒安裝,咱們先去安裝 php,再作相關配置。

php

關於php-fpm

Mac上默認安裝了php和php-fpm,因此暫沒使用brew進行php安裝。但須要注意的幾個點:

1、php-fpm對應的配置文件只有默認的。

/private/etc/php-fpm.conf.default

2、拷貝配置文件

sudo /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf

3、修改配置

修改error_log 錯誤日誌路徑,不然php-fpm會啓動失敗。

error_log = /usr/local/var/log/php-fpm.log

修改nginx配置

當nginx和php-fpm都按照上述配置完畢後,再進行nginx的配置:

1、找到server中的下面代碼,添加index.php

location / {
        root   html;
        index  index.html index.htm index.php;
}

2、在找到開啓FastCGI server

# 下面是針對Apache服務
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}
# 這裏是針對nginx服務
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
   #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   include        fastcgi_params;
}

!!!注意注意注意!!!

須要修改上面的配置

須要把1⃣️修改成2⃣️
1⃣️ fastcgi_param SCRIPT_FILENAME/scripts$fastcgi_script_name;
2⃣️ fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

3、啓動nginx和php-fpm

sudo nginx -s reload
sudo php-fpm

4、驗證php服務

/usr/local/var/www下添加index.php文件,文件裏面隨意寫些合法的驗證代碼便可。

在瀏覽器裏面運行localhost:8080,若是返回預期,則說明nginx和php-fpm配置OK,能夠進行後續開發啦。

添加本身的servers配置

touch jasper.conf

添加servers配置(配置基本核心的便可)

server {
    listen 8081;
    index index.php index.html index.htm;
    server_name jasper.tme.com;
    root /Users/songzeyu/workspace/code/;

    # charset utf - 8;

    access_log /usr/local/etc/nginx/log/localhost.access.log;
    error_log /usr/local/etc/nginx/log/localhost.error.log;

    location / {
       # if (!-f $request_filename) {
         #   rewrite ^ /(.*)$ /index.php last;
        #}
    index index.php;
    }

    error_page 404 /404.html;
    location = /40x.html {
    }

    error_page   500 502 503 504  /50x.html;
    location =
        /50x.html {
    }

    #proxy the php scripts to php-fpm
    location ~ .*\.(php)?$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        include        fastcgi.conf;
        proxy_buffering off;
    }

    location ~ /\.ht {
        deny all;
    }
}

添加hosts配置

sodo vi /private/etc/hosts
# 添加一行
127.0.0.1  jasper.tme.com

而後就能夠在/Users/songzeyu/workspace/code/目錄進行開發啦。

其餘常見問題

重啓php-fpm失敗1

/private/etc/php-fpm.conf

若是不修改error_log值,運行php-fpm的時候會提示log文件輸出路徑不存在的錯誤。

修改以下:

error_log = /usr/local/var/log/php-fpm.log

無php.ini配置

在目錄下只有默認配置

/private/etc/php.ini.default

須要手動拷貝php.ini配置。

sudo cp /private/etc/php.ini.default  /private/etc/php.ini

啓動nginx失敗(端口被佔用)

sudo nginx

結果提示80端口被佔用。

返回結果:

nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8081 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8081 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8081 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8081 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8081 failed (48: Address already in use)
nginx: [emerg] still could not bind()

查詢到使用nginx的進程,進行kill掉。

netstat -ano|grep 80
ps aux | grep nginx
sudo kill -9  主進程號

啓動php-fpm失敗

sudo php-fpm

返回結果:

[13-Jul-2019 18:46:39] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (48)
[13-Jul-2019 18:46:39] ERROR: FPM initialization failed

查詢使用進程

ps aux | grep php-fpm

返回結果:

songzeyu         56929   0.0  0.0  4298044   1584 s005  T     5:56下午   0:00.03 vi /private/etc/php-fpm.conf
nobody           41541   0.0  0.1  4312548   7784   ??  S     6:49下午   0:00.63 php-fpm
nobody           41540   0.0  0.1  4314684   9584   ??  S     6:49下午   0:00.82 php-fpm
nobody           41515   0.0  0.1  4314640   9568   ??  S     6:48下午   0:00.87 php-fpm
root             16452   0.0  0.0  4309868    300   ??  Ss   五01下午   0:01.04 php-fpm
songzeyu         78046   0.0  0.0  4297908    124 s004  T    四03下午   0:00.02 vi /private/etc/php-fpm.conf
songzeyu         77981   0.0  0.0  4298044    124 s004  T    四03下午   0:00.02 vi /private/etc/php-fpm.conf.default
songzeyu         60719   0.0  0.0  4286472    836 s005  S+    6:47下午   0:00.00 grep php-fpm

拿到主進程號S,即上圖的41541, 41540,41515,S+表明有子進程,即16452

sudo kill -9  主進程號(41541) // 若是有多個,須要執行多個。
相關文章
相關標籤/搜索