Mac nginx环境搭建

来源:互联网 发布:我知谁掌管明天视频 编辑:程序博客网 时间:2024/05/16 08:46

首先下载homebrew(相当于yum,apt-get)
下载之后处于/usr目录下
通过homebrew下载的东西安装在/usr/local/cellar
配置文件处于/usr/local/etc/

安装nginx
brew install nginx
开机启动
mkdir -p ~/Library/LaunchAgents
cp /usr/local/opt/nginx/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist(取消开机启动)
nginx命令

       sudo nginx #打开 nginx       nginx -s reload|reopen|stop|quit  #重新加载配置|重启|停止|退出 nginx       nginx -t   #测试配置是否有语法错误       如果提示pid丢失的话,就用这句话       nginx -c "/usr/local/etc/nginx/nginx.conf"

安装PHP
brew install php70 –without-apache –with-fpm
安装完成后,加入全局变量

          vim ~/.bash_profile 添加  export PATH=/usr/local/bin:/usr/sbin:$PATH          然后 vim ~/.bashrc  添加   export PATH = "$(brew --prefix homebrew/php/php70)/bin:$PATH"          最后 sourc ~/.bash_profile  source ~/.bashrc

开机启动
mkdir -p ~/Library/LaunchAgents
cp /usr/local/opt/php70/homebrew.mxcl.php70.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist
安装php5.6.18

       brew install php56 --without-apache --with-fpm          vim ~/.bashrc  修改 export PATH = "$(brew --prefix homebrew/php/php70)/bin:$PATH"  为 export PATH = "$(brew --prefix homebrew/php/php70)/bin:$(brew --prefix             homebrew/php/php56)/bin$PATH";

然后加入开机自启

           mkdir -p ~/Library/LaunchAgents           cp /usr/local/opt/php70/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/           launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist 

安装php版本切换公具

     brew install php-version    安装完成后 vim ~/.bashrc 添加  source $(brew --prefix php-version)/php-version.sh && php-version 7后面的 &&php-version是你想默认开启的php版本。自己选一个就行    sourc .bashrc后,命令行输入php-version,就可以看到在你电脑中的所有php版本,你想用哪个就php-version 5.6.18 或 php-version7.0.2来切换版本

安装php扩展包

    其实很简单,比如你想安装php7的xdebug扩展,就brew install php70-xdebug,想安装mcrypt就 brew install php70-mcrypt    如果想按5.6的就 brew install php56-xxxx,是不so easy,另外你可以 brew search php70,可以看到所有php7的扩展库,同理   也可以查php56的

配置nginx 和 php-fpm

  nginx 配置 很简单,nginx的配置文件在 /usr/local/etc/nginx/文件中  vim nginx.conf  然后 根据 自己的需求去配置就行,这里就不用详细说了  php-fpm的配置:     先cd到  /usr/local/etc/php,你可以看到7.0和5.6这俩个文件夹,至于你想要改哪个,就改就行 这没什么配置的,就是把nginx和fpm的端口统一就行

最后关键

  每次修改了php.ini文件。都要重启php-fpm,就行启动apache一样,不过这会不用重启nginx 重启步骤sudo killall php-fpm 先停掉所有php-fpm的进程cd  /usr/local/Cellar/php/版本号/sbin 不同版本的php,sbin目录里会有不同的php-fpm,比如php7.0的是php70-fpm, php5.6.18的是 php56-fpm,你想启动哪个fpm就

敲 sudo ./php70-fpm start 或php56-fpm start就行
取自这里

虚拟域名设置
首先不解释本地的虚拟域名是需要在本地hosts文件下添加的;
nginx.conf下有虚拟域名的示例

    #server {    #    listen       8000;    #    listen       somename:8080;    #    server_name  somename  alias  another.alias;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}

我们只需要按照这样的格式写进新的文件中,然后在nginx.conf结尾处 include 就可以了,nginx.conf的默认路径是/usr/local/etc/nginx,可以在这个文件夹下创建一个sites-enabled文件夹来保存各个虚拟域配置文件
值得注意的是,root还有html,php文件的处理,给个示例:

server{    listen 80;    server_name test.com;    charset utf-8;    root /Users/xuwei/www/test;    location /{        index index.html index.htm index.php;    }    location ~ \.php$ {        #root   html;        fastcgi_pass   127.0.0.1:9000;        fastcgi_index    index.php;        fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;        include    fastcgi_params;            # proxy_pass   http://127.0.0.1;        }    location ~ \.html$ {        #root   html;        fastcgi_pass   127.0.0.1:9000;        fastcgi_index    index.php;        fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;        include    fastcgi_params;            # proxy_pass   http://127.0.0.1;        }}

php-fpm的启用
在/usr/local/etc/php的配置文件下有php-fpm.conf的配置文件
然后在对应的/usr/local/cellar/php/php-fpm下启用

补充学习tp5的时候发现controller层只能访问index方法,其他的方法都不能访问,这就是要nginx支持pathinfo:

location ~ \.php {    #去掉$     #root          H:/PHPServer/WWW;     fastcgi_pass   127.0.0.1:9000;     fastcgi_index  index.php;     fastcgi_split_path_info ^(.+\.php)(.*)$;     #增加这一句     fastcgi_param PATH_INFO $fastcgi_path_info;    #增加这一句     fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;     include        fastcgi_params;}
0 0