CentOS8 安装部署Apache+Php+MariaDB,pdo扩展

使用新的CentOS8系统架设PHP服务器,因现在主流数据库mysql已闭源了,所以现在改为使用MariaDB.而php7以后不支持mysqli链接,只有pdo方式,为了安装pdo扩展,所以重新编译安装了PHP,折腾很久才完成,收获还是不错的,了解了很多方面的知识.

安装Apache

  1. 安装

    yum -y install httpd

  2. 开启apache服务

    systemctl start httpd.service

  3. 设置apache服务开机启动

    systemctl enable httpd.service

  4. 开启防火墙

    firewall-cmd --permanent --zone=public --add-service=http

    firewall-cmd --permanent --zone=public --add-service=https

    firewall-cmd --reload

  5. 验证apache服务是否安装成功

    打开http://xx.xx.xx.xx/,apache默认的页面--有Testing 123...字样

安装PHP

  • 安装工具与软件

    yum install -y wget tar nano

  • 下载并解压PHP源码
cd /home
wget https://www.php.net/distributions/php-7.3.13.tar.gz
tar -xzf php-7.3.13.tar.gz php-7.3.13
  • 安装编译工具与依赖
yum install -y gcc make gcc-c++ libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel curl-devel postgresql-devel libpng libjpeg-devel libjpeg libpng-devel freetype freetype-devel libicu-devel libzip cmake
  • 安装libsodium
wget https://download.libsodium.org/libsodium/releases/libsodium-1.0.18-stable.tar.gz
tar -zxf libsodium-1.0.18-stable.tar.gz libsodium-stable
cd libsodium-stable
./configure --prefix=/usr
make && make check

php-devel

  • 创建用户与群组
groupadd www
useradd -g www www
  • 内连ldap
ln -sv  /usr/lib64/libldap* /usr/lib/ 
  • 安装libzip
wget https://libzip.org/download/libzip-1.5.2.tar.gz
tar -zxf libzip-1.5.2.tar.gz
cd libzip-1.5.2
mkdir build
cd build 
cmake ..
make -j4
make install
  • 添加搜索路径到配置文件

    nano /etc/ld.so.conf

    在文件最后添加

/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64
  • 更新配置

    ldconfig -v

  • 配置php源码

    cd /home/php-7.3.13

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-config-file-scan-dir=/usr/local/php/etc/php.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mysqlnd-compression-support --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl=/usr/local/curl --enable-mbregex --enable-mbstring --enable-intl --enable-ftp --with-gd --enable-gd-jis-conv --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache --enable-maintainer-zts --with-ldap=shared --without-gdbm --with-apxs2=/usr/bin/apxs
  • 编译与安装

    make && make install

  • 将路径加入到系统环境变量中

    nano /etc/profile

    在最后加入PHP路径:

PATH=$PATH:/usr/local/php/bin
export PATH

保存后,在任意地方尝试运行php -version成功。

  • 修改Apache配置

    nano /etc/httpd/conf/httpd.conf

#在LoadModule后面添加:(未添加.php文件会变成下载)
LoadModule php7_module modules/libphp7.so
#在DirectoryIndex后面添加index.php:(让网站默认显示页面)
DirectoryIndex index.html index.php
#在AddType application/x-gzip .gz .tgz后面添加:
AddType application/x-httpd-php .php //.php前面有一个空格

然后重启Apache服务

systemctl restart httpd.service

安装MariaDB

  1. 安装MariaDB

    yum install mariadb-server -y

  2. 重启MariaDB

    systemctl enable mariadb.service

    systemctl restart mariadb.service

  3. 设置MariaDB权限与密码等
  • 登陆MariaDB:无密码mysql -uroot有密码用mysql -uroot -p123456
  • 显示数据库show databases;
  • 选择数据库use mysql;
  • 查询用户SELECT host,user,password from user;
  • 设置密码set password for 'root'@'localhost' =password('123456');
  • 远程连接设置grant all privileges on *.* to root@'%'identified by '123456';
  • 如果是不是root则先新建用户

    create user '用户名'@'%' ip地址 by '密码'

  1. 设置防火墙

    firewall-cmd --permanent --zone=public --add-port=3306/tcp

    firewall-cmd --reload

  2. 设置SElinux,(setenforce 0临时关闭也行)

    setsebool -P httpd_can_network_connect=1

    测试pdo代码(方便不懂的人使用修改,注意127.0.0.1与localhost有可能不一样!请修改mysql密码)

<?php
$user = "root";
$pass = "123456";
try {
    $dbh = new PDO('mysql:host=127.0.0.1;dbname=mysql', $user, $pass);
    foreach($dbh->query('SELECT host,user from user') as $row) {
        print_r($row);
    }
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>

至此数据库已经可以连接了(windows客户端可以使用HeidiSQL来连接数据库)