Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

การย้าย PHP 5.x เป็น PHP 7 บน CentOS 7


ในบทความนี้ เราจะเรียนรู้เกี่ยวกับวิธีการอัปเกรดและอัปเดต PHP 5.x เป็น PHP 7, PHP 7 ซึ่งเปิดตัวในปี 2015 ด้วยการปรับปรุงความเร็วที่เทียบได้กับ PHP เวอร์ชันเก่า

ข้อกำหนดเบื้องต้น

สมมติว่าเราได้ติดตั้ง PHP 5.x บน CentOS7 แล้ว และโมดูล mod_php ควรเปิดใช้งานโดย Apache และเราต้องการสิทธิ์ Sudo หรือผู้ใช้รูท

การเปิดใช้งานที่เก็บ PHP 7

เนื่องจาก PHP 7.x ไม่พร้อมใช้งานในที่เก็บอย่างเป็นทางการ เราจึงจำเป็นต้องใช้ Project Repository ของชุมชน IUS

ดาวน์โหลดที่เก็บ IUS ในเครื่องโดยใช้คำสั่งด้านล่าง

# curl 'https://setup.ius.io/' -o setup-ius.sh
curl 'https://setup.ius.io/' -o setup-ius.sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1913 100 1913 0 0 1554 0 0:00:01 0:00:01 --:--:-- 1555


การติดตั้งที่เก็บ IUS ลงในเครื่องโลคัลด้วยคำสั่งด้านล่าง

# bash setup-ius.sh
bash setup-ius.sh
detected CentOS 7
Loaded plugins: fastestmirror
base | 3.6 kB
epel/x86_64/metalink | 5.2 kB
epel | 4.3 kB
extras | 3. 4 kB 00:00:00
mysql56-community | 2.5 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/3): updates/7/x86_64/primary_db | 5.7 MB 00:00:00
(2/3): epel/x86_64/updateinfo | 584 kB 00:00:02
(3/3): epel/x86_64/primary_db | 4.3 MB 00:00:03
Loading mirror speeds from cached hostfile
* base: mirrors.nhanhoa.com
* epel: kodeterbuka.beritagar.id
* extras: mirrors.nhanhoa.com
* updates: mirrors.nhanhoa.com
Package epel-release-7-7.noarch already installed and latest version
Nothing to do
Loaded plugins: fastestmirror
ius-release.rpm | 8.1 kB 00:00:00
Examining /var/tmp/yum-root-BRDs50/ius-release.rpm: ius-release-1.0-14.ius.centos7.noarch
Marking /var/tmp/yum-root-BRDs50/ius-release.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package ius-release.noarch 0:1.0-14.ius.centos7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=========================================================================================================================
Package Arch Version Repository Size=========================================================================================================================
Installing:
ius-release noarch 1.0-14.ius.centos7 /ius-release 8.
Transaction Summary
=========================================================================================================================
Install 1 Package
Total size: 8.5 k
Installed size: 8.5 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : ius-release-1.0-14.ius.centos7.noarch 1/1
Verifying : ius-release-1.0-14.ius.centos7.noarch 1/1
Installed:
ius-release.noarch 0:1.0-14.ius.centos7
Complete!

การอัพเกรดโมดูล mod_php ใน Apache

ที่นี่ เราจะอัปเกรดระบบสำหรับโมดูล Apache mod_php ขั้นแรก เราจะลบแพ็คเกจเก่าโดยใช้คำสั่งด้านล่าง

# yum remove php-cli mod_php php-common

การติดตั้งโมดูล PHP 7 ใหม่โดยใช้ที่เก็บ IUS โดยใช้คำสั่งด้านล่าง

