您好,欢迎光临赵帅的博客,如果有什么不足或者错误之处,感谢您留言指出!

【Mac】OXS系统 装 Nginx + php7.0 + mysql 配置 LNMP环境

mac 赵 帅 494浏览 0评论

最近在使用了Mac系统,然后想把它弄成编程本,啊哈,搞起。

环境介绍

macOS Series 10.12.2

Homebrew 1.1.7

1、Homebrew

习惯了Ubuntu的小伙伴,可能热衷于apt-get命令行,在这里homebrew有着相类似的功能,是mac上一个套间管理器。

我们来先安装homebrew,将下面这行代码粘贴到命令行即可进行安装:

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

更新brew

brew update

2、Nginx

2.1 安装nginx

Nginx作为一个反向代理服务器,其在处理高并发情况下有着Apache不可比拟的优势。我们还是来试试nginx吧。
Mac新版本里面带有 Apache 和 php,但是他们不是特别优雅,

在安装nginx之前我们可以先关闭apache:

sudo apachectl stop  #关闭apache,如果事先没开启过,可以忽略报错信息

如果apache已经加入了launchctl,使用下面的命令来关闭:

sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

使用下面两个命令来安装nginx(当前版本 1.10.2):

brew search nginx
brew install nginx

运行nginx:

sudo nginx #打开 nginx
nginx -s reload|reopen|stop|quit  #重新加载配置|重启|停止|退出 nginx
nginx -t   #测试配置是否有语法错误

用法详解:

nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]
选项列表
-?,-h           : 打开帮助信息
-v              : 显示版本信息并退出
-V              : 显示版本和配置选项信息,然后退出
-t              : 检测配置文件是否有语法错误,然后退出
-q              : 在检测配置文件期间屏蔽非错误信息
-s signal       : 给一个 nginx 主进程发送信号:stop(停止), quit(退出), reopen(重启), reload(重新加载配置文件)
-p prefix       : 设置前缀路径(默认是:/usr/local/Cellar/nginx/1.2.6/)
-c filename     : 设置配置文件(默认是:/usr/local/etc/nginx/nginx.conf)
-g directives   : 设置配置文件外的全局指令

安装完以后,可以在终端输出的信息里看到一些配置路径:
/usr/local/etc/nginx/nginx.conf (配置文件路径)
/usr/local/var/www (服务器默认路径)
/usr/local/Cellar/nginx/1.10.2 (貌似是安装路径)

2.2 修改配置

nginx安装完成之后,默认是8080端口,在浏览器打开会看到一个欢迎界面。
可以修改端口为80:

sudo vim /usr/local/etc/nginx/nginx.conf

给予管理员权限

sudo chown root:wheel /usr/local/opt/nginx/bin/nginx
sudo chmod u+s /usr/local/opt/nginx/bin/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

配置 Nginx 支持 PHP-FPM

sudo vim /usr/local/etc/nginx/nginx.conf    

server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;

       # 添加默认首页 php
       index  index.php index.html index.htm;
        location / {
            root   /usr/local/var/www;
            index  index.php  index.html index.htm;
            autoindex on; 
            autoindex_exact_size on;
        }
      # 取消以下内容的注释,并做修改
      location ~ \.php$ {
             root           html;
             fastcgi_pass   127.0.0.1:9999;    这里的端口要和 php-fpm的端口保持一致
             fastcgi_index  index.php;
             fastcgi_param  SCRIPT_FILENAME  $document_root$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;
        }
}

3、mysql

安装mysql(mysql 没有 my.conf文件)

brew install mysql

加入launchctl开机启动控制

mkdir -p ~/Library/LaunchAgents/
cp /usr/local/opt/mysql/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
#取消启动
#launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

如果出现 无法通过 mysql.sock 连接mysql的错误,很有可能你没有启动 mysql,使用下面命令:

mysql.server start

安装完成之后,mysql5.7的一个新特性就是 root 初始化密码不为空!所以我们要改密码~~

3.1 第一种情况,我们初始化密码:

3.1.1 首先要开启 mysql,然后运行 mysql_secure_installation 初始化密码:

/usr/local/opt/mysql/bin/mysql_secure_installation

运行之后,会有一些选项:

  1. 输入root的密码
  2. 会让你选择 root 密码的保密级别(看个人了,不想设置可以输入 n)
  3. 是否删除匿名用户
  4. 是否允许远程登录
  5. 是否删除test数据库
  6. 是否刷新授权表使修改生效

代码如下:

