使用新的CentOS8系統架設PHP服務器,因現在主流數據庫mysql已閉源了,所以現在改為使用MariaDB.而php7以后不支持mysqli鏈接,只有pdo方式,為了安裝pdo擴展,所以重新編譯安裝了PHP,折騰很久才完成,收獲還是不錯的,了解了很多方面的知識.
安裝Apache
- 安裝
yum -y install httpd
- 開啟apache服務
systemctl start httpd.service
- 設置apache服務開機啟動
systemctl enable httpd.service
- 開啟防火牆
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload
- 驗證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
- 安裝MariaDB
yum install mariadb-server -y
- 重啟MariaDB
systemctl enable mariadb.service
systemctl restart mariadb.service
- 設置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 '密碼'
- 設置防火牆
firewall-cmd --permanent --zone=public --add-port=3306/tcp
firewall-cmd --reload
- 設置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來連接數據庫)