Linux Admin 简明教程
Configure PHP in CentOS Linux
PHP 是当今使用最多的 Web 语言之一。在 CentOS 上安装一个 LAMP 堆栈是每个系统管理员都需要执行的事情,而且可能早于预期。
PHP is the one of the most prolific web languages in use today. Installing a LAMP Stack on CentOS is something every system administrator will need to perform, most likely sooner than later.
一个传统的 LAMP 堆栈包含 (L)inux (A)pache (M)ySQL (P)HP。
A traditional LAMP Stack consists of (L)inux (A)pache (M)ySQL (P)HP.
在 CentOS 中,一个 LAMP 堆栈有三个主要组件 -
There are three main components to a LAMP Stack on CentOS −
-
Web Server
-
Web Development Platform / Language
-
Database Server
Note - LAMP 堆栈这个术语也可能包含以下技术:PostgreSQL、MariaDB、Perl、Python、Ruby、NGINX Web 服务器。
Note − The term LAMP Stack can also include the following technologies: PostgreSQL, MariaDB, Perl, Python, Ruby, NGINX Webserver.
本教程中,我们坚持使用 CentOS GNU Linux 的传统 LAMP 堆栈: Apache Web 服务器,MySQL 数据库服务器和 PHP。
For this tutorial, we will stick with the traditional LAMP Stack of CentOS GNU Linux: Apache web server, MySQL Database Server, and PHP.
我们实际上将使用 MariaDB。MySQL 配置文件、数据库和表对 MariaDB 来说是透明的。由于 Oracle 已接管 MySQL 的开发,因此 MariaDB 现在包含在标准 CentOS 存储库中,而不是 MySQL。这是由于许可和开源合规性的限制。
We will actually be using MariaDB. MySQL configuration files, databases and tables are transparent to MariaDB. MariaDB is now included in the standard CentOS repository instead of MySQL. This is due to the limitations of licensing and open-source compliance, since Oracle has taken over the development of MySQL.
我们需要做的第一件事是安装 Apache。
The first thing we need to do is install 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 服务。
Let’s configure httpd service.
[root@CentOS]# systemctl start httpd && systemctl enable httpd
现在,让我们确保防火墙可以访问 Web 服务器。
Now, let’s make sure the web-server is accessible through 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 主机上的请求。
As you can see by the nmap service probe, Apache webserver is listening and responding to requests on the CentOS host.
Install MySQL Database Server
[root@CentOS rdc]# yum install mariadb-server.x86_64 && yum install mariadb-
devel.x86_64 && mariadb.x86_64 && mariadb-libs.x86_64
我们正在为 MariaDB 安装以下存储库包 −
We are installing the following repository packages for MariaDB −
mariadb-devel.x86_64
需要从具有 MySQL/MariaDB 兼容性的源代码编译的文件。
Files need to compile from the source with MySQL/MariaDB compatibility.
mariadb.x86_64
用于从命令行管理 MariaDB 服务器的 MariaDB 客户端实用程序。
MariaDB client utilities for administering MariaDB Server from the command line.
mariadb-libs.x86_64
可能需要用于与 MySQL/MariaDB 支持一起编译的其他应用程序的 MariaDB 公共库。
Common libraries for MariaDB that could be needed for other applications compiled with MySQL/MariaDB support.
现在,启动并启用 MariaDB 服务。
Now, let’s start and enable the MariaDB Service.
[root@CentOS]# systemctl start mariadb
[root@CentOS]# systemctl enable mariadb
Note − 与 Apache 不同,我们不会通过基于主机的防火墙 (firewalld) 启用到 MariaDB 的连接。在使用数据库服务器时,除非明确需要远程套接字访问,否则仅允许本地套接字连接被认为是最佳安全实践。
Note − Unlike Apache, we will not enable connections to MariaDB through our host-based firewall (firewalld). When using a database server, it’s considered best security practice to only allow local socket connections, unless the remote socket access is specifically needed.
确保 MariaDB 服务器已接受连接。
Let’s make sure the MariaDB Server is accepting connections.
[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 端口的连接。
As we can see, MariaDB is listening on port 3306 tcp. We will leave our host-based firewall (firewalld) blocking incoming connections to port 3306.
Install and Configure 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 包以实现通用兼容性 −
I’d recommend installing the following php packages for common compatibility −
-
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
这是我们位于 /var/www/html/ 的 Apache Web 根目录中的简单 php 文件。
This is our simple php file located in the Apache webroot of /var/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 守护程序运行的系统用户。
Let’s change the owning group of our page to the system user our http daemon is running under.
[root@CentOS]# chgrp httpd /var/www/html/index.php && chmod g+rx /var/www/html/index.php
---
通过 ncat 手动请求时。
When requested manually via 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。
PHP and LAMP are very popular web-programming technologies. LAMP installation and configuration is sure to come up on your list of needs as a CentOS Administrator. Easy to use CentOS packages have taken a lot of work from compiling Apache, MySQL, and PHP from the source code.