本站停止维护,已转移至laravel学习网;欢迎大家移步访问!

Mac上通过brew安装lnmp (php7)开发环境

很多开发者都喜欢使用lnmp作为项目服务器,但是我们很多开发者本地喜欢用集成lamp环境开发,一到上线,各种水土不服,那么我们直接在mac上安装lnmp,就不会浪费那么多时间调代码了,接下来我们开始安装linux + nginx + php70 + mysql5+环境,brew对于Mac 相当于 ubuntu 的apt-get那么重要以及方便,大家可以先去安装brew,很简单

一. 准备条件

1. 升级brew

brew update

2. 配置

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

二. 安装PHP7

1. 安装

brew install php70

2. 遇到问题以及解决,如果以前安装过的话会出现

➜  ~ brew install php70

==> Installing php70 from homebrew/php

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

3. 配置文件

好安装后生成的配置文件都在/usr/local/etc/php/7.0目录里,分别如下:

php.ini中 /usr/local/etc/php/7.0/php.ini 

的php-fpm.conf /usr/local/etc/php/7.0/php-fpm.conf

PHP,运行phpize,PHP-配置ls /usr/local/opt/php70/bin

PHP-FPM /usr/local/opt/php70/sbin/php-fpm

4. 加入开机启动

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

5. 将php加入$PATH

vim ~/.bash_profile

添加如下配置文件

export PATH="/usr/local/sbin:$PATH"
export PATH="$(brew --prefix php70)/bin:$PATH"
export PATH="$(brew --prefix php70)/sbin:$PATH"
export PATH="/usr/local/bin:/usr/local/sbin:$PATH"

保存退出,运行配置

source ~/.bash_profile

6. 查看是否安装成功

lsof -Pni4 | grep LISTEN | grep php

显示如下代码代表安装成功

php-fpm    782 songlu    7u  IPv4 0x7acb53cb3b05a795      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm    783 songlu    0u  IPv4 0x7acb53cb3b05a795      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm    784 songlu    0u  IPv4 0x7acb53cb3b05a795      0t0  TCP 127.0.0.1:9000 (LISTEN)

7. 安装扩展

yaf

brew install php70-yaf

配置文件

/usr/local/etc/php/7.0/conf.d/ext-yaf.ini

memcached

brew install php70-memcached

配置文件

/usr/local/etc/php/7.0/conf.d/ext-memcached.ini

redis

brew install php70-redis

配置文件

/usr/local/etc/php/7.0/conf.d/ext-redis.ini

xdebug

brew install php70-xdebug

配置文件

/usr/local/etc/php/7.0/conf.d/ext-xdebug.ini

扩展更多执行brew install 扩展名进行安装

二. 安装Nginx

1. 安装

brew install nginx

通过homebrew,nginx文件默认被安装在/usr/local/etc/nginx/nginx.conf ,然后再浏览器中键入http://localhost:8080,即可访问到nginx的欢迎界面。

2. 配置

配置文件

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

给nginx添加一个配置文件sites-enabled,方便以后我们添多站点,没有的话可以自己建一个

include /usr/local/etc/nginx/sites-enabled/*;

接下来开始配置多站点

vim /usr/local/etc/nginx/sites-enabled/default 

添加如下代码,每个意义看备注,多站点直接复制如下代码重新粘贴,然后修改root,server_name重启nginx即可

server {
        listen 80;
        root /var/www/admin/public;  #项目文件地址
        index index.php index.html index.htm;
        server_name b.com; #本地域名,可以在host里定义
        location / {
                try_files $uri $uri/ /index.php?$query_string;
        }
        location ~ \.php$ {
                try_files $uri /index.php =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
        location ~ /\.ht {
                deny all;
        }
} 

重启nginx,让配置生效

nginx -s reload

3. Nginx启动关闭命令:

#测试配置是否有语法错误

nginx -t

#打开 nginx

sudo nginx

#重新加载配置|重启|停止|退出 nginx

nginx -s reload|reopen|stop|quit

#也可以使用Mac的launchctl来启动|停止

launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

Nginx开机启动

ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

三. 安装MySQL

MySQL也可以使用brew命令直接进行安装:

brew install mysql

同样,可以设置MySQL的开机自启动:

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

然后进行MySQL的安全安装,使用以下命令,可以更改root密码、删除匿名用户、关闭远程连接等:

mysql_secure_installation

然后会输出以下内容:

> Enter current password for root (enter for none):     //默认没有密码,直接回车即可

> Change the root password? [Y/n]                       //是否更改root密码,选择是,然后输入并确认密码

> Remove anonymous users? [Y/n]                         //是否删除匿名用户,选择是

> Disallow root login remotely? [Y/n]                   //是否禁止远程登录,选择是

> Remove test database and access to it? [Y/n]          //是否删除test数据库,选择是

> Reload privilege tables now? [Y/n]                    //是否重载表格数据,选择是

测试数据库是否安装成功:

mysql -u root -p

然后输入刚才设置的root密码,将会输出以下内容:

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit         //输入exit退出数据库


转载请注明 :一沙网络原文出处:http://bbs.earnp.com/article/316
问题交流群 :562864481
2
打赏
发布时间 :2017-01-12 11:57:14
分享

3 个评论

  • 快乐
    回复
    2017-03-14 18:38

    重启php7

    brew services restart php70


  • 快乐
    回复
    2017-02-24 22:58

    mcryp模块安装只需要执行如下命令:

    brew install php70-mcrypt


  • 快乐
    回复
    2017-02-23 09:27

    如果出现-bash: mysql: command not found 可以执行

    PATH="$PATH":/usr/local/mysql/bin


要回复文章请先登录注册