Skip to content

CentOS安装PHP7.x

  • by

在CentOS安裝PHP7.x。

1. 安装PHP7.x

  1. 安装EPEL和Remi源1
$ sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
$ sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
  1. 安装yum-utils并配置PHP7.x源:
$ sudo yum install yum-utils
$ sudo yum-config-manager --enable remi-php7x
  1. 安装PHP7.x以及相关的扩展
$ sudo yum install php php-fpm php-common php-mysqlnd php-gd php-xml php-mbstring
  1. 安装完成后启动PHP:
$ sudo systemctl start php-fpm.service
$ systemctl status php-fpm.service
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2020-06-08 21:06:55 EDT; 6min ago
 Main PID: 1327 (php-fpm)
   Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec"
    Tasks: 6 (limit: 11344)
   Memory: 24.7M
   CGroup: /system.slice/php-fpm.service
           ├─1327 php-fpm: master process (/etc/php-fpm.conf)
           ├─1406 php-fpm: pool www
           ├─1407 php-fpm: pool www
           ├─1408 php-fpm: pool www
           ├─1411 php-fpm: pool www
           └─1415 php-fpm: pool www

2. 配置

  1. 修改PHP-FPM配置文件/etc/opt/remi/php7x/php-fpm.d/www.conf
; user = apache
; group = apache
user = nginx
group = nginx
  • 根据使用的Web服务器,将默认的user和group从apache更改为nginx。如果不更改的话,apache运行的php-fpm不能对nginx的文件和文件夹写操作。
  1. 验证并重启php-fpm:
$ php-fpm -t
$ sudo systemctl restart php-fpm.service

2.1 验证PHP安装:

  1. 在Nginx html目录下创建info.php文件
$ sudo touch /usr/share/nginx/html/info.php
  1. 文件中输入
<?php phpinfo(); ?>
  1. 重启Nginx后,验证
$ curl -I localhost/info.php
HTTP/1.1 200 OK
Server: nginx/...
Date: Sat, 23 May 2020 03:07:19 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/...

至此安装成功。

Leave a Reply

Your email address will not be published. Required fields are marked *