MAC搭建LNMP环境

来源:互联网 发布:宋太宗997 知乎 编辑:程序博客网 时间:2024/04/29 18:41

1、安装brew

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

这样就直接安装完成了,安装出现问题请查看brew官方说明,不知道brew请弃用Mac;
移除已安装的程序:

sudo brew remove XXXX

彻底卸载需要以下命令:

sudo brew cleanupsudo rm -rf ~/Library/LaunchAgents/xxx.plist

如果是卸载MySQL,则需要使用命令:

sudo rm -rf /usr/local/var/mysql

2、安装MySQL

查看MySQL可用版本信息:

brew info mysql

我这边看到的版本是5.7.10:

mysql: stable 5.7.10 (bottled)

接下来安装MySQL5.7.10:

brew install mysql

安装完成之后按照提示将plist文件放入~/Library/LaunchAgents/中并load,设定MySQL开机启动:

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

启动MySQL:

mysql.server start

启动之后由于MySQL默认没有设置密码,所以要设置root的密码:

mysql -uroot -p

提示输入密码的时候直接按回车就登录了,登录MySQL后提示如下:

Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.7.10 Homebrew

接下来设置root的密码:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';

设置密码的时候最好设置一个强密码,关于强密码的规则,官方有如下说明:

NoteMySQL's validate_password plugin is installed by default. This will require that passwords contain at least one upper case letter, one lower case letter, one digit, and one special character, and that the total password length is at least 8 characters.

为了方便使用,我们经常会创建任意连接的root用户:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'MyNewPass4!' WITH GRANT OPTION;

刷新权限使命令生效:

flush privileges;

退出MySQL:exit


3、安装PHP

首先加入brew的几个官方源:

brew tap homebrew/dupesbrew tap homebrew/versionsbrew tap homebrew/php

然后查看版本库中php的版本信息,我要安装php5.6,所以直接查看php56具体的版本信息:

brew info php56

结果显示的是5.6.17:

homebrew/php/php56: stable 5.6.17 (bottled), HEADPHP Version 5.6

下面开始安装php5.6
PHP如果采用默认配置安装,会编译mod_php模块并只运行在Apache环境下,为了使用Nginx,这里需要编译php-fpm并且禁用apache,主要通过参数--without-fpm --without-apache来实现。完整的安装指令为

brew install php56 --with-fpm --with-enchant --with-gmp --with-homebrew-curl --with-homebrew-libressl --with-homebrew-libxml2 --with-homebrew-libxslt --with-imap --with-libmysql --with-mssql --with-pear --with-phpdbg --with-postgresql --without-apache --with-bz2 --with-ldap --with-legacy-mysql --with-mysql --with-pcntl

安装完成之后将php路径加入PATH,由于我的mac使用的是zsh,所以我这里修改zshrc:

sudo vi ~/.zshrc

在里面添加如下内容:

#php5.6export PATH="/usr/local/bin:/usr/local/sbin:$PATH"

保存之后引入刚才的path:

source ~/.zshrc 

加入launchctl启动控制:

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

php.ini、fpm.conf配置的路径如下:

/usr/local/etc/php/5.6/php.ini/usr/local/etc/php/5.6/php-fpm.conf

将配置文件放在/etc/下:

sudo ln -s /usr/local/etc/php/5.6/php.ini /etc/php.inisudo ln -s /usr/local/etc/php/5.6/php-fpm.conf /etc/php-fpm.conf

接下来安装php5.6的扩展,首先查找版本库中php5.6可用的扩展:

brew search php56

找到扩展之后开始安装所需扩展:

brew install php56-gearman php56-imagick php56-msgpack php56-opcache php56-phalcon php56-redis php56-xdebug php56-swoole php56-yac php56-yaf php56-yar php56-oauth

安装完成之后一些需要启动的服务如下:
gearman启动:

ln -sfv /usr/local/opt/gearman/*.plist ~/Library/LaunchAgentsgearmand -d

安装完扩展之后,有些扩展需要有服务端支持,redis等,那就需要先安装服务端,具体如何安装使用,自行查找;


4、安装nginx

brew install nginx

安装完成的nginx,默认的root路径如下:

Docroot is: /usr/local/var/www

nginx的配置文件目录如下:

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

nginx虚拟站点目录如下:

nginx will load all files in /usr/local/etc/nginx/servers/.

开机启动nginx:

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

nginx监听80端口是需要root权限的:

sudo chown root:wheel /usr/local/Cellar/nginx/1.8.1/bin/nginxsudo chmod u+s /usr/local/Cellar/nginx/1.8.1/bin/nginx

①、先将nginx的配置文件放至/etc下:

sudo ln -s /usr/local/etc/nginx/nginx.conf /etcsudo ln -s /usr/local/etc/nginx/servers /etc/nginxservers

②、修改nginx监听端口:

sudo vi /etc/nginx.conf

修改配置文件如下:

#user  nobody;worker_processes  1;#error_log  /usrlogs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;pid        /usr/local/var/run/nginx.pid;events {    worker_connections  1024;}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  logs/access.log  main;    port_in_redirect off;    sendfile        on;    tcp_nopush     on;    keepalive_timeout  65;    gzip  on;    include servers/*.conf;}

然后在/etc/nginxservers/下创建default.conf,编辑default.conf,加入以下内容:

server {        listen       80;        server_name  localhost;        #charset koi8-r;        root   html;        index  index.php index.html index.htm;        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000        location ~ \.php$ {                fastcgi_pass   127.0.0.1:9000;                fastcgi_index  index.php;                fastcgi_intercept_errors    on;                include /usr/local/etc/nginx/fastcgi.conf;        }        access_log  /usr/local/var/log/local.access.log  main;        error_log  /usr/local/var/log/local.error.log  info;        #error_page  404              /404.html;        # redirect server error pages to the static page /50x.html        #        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }        # deny access to .htaccess files, if Apache's document root        # concurs with nginx's one        #        #location ~ /\.ht {        #    deny  all;        #}    }

修改完配置,重启nginx和php-fpm。

0 0