在CentOS Linux中配置PHP

PHP是当今使用最多产的网络语言之一.在CentOS上安装 LAMP 堆栈是每个系统管理员都需要执行的,很可能很快就会发生.

传统的LAMP堆栈由(L)inux组成(A)pache(M)ySQL(P)HP.

CentOS上的 LAMP堆栈有三个主要组件 :

  • Web服务器

  • Web开发平台/语言

  • 数据库服务器

注意 : 术语 LAMP Stack 还可以包括以下技术:PostgreSQL,MariaDB,Perl,Python,Ruby,NGINX Webserver.

对于本教程,我们将坚持使用CentOS GNU Linux的传统 LAMP Stack :Apache Web服务器,MySQL数据库服务器和PHP.

我们实际上将使用MariaDB. MySQL配置文件,数据库和表对MariaDB是透明的. MariaDB现在包含在标准的CentOS存储库中,而不是MySQL中.这是由于许可和开源合规性的限制,因为Oracle已经接管了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

现在,让我们确保网络 - 服务器可以通过firewalld访问.

bash-3.2# nmap -sS -p 1-1024 -T 5  -sV 10.211.55.1 
Starting Nmap 7.30 ( https://img01.yuandaxia.cn/Content/img/tutorials/linux_admin/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://img01.yuandaxia.cn/Content/img/tutorials/linux_admin/nmap.org/submit/ . 
Nmap done: 1 IP address (1 host up) scanned in 10.82 seconds bash-3.2#

正如nmap服务探测器所见,Apache webserver正在侦听并响应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 Server守护程序包.

mariadb-devel.x86_64

文件需要从具有MySQL/MariaDB兼容性的源.

mariadb.x86_64

用于从命令行管理MariaDB Server的MariaDB客户端实用程序.

mariadb-libs.x86_64

使用MySQL/MariaDB支持编译的其他应用程序可能需要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

这是我们位于/web的Apache webroot中的简单php文件/www/html/

[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是非常流行的网络编程技术.作为CentOS管理员,LAMP安装和配置肯定会出现在您的需求列表中.易于使用的CentOS软件包从源代码编译Apache,MySQL和PHP中花费了大量的精力.