本文主要介绍在 CentOS 系统中搭建 Apache、PHP、MariaDB 环境。

博主博客

附录: 如果需要安装 MySQL, 把 MariaDB 替换成 mysql 即可。

[admin@localhost Desktop]$ yum list | grep mysql
[admin@localhost Desktop]$ sudo yum install mysql mysql-server -y
[admin@localhost Desktop]$ rpm -qa | grep mysql

一、安装 MariaDB

  1. 首先查看yum里面是否包含MariaDB的包。
[admin@localhost Desktop]$ yum list | grep mariadb
mariadb.x86_64                              1:5.5.60-1.el7_5           @base    
mariadb-libs.x86_64                         1:5.5.60-1.el7_5           @base    
mariadb-bench.x86_64                        1:5.5.60-1.el7_5           base     
mariadb-devel.i686                          1:5.5.60-1.el7_5           base     
mariadb-devel.x86_64                        1:5.5.60-1.el7_5           base     
mariadb-embedded.i686                       1:5.5.60-1.el7_5           base     
mariadb-embedded.x86_64                     1:5.5.60-1.el7_5           base     
mariadb-embedded-devel.i686                 1:5.5.60-1.el7_5           base     
mariadb-embedded-devel.x86_64               1:5.5.60-1.el7_5           base     
mariadb-libs.i686                           1:5.5.60-1.el7_5           base     
mariadb-server.x86_64                       1:5.5.60-1.el7_5           base     
mariadb-test.x86_64                         1:5.5.60-1.el7_5           base

2.进行安装。

[admin@localhost Desktop]$ sudo yum install mariadb mariadb-server -y

3.查看是否安装成功。

[admin@localhost Desktop]$ rpm -qa | grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
mariadb-server-5.5.60-1.el7_5.x86_64
mariadb-5.5.60-1.el7_5.x86_64

4.常用命令。

# 查看 mariadb 安装位置
[admin@localhost Desktop]$ rpm -ql mariadb
# 设置开机自动启动
[admin@localhost Desktop]$ systemctl enable mariadb
# 开启mariadb服务
[admin@localhost Desktop]$ systemctl start mariadb
# 关闭 mariadb 服务
[admin@localhost Desktop]$ systemctl stop mariadb
# 重启mariadb服务
[admin@localhost Desktop]$ systemctl restart mariadb

5.使用数据库。

[admin@localhost Desktop]$ mysql -u root -p

二、安装Apache组件

1.首先查看yum里面是否包含httpd的包。

[admin@localhost Desktop]$ yum list | grep httpd
httpd.x86_64                                2.4.6-89.el7.centos.1      updates  
httpd-devel.x86_64                          2.4.6-89.el7.centos.1      updates  
httpd-manual.noarch                         2.4.6-89.el7.centos.1      updates  
httpd-tools.x86_64                          2.4.6-89.el7.centos.1      updates  
keycloak-httpd-client-install.noarch        0.6-3.el7                  base     
libmicrohttpd.i686                          0.9.33-2.el7               base     
libmicrohttpd.x86_64                        0.9.33-2.el7               base     
libmicrohttpd-devel.i686                    0.9.33-2.el7               base     
libmicrohttpd-devel.x86_64                  0.9.33-2.el7               base     
libmicrohttpd-doc.noarch                    0.9.33-2.el7               base     
python2-keycloak-httpd-client-install.noarch

2.进行安装。

[admin@localhost Desktop]$ sudo yum install httpd -y

3.查看是否安装成功。

[admin@localhost Desktop]$ rpm -qa | grep httpd
httpd-tools-2.4.6-89.el7.centos.1.x86_64
httpd-2.4.6-89.el7.centos.1.x86_64

4.常用命令。

# 查看 httpd 安装位置
[admin@localhost Desktop]$ rpm -ql httpd
# 设置开机自动启动
[admin@localhost Desktop]$ systemctl enable httpd
# 开启httpd服务
[admin@localhost Desktop]$ systemctl start httpd
# 关闭httpd服务
[admin@localhost Desktop]$ systemctl stop httpd
# 重启http服务
[admin@localhost Desktop]$ systemctl restart httpd

5.测试。
启动服务后, 浏览器打开http://127.0.0.1/即可查看网页。
配置文件在 /etc/httpd/conf

6.说明。
打开http://127.0.0.1/所看到的网页可以在 /etc/httpd/conf.d/welcome.conf 中找到位置。
例如我的位置为 /usr/share/httpd/noindex/index.html , 这个是 apache 默认页面文件。

三、安装PHP组件

1.首先查看yum里面是否包含php的包。

[admin@localhost Desktop]$ yum list | grep php
emacs-php-mode.noarch                     1.18.2-1.el7                 base     
geos-php.x86_64                           3.4.2-2.el7                  epel     
graphviz-php.x86_64                       2.30.1-22.el7                base     
mlt-php.x86_64                            6.4.1-4.el7                  epel     
php.x86_64                                5.4.16-48.el7                base     
...

2.进行安装。

[admin@localhost Desktop]$ sudo yum install php -y

3.查看是否安装成功。

[admin@localhost Desktop]$ rpm -qa | grep php
php-common-5.4.16-48.el7.x86_64
php-cli-5.4.16-48.el7.x86_64
php-5.4.16-48.el7.x86_64

4.测试。

  • 首先使用命令systemctl restart httpd重启apache。
  • 再使用 vim /var/www/html/info.php 编辑文件。
  • 输入内容
<?php
	phpinfo();
?>

四、关联PHP和MySQL组件

1.进行安装。

[admin@localhost Desktop]$ sudo yum install php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc

2.测试。

  • 首先使用命令重启 Apache。systemctl restart httpd
  • 请求http://127.0.0.1/info.php, 搜索 mysql, 看到有 mysql 模块表示关联成功。

参考文献

https://www.linuxidc.com/Linux/2018-03/151403.htm
http://down.chinaz.com/server/201109/1064_1.htm