mac搭建nginx、mysql、php(LNMP)开发环境

自上次翻译了“homebrew”命令后,拖了好久,才写这篇环境搭建,因为平常在公司,只有晚上回去有时间折腾电脑,晚上有时候又有事,或者懒。。,加之穿插了nginx文档的翻译等,还有就是照着教程搭建环境,一直有问题,直到写这篇博客,nginx的开机启动,扔没搞好,等有时间我把 man launchctl 的文档给翻译了,就明白为什么了,有些东西网上不好找,得自己解决了。明早报团去旅游,今天务必把这篇安装教程给整理处理!下面开始:

安装工具:

homebrew一个即可

brew是mac下的包管理工具,就像linux下的apt,它确保安装的包是最新版本。具体可查看之前的命令翻译:

mac的homebrew命令详解&全部选项翻译

安装开发常用的包&软件

brew install wget watch tmux cmake openssl imagemagick graphicsmagick gearman geoip readline autoconf multitail source-highlight autojump zsh-completions sshfs 

mac系统使用的shell

查看系统所有shell:

cat /etc/shells
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

查看当前使用的shell:

echo $SHELL

安装mysql

brew install mysql

安装完成后,会有mysql的一些提示,包括mysql如何启动,mysql的安全机制命令。这里我们开启mysql的安全机制:

/usr/local/opt/mysql/bin/mysql_secure_installation
默认mysql的用户管理员用户为root,密码为空。运行此命令,可以改变root密码,移除匿名用户以及禁止远程使用root管理员账号来登录,这里粘贴下老外博客的截图,真实的mac环境提示就是这样:


  • 1.是否设置root密码,默认应该是空,直接enter即可,然后输入自定义的密码
  • 2.是否删除匿名账户
  • 3.是否禁用root远程登录
  • 4.是否删除test数据表
  • 5.重新加载表权限,让上面的修改,立即生效。

到这里,mysql的安装和配置就结束了,就是这么简单,而且安装的是mysql的最新版本,查看下mysql是否安装成功:

#查看一下MySQL运行情况
➜  ~  ps aux | grep mysql
calvin           1695   0.0  0.5  2719864  90908   ??  S     1:38上午   0:00.31 xxx
calvin           1323   0.0  0.0  2444628   1020   ??  S     1:38上午   0:00.04 xxx
#测试连接MySQL
mysql -uroot -p
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 23
Server version: 5.6.19-log Homebrew

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql>

mysql设置开机启动:

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

安装php

安装php之前有一步需要处理。默认,homebrew没有php-fpm的软件,首先我们需要tap(register)一个特殊的PHP仓库:

brew tap homebrew/dupes

brew tap homebrew/php

现在,可以开始运行php的命令了,我们安装php71,最新的:

brew install php71 --with-fpm --with-gmp --with-imap --with-tidy --with-debug --with-mysql --with-libmysql

等php安装完成后,开始安装php的扩展:

可以查看php71有哪些扩展可以安装,可列出所有php71匹配的软件:

brew search php71

具体可安装的扩展列表给忘记了,这里给出几个:

brew install php71-apcu\

php71-gearman

php71-gmagick

php71-imagick

php71-intl

php71-mcrypt

php71-opcache

php71-redis

php71-xdebug

由于mac自带了php和php-fpm,因此,我们需要添加系统环境变量PATH,来替代之前的php版本的命令:

--------------------------------------------------------------------------------

如果是默认的Bash shell,使用:

echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.bash_profile && . ~/.bash_profile

如果安装了zsh,使用:

echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.zshrc && . ~/.zshrc

--------------------------------------------------------------------------------
不好意思。。这里我忘记了哪个可用了:

echo 'export PATH="$(brew --prefix php71)/bin:$PATH"' >> ~/.bash_profile  #for php
echo 'export PATH="$(brew --prefix php71)/sbin:$PATH"' >> ~/.bash_profile  #for php-fpm
echo 'export PATH="/usr/local/bin:/usr/local/sbin:$PATH"' >> ~/.bash_profile #for other brew install soft
source ~/.bash_profile

