Mac 下配置 nginx+mysql+php+redis

2017-08-03 17:46:45   Linux

  redis   mysql   Mac   nginx   php  

Mac 下配置 nginx+mysql+php+redis

PHP升级

解除原php

  1. brew unlink php70//有可能是其他版本 自行调节

删除原配置

  1. //为了方便可以 先切刀root权限 sudo -i 然后整体复制下方的代码执行 完毕后再切到普通用户目录
  2. cd /usr/bin/ && sudo rm -rf php php-config phpdoc phpize
  3. cd /usr/include && sudo rm -rf php
  4. cd /usr/lib && sudo rm -rf php
  5. cd /usr/sbin && sudo rm -rf php-fpm
  6. cd /usr/share && sudo rm -rf php
  7. cd /usr/share/man/man1 && sudo rm -rf php-config.1 php.1 phpize.1
  8. cd /usr/share/man/man8 && sudo rm -rf php-fpm.8
  9. cd /usr/bin/ && sudo rm -rf php php-config phpdoc phpize

安装php7.1

  1. brew install homebrew/php/php71
  2. brew install homebrew/php/php71-mcrypt

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

启动PHP-FPM

加一个symlink先

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

还需要设置开机启动

  1. //不出意外情况直接执行
  2. ln -sfv /usr/local/opt/php71/*.plist ~/Library/LaunchAgents
  3. //可能存在 LaunchAgents不是文件夹 而是文件 这就需要删掉 自行重新创建
  4. //检查一下 cd ~/Library/LaunchAgents 如果报错 执行:
  5. cd ~/Library/
  6. mkdir LaunchAgents
  7. ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents

启动

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

停止

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

强杀

  1. sudo pkill -INT -o php-fpm

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

  1. sudo lsof -Pni4 | grep LISTEN | grep php

或者通过端口查看

  1. sudo lsof -i tcp:9000

如果失败的化 卸载重装

  1. brew unlink php70
  2. brew uninstall homebrew/php/php71
  3. brew uninstall homebrew/php/php71-mcrypt

安装nginx

卸载

  1. brew remove nginx

清除配置

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

安装

brew install nginx —with-http_geoip_module

开机启动

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

启动

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

停止

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

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

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

配置

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

  1. mkdir -p /usr/local/var/logs/nginx
  2. mkdir -p /usr/local/etc/nginx/sites-available
  3. mkdir -p /usr/local/etc/nginx/sites-enabled
  4. mkdir -p /usr/local/etc/nginx/conf.d
  5. mkdir -p /usr/local/etc/nginx/ssl
  6. sudo mkdir -p /var/www
  7. sudo chown :staff /var/www
  8. sudo chmod 777 /var/www

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

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

  1. user root wheel; #默认的是nobody会导致403
  2. worker_processes 1;
  3. error_log /usr/local/var/logs/nginx/error.log debug;
  4. pid /usr/local/var/run/nginx.pid;
  5. events {
  6. worker_connections 256;
  7. }
  8. http {
  9. include mime.types;
  10. default_type application/octet-stream;
  11. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  12. '$status $body_bytes_sent "$http_referer" '
  13. '"$http_user_agent" "$http_x_forwarded_for"';
  14. access_log /usr/local/var/logs/access.log main;
  15. sendfile on;
  16. keepalive_timeout 65;
  17. port_in_redirect off;
  18. include /usr/local/etc/nginx/sites-enabled/*;
  19. }

设置nginx php-fpm配置文件

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

修改为

  1. #proxy the php scripts to php-fpm
  2. location ~ \.php$ {
  3. try_files $uri = 404;
  4. fastcgi_pass 127.0.0.1:9000;
  5. fastcgi_index index.php;
  6. fastcgi_intercept_errors on;
  7. include /usr/local/etc/nginx/fastcgi.conf;
  8. }

创建默认虚拟主机default

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

  1. server {
  2. listen 80;
  3. server_name www.qilipet.com admin.qilipet.com;
  4. root /var/www/pet/public;
  5. access_log /usr/local/var/logs/nginx/default.access.log main;
  6. index index.php index.html index.htm;
  7. location / {
  8. # First attempt to serve request as file, then
  9. # as directory, then fall back to displaying a 404.
  10. try_files $uri $uri/ /index.php?$query_string;
  11. # Uncomment to enable naxsi on this location
  12. # include /etc/nginx/naxsi.rules
  13. }
  14. location ~ \.php$ {
  15. fastcgi_pass 127.0.0.1:9000;
  16. fastcgi_index index.php;
  17. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  18. include fastcgi_params;
  19. }
  20. }

创建软连接

  1. ln -s /usr/local/etc/nginx/sites-available/default ../sites-enabled/

重启nginx服务

重要

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

安装MySQL

  1. brew install mysql
  2. brew link mysql
  3. brew info mysql

查看一下MySQL运行情况

  1. ps aux | grep mysql

测试连接MySQL

  1. mysql -uroot -p

安装Redis

  1. brew install redis

查看一下Redis是否运行

  1. redis-cli

设置开机自启

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

设置快捷服务控制命令

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

  1. alias nginx.start='launchctl load -w /usr/local/opt/nginx/homebrew.mxcl.nginx.plist'
  2. alias nginx.stop='launchctl unload -w /usr/local/opt/nginx/homebrew.mxcl.nginx.plist'
  3. alias nginx.restart='nginx.stop && nginx.start'
  4. alias php-fpm.start="launchctl load -w /usr/local/opt/php71/homebrew.mxcl.php71.plist"
  5. alias php-fpm.stop="launchctl unload -w /usr/local/opt/php71/homebrew.mxcl.php71.plist"
  6. alias php-fpm.restart='php-fpm.stop && php-fpm.start'
  7. alias mysql.start="launchctl load -w /usr/local/opt/mysql/homebrew.mxcl.mysql.plist"
  8. alias mysql.stop="launchctl unload -w /usr/local/opt/mysql/homebrew.mxcl.mysql.plist"
  9. alias mysql.restart='mysql.stop && mysql.start'
  10. alias redis.start="launchctl load -w /usr/local/opt/redis/homebrew.mxcl.redis.plist"
  11. alias redis.stop="launchctl unload -w /usr/local/opt/redis/homebrew.mxcl.redis.plist"
  12. alias redis.restart='redis.stop && redis.start'

让快捷命令生效

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

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

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

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

ln -sfv /var/www ~/htdocs

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

感谢你的支持,我会继续努力!
扫码打赏,建议金额1-10元