Upgrading Apache 2.2.15 → 2.4.25 and PHP 5.6 → 7 on CentOS 6.9
Overview
Are you still using PHP5?
The security support for PHP5.6 ends on 31 Dec 2018.
- See Supported Versions for reference.
I had been reluctant to update Apache/PHP, but since it’s a personally contracted server, it won’t bother anyone, so why not ♪
So I put together a summary of what I did to update a personal server running the neglected Apache2.2.15/PHP5.6.
Three-Line Summary
- Using SoftwareCollection, I kept the existing Apache/PHP in place while letting the updated versions coexist, then switched over. Afterwards, I removed the old Apache/PHP.
- Installed the required modules (MySQLi, PHPRedis).
- Fixed PHP5.6 features and syntax that were removed in PHP 7.
What is SoftwareCollection?
According to the official site, it is described as follows.
English
Software Collections give you the power to build, install, and use multiple versions of software on the same system, without affecting system-wide installed packages.
Japanese
ソフトウェアコレクションは、システム全体でインストールされたパッケージに影響を与えることなく、同じシステム上に複数のバージョンのソフトウェアを構築、インストール、使用する能力を提供します。
In other words, it lets you install multiple versions of software on the same system.
Installing SoftwareCollection
1 | $ sudo yum install centos-release-scl |
1 | $ sudo yum-config-manager --enable rhel-server-rhscl-6-rpms |
1 | $ httpd -v |
Enabling the RHSCL repository
1 | $ sudo yum-config-manager --enable rhel-server-rhscl-7-rpms |
1 | # yum install -y scl-utils |
Installing mysqli
1 | # yum --enablerepo=remi-php70 install php-mysqli |
Installing phpredis for PHP7
1 | # cd /usr/local/src |
Restarting php-fpm
1 | # /etc/init.d/php70-php-fpm restart |
Restarting httpd
1 | # /etc/init.d/httpd24-httpd restart |
At this point, you should have an environment that runs on PHP7. Make your fixes while watching the error logs.
Fixing syntax removed in PHP 7
PHP Parse error: syntax error, unexpected ‘new’ (T_NEW)
- The
&= new <class name>notation is no longer allowed; you need to change it to= new <class name>.
1 | &= new Class |
PHP Fatal error: Cannot use ‘String’ as class name as it is reserved
- In PHP7, you can no longer create classes whose names are type names such as
StringorInt.
I fixed mine as follows.
- Please change them as appropriate according to your project’s coding rules.
1 | class String { |
1 | class Int { |
Depending on the project, many more issues may come up, so please fix them as needed.
Wrap-up
It’s something you want to keep on top of regularly: updating middleware, which tends to get neglected. I really need to take stock of vulnerabilities on a regular basis.
If you’re doing the update as part of your job, it might be a good idea to prepare a separate environment for the update, perform the update there, summarize the update procedure at both the middleware and application code level, and then carry it out in the production environment.
Another viable approach would be to route only the features (URLs) that have been confirmed to work correctly to PHP7 via a proxy, on a per-feature basis.
That’s all.
References
Upgrading Apache 2.2.15 → 2.4.25 and PHP 5.6 → 7 on CentOS 6.9
https://kenzo0107.github.io/en/2017/06/13/update-apache-php/