可通过:在/usr/local/sbin和$(brew --prefix php71),查找有无php-fpm文件,无非是路径下查找文件而已。

--------------------------------------------------------------------------------

设置php开机启动:

ln -sfv /usr/local/opt/php56/homebrew.mxcl.php71.plist ~/Library/LaunchAgents/

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

启动php-fpm之后,确保它正常运行监听9000端口:

lsof -Pni4 | grep LISTEN | grep php
php-fpm   30907 calvin    9u  IPv4 0xf11f9e8e8033a2a7      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   30917 calvin    0u  IPv4 0xf11f9e8e8033a2a7      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   30918 calvin    0u  IPv4 0xf11f9e8e8033a2a7      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   30919 calvin    0u  IPv4 0xf11f9e8e8033a2a7      0t0  TCP 127.0.0.1:9000 (LISTEN)
#正常情况,会看到上面这些进程

安装php composer

brew install composer

redis和memcached可能没安装,可使用brew info redis和brew info memcached来查看,未安装调用命令:

brew install redis

brew install memcached

安装phpMyAdmin

phpmyadmin的安装,首先需要安装autoconf:

brew install autoconf  // 之前安装常用软件,可能已经安装过了

brew install phpmyadmin // 安装phpmyadmin

如果失败,尝试下面操作,记忆中,我好像没用这个:

默认的bash shell:

echo 'PHP_AUTOCONF="'$(which autoconf)'"' >> ~/.bash_profile && . ~/.bash_profile

zsh:

echo 'PHP_AUTOCONF="'$(which autoconf)'"' >> ~/.zshrc && . ~/.zshrc

安装nginx

brew install nginx

nginx启动和关闭命令,可查看之前nginx的翻译:

nginx文档翻译

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

#打开 nginx
sudo nginx

#重新加载配置|重启|停止|退出 nginx
nginx -s reload|reopen|stop|quit

nginx设置开机启动

ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
nginx默认监听80端口,而mac系统,只有root用户才可以打开<1024的监听端口,所以,这里得设置额外的权限:

sudo chown root:wheel /usr/local/Cellar/nginx/1.6.0_1/bin/nginx	// 这里的版本号换成你自己的
sudo chmod u+s /usr/local/Cellar/nginx/1.6.0_1/bin/nginx	// 这里的版本号换成你自己的
--------------------------------------------------------------------------------

很不幸,nginx设置开机自启动(也就是调用launchctl load xxx 来打开nginx),我一直没有成功。。。因为mysql和php-fpm的自启动,都是链接到了~/Library/用户的自启动目录下,所以希望nginx的也放到这里。老外的设置,我一直没有尝试,有时间尝试下:

sudo cp -v /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/

sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

并不需要修改nginx的命令文件权限

--------------------------------------------------------------------------------

默认安装的nginx,我们可以通过:nginx (找到命令存放的目录,或者保证命令在环境变量中可以找到),如果需要权限,使用:sudo nginx

nginx启动后,测试nginx是否可用:

curl -IL http://127.0.0.1:8080 // 默认安装的nginx监听8080

可以得到如下输出,说明nginx已经成功启动:

HTTP/1.1 200 OK

Server: nginx/1.6.2

Date: Mon, 19 Oct 2014 19:07:47 GMT

Content-Type: text/html

Content-Length: 612

Last-Modified: Mon, 19 Oct 2014 19:01:32 GMT

Connection: keep-alive

ETag: "5444dea7-264"

Accept-Ranges: bytes

至此,nginx已经安装成功。LNMP也已经搭建完毕。

配置nginx的虚拟主机(可继续看我翻译的nginx文档,针对nginx新手),这里就大段的复制老外的代码了,都是nginx的配置,可以放心配置。。

创建需要用到的目录:

kdir -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 775 /var/www

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

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;
}

nginx虚拟主机准备工作

#创建 info.php index.html 404.html 403.html文件到 /var/www 下面
vi /var/www/info.php
vi /var/www/index.html
vi /var/www/403.html
vi /var/www/404.html

创建默认虚拟主机default

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

