Mac apache2, php, mysql 配置

1、启动,停止,重启apache2

sudo apachectl start
sudo apachectl stop
sudo apachectl restart

2、mac php 使用homebrew升级:

      brew tap homebrew/php
      brew install php70 --with-homebrew-apxs --with-apache --with-homebrew-curl --with-homebrew-openssl 

虽然说是升级,其实是重新安装了一个新php,覆盖了之前的指令,系统自带的php仍然存在,只是没法通过命令行启动。

php70 说明php的版本是7.0.*, “--with-homebrew-apxs --with-apache --with-homebrew-curl --with-homebrew-openssl ”必须加上,这样才会有libphp70.so这个文件,这个文件在后面apache2配置需要使用. (具体是哪个指令导致会生成so文件,没有进行测试)

使用以下指令可以测试php版本:

  php -v

3、apache2 配置

再次建议使用mac原装的apache2。使用homebrew安装新的apache2的话, apachectl -v会显示版本更改了, 但localhost采用的仍然是系统自带的apache2。(可能有办法解决这个问题,没有深究下去)

配置文件路径在/private/etc/apache2/httpd.conf

3.1、添加php

将代码添加到httpd.conf底部:

# ====php module====
LoadModule php7_module /usr/local/opt/php70/libexec/apache2/libphp7.so #(brew 安装的php路径)

# 确保能够识别php文件
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
 

DirectoryIndex index.html index.php


3.2、修改root根目录

mac中有两种根目录,一个是系统级的根目录,默认设置在/Library/WebServer/Documents, 另外一个是用户级的根目录

修改系统级root根目录

DocumentRoot "/Users/pandaczm/php_workspace"

修改成你自己之后想要建立php workspace的位置即可。

更改完成之后,在其中建立phpinfo.php,用来测试php是否添加成功:

echo ' /Users/pandaczm/php_workspace/phpinfo.php

重新启动apache2之后,浏览器中输入http://localhost/phpinfo.php,显示出php版本信息:

Mac apache2, php, mysql 配置_第1张图片
QQ20160929-0.png

3.3、修改用户级的根目录

用户级的根目录相对而言就有点麻烦了。
首先建立一个新文件夹~/Sites,使用这个来对应用户级根目录的网址:http://localhost/~username
httpd.conf中找到下面几行:

LoadModule userdir_module libexec/apache2/mod_userdir.so
Include /private/etc/apache2/extra/httpd-userdir.conf

uncomment掉(去除前面的#

然后打开httpd-userdir.conf(位于/private/etc/apache2/extra里面)
uncomment掉:

Include /private/etc/apache2/users/*.conf

/private/etc/apache2/users里面新建一个username.conf, 写入:


AllowOverride All
Options Indexes MultiViews FollowSymLinks
Require all granted

对该文件进行授权:

sudo chmod 755 username.conf

然后重新启动Apache2:

sudo apachectl restart

在浏览器中输入localhost/~username,可以看到:

Mac apache2, php, mysql 配置_第2张图片
QQ20160929-1.png

mysql 安装

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

安装结束之后,查看mysql默认调用的配置文件路径:

mysqld --help --verbose | more

一般情况下,显示出的文件夹,是没有创建配置文件 ,没有配置文件,如果使用mysql_secure_installation会出现错误:Can't connect to local MySQL server through socket '/tmp/mysql.sock'(但有的时候又不会出现这种情况,直接安装好了,就可以直接使用secure配置了。。。不知道为什么)

可以自己创建配置文件(样例在support-files 中):

cp /usr/local/Cellar/mysql/5.7.15/support-files/my-default.cnf /etc/my.cnf

这样结束后之后,就可以使用mysql_secure_installation 记性基本的设置 (不要设置validate password,会很麻烦,密码设置起来蛋疼啊。。。)

参考:

https://coolestguidesontheplanet.com/get-apache-mysql-php-phpmyadmin-working-osx-10-10-yosemite/
https://coolestguidesontheplanet.com/forbidden-403-you-dont-have-permission-to-access-username-on-this-server/
http://iosfen.com/14526754330295.html
https://my.oschina.net/jiec/blog/518590

你可能感兴趣的:(Mac apache2, php, mysql 配置)