美文网首页
Mac OS上搭建LNMP开发环境

Mac OS上搭建LNMP开发环境

作者: nanhangfei | 来源:发表于2016-03-24 23:46 被阅读5622次

    安装Homebrew

    安装Homebrew

    ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

    检查是否存在冲突

    brew doctor

    对Homebrew更新升级

    brew update && brew upgrade


    安装Nginx

    安装nginx

    brew install nginx

    安装oh-my-zsh

    mac 自带zsh,在终端中输入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

    输入zsh --version,可以查看zsh版本。

    输入brew install zsh,可以安装最新版的zsh。

    输入 chsh -s /bin/zsh,并重启终端,可以将系统默认的终端shell改为zsh。

    然后安装oh-my-zsh

    sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"


    安装php-fpm

    添加源

    brew tap homebrew/dupes

    brew tap homebrew/php

    安装php-fpm

    brew install php56 --whitout-apache --with-imap --with-tidy --with-debug --with-pgsql --with-mysql --with-fpm

    将PHP、PHP-FPM加入环境变量

    在终端中

    vim .zshrc

    找到export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"这一行,在其后面添加

    homebrew下载php、php-fpm路径配置,使其如下所示

    export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"

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

    export PATH="$(brew --prefix homebrew/php/php56)/sbin:$PATH"

    $(brew --prefix homebrew/php/php56)/bin是使用homebrew下载的php

    $(brew --prefix homebrew/php/php56)/sbin是使用homebrew下载的php-fpm

    /usr/bin是MAC自带的php

    /usr/sbin是MAC自带php-fpm

    然后使其立即生效

    source .zshrc

    查看$PATH,最前面的路径先后后面的路径

    ~echo $PATH

    /usr/local/opt/php56/sbin:/usr/local/opt/php56/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

    查看php、php-fpm版本

    ~php -v

    PHP 5.6.19 (cli) (built: Mar 24 2016 20:55:30) (DEBUG)

    Copyright (c) 1997-2016 The PHP Group

    Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

    ~php-fpm -v

    PHP 5.6.19 (fpm-fcgi) (built: Mar 24 2016 20:55:33) (DEBUG)

    Copyright (c) 1997-2016 The PHP Group

    Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

    ~/usr/bin/php -v

    PHP 5.5.31 (cli) (built: Feb 20 2016 20:33:10)

    Copyright (c) 1997-2015 The PHP Group

    Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies

    ~/usr/sbin/php-fpm -v

    PHP 5.5.31 (fpm-fcgi) (built: Feb 20 2016 20:33:26)

    Copyright (c) 1997-2015 The PHP Group

    Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies

    从上面可见,配置的环境变量是正确的,homebrew先于系统自带的执行。

    //启动php-fmp

    php-fmp

    参考

    MAC 设置环境变量path的几种方法

    mac系统如何显示和隐藏文件

    MySQL PHP Nginx Redis Memcache

    使用内置的 PHP

    安装MySQL

    brew install mysql

    执行完如上命令后,有如下输出结果

    We've installed your MySQL database without a root password. To secure it run:

    mysql_secure_installation

    To connect run:

    mysql -uroot

    To have launchd start mysql at login:

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

    Then to load mysql now:

    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

    Or, if you don't want/need launchctl, you can just run:

    mysql.server start

    从上面的一段文字来看,安装mysql后,用户名是root,但是没有密码。如果设置密码的话,执行mysql_secure_installation命令。如果不想设置密码的话,可以执行mysql.server start,然后mysql -uroot,如下图所示

    nhf:~ nanhangfei$mysql.server start

    Starting MySQL

    . SUCCESS!

    nhf:~ nanhangfei$ mysql -uroot

    Welcome to the MySQL monitor.Commands end with ; or \g.

    Your MySQL connection id is 2

    Server version: 5.7.11 Homebrew

    Copyright (c) 2000, 2016, 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>

    如果想要设置root的密码的话,执行下面的命令,做些相关的设置。

    mysql_secure_installation


    安装phpmyadmin

    brew install phpmyadmin


    配置Nginx

    首先,为我们的配置文件创建一些文件夹

    mkdir -p /usr/local/etc/nginx/logs

    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

    修改nginx配置文件

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

    替换其内容为

    worker_processes  1;

    error_log  /usr/local/etc/nginx/logs/error.log debug;

    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/etc/nginx/logs/access.log  main;

    sendfile            on;

    keepalive_timeout  65;

    index index.html index.php;

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

    }

    然后创建php-fpm配置文件:

    vim /usr/local/ect/nginx/conf.d/php-fpm

    其内容如下,这个文件的意思是nginx中的php文件交由php-fmp解析

    location ~ \.php$ {

    try_files      $uri = 404;

    fastcgi_pass  127.0.0.1:9000;

    fastcgi_index  index.php;

    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

    include        fastcgi_params;

    }

    搞不清FastCgi与PHP-fpm之间是个什么样的关系

    创建默认虚拟主机default:

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

    其内容如下所示

    server {

    listen      80;

    server_name  localhost;

    root      /var/www/;

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

    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;

    }

    创建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/etc/nginx/logs/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      306;

    server_name  localhost;

    root    /usr/local/share/phpmyadmin;

    error_log  /usr/local/etc/nginx/logs/phpmyadmin.error.log;

    access_log  /usr/local/etc/nginx/logs/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-days365-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-days365-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

    Mac OS上搭建LNMP开发环境

    MySQL PHP Nginx Redis Memcache

    安装php-fpm、nginx、mysql、phpmyadmin是参考的这两篇文章, 但在安装过程中,会出现几个问题,但原文章并没有说明。下面是碰到的问题及解决方案。

    启动php-fmp、nginx、mysql

    php-fmp

    确认php-fpm监听9000端口

    ~sudo lsof -Pni4 | grep LISTEN | grep php

    Password:

    php-fpm32898root8uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)

    php-fpm32899_www0uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)

    php-fpm32900_www0uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)

    php-fpm32901_www0uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)

    启动nginx

    sudo nginx

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

    nginx -s reload|reopen|stop|quit

    确认nginx在监听80端口

    ~sudo lsof -Pni4 | grep LISTEN | grep nginx

    或者

    index.html复制到nginx的根目录/var/www中去

    ~curl -IL http://localhost/index.html

    HTTP/1.1 200 OK

    Server: nginx/1.8.1

    Date: Sun, 27 Mar 2016 03:18:30 GMT

    Content-Type: text/html

    Content-Length: 611

    Last-Modified: Thu, 24 Mar 2016 23:16:35 GMT

    Connection: keep-alive

    ETag: "56f47553-263"

    Accept-Ranges: bytes

    启动mysql

    mysql.server start

    先登录

    mysql -uroot -p

    确定mysql端口

    mysql> show global variables like 'port';

    +---------------+-------+

    | Variable_name | Value |

    +---------------+-------+

    | port| 3306|

    +---------------+-------+ 

    如果安装PHP时出现如下错误

    checking if the location of ZLIB install directory is defined... no

    configure: error: Cannot find libz

    解决方式是在终端中执行如下命令

    xcode-select --install

    参考 

    configure: error: Cannot find libz


    启动php-fmp时的问题

    ~/usr/local/opt/php56/sbin/php-fpm

    [26-Mar-2016 12:15:40] NOTICE: PHP message: PHP Warning:PHP Startup: mcrypt: Unable to initialize module

    Module compiled with build ID=API20131226,NTS

    PHPcompiled with build ID=API20131226,NTS,debug

    These options need to match

    in Unknown on line 0

    Warning:PHP Startup: mcrypt: Unable to initialize module

    Module compiled with build ID=API20131226,NTS

    PHPcompiled with build ID=API20131226,NTS,debug

    These options need to match

    in Unknown on line 0

    Unknown(0) : Warning - PHP Startup: mcrypt: Unable to initialize module

    Module compiled with build ID=API20131226,NTS

    PHPcompiled with build ID=API20131226,NTS,debug

    These options need to match

    [26-Mar-2016 12:15:40] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root

    [26-Mar-2016 12:15:40] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root

    [26-Mar-2016 12:15:40] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (48)

    [26-Mar-2016 12:15:40] ERROR: FPM initialization failed

    解决办法,在终端执行如下命令

    brew reinstall php56-mcrypt --build-from-source

    参考 PHP Warning: PHP Startup: ????????: Unable to initialize module

    上一步完成后,又出现如下问题

    ~sudo /usr/local/opt/php56/sbin/php-fpm

    Password:

    [26-Mar-2016 12:40:10] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (48)

    [26-Mar-2016 12:40:10] ERROR: FPM initialization failed

    查看谁占用了9000端口

    ~sudo lsof -i:9000

    COMMAND PID USERFDTYPEDEVICE SIZE/OFF NODE NAME

    php-fpm 707 root7uIPv4 0x9177969b065b477d0t0TCP localhost:cslistener (LISTEN)

    php-fpm 708 _www0uIPv4 0x9177969b065b477d0t0TCP localhost:cslistener (LISTEN)

    php-fpm 709 _www0uIPv4 0x9177969b065b477d0t0TCP localhost:cslistener (LISTEN)

    php-fpm 710 _www0uIPv4 0x9177969b065b477d0t0TCP localhost:cslistener (LISTEN)

    杀掉进程id 707、708、709、710

    ~sudo kill -9 707

    ~sudo kill -9 708

    ~sudo kill -9 709

    ~sudo kill -9 710


    ~sudo /usr/local/opt/php56/sbin/php-fpm

    [26-Mar-2016 12:55:29] NOTICE: fpm is running, pid 32706

    [26-Mar-2016 12:55:29] NOTICE: ready to handle connections

    确认php-fpm监听9000端口

    ~sudo lsof -Pni4 | grep LISTEN | grep php

    Password:

    php-fpm32898root8uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)

    php-fpm32899_www0uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)

    php-fpm32900_www0uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)

    php-fpm32901_www0uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)

    ~

    ~php-fpm -D

    [25-Mar-2016 20:23:28] ERROR: failed to open configuration file '/private/etc/php-fpm.conf': No such file or directory (2)

    [25-Mar-2016 20:23:28] ERROR: failed to load configuration file '/private/etc/php-fpm.conf'

    [25-Mar-2016 20:23:28] ERROR: FPM initialization failed

    ~

    解决方法: 

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

    又出现了如下错误

    ~php-fpm --fpm-config /usr/local/etc/php/5.6/php-fpm.conf

    [25-Mar-2016 20:24:52] ERROR: failed to open error_log (/usr/var/log/php-fpm.log): No such file or directory (2)

    [25-Mar-2016 20:24:52] ERROR: failed to post process the configuration

    [25-Mar-2016 20:24:52] ERROR: FPM initialization failed

    ~

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

    然后添加,error_log = /usr/local/var/log/php-fpm.log 保存

    ~php-fpm --fpm-config /usr/local/etc/php/5.6/php-fpm.conf

    [25-Mar-2016 20:29:54] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root

    [25-Mar-2016 20:29:54] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root

    [25-Mar-2016 20:29:54] NOTICE: fpm is running, pid 1026

    [25-Mar-2016 20:29:54] NOTICE: ready to handle connections

    参考

    拣点芝麻-Mac Nginx Php配置

    Nginx的网站目录

    配置文件路径: /usr/local/etc/nginx/nginx.conf

    默认的网站根目录: /usr/local/opt//nginx/html

    启动nginx问题

    nginx: [error] open() “/usr/local/var/run/nginx.pid” failed

    参考http://www.inbeijing.org/archives/1290

    ~sudo nginx

    nginx: [warn] 1024 worker_connections exceed open file resource limit: 256

    因为ulimit -n的值为256,所以将/usr/local/etc/nginx/nginx.conf配置文件中的worker_connections设置为256

    相关文章

      网友评论

          本文标题:Mac OS上搭建LNMP开发环境

          本文链接:https://www.haomeiwen.com/subject/pukgkttx.html