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

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程序可以运行,则证明配置成功。

猜你喜欢

转载自blog.csdn.net/u012839837/article/details/81078782