Mac Os下安装MNMP(Mac+NGINX+MySQL+PHP)

目录

一、准备工作

  • 1 创建目录
  • 2 添加用户
  • 3 下载需要的文件

二、MySQL安装

  • 1 编译安装MySQL
  • 2 后续操作
  • 3 设置MySQL开机自启动
  • 4 添加MySQL开启/关闭脚本

三、NGINX安装

  • 1 安装准备
  • 2 编译安装NGINX
  • 3 设置NGINX开机自启动
  • 4 添加NGINX开启/关闭脚本

四、PHP安装

  • 1 安装准备
  • 2 编译安装PHP
  • 3 设置PHP开机自启动
  • 4 添加PHP开启/关闭脚本
  • 5 修改配置文件
  • 6 配置NGINX支持PHP

五、测试

六、参考资料

准备工作

mac版本10.12.6

创建目录

/apps/pkg  #软件源代码包存放位置
/apps/env  #源码包编译安装位置
/logs  #相关日志存放位置
/apps/env/mysql  #MySQL安装目录
/apps/data/mysql  #MySQL数据存储目录
/etc/mysql  #MySQL配置文件

添加用户

#MySQL用户及用户组
sudo dscl . -create /Groups/mysql  #添加mysql用户组
sudo dscl . -create /Users/mysql  #添加mysql用户
sudo dscl . -append /Groups/mysql GroupMembership mysql  #将mysql用户加入mysql用户组
#www用户及用户组(用于NGINX及PHP)
sudo dscl . -create /Groups/www  #添加www用户组
sudo dscl . -create /Users/www  #添加www用户
sudo dscl . -append /Groups/www GroupMembership www  #将www用户加入www用户组

sudo chown www:www /apps/www/ -R #设置目录所有者
sudo chmod 700 /apps/www/ -R #设置目录权限

下载需要的文件

1、下载nginx http://nginx.org/download/nginx-1.14.2.tar.gz
2、下载MySQL http://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.21.tar.gz
3、下载php http://cn2.php.net/distributions/php-5.6.3.tar.gz
4、下载pcre (支持nginx伪静态) https://sourceforge.net/projects/pcre/files/pcre/8.36/pcre-8.36.tar.gz/download
5、下载openssl(nginx扩展) http://www.openssl.org/source/openssl-1.0.1j.tar.gz
6、下载zlib(nginx扩展) http://zlib.net/zlib-1.2.8.tar.gz
7、下载cmake(MySQL编译工具) http://www.cmake.org/files/v3.0/cmake-3.0.2.tar.gz
8、下载libmcrypt(php扩展) http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
9、下载yasm(php扩展) http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
10、t1lib(php扩展) https://merges.ubuntu.com/t/t1lib/
11、下载gd库安装包 https://github.com/libgd/libgd/archive/gd-2.2.5.tar.gz
12、下载fontconfig安装包(gd库需要)https://www.freedesktop.org/software/fontconfig/release/fontconfig-2.4.2.tar.gz
13、tiff(gd库需要) http://download.osgeo.org/libtiff/tiff-4.0.3.tar.gz
https://sourceforge.net/projects/libpng/files/libpng16/1.6.36/libpng-1.6.36.tar.gz/download
15、freetype(gd库需要) http://ring.u-toyama.ac.jp/archives/graphics/freetype/freetype2/freetype-2.5.4.tar.gz
16、jpegsrc(gd库需要) http://www.ijg.org/files/jpegsrc.v9a.tar.gz

二、MySQL安装

编译安装MySQL

tar zxvf mysql-5.6.21.tar.gz  #解压
cd mysql-5.6.21
sudo cmake . -DCMAKE_INSTALL_PREFIX=/apps/env/mysql -DMYSQL_DATADIR=/apps/data/mysql -DSYSCONFDIR=/etc/mysql
sudo make
sudo make install

后续操作