# zhaoshuai @ zhaoshuaideMacBook-Air in /usr/local/opt/mysql/bin [9:57:09] C:1
$ mysql_secure_installation 

Securing the MySQL server deployment.

Enter password for user root: ********(在这里输入你的密码)

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Using existing password for root.

Estimated strength of the password: 25 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : 

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : (这里是否删除匿名用户,生产环境建议删除,所以输入Y)

 ... skipping.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : (是否禁止root远程登录,
根据自己的需求选择Y/n并回车,建议禁止)

 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : (是否删除test数据库 y/n)
)

 ... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : (是否刷新授权表使修改生效,可以直接回车)

 ... skipping.
All done! 

登录mysql:

mysql -u root -p 

3.2 第二种情况,忘记了root密码

3.2.1 打开终端,这次首先我们要关掉mysql:

 sudo mysql.server stop 

或者,我们可以在进程中杀死,

ps -A | grep mysqld mysqld_safe
kill -9  ****

3.2.2 然后我们进入到 /usr/local/opt/mysql/bin ,输入:

mysqld_safe --skip-grant-tables

会出现:

2017-04-13T01:55:47.6NZ mysqld_safe Logging to '/usr/local/var/mysql/zhaoshuaideMacBook-Air.local.err'.
2017-04-13T01:55:47.6NZ mysqld_safe Logging to '/usr/local/var/mysql/zhaoshuaideMacBook-Air.local.err'.
2017-04-13T01:55:47.6NZ mysqld_safe A mysqld process already exists

3.2.3 然后我们输入:

mysql -u root -p

这时候会出现mysql> 了,输入

use mysql

3.2.4 最后输入:

update user set authentication_string=password('你需要设置的密码') where user='root';

4、php

在brew没有php的包,所以我们需要配置一下:

brew tap homebrew/dupes
brew tap homebrew/versions  
brew tap homebrew/homebrew-php

4.1安装php7.0:

brew install php70 --with-imap --with-tidy --with-debug --with-mysql --with-fpm  --without-apache

加入launchctl启动控制:

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

4.2 配置文件

可能会遇到的问题:

Error: Cannot install homebrew/php/php70 because conflicting formulae are installed.
php55: because different php versions install the same binaries.
Please `brew unlink php55` before continuing.
Unlinking removes a formula's symlinks from /usr/local. You can
link the formula again after the install finishes. You can --force this
install, but the build may fail or cause obscure side-effects in the
resulting software.

解决方法(根据提示):

根据提示执行: brew unlink php55
继续安装: brew install php70

将php55替换成php70:

vim ~/.zshrc

export PATH="$(brew --prefix php55)/bin:$PATH"

替换成:

export PATH="$(brew --prefix homebrew/php/php70)/bin:$PATH"

alias php-fpm.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist"
alias php-fpm.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php5.plist"
alias php-fpm.restart='php-fpm.stop && php-fpm.start'

替换成:

alias php-fpm.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist"
alias php-fpm.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist"
alias php-fpm.restart='php-fpm.stop && php-fpm.start'

完成之后重新加载配置文件:

source ~./zshrc

4.3验证版本:

(1)php的版本

zhaoshuaideMacBook-Air:nginx zhaoshuai$ php -v
PHP 7.0.14 (cli) (built: Dec  9 2016 07:34:25) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

(2)php-fpm版本

PHP 7.0.14 (fpm-fcgi) (built: Dec  9 2016 07:34:31)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

4.4 启动php-fpm

sudo php-fpm start

4.5 关闭php-fpm

sudo pkill -INT -o php-fpm  // 这条命令简单粗暴!直接有效

4.6 安装扩展

redis扩展

brew install php70-redis
配置文件
/usr/local/etc/php/7.0/conf.d/ext-redis.ini

memcached扩展

brew install php70-memcached
配置文件
/usr/local/etc/php/7.0/conf.d/ext-memcached.ini

swoole扩展

brew install php70-swoole
配置文件
/usr/local/etc/php/7.0/conf.d/ext-swoole.ini

总结

mac上不比Ubuntu,比较麻烦,希望在配置环境过程多去看一些资料,遇到问题多去google,会解决的。
啊哈,总之就是这样~
mac体验很好~~~

感谢分享:

homebrew安装mysql  :http://blog.neten.de/posts/2014/01/27/install-mysql-using-homebrew/

Mac下配置Nginx :http://www.cnblogs.com/cheemon/p/5638394.html

转载请注明:碎念 » 【Mac】OXS系统 装 Nginx + php7.0 + mysql 配置 LNMP环境

喜欢 (14)or分享 (0)