mac下配置 apache、升级PHP 并安装 MySQL

首先保证你的本机上有安装好了brew,没有请查看本博客其他篇:

——请参考这个网站给出的一条指令,即可安装:https://brew.sh/index_zh-cn

一、启动和配置apache

1、启动:

sudo apachectl start/restart   #启动apache
sudo apachectl stop            #停止apache

——访问127.0.0.1 或者 localhost 应该可以看到 It work,标识成功了。

2、修改默认网址路径:

mac 下的默认网站路径在:/Library/WebServer/Documents,我们修改为/Users/linfeng/project:

vim /etc/apache2/httpd.conf

并设置 Directory 内的参数:Options All、AllowOverride All、Require all granted

DocumentRoot "/Users/linfeng/project"
<Directory "/Users/linfeng/project">
    # Options FollowSymLinks Multiviews
    # MultiviewsMatch Any
    Options All

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Require all granted

</Directory>

3、设置 Directory参数

<Directory />
    AllowOverride All
    Require all granted
</Directory>

4、开启虚拟主机:

vim /etc/apache2/httpd.conf
// 去掉虚拟主机注释,则开启
Include /private/etc/apache2/extra/httpd-vhosts.conf

// 常用:

1、启动和停止:

sudo apachectl start/restart   #启动apache
sudo apachectl stop            #停止apache
ps -aef | grep httpd    #查看httpd进程

2、配置文件位置:

配置文件:/etc/apache2/httpd.conf

虚拟主机:/etc/apache2/extra/httpd-vhosts.conf

错误日志:tail -f /usr/local/var/log/httpd/error_log

二、升级mac的PHP版本从5.6到7.2,并配置 php 与 apache 关联

1、安装php7.2

brew install php     //安装最新版的php
brew install php72    //安装php7.2
brew install php71    //安装php7.1
brew install php56    //安装php5.6
brew reinstall php71    //重装php7.1

2、配置apache和php关联

1、打开apache配置文件
vim /etc/apache2/httpd.conf

2、添加提示的内容:
LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.so
<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

3、重启apache
sudo apachectl restart

4、启动php
brew services start php

5、logout出终端,重新进入,查看php版本
php -v

3、配置默认页面为index.php

// 1、打开apache配置文件
vim /etc/apache2/httpd.conf

// 2、在IfModule dir_module中添加index.php,放在index.html前面
<IfModule dir_module> 
    DirectoryIndex  index.php index.html
</IfModule> 

// 3、重启apache服务
sudo apachectl restart

测试:

echo \'<?php phpinfo(); ?>\' > /Users/linfeng/project/index.php 

——访问localhost,即可访问到php的信息了

三、开启虚拟主机,支持多站点

1、配置虚拟主机:

// 第一步:
open -e /usr/local/etc/httpd/extra/httpd-vhosts.conf

// 第二步:配置test.com站点,加入以下内容
<VirtualHost *:80>
    DocumentRoot "/Users/linfeng/www/test.com"
    ServerName test.com
</VirtualHost>

// 第三步:修改hosts
1)vim /etc/hosts
2)加入:127.0.0.1    test.com 

——重启apache:sudo apachectl restart

——访问test.com即可

四、路由重写,隐藏入口文件index.php

1、在apache 配置文件 httpd.conf 中加载mod_rewrite.so

2、AllowOverride None 改成 AllowOverride All

3、应用入口文件同级目录添加.htaccess文件,设置以下内容:

<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>

五、安装mariadb

1、安装mariadb:

brew install mariadb     //也有可能提示已经有包,需要更新brew upgrade mariadb
cd /usr/local/Cellar/mariadb/10.3.9/bin
mysql_install_db

2、使用以下命令查看mariadb的信息:

brew info mariadb

3、启动和重启mariadb:

mysql.server start  和  mysql.server restart

4、重置root密码和其他操作:

mysql_secure_installation    //会有修改密码,重置权限等操作

5、登录mariadb:

mysql -u root -pxxxxxx

6、验证mariadb版本:

select @@version; 

基础命令:

// 显示数据库列表
show databases;

// 切换到名为mysql的数据库,显示该库中的数据表
use mysql; 
show tables;

// 显示数据表table的结构
desc table;

// 建数据库A与删数据库A
create database `database_A`; 
drop database `database_A`;

// 建表:
use database_A; 
create table table_A(字段列表); 
drop table table_A;

// 显示表中的记录:
select * from table_A;

-- 清空表中记录:
delete from table_A; 

附注命令:

// 查看端口占用
sudo lsof -i:80
// 多使用 brew 启动,brew会随机启动,不需要开启随机启动。 // 关闭随机启动 sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist // 开启随机启动 sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist // 启动 apache sudo apachectl start // 重启 Apache sudo apachectl restart // 停止 apache sudo apachectl stop // 启动 mariadb mysql.server start //该命令每次电脑重启都需要执行
brew services start mariadb // 该命令可跟随系统启动。 // 停止 mariadb mysql.server stop brew service stop mariadb
// 启动 php brew services start php // 停止 php brew services stop php