cd /apps/env/mysql  #进入mysql安装目录
sudo ./scripts/mysql_install_db --user=mysql --basedir=/apps/env/mysql --datadir=/apps/data/mysql  #生成mysql系统数据库
sudo vim /apps/env/mysql/my.cnf  #设置一下mysql服务生成的pid文件路径
[mysqld]
pid-file = /apps/data/mysql/mysqld.pid
log-error = /apps/data/mysql/mysqld.err
:wq  #保存退出
ln -s /apps/env/mysql/my.cnf /etc/mysql/my.cnf  #添加到/etc/mysql目录的软连接

设置MySQL开机自启动

sudo vi /Library/LaunchDaemons/com.mysql.mysql.plist  #创建mysql启动文件


 
   
     KeepAlive 
      Label 
      com.mysql.mysqld 
      ProgramArguments     
                  
        /apps/env/mysql/bin/mysqld_safe                
        --user=mysql
        --datadir=/apps/data/mysql
        --pid-file=/apps/data/mysql/mysqld.pid
        --log-error=/apps/data/mysql/mysqld.err
          
      
      LaunchOnlyOnce
  

加载自启动文件

sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysql.plist

加载启动文件可能失败情况

执行launchctl 的时候,出现Dubious ownership on file (skipping) 这个错误
原因是:这个plist文件必须是属于root用户,wheel组,用chown修改

sudo chown root:wheel com.mysql.plist  

添加MySQL开启/关闭脚本

sudo vim /etc/init.d/mysqld  #创建mysql服务启动文件
#!/bin/sh
. /etc/rc.common

MYSQL_PATH=/apps/env
DESC="mysqld daemon"
NAME=mysql.server
#mysql_server路径
DAEMON=$MYSQL_PATH/mysql/support-files/$NAME

# The start subroutine
StartService() {
    echo -n "Starting $DESC: $NAME"
    $DAEMON start
    echo "."

}
# The stop subroutine
StopService() {
    echo -n "Stopping $DESC: $NAME"
    $DAEMON stop
    echo "."

}
# The restart subroutine
RestartService() {
    echo -n "Restart $DESC configuration..."
    $DAEMON restart
    echo "restart"
}
RunService "$1"
sudo chmod +x /etc/init.d/mysqld #给该脚本添加执行权限
该脚本操作命令如下:

mysql服务启动 sudo /etc/init.d/mysqld start
mysql服务关闭 sudo /etc/init.d/mysqld stop
mysql服务重启 sudo /etc/init.d/mysqld restart

三、NGINX安装

安装准备

安装PCRE

cd /apps/pkg
sudo mkdir /apps/env/pcre
sudo tar zxvf pcre-8.37.tar.gz
cd pcre-8.37
sudo ./configure --prefix=/apps/env/pcre
sudo make
sudo make install

安装OpenSSL

cd /apps/pkg
sudo mkdir /apps/env/openssl
sudo tar zxvf openssl-1.0.1j.tar.gz
cd openssl-1.0.1j
export KERNEL_BITS=64
sudo ./Configure darwin64-x86_64-cc -prefix=/apps/env/openssl
sudo make
sudo make install
sudo vi /etc/profile
export PATH=$PATH:/usr/local/openssl/bin
:wq!
source /etc/profile

安装zlib

cd /apps/pkg
sudo mkdir /apps/env/zlib
sudo tar zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
sudo ./configure --prefix=/apps/env/zlib
sudo make
sudo make install

编译过程中时候出现了一个情况,装OpenSSL的时候,该软件默认安装的是32位,忘记指定系统版本,导致编译过程出现了如下错误:

/#bin/sh: 28: cannot create .../.openssl/ssl/man/man3/hmac.3: Too many levels of symbolic links

这里补充一下,换成64位的步骤
1、删除已安装的openssl目录 sudo rm -rf /apps/env/openssl
2、进入OpenSSL安装包 cd /apps/pkg/openssl-1.0.1j
3、export KERNEL_BITS=64
4、sudo ./Configure darwin64-x86_64-cc -prefix=/apps/env/openssl
5、sudo make
6、sudo make install

编译安装NGINX

cd /apps/pkg
sudo mkdir /apps/env/nginx
sudo tar zxvf nginx-1.14.2.tar.gz
cd nginx-1.14.2
sudo ./configure --prefix=/apps/env/zlib
sudo ./configure --prefix=/apps/env/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/apps/pkg/openssl-1.0.1j --with-zlib=/apps/pkg/zlib-1.2.11 --with-pcre=/apps/pkg/pcre-8.36 --error-log-path=/logs/nginx/error.log --http-log-path=/logs/nginx/access.log
sudo make
sudo make install

由于一开始编译的openssl版本是32位的,然后configure后发现以下提示

解决方法:

sudo vim /apps/pkg/nginx-1.14.2/objs/Makefile
找到: ./config --prefix=xxx.openssl no-shared (注释:XXX是已的openssl路径,对于本文为 ./Config darwin64-i386-cc --prefix=/apps/pkg/openssl-1.0.1j/.openssl no-shared no-threads )
把该段的 ./config 改成 ./Configure darwin64-x86_64-cc 其他后面参数不变,保存
:wq
sudo make
sudo make install

设置NGINX开机自启动

sudo vi /Library/LaunchDaemons/com.nginx.plist #创建NGINX启动文件
 
 
 
   
        
        Label 
        com.nginx
        KeepAlive 
        RunAtLoad
        
        ProgramArguments 
         
          /apps/env/nginx/sbin/nginx 
          
          -c 
          /apps/env/nginx/conf/nginx.conf 
         
       
       StandardOutPath
       /logs/nginx/run.log
       
       StandardErrorPath
       /logs/nginx/run.err
       LaunchOnlyOnce
   

加载自启动文件

sudo launchctl load -w /Library/LaunchDaemons/com.nginx.plist

添加NGINX开启/关闭脚本

sudo vim /etc/init.d/nginx  #创建脚本
#!/bin/sh
. /etc/rc.common

NGINX_PATH=/apps/env
DESC="nginx daemon"
NAME=nginx

#nginx路径
DAEMON=$NGINX_PATH/nginx/sbin/$NAME
#配置文件路径
CONFIGFILE=$NGINX_PATH/nginx/conf/nginx.conf
# The start subroutine
StartService() {
    echo -n "Starting $DESC: $NAME"
    $DAEMON -c $CONFIGFILE
    echo "."
}
# The stop subroutine
StopService() {
    echo -n "Stopping $DESC: $NAME"
    $DAEMON -s stop
    echo "."
}

# The restart subroutine
RestartService() {
    echo -n "Reloading $DESC configuration..."
    $DAEMON -s reload
    echo "."
}

RunService "$1"
sudo chmod +x /etc/init.d/nginx #给该脚本添加执行权限
该脚本操作命令如下:

nginx服务启动 sudo /etc/init.d/nginx start
nginx服务关闭 sudo /etc/init.d/nginx stop
nginx服务平滑重启 sudo /etc/init.d/nginx restart

四、PHP安装

安装准备

安装yasm

cd /apps/pkg
sudo tar zxvf yasm-1.3.0.tar.gz
sudo cd yasm-1.3.0
sudo ./configure
sudo make
sudo make install

安装libmcrypt

cd /apps/pkg
sudo tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
sudo ./configure
sudo make
sudo make install

安装libvpx

brew install libvpx
安装路径为/usr/local/Cellar/libvpx

安装tiff

cd /apps/pkg
sudo tar zxvf tiff-4.0.4.tar.gz
sudo cd tiff-4.0.4
sudo ./configure
sudo make
sudo make install

安装libpng

cd /apps/pkg
sudo tar zxvf libpng-1.6.36.tar.gz
sudo cd libpng-1.6.36
sudo ./configure
sudo make
sudo make install

安装freetype

cd /apps/pkg
sudo tar zxvf freetype-2.5.4.tar.gz
sudo cd freetype-2.5.4
sudo ./configure
sudo make
sudo make install

安装jpeg

cd /apps/pkg
sudo tar zxvf jpegsrc.v9a.tar.gz
sudo cd jpeg-9a
sudo ./configure
sudo make
sudo make install

安装fontconfig

cd /apps/pkg
sudo mkdir /apps/env/fontconfig
sudo tar zxvf fontconfig-2.4.2.tar.gz
sudo cd fontconfig-2.4.2
sudo ./configure --prefix=/apps/env/fontconfig
sudo make
sudo make install

安装gd库依赖X11,也就是libxpm库,可是mac这个版本找不到,mac从lion版本已经抛弃并且继承在了XQuartz中了,我们需要下载dmg安装,最后把inclue目录引入进来。

X11代替方案:下载安装 https://dl.bintray.com/xquartz/downloads/XQuartz-2.7.11.dmg

安装libgd

cd /apps/pkg
sudo mkdir /apps/env/libgd
sudo tar zxvf libgd-gd-2.2.5.tar.gz
sudo cdl ibgd-gd-2.2.5
sudo ./configure --prefix=/apps/env/libgd --with-jpeg=/apps/env/jpeg --with-png=/apps/env/libpng --with-freetype=/apps/env/freetype --with-fontconfig=/apps/env/fontconfig --with-xpm=/usr --with-tiff=/apps/env/tiff
还是configure不通过出现了如下信息:


image
猜测,需要如下依赖

brew install libtool
brew install gettext
brew install pkg-config
重新编译
sudo ./configure --prefix=/apps/env/libgd --with-jpeg=/apps/env/jpeg --with-png=/apps/env/libpng --with-freetype=/apps/env/freetype --with-fontconfig=/apps/env/fontconfig --with-xpm=/usr --with-tiff=/apps/env/tiff
sudo make
sudo make install

此处,补充一个信息,希望可以参考,一开始编译安装gd2.1.0过程中时候出现如下错误提示:

webpimg.c:716:16: error: use of undeclared identifier 'PLANE_Y' img.planes[PLANE_Y] = (uint8)(Y);
^webpimg.c:717:16: error: use of undeclared identifier 'PLANE_U' img.planes[PLANE_U] = (uint8
)(U);
^webpimg.c:718:16: error: use of undeclared identifier 'PLANE_V' img.planes[PLANE_V] = (uint8*)(V);
^webpimg.c:719:16: error: use of undeclared identifier 'PLANE_Y' img.stride[PLANE_Y] = y_stride;
^webpimg.c:720:16: error: use of undeclared identifier 'PLANE_U' img.stride[PLANE_U] = uv_stride;
^webpimg.c:721:16: error: use of undeclared identifier 'PLANE_V' img.stride[PLANE_V] = uv_stride;
找了好久的解决方案,后面发现这是该版本的bug,需要找补丁,此处直接更换了成了gd-2.2.5版本

编译安装php

cd /apps/pkg
sudo mkdir /apps/env/php
sudo tar zxvf php-5.6.3.tar.gz
cd php-5.6.3
sudo ./configure \
--prefix=/apps/env/php --with-config-file-path=/apps/env/php/lib \ 
--with-mysql=/apps/env/mysql --with-mysqli=/apps/env/mysql/bin/mysql_config \
--with-mysql-sock=/tmp/mysql.sock --with-pdo-mysql=/apps/env/mysql \
 --with-mysql --with-mysqli --with-gd=/apps/env/libgd --enable-fpm --enable-pdo \