# yum install php70-mysqlnd mod_php70u php70u-cli
Loaded plugins: fastestmirror
ius | 2.2 kB 00:00:00
ius/x86_64/primary_db | 173 kB 00:00:00
Loading mirror speeds from cached hostfile
* base: mirrors.nhanhoa.com
* epel: kodeterbuka.beritagar.id
* extras: mirrors.nhanhoa.com
* ius: hkg.mirror.rackspace.com
* updates: mirrors.nhanhoa.com
Resolving Dependencies
--> Running transaction check
---> Package mod_php70u.x86_64 0:7.0.8-1.ius.centos7 will be installed
--> Processing Dependency: php-common(x86-64) = 7.0.8-1.ius.centos7 for package: mod_php70u-7.0.8-1.ius.centos7.x86_64
---> Package php70u-cli.x86_64 0:7.0.8-1.ius.centos7 will be installed
---> Package php70u-mysqlnd.x86_64 0:7.0.8-1.ius.centos7 will be installed
--> Processing Dependency: php-pdo(x86-64) = 7.0.8-1.ius.centos7 for package: php70u-mysqlnd-7.0.8-1.ius.centos7.x86_64
--> Running transaction check
---> Package php70u-common.x86_64 0:7.0.8-1.ius.centos7 will be installed
---> Package php70u-pdo.x86_64 0:7.0.8-1.ius.centos7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=========================================================================================================================
Package Arch Version Repository Size
=========================================================================================================================
Installing:
mod_php70u x86_64 7.0.8-1.ius.centos7 ius 2.6 M
php70u-cli x86_64 7.0.8-1.ius.centos7 ius 4.0 M
php70u-mysqlnd x86_64 7.0.8-1.ius.centos7 ius 239 k
Installing for dependencies:
php70u-common x86_64 7.0.8-1.ius.centos7 ius 1.1 M
php70u-pdo x86_64 7.0.8-1.ius.centos7 ius 126 k
Transaction Summary
=========================================================================================================================
Install 3 Packages (+2 Dependent packages)
Total download size: 8.0 M
Installed size: 30 M
Is this ok [y/d/N]: y
Downloading packages:
(1/5): php70u-pdo-7.0.8-1.ius.centos7.x86_64.rpm | 126 kB 00:00:00
(2/5): mod_php70u-7.0.8-1.ius.centos7.x86_64.rpm | 2.6 MB 00:00:01
(3/5): php70u-common-7.0.8-1.ius.centos7.x86_64.rpm | 1.1 MB 00:00:01
(4/5): php70u-mysqlnd-7.0.8-1.ius.centos7.x86_64.rpm | 239 kB 00:00:01
(5/5): php70u-cli-7.0.8-1.ius.centos7.x86_64.rpm | 4.0 MB 00:00:03
-------------------------------------------------------------------------------------------------------------------------
Total 2.2 MB/s | 8.0 MB 00:00:03
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : php70u-common-7.0.8-1.ius.centos7.x86_64 1/5
Installing : php70u-pdo-7.0.8-1.ius.centos7.x86_64 2/5
Installing : php70u-mysqlnd-7.0.8-1.ius.centos7.x86_64 3/5
Installing : php70u-cli-7.0.8-1.ius.centos7.x86_64 4/5
Installing : mod_php70u-7.0.8-1.ius.centos7.x86_64 5/5
Verifying : php70u-cli-7.0.8-1.ius.centos7.x86_64 1/5
Verifying : php70u-common-7.0.8-1.ius.centos7.x86_64 2/5
Verifying : mod_php70u-7.0.8-1.ius.centos7.x86_64 3/5
Verifying : php70u-pdo-7.0.8-1.ius.centos7.x86_64 4/5
Verifying : php70u-mysqlnd-7.0.8-1.ius.centos7.x86_64 5/5
Installed:
mod_php70u.x86_64 0:7.0.8-1.ius.centos7 php70u-cli.x86_64 0:7.0.8-1.ius.centos7
php70u-mysqlnd.x86_64 0:7.0.8-1.ius.centos7
Dependency Installed:
php70u-common.x86_64 0:7.0.8-1.ius.centos7 php70u-pdo.x86_64 0:7.0.8-1.ius.centos7
Complete!

สุดท้าย เราจะรีสตาร์ท Apache และตรวจสอบสถานะของ Apache

# systemctl restart httpd

การตรวจสอบสถานะบริการ apache

# systemctl status httpd
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2016-07-21 10:24:44 EDT; 46s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 3636 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─3636 /usr/sbin/httpd -DFOREGROUND
├─3637 /usr/sbin/httpd -DFOREGROUND
├─3638 /usr/sbin/httpd -DFOREGROUND
├─3639 /usr/sbin/httpd -DFOREGROUND
├─3640 /usr/sbin/httpd -DFOREGROUND
└─3641 /usr/sbin/httpd -DFOREGROUND
Jul 21 10:24:43 centos-linux-1.shared systemd[1]: Starting The Apache HTTP Server...
Jul 21 10:24:44 centos-linux-1.shared systemd[1]: Started The Apache HTTP Server.

การทดสอบเวอร์ชัน PHP

เราสามารถตรวจสอบเวอร์ชันของ PHP ได้โดยใช้โหมดบรรทัดคำสั่ง และเราสามารถสร้าง info.php และตรวจสอบเวอร์ชันของ PHP ได้

# php -v
PHP 7.0.8 (cli) (built: Jun 23 2016 11:34:57) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

การสร้างไฟล์ info.php โดยใช้โค้ดด้านล่าง และตรวจสอบข้อมูลในโหมด GUI

# /var/www/html/info.php
<?php
Phpinfo();
?>

หลังจากที่เราสร้างไฟล์แล้ว ให้เข้าถึงเว็บเบราว์เซอร์ด้วย url ด้านล่าง

https://IP_Address /info.php

การย้าย PHP 5.x เป็น PHP 7 บน CentOS 7

หลังจากที่เราตั้งค่าและกำหนดค่าขั้นตอนข้างต้นแล้ว เราสามารถอัปเดต PHP 5.x เป็น PHP 7 บน Centos 7 / RHEL 7 ได้ ณ ตอนนี้ ที่เก็บอย่างเป็นทางการสำหรับ CentOS 7 ยังไม่เผยแพร่ นอกจากนี้เรายังสามารถเห็นได้ว่า PHP 7 ได้รับการอัพเกรดบนเซิร์ฟเวอร์