zoukankan      html  css  js  c++  java
  • mac配置php7运行环境

    不用mac自带的apache和php,安装自己想要的版本。配置过程一直采坑,需要有闲时间和好的心理素质才行,哈哈,因为网上很教程都有纰漏之处,所以先把采坑无数后发现的个人认为最好的一个教程链接放在这里

    https://github.com/nodejh/nodejh.github.io/issues/25下面记录我的采坑过程。

    安装mysql

    官网https://www.mysql.com/downloads/下载mysql,有问题百度。下载后安装,安装过程中会有一个临时密码的提示,要注意保存一下,后面会用到。安装完毕进入系统偏好设置在最下面找到mysql图标并打开,点击Start MySQL Server启动mysql。命令行cd到/usr/local/mysql/bin,分别执行alias mysql=/usr/local/mysql/bin/mysqlalias mysqladmin=/usr/local/mysql/bin/mysqladmin,这两条命令是为了方便直接打开 iTerm 就可以运行mysql命令,而不是必须进入mysql安装目录才能运行。接下来,重置密码。执行命令mysqladmin -u root -p password ******是你的新密码。回车后提示输入密码,此时数次刚刚保存的临时密码,我的是)6m1d/m<joVi。
    此时提示mysqladmin: [Warning] Using a password on the command line interface can be insecure.Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.说明已经重置密码成功。我在这里浪费了很多时间,以为这是错误提示,其实已经ok了。
    登录mysql。命令mysql -u root -p,然后输入刚刚重置的密码。然后登录成功会跳出提示信息

    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 1032
    Server version: 5.7.19 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2017, 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.
    

    大意是说sql命令以;结尾,当前mysql版本是5.7.19。另外我们也可以通过sql命令select version();来查看版本。用exit;退出登录。安装完毕。

    apache使用mac自带的

    (开始的时候想用mac自带apache来着,后来和配置php7时候各种问题,放弃了使用mac自带apache)
    基本用法的命令如下:
    apachectl -v查看版本,提示Server version: Apache/2.4.25 (Unix)
    sudo apachectl start启动
    sudo spachectl stop停止
    sudo apachectl restart重启
    相关目录:
    apache日志/private/var/log/apache2/error_log
    apache配置主文件/etc/apache2/httpd.conf
    apache的vhost配置/etc/apache2/extra/httpd-vhost.conf

    安装php7.0

    参考博客http://www.cnblogs.com/redirect/p/6131751.html
    添加brew的php扩展库(mac上没有php的包,需要绑定其他人的git仓库,命令brew tap github_user/repo),执行如下命令

    brew update
    brew tap homebrew/dupes
    brew tap homebrew/php
    

    可以使用brew options php70来查看安装php的选项。注意如果使用apache作为web server,安装php命令要加--with-apache选项,如果使用nginx作为web server,安装php命令要加--with-fpm选项,我的安装命令和选项brew install php70 --with-apache --width-apxs2 --with-gmp --with-imap --with-tidy --with-debug,然后我按照教程http://www.cnblogs.com/redirect/p/6131751.html上执行brew link php70开启php7.0的进程,但是查看php -v提示版本还是php5.6,执行brew unlink php56来关闭php5.6的进程但是报错提示Error: No such keg: /usr/local/Cellar/php56,反复试了很多次耽误了时间,然后我查其他资料,执行brew install php-version安装了php-version来切换php版本,php-version是一个帮助管理从brew安装的php版本切换的工具。安装完执行php-veision 7.0.22切换版本,然后php -v版本果然切换到php7.0.22了。
    下面开始修改apache配置文件。又遇见很耗时的一个坑。
    按照上面的教程,打开apache配置文件sudo vi /etc/apache2/httpd.conf,不加sudo权限不够没办法修改,注释(前面加#)掉LoadModule php5_module libexec/apache2/libphp5.soInclude /private/etc/apache2/other/*.conf两句,然后加上LoadModule php7_module /usr/local/opt/php70/libexec/apache2/libphp7.so这句,注意,libphp7.so的地址是对应自己电脑文件所在的地址,于是我去检查下我的libphp7.so文件是不是上面命令中的地址。结果发现/usr/local/opt/php70/下并没有libphp7目录,搞了半天不知道怎么整,于是brew uninstall php7.0.22卸载php7.0,卸载后/usr/local/opt/下面没有了php70文件夹,然后重新安装php7.0,并且又加了一个选项--httpd24,因为看其他教程里有加。brew install php70 --with-apxs2 --with-apache --with-gmp --with-imap --with-tidy --with-debug --with-httpd24,然后会提示如下

    ./configure --prefix=/usr/local/Cellar/php70/7.0.22_14 --localstatedir=/usr/local/var --sysconfdir=/usr/local/etc/php/7.0 --with-config-file-path=/usr/local/etc/php/7.0 --with-config-file-scan-dir=/us
    Last 15 lines from /Users/yanhaoqi/Library/Logs/Homebrew/php70/01.configure:
    checking for Kerberos support... /usr
    checking whether to use system default cipher list instead of hardcoded value... no
    checking for krb5-config... /usr/bin/krb5-config
    checking for RAND_egd... no
    checking for pkg-config... no
    checking for OpenSSL version... >= 0.9.8
    checking for CRYPTO_free in -lcrypto... yes
    checking for SSL_CTX_set_ssl_version in -lssl... yes
    checking for PCRE library to use... bundled
    checking whether to enable PCRE JIT functionality... yes
    checking whether to enable the SQLite3 extension... yes
    checking bundled sqlite3 library... yes
    checking for ZLIB support... yes
    checking if the location of ZLIB install directory is defined... no
    configure: error: Cannot find libz
    

    其实前面最开始安装php7的时候记得也有这个提示,当时也没有在意。这时候执行php -v提示版本是php5.6,执行php-version提示Sorry, but you do not seem to have any PHP versions installed.,折腾半天后来查到资料,需要执行xcode-select --install,然后按照提示去安装,安装完毕后再重新装php7.0,brew install php70 --with-apxs2 --with-apache --with-gmp --with-imap --with-tidy --with-debug --with-httpd24,这次没有提示configure: error: Cannot find libz,然后查看版本是7.0,php-version也提示7.0.22,并且找到了/usr/local/opt/php70/libexec/apache2/libphp7.so文件。
    ok,继续apache配置文件。
    注释完上面所说的两句话后,加上如下配置

    LoadModule php7_module /usr/local/opt/php70/libexec/apache2/libphp7.so
     
    <FilesMatch .php$>
        SetHandler application/x-httpd-php
    </FilesMatch>
     
    <IfModule php7_module>
        AddType application/x-httpd-php .php
        AddType application/x-httpd-php-source .phps
        <IfModule dir_module>
            DirectoryIndex index.html index.php
        </IfModule>
    </IfModule>
    

    #ServerName www.example.com:80后面加上ServerName localhost:80,否则下面重启apache的时候回报错提示AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using ::1. Set the 'ServerName' directive globally to suppress this message
    这是个更大的坑。我卡在这里将近两天,各种博客资料查询各种尝试。曲折过程已经忘记,现在只记录下结果。
    我在上面写apache用mac自带的,版本是Apache/2.4.25 (Unix),但是后面经过我安装php7的各种折腾后,记不清在什么时候我通过brew安装了Apache/2.4.27我想执行brew uninstall httpd24卸载掉但是提示Refusing to uninstall /usr/local/Cellar/httpd24/2.4.27 because it is required by php70 7.0.22_14, which is currently installed.,php7引用了Apache/2.4.27。而且,brew安装的Apache/2.4.27,配置文件所在目录/usr/local/etc/apache2/2.4/httpd.conf,其编译安装的目录/usr/local/Cellar/httpd24/2.4.27。oh my god!!!上面修改apache配置弄了这么长时间,配置的文件根本就不是正确的文件!
    apache配置的正确姿势
    执行命令使用管理员权限为通过brew安装的apache配置为自动启动。

    sudo cp -v  /usr/local/Cellar/httpd24/2.4.27/homebrew.mxcl.httpd24.plist /Library/LaunchDaemons
    sudo chown -v root:wheel /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist
    sudo chmod -v 644 /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist
    sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist
    

    我的apache编译安装路径是/usr/local/Cellar/httpd24/2.4.27,如果你的路径不是这个,自行替换就好了。
    此时浏览器访问localhost将会看到

     
    屏幕快照 2017-09-07 下午4.57.36.png

    可以通过ps -aef | grep httpd命令查看apache服务是否启动,如果apache正在运行会看到一些http进程。

    sudo vi /usr/local/etc/apache2/2.4/httpd.conf

    修改php模块配置及apache端口

    LoadModule php7_module /usr/local/Cellar/php70/7.0.22_14/libexec/apache2/libphp7.so替换为LoadModule php7_module /usr/local/Cellar/php70/7.0.22_14/libexec/apache2/libphp7.so
    找到代码块

    <IfModule dir_module>
        DirectoryIndex index.html
    </IfModule>
    

    替换为

    <IfModule dir_module>
        DirectoryIndex index.php index.html
    </IfModule>
    
    <FilesMatch .php$>
        SetHandler application/x-httpd-php
    </FilesMatch>
    

    修改ServerName为ServerName localhost:8080
    Listen:8080 apache监听了8080端口,如果修改的话直接改端口号即可。

    修改web服务的根目录在自己用户主目录下方便开发

    找到DocumentRoot "/usr/local/var/www/htdocs"修改为DocumentRoot "/Users/yanhaoqi/sites"
    修改下面的<Directory><Directory "/Users/yanhaoqi/sites">
    修改AllowOverride NoneAllowOverride All
    取消注释,使用mod_rewrite模块 LoadModule rewrite_module libexec/mod_rewrite.so

    修改用户和用户组

    修改User和Group如下

    User yanhaoqi
    Group staff
    

    用户主目录下新建sites目录

    cd /Users/yanhaoqi
    mkdir sites
    sudo vi sites/index.html
    sudo vi sites/info.php
    

    在新建的index.html中编辑<h1>yanhaoqi web root</h1>
    在新建的info.php中编辑

    <?php
        phpinfo();
    ?>
    

    保存退出后重启apachesudo apachectl restart
    访问localhost:8080

     
    屏幕快照 2017-09-07 下午5.32.28.png

    访问localhost:8080/info.php

     
    屏幕快照 2017-09-07 下午5.36.30.png

    大功告成!几乎热泪盈眶!!!

  • 相关阅读:
    【JUC源码解析】AQS
    【JUC源码解析】Exchanger
    圣杯布局和双飞翼布局的理解和区别
    右侧固定宽度,左侧自适应屏幕的布局(笔试题)
    CSS和CSS3中的伪元素和伪类(总结)
    用CSS创建小三角形问题(笔试题常考)
    Pycharm下载安装和使用
    PIC33单片机中断服务函数写法(其他单片机同样适用)
    记录,ubuntu16.04将gitlab迁移到docker上遇到的一些问题与解决方法
    【算法】 算法效率
  • 原文地址:https://www.cnblogs.com/it-tsz/p/9311764.html
Copyright © 2011-2022 走看看