mac系統下安裝配置PHP和nginx

                mac系統下安裝配置PHP和nginx

這段時間由於工作的需要開始學習後臺開發,需要學習php和nginx。下面就這兩天的工作做一下總結。

Mac 系統版本10.13.4

PHP版本7.1.19

nginx版本1.15.1

下面就開始一步一步安裝。

1、安裝 homebrew

1.1安裝命令:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

                  注意:進行安裝,如果期間有錯誤就再重新運行一遍。

1.2安裝參考:

https://blog.csdn.net/qq_41234116/article/details/79366454

1.3簡單使用:

  1. 安裝軟件:brew install 軟件名,例:brew install wget

  2. 搜索軟件:brew search 軟件名,例:brew search wget

  3. 卸載軟件:brew uninstall 軟件名,例:brew uninstall wget
  4. 更新所有軟件:brew update

  5. 更新具體軟件:brew upgrade 軟件名 ,例:brew upgrade git
  6. 顯示已安裝軟件:brew list
  7. 查看軟件信息:brew info/home 軟件名 ,例:brew info git / brew home git
    PS:brew home指令是用瀏覽器打開官方網頁查看軟件信息
  8. 查看哪些已安裝的程序需要更新: brew outdated
  9. 顯示包依賴:brew reps
  10. 顯示幫助:brew help

2、安裝nginx

2.1安裝命令:

         brew install nginx

                     注意:進行安裝,如果期間有錯誤就再重新運行一遍。

2.2安裝參考

https://blog.csdn.net/qq_37847473/article/details/69259100

2.3基本使用:

     # 啓動nginx

               sudo nginx

      # 停止nginx

               sudo nginx -s stop

      # 退出 nginx

                sudo nginx -s quit

      # 修改nginx.conf文件後,重新加載配置文件

                  sudo nginx -s reload

      測試nginx是否啓動

           在瀏覽器中輸入localhost:8080,如果能打開nginx的歡迎界面就表示啓動成功。

         

2.4路徑說明

安裝路徑

     /usr/local/Cellar/nginx/1.15.1

配置文件路徑

    /usr/local/etc/nginx

    配置文件:nginx.conf,nginx.conf.default

    配置文件後下需要修改。    

3 安裝PHP7.1

3.1安裝命令:

        brew install php71 --without-apache --with-fpm

                     注意:進行安裝,如果期間有錯誤就再重新運行一遍。

3.2安裝參考

https://blog.csdn.net/qq_37847473/article/details/69259100

3.3基本使用

3.3.1 查看版本

php-fpm -v

3.4啓動/停止 php-fpm:

3.4.1切換到 /usr/local/opt/nginx 路徑下

3.4.2啓動

launchctl load -w homebrew.mxcl.nginx.plist

3.4.3停止

launchctl unload -w homebrew.mxcl.nginx.plist

3.4.4殺死所有php-fpm

sudo pkill php-fpm

3.4.5啓動php-fpm

sudo php-fpm

4、配置php和nginx

4.1修改nginx配置

配置文件路徑

    /usr/local/etc/nginx

    配置文件:nginx.conf,nginx.conf.default

nginx.conf 文件修改後如下
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #這個地方做了修改

        location / {
            root  /usr/local/var/www/;
            index  index.html index.htm index.php;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #

       #這個地方做了修改
        location ~ \.php$ {
            root           /usr/local/var/www/;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/var/www/$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}
 

nginx.conf.default 修改後如下


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

       #zh

        location / {
            root  /usr/local/var/www/;
            index  index.html index.htm index.php;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #

        #這個地方做了修改
        location ~ \.php$ {
            root           /usr/local/var/www/;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/var/www/$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}
 

5 增加PHP文件測試

在配置路徑中增加index.php文件,我的配置路徑是:

 /usr/local/var/www/

index.php 文件中的信息如下:

<?php
echo "Hello World";
Echo "<br>";
$arr = array("good","zhangsan","liming","nihaoa");
json_encode($arr);
echo json_encode($arr);
Echo "<br>";
phpinfo();
?>
 

在瀏覽器中測試:

localhost:8080/index.php

如果php程序可以運行,則證明配置成功。

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章