PHP 7.0 to PHP 7.x – How to upgrade your server

使用 wordpress 的 woocommerce 的 status 工具,顯示目前的 php 7.0 版本太舊,建議使用 php 7.2,這篇文章要教大家怎麼升級server 裡的 php 並設定 apache.

目前已升到最新的版本 php 7.3.4-1

Ubuntu在 16.04 的版本中,PHP 版號只能到 7.0,如果想要裝 7.2 就必須手動加入 repository

# 加入 repository
sudo add-apt-repository ppa:ondrej/php
sudo apt update

Install

# Install PHP 7.x
sudo apt-get install php

# Install PHP 7.x modules
sudo apt install php-pear php-curl php-dev php-gd php-mbstring php-zip php-mysql php-xml libapache2-mod-php

Upgrade

sudo apt upgrade php

Notice:Ubuntu 18.04 的 PHP 預設版本已經是 7.2,因此就不需要另外新增 repository 了,直接安裝即可。


直接執行上面的句子,apache2 還是會去使用舊的 php, 因為 /etc/apache2/mods-available 裡雖然有多 7.x 的版本出來,但沒有被 enable.


測試用 info.php

<?php
phpinfo();
?>

懶的開網頁,也可以透過curl 指令直接看結果:

curl your-host-url/info.php


Before we upgrade to PHP 7.x, you need to update your packages list first. Run the following commands to update your packages and install PHP 7.x on your server:

$ apt-get update
$ apt-get install python-software-properties
$ LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
$ apt-get update
$ apt-get install php

php version

After installed PHP 7.x, try to run php -v in your console. And you will see PHP 7.x installed on your server!


Disable old version PHP on Apache and enable the new version

However, we need to disable the old version on Apache and tell Apache to use PHP 7.3. Try the following commands:

$ a2dismod php7.0
$ a2enmod php7.3
$ service apache2 restart

After restarted apache2 service, now PHP 7.3 is finally running on your web server. Though we have the newer version of PHP, we still need to install those modules back.


Install PHP modules

We will run the following command to install those modules back. You may install more or fewer modules, depend on your PHP application.

$ apt-get install libapache2-mod-php7.3 php7.3-cgi php7.3-cli php7.3-common php7.3-curl php7.3-gd php7.3-imap php7.3-intl php7.3-json php7.3-ldap php7.3-mbstring php7.3-mysql php7.3-opcache php7.3-pspell php7.3-readline php7.3-soap php7.3-xml

And yes, restart the apache2 service again.

$ service apache2 restart

Congratulations! PHP 7.3 is now running on your web server.

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *