在 CentOS Linux 中配置 PHP



PHP 是當今使用最廣泛的 Web 語言之一。在 CentOS 上安裝 *LAMP* 棧是每個系統管理員都需要執行的操作,很可能 sooner than later。

傳統的 LAMP 棧由 (L)inux (A)pache (M)ySQL (P)HP 組成。

在 CentOS 上,*LAMP 棧* 主要包含三個元件:

  • Web 伺服器
  • Web 開發平臺/語言
  • 資料庫伺服器

注意 - *LAMP 棧* 還可以包含以下技術:PostgreSQL、MariaDB、Perl、Python、Ruby、NGINX Web 伺服器。

在本教程中,我們將堅持使用 CentOS GNU Linux 的傳統 *LAMP 棧*:Apache Web 伺服器、MySQL 資料庫伺服器和 PHP。

我們實際上將使用 MariaDB。MySQL 配置檔案、資料庫和表對 MariaDB 是透明的。由於 Oracle 接管了 MySQL 的開發,並且由於許可和開源合規性的限制,MariaDB 現在包含在標準 CentOS 儲存庫中,而不是 MySQL。

首先,我們需要安裝 Apache。

[root@CentOS]# yum install httpd
Loaded plugins: fastestmirror, langpacks
base
| 3.6 kB  00:00:00
extras
| 3.4 kB  00:00:00
updates
| 3.4 kB  00:00:00
extras/7/x86_64/primary_d
| 121 kB  00:00:00
Loading mirror speeds from cached hostfile
* base: mirror.sigmanet.com
* extras: linux.mirrors.es.net
* updates: mirror.eboundhost.com
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-45.el7.centos will be installed
--> Processing Dependency: httpd-tools = 2.4.6-45.el7.centos for package:
httpd-2.4.6-45.el7.centos.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.645.el7.centos.x86_64
--> Running transaction check
---> Package httpd-tools.x86_64 0:2.4.6-45.el7.centos will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution
Installed:
httpd.x86_64 0:2.4.6-45.el7.centos

Dependency Installed:
httpd-tools.x86_64 0:2.4.6-45.el7.centos
mailcap.noarch 0:2.1.41-2.el7

Complete!
[root@CentOS]#

讓我們配置 *httpd* 服務。

[root@CentOS]# systemctl start httpd && systemctl enable httpd

現在,讓我們確保 Web 伺服器可以透過 firewalld 訪問。

bash-3.2# nmap -sS -p 1-1024 -T 5  -sV 10.211.55.1 
Starting Nmap 7.30 ( https://nmap.org ) at 2017-01-28 02:00 MST 
Nmap scan report for centos.shared (10.211.55.1) 
Host is up (0.00054s latency). 
Not shown: 1022 filtered ports 
PORT   STATE SERVICE VERSION 
22/tcp open  ssh     OpenSSH 6.6.1 (protocol 2.0) 
80/tcp open  http    Apache httpd 2.4.6 ((CentOS))

Service detection performed. Please report any incorrect results at 
https://nmap.org/submit/ . 
Nmap done: 1 IP address (1 host up) scanned in 10.82 seconds bash-3.2#

正如 nmap 服務探測所示,Apache Web 伺服器正在監聽並響應 CentOS 主機上的請求。

安裝 MySQL 資料庫伺服器

[root@CentOS rdc]# yum install mariadb-server.x86_64 && yum install mariadb-
devel.x86_64 && mariadb.x86_64 && mariadb-libs.x86_64

我們正在安裝以下 MariaDB 儲存庫包:

mariadb-server.x86_64

主要的 MariaDB 伺服器守護程序包。

mariadb-devel.x86_64

需要使用 MySQL/MariaDB 相容性從原始碼編譯的檔案。

mariadb.x86_64

用於從命令列管理 MariaDB 伺服器的 MariaDB 客戶端實用程式。

mariadb-libs.x86_64

MariaDB 的常用庫,其他使用 MySQL/MariaDB 支援編譯的應用程式可能需要這些庫。

現在,讓我們啟動並啟用 MariaDB 服務。

[root@CentOS]# systemctl start mariadb 
[root@CentOS]# systemctl enable  mariadb

注意 - 與 Apache 不同,我們不會透過基於主機的防火牆 (firewalld) 啟用對 MariaDB 的連線。使用資料庫伺服器時,最佳安全實踐是不允許遠端套接字訪問,除非特別需要遠端套接字訪問,否則只允許本地套接字連線。

讓我們確保 MariaDB 伺服器正在接受連線。

[root@CentOS#] netstat -lnt 
Active Internet connections (only servers) 
Proto     Recv-Q     Send-Q     Local Address        Foreign Address      State       
tcp            0          0     0.0.0.0:3306         0.0.0.0:*            LISTEN      
tcp            0          0     0.0.0.0:111          0.0.0.0:*            LISTEN      
tcp            0          0     192.168.122.1:53     0.0.0.0:*            LISTEN      
tcp            0          0     0.0.0.0:22           0.0.0.0:*            LISTEN      
tcp            0          0     127.0.0.1:631        0.0.0.0:*            LISTEN      
tcp            0          0     127.0.0.1:25         0.0.0.0:*            LISTEN 
     
[root@CentOS rdc]#

如我們所見,MariaDB 正在 3306 tcp 埠上監聽。我們將保留基於主機的防火牆 (firewalld) 來阻止傳入的 3306 埠連線。

安裝和配置 PHP

[root@CentOS#]  yum install php.x86_64 && php-common.x86_64 && php-mysql.x86_64 
&& php-mysqlnd.x86_64 && php-pdo.x86_64 && php-soap.x86_64 && php-xml.x86_64

我建議安裝以下 php 包以實現常見的相容性:

  • php-common.x86_64
  • php-mysql.x86_64
  • php-mysqlnd.x86_64
  • php-pdo.x86_64
  • php-soap.x86_64
  • php-xml.x86_64
[root@CentOS]# yum install -y php-common.x86_64 php-mysql.x86_64 php-
mysqlnd.x86_64 php-pdo.x86_64 php-soap.x86_64 php-xml.x86_64

這是我們位於 Apache webroot /var/www/html/ 中的簡單 php 檔案。

[root@CentOS]# cat /var/www/html/index.php  
<html> 
   <head> 
      <title>PHP Test Page</title> 
   </head>
   
   <body> 
      PHP Install 
      <?php 
         echo "We are now running PHP on GNU Centos Linux!<br />" 
      ?> 
   </body> 
</html>

[root@CentOS]#

讓我們將頁面的擁有組更改為我們的 http 守護程式執行的系統使用者。

[root@CentOS]# chgrp httpd /var/www/html/index.php && chmod g+rx /var/www/html/index.php
---

透過 ncat 手動請求時。

bash-3.2# ncat 10.211.55.1 80 
   GET / index.php 
   HTTP/1.1 200 OK 
   Date: Sat, 28 Jan 2017 12:06:02 GMT 
   Server: Apache/2.4.6 (CentOS) PHP/5.4.16 
   X-Powered-By: PHP/5.4.16 
   Content-Length: 137 
   Connection: close 
   Content-Type: text/html; charset=UTF-8
   
<html> 
   <head> 
      <title>PHP Test Page</title> 
   </head>
   
   <body> 
      PHP Install 
      We are now running PHP on GNU Centos Linux!<br />
   </body> 
</html>

bash-3.2#

PHP 和 LAMP 是非常流行的 Web 程式設計技術。作為 CentOS 管理員,LAMP 的安裝和配置肯定會出現在您的需求列表中。易於使用的 CentOS 包已經省去了很多從原始碼編譯 Apache、MySQL 和 PHP 的工作。

廣告