美文网首页
mac下配置php、nginx、mysql、redis

mac下配置php、nginx、mysql、redis

作者: GQ1994 | 来源:发表于2017-06-15 12:22 被阅读0次

    mac下配置php、nginx、mysql、redis

    一、PHP升级

    解除原php

    brew unlink php70
    

    删除原配置

    cd /usr/bin/ && sudo rm -rf php php-config phpdoc phpize
    cd /usr/include && sudo rm -rf php
    cd /usr/lib && sudo rm -rf php
    cd /usr/sbin && sudo rm -rf php-fpm
    cd /usr/share && sudo rm -rf php
    cd /usr/share/man/man1 && sudo rm -rf php-config.1 php.1 phpize.1
    cd /usr/share/man/man8 && sudo rm -rf php-fpm.8
    cd /usr/bin/ && sudo rm -rf php php-config phpdoc phpize
    

    安装php7.1

    brew install homebrew/php/php71
    brew install homebrew/php/php71-mcrypt
    

    安装完了之后它会自带PHP-FPM

    启动PHP-FPM

    加一个symlink先

    ln -s /usr/local/opt/php71/sbin/php-fpm /usr/local/bin/php-fpm
    

    还需要设置开机启动

    ln -sfv /usr/local/opt/php71/*.plist ~/Library/LaunchAgents
    

    启动

    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php71.plist
    

    停止

    launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.php71.plist
    

    强杀

    sudo pkill -INT -o php-fpm
    

    确保它正常运行监听9000端口

    sudo lsof -Pni4 | grep LISTEN | grep php
    

    或者通过端口查看

    sudo lsof -i tcp:9000
    

    二、安装nginx

    卸载

    brew remove nginx
    

    清除配置

    sudo rm -rf /usr/local/etc/nginx/
    

    安装

    brew install nginx --with-http_geoip_module

    开机启动

    ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
    

    启动

    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
    

    停止

    launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
    

    监听80端口需要root权限执行,因此:

    sudo chown root:wheel /usr/local/Cellar/nginx/1.12.0_1/bin/nginx
    sudo chmod u+s /usr/local/Cellar/nginx/1.12.0_1/bin/nginx
    

    配置

    配置nginx.conf
    创建需要用到的目录:

    mkdir -p /usr/local/var/logs/nginx
    mkdir -p /usr/local/etc/nginx/sites-available
    mkdir -p /usr/local/etc/nginx/sites-enabled
    mkdir -p /usr/local/etc/nginx/conf.d
    mkdir -p /usr/local/etc/nginx/ssl
    sudo mkdir -p /var/www
    sudo chown :staff /var/www
    sudo chmod 777 /var/www
    

    这里的文件夹给777,方便平时开发

    vim /usr/local/etc/nginx/nginx.conf 输入以下内容:

    user root wheel; #默认的是nobody会导致403
    worker_processes  1;
    
    error_log   /usr/local/var/logs/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/logs/access.log  main;
    
        sendfile        on;
        keepalive_timeout  65;
        port_in_redirect off;
    
        include /usr/local/etc/nginx/sites-enabled/*;
    }
    

    设置nginx php-fpm配置文件

    vim /usr/local/etc/nginx/conf.d/php-fpm
    

    修改为(没有则创建)

    #proxy the php scripts to 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;
    }
    

    创建默认虚拟主机default

    vim /usr/local/etc/nginx/sites-available/default输入:

    server {
    listen       80;
    server_name  www.qilipet.com admin.qilipet.com;
    root   /var/www/pet/public;
    
    access_log  /usr/local/var/logs/nginx/default.access.log  main;
    index index.php index.html index.htm;
    
    location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.php?$query_string;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }
    
    location ~ \.php$ {
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include    fastcgi_params;
        }
    }
    

    重要

    为了方便开发,将网站代码所在目录修改为 777权限

    三、安装MySQL

    brew install mysql
    

    查看一下MySQL运行情况

    ps aux | grep mysql
    

    测试连接MySQL(默认密码为空)

    mysql -uroot -p
    

    四、安装redis

    brew install redis
    

    默认密码为空

    五、设置快捷服务控制命令

    为了后面管理方便,将命令 alias 下,vim ~/.bash_aliases 输入一下内容:

    alias nginx.start='launchctl load -w /usr/local/opt/nginx/homebrew.mxcl.nginx.plist'
    alias nginx.stop='launchctl unload -w /usr/local/opt/nginx/homebrew.mxcl.nginx.plist'
    alias nginx.restart='nginx.stop && nginx.start'
    
    alias php-fpm.start="launchctl load -w /usr/local/opt/php71/homebrew.mxcl.php71.plist"
    alias php-fpm.stop="launchctl unload -w /usr/local/opt/php71/homebrew.mxcl.php71.plist"
    alias php-fpm.restart='php-fpm.stop && php-fpm.start'
    
    alias mysql.start="launchctl load -w /usr/local/opt/mysql/homebrew.mxcl.mysql.plist"
    alias mysql.stop="launchctl unload -w /usr/local/opt/mysql/homebrew.mxcl.mysql.plist"
    alias mysql.restart='mysql.stop && mysql.start'
    
    alias redis.start="launchctl load -w /usr/local/opt/redis/homebrew.mxcl.redis.plist"
    alias redis.stop="launchctl unload -w /usr/local/opt/redis/homebrew.mxcl.redis.plist"
    alias redis.restart='redis.stop && redis.start'
    

    让快捷命令生效

    echo "[[ -f ~/.bash_aliases ]] && . ~/.bash_aliases" >> ~/.bash_profile     
    source ~/.bash_profile
    

    有时终端重启之后依然不可以使用快捷方式,需要重新source,博主利用mac终端偏好设置,在开启终端之前先去执行source编译,设置如下图:

    1.jpg

    注意:

    考虑到部分服务不用开机启动,所以这里启动的路径都是程序目录。如果需要开机启动,将plist文件软连接到~/Library/LaunchAgents下。

    比如:ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents

    六、创建站点目录到主目录,方便快捷访问

    ln -sfv /var/www ~/htdocs

    七、引用

    本文参考鑫哥mac环境搭建笔记。

    相关文章

      网友评论

          本文标题:mac下配置php、nginx、mysql、redis

          本文链接:https://www.haomeiwen.com/subject/rziyqxtx.html