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,而后输入刚刚重置的密码。而后登陆成功会跳出提示信息php

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;退出登陆。安装完毕。html

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.confnode

安装php7.0

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

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,而后会提示以下mysql

./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配置文件。
注释完上面所说的两句话后,加上以下配置nginx

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配置为自动启动。git

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将会看到github

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

能够经过ps -aef | grep httpd命令查看apache服务是否启动,若是apache正在运行会看到一些http进程。web

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

修改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

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

相关文章
相关标签/搜索