server {
    listen       80;
    server_name  localhost;
    root         /var/www/;

    access_log  /usr/local/var/logs/nginx/default.access.log  main;

    location / {
        index  index.html index.htm index.php;
        autoindex   on;
        include     /usr/local/etc/nginx/conf.d/php-fpm;
    }

    location = /info {
        allow   127.0.0.1;
        deny    all;
        rewrite (.*) /.info.php;
    }

    error_page  404     /404.html;
    error_page  403     /403.html;
}

创建ssl默认虚拟主机default-ssl

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

server {
    listen       443;
    server_name  localhost;
    root       /var/www/;

    access_log  /usr/local/var/logs/nginx/default-ssl.access.log  main;

    ssl                  on;
    ssl_certificate      ssl/localhost.crt;
    ssl_certificate_key  ssl/localhost.key;

    ssl_session_timeout  5m;

    ssl_protocols  SSLv2 SSLv3 TLSv1;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;

    location / {
        include   /usr/local/etc/nginx/conf.d/php-fpm;
    }

    location = /info {
        allow   127.0.0.1;
        deny    all;
        rewrite (.*) /.info.php;
    }

    error_page  404     /404.html;
    error_page  403     /403.html;
}

创建phpmyadmin虚拟主机

vim /usr/local/etc/nginx/sites-available/phpmyadmin #输入以下配置
server {
    listen       80;
    server_name  phpmyadmin.devil.com;
    root    /usr/local/share/phpmyadmin;

    error_log   /usr/local/var/logs/nginx/phpmyadmin.error.log;
    access_log  /usr/local/var/logs/nginx/phpmyadmin.access.log main;

    ssl                  on;
    ssl_certificate      ssl/phpmyadmin.crt;
    ssl_certificate_key  ssl/phpmyadmin.key;

    ssl_session_timeout  5m;

    ssl_protocols  SSLv2 SSLv3 TLSv1;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;

    location / {
        index  index.html index.htm index.php;
        include   /usr/local/etc/nginx/conf.d/php-fpm;
    }
}

设置SSL

mkdir -p /usr/local/etc/nginx/ssl
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=localhost" -keyout /usr/local/etc/nginx/ssl/localhost.key -out /usr/local/etc/nginx/ssl/localhost.crt
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=phpmyadmin" -keyout /usr/local/etc/nginx/ssl/phpmyadmin.key -out /usr/local/etc/nginx/ssl/phpmyadmin.crt

创建虚拟主机软连接,开启虚拟主机

ln -sfv /usr/local/etc/nginx/sites-available/default /usr/local/etc/nginx/sites-enabled/default
ln -sfv /usr/local/etc/nginx/sites-available/default-ssl /usr/local/etc/nginx/sites-enabled/default-ssl
ln -sfv /usr/local/etc/nginx/sites-available/phpmyadmin /usr/local/etc/nginx/sites-enabled/phpmyadmin

设置服务控制命令别名,vim ~/.bash_aliases

alias nginx.start='launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist'
alias nginx.stop='launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist'
alias nginx.restart='nginx.stop && nginx.start'
alias php-fpm.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist"
alias php-fpm.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist"
alias php-fpm.restart='php-fpm.stop && php-fpm.start'
alias mysql.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
alias mysql.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
alias mysql.restart='mysql.stop && mysql.start'
alias redis.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist"
alias redis.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist"
alias redis.restart='redis.stop && redis.start'
alias memcached.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist"
alias memcached.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist"
alias memcached.restart='memcached.stop && memcached.start'
同样是根据shell环境,bash shell:
echo "source ~/.bash_aliases" >> ~/.bash_profile && . ~/.bash_profile
zsh shell:
echo "source ~/.bash_aliases" >> ~/.zshrc && ~/.zshrc

(memcached和redis,以同样的自启动方式,来设置。上面未设置。)


参考文章:

https://segmentfault.com/a/1190000000606752

它里面提的老外的文章,很多地址失效,尤其是curl命令获取文件的地址,所以非常感谢第一篇作者”fish“:

https://blog.frd.mn/install-nginx-php-fpm-mysql-and-phpmyadmin-on-os-x-mavericks-using-homebrew/


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值