--with-curl --disable-debug --disable-rpath --enable-inline-optimization \
--with-bz2 --enable-pcntl --with-mhash --enable-zip \
--with-libzip=/usr/local/Cellar/libzip --with-pcre-regex --enable-mbregex \
--with-xpm-dir=/opt/X11/ --with-vpx-dir=/usr/local/Cellar/libvpx \
--with-zlib-dir=/apps/env/zlib --with-t1lib=/apps/env/t1lib --with-mcrypt \
--enable-mbstring --enable-gd-native-ttf --with-gettext --enable-sockets \
--enable-sysvsem --enable-ftp --enable-session --with-png-dir=/apps/env/libpng \
--with-jpeg-dir=/apps/env/jpeg --with-freetype-dir=/apps/env/freetype \
--with-iconv --enable-libxml --enable-xml --with-xmlrpc --enable-opcache \
--enable-soap --without-pear --enable-ctype --with-openssl=/apps/env/openssl
出现以下提示

configure: error: Cannot locate header file libintl.h

解决方案参考configure: error: Cannot locate header file libint:

sudo make

出现如下错误

php-5.6.8/ext/zip/lib/zipint.h:118:2: error:#error unsupported size of off_t
make: *** [ext/zip/lib/zip_add.lo] 错误1

安装libzip

brew install libzip
sudo make

sudo install_name_tool -id /apps/env/mysql/lib/libmysqlclient.18.dylib /apps/env/mysql/lib/libmysqlclient.dylib

导致这个编译问题的原因是变量HAVE_DNS_SEARCH和HAVE_RES_NSEARCH在PHP和MySQL中的定义冲突。

sudo make
sudo make install

后续操作
sudo cp php.ini-production /apps/env/php/etc/php.ini #复制php配置文件到安装目录
sudo rm -rf /etc/php.ini.default  #删除系统自带配置文件
sudo ln -s /apps/env/php/etc/php.ini /etc/php.ini #添加软链接到 /etc目录
sudo mv /apps/env/php/etc/php-fpm.conf.default  /apps/env/php/etc/php-fpm.conf#拷贝模板文件为php-fpm配置文件
ln -s  /apps/env/php/etc/php-fpm.conf /etc/php-fpm.conf #添加软连接到 /etc目录
sudo vi /apps/env/php/etc/php-fpm.conf #编辑
user = www #设置php-fpm运行账号为www

group = www #设置php-fpm运行组为www

pid = run/php-fpm.pid #取消前面的分号

:wq! #保存退出

设置 php-fpm开机启动

sudo vi /Library/LaunchDaemons/com.php-fpm.plist




      
        
        Label
        com.php-fpm
         KeepAlive
        RunAtLoad
          
        ProgramArguments
        
            /apps/env/php/sbin/php-fpm
            
            -y
            /apps/env/php/etc/php-fpm.conf
             -c
            /apps/env/php/etc/php.ini
        
        
        StandardOutPath
        /logs/php/run.log
        
        StandardErrorPath
        /logs/php/run.err
       LaunchOnlyOnce
      


加载

添加PHP开启/关闭脚本

sudo vim /etc/init.d/php-fpm  #创建脚本
#!/bin/sh
. /etc/rc.common

PHP_PATH=/apps/env/php
DESC="php-fpm daemon"
NAME=php-fpm
#php-fpm路径
DAEMON=$PHP_PATH/sbin/$NAME

#配置文件路径
CONFIGFILE=$PHP_PATH//etc/php-fpm.conf
CONFIG_INI_FILE=$PHP_PATH/etc/php.ini
#PID文件路径(在php-fpm.conf设置)
PIDFILE=$PHP_PATH/var/run/$NAME.pid
rh_start() {
  $DAEMON -y $CONFIGFILE -c $CONFIG_INI_FILE || echo -n " already running"
}
rh_stop() {
  kill -QUIT `cat $PIDFILE` || echo -n " not running"
}
rh_reload() {
  kill -USR2 `cat $PIDFILE` || echo -n " can't reload"
}
# The start subroutine
StartService() {
    echo -n "Starting $DESC: $NAME"
    rh_start
    echo "."

}

# The stop subroutine
StopService() {
    echo -n "Stopping $DESC: $NAME"
    rh_stop
    echo "."

}

