Linux Admin 配置PHP
PHP 是当今使用最多的网络语言之一。在 CentOS 上安装
LAMP 堆栈是每个系统管理员都需要执行的事情,很可能是迟早的事。
A tr额外的 LAMP 堆栈由 (L)inux (A)pache (M)ySQL (P)HP 组成。
CentOS 上的
LAMP 堆栈 包含三个主要组件-
网络服务器
网络开发平台/语言
数据库服务器
注意-术语
LAMP 堆栈 还可以包括以下技术:PostgreSQL、MariaDB、Perl、Python、Ruby、NGINX Webserver。
在本教程中,我们将坚持使用 CentOS GNU Linux 的传统
LAMP 堆栈: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://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 网络服务器正在侦听和响应 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
使用 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
这是我们位于/var/www/html/的 Apache webroot 中的简单 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 是非常流行的网络编程技术。作为 CentOS 管理员,LAMP 的安装和配置肯定会出现在您的需求列表中。易于使用的 CentOS 软件包在从源代码编译 Apache、MySQL 和 PHP 方面花费了大量工作。