# The restart subroutine
RestartService() {
    echo -n "Reloading $DESC configuration..."
    rh_reload
    echo "reloaded."
}

RunService "$1"
sudo chmod +x /etc/init.d/php-fpm #给该脚本添加执行权限
该脚本操作命令如下:

php-fpm服务启动 sudo /etc/init.d/php-fpm start
php-fpm服务关闭 sudo /etc/init.d/php-fpm stop
php-fpm服务平滑重启 sudo /etc/init.d/php-fpm restart

修改配置文件

sudo vi /apps/env/php/etc/php.ini #编辑配置文件

找到:disable_functions =修改为:disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroupths,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname

列出PHP可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。
找到:;date.timezone =
修改为:date.timezone =  Asia/Shanghai #设置时区
找到:expose_php = On
修改为:expose_php = Off #禁止显示php版本的信息
找到:short_open_tag = Off
修改为:short_open_tag = ON #支持php短标签
找到opcache.enable=0
修改为opcache.enable=1 #php支持opcode缓存
找到:;opcache.enable_cli=0 
修改为:opcache.enable_cli=1 
添加:zend_extension=opcache.so #开启opcode缓存功能
 :wq! #保存退出

配置NGINX支持PHP

sudo vim /apps/env/nginx/conf/nginx.conf #配置

修改user www www; #首行user去掉注释,修改Nginx运行组为www www;
必须与/apps/env/php/etc/php-fpm.conf中的user,group配置相同,否则php运行出错
index index.html index.htm index.php; #添加index.php

pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000#
location ~ \.php$ {
     root /apps/www;
     fastcgi_pass 127.0.0.1:9000;
     fastcgi_index index.php;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     include fastcgi_params;
}
:wq

sudo /etc/init.d/nginx restart #重启nginx

五、测试

sudo vim /apps/www/test_php.php

>:wq

在浏览器中打开服务器IP地址,会看到phpinfo 内容

六、参考资料

Mac下Nginx无法关闭的问题
https://blog.csdn.net/danssion/article/details/86585037
如何提高工作效率 - 自动化篇
https://juejin.im/post/5bfac61ee51d454af013a900
Undefined symbols for architecture x86_64: _res_9_init
http://www.opensoce.com/3308.html
关于php-fpm与nginx进程重载的坑
https://www.cnblogs.com/wpjamer/articles/6819485.html
Daemons and Services Programming Guide
https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/StartupItems.html
configure: error: Cannot locate header file libintl.h 错误的解决方法
https://blog.csdn.net/fafa211/article/details/48807625
| Bug #68302 | impossible to compile php with zip support |
https://bugs.php.net/bug.php?id=68302
mac 编译安装php5.6.40
https://www.jianshu.com/p/095c46149bbe
nginx主模块指令
https://blog.csdn.net/aspnet_lyc/article/details/47209873
mac 10.10.4 编译nginx1.9.5遇到的问题
https://blog.csdn.net/wgw335363240/article/details/50432979
Mac 下利用 Launchctl 自启动 mysql
https://www.cnblogs.com/mrcrazy/p/4205223.html
MySQL CMake参数说明手册
https://www.cnblogs.com/martinzhang/p/3455681.html
nginx主模块指令
https://blog.csdn.net/aspnet_lyc/article/details/47209873
mac 10.10.4 编译nginx1.9.5遇到的问题
https://blog.csdn.net/wgw335363240/article/details/50432979
Mac 下利用 Launchctl 自启动 mysql
https://www.cnblogs.com/mrcrazy/p/4205223.html
Linux下安装Lnmp环境之准备篇(一)
https://blog.csdn.net/qq_27968607/article/details/55270774
MySQL CMake参数说明手册
https://www.cnblogs.com/martinzhang/p/3455681.html

你可能感兴趣的:(Mac Os下安装MNMP(Mac+NGINX+MySQL+PHP))