Installing PHP8 on an OCI A1 Arm Instance running Oracle Linux 8

Tim Clegg
Oracle Developers
Published in
6 min readMay 5, 2022
Photo by Jean van der Meulen from Pexels

Do you like PHP? Maybe you enjoy writing PHP apps, or you’re needing to use one of the many open-source applications that use PHP? There are too many to list here, but you’re in good company! Here’s a quick snapshot of what we’ll be looking at in this article: PHP8 on an A1 (Arm) OCI instance running Oracle Linux 8.

Oracle Linux has PHP available as an easy-to-install RPM (sudo dnf install php). Let’s look at it:

$ dnf info php
Last metadata expiration check: 6:04:47 ago on Tue 03 May 2022 02:19:09 PM GMT.
Available Packages
Name : php
Version : 7.2.24
Release : 1.module+el8.2.0+5510+6771133c
Architecture : aarch64
Size : 1.4 M
Source : php-7.2.24-1.module+el8.2.0+5510+6771133c.src.rpm
Repository : ol8_appstream
Summary : PHP scripting language for creating dynamic web sites
URL : http://www.php.net/
License : PHP and Zend and BSD and MIT and ASL 1.0
Description : PHP is an HTML-embedded scripting language. PHP attempts to make it
: easy for developers to write dynamically generated web pages. PHP also
: offers built-in database integration for several commercial and
: non-commercial database management systems, so writing a
: database-enabled webpage with PHP is fairly simple. The most common
: use of PHP coding is probably as a replacement for CGI scripts.
:
: The php package contains the module (often referred to as mod_php)
: which adds support for the PHP language to Apache HTTP Server.

This is fine if you’d like to be using PHP7, but with PHP8 being the latest major release, there are times when PHP8 might be needed. If you’re using an x86_64 (aka amd64) based instance, Remirepo has RPMs pre-built that make it easy to install PHP8. There’s a catch — remirepo doesn’t have arm64 (aka aarch64) RPMs.

There are several options for getting PHP8 running on arm64 (and OL8):

  1. Install using a prebuilt RPM
  2. Compile from source

Option 1 — Install by RPM

Option 1 sounds like a terrific idea, except that there’s no well-known, trusted source for a prebuilt RPM (at least that I’m aware of at the time of this writing). Going to an untrusted third-party RPM provider can be scary and usually isn’t a good idea. For the time being, this rules out this option.

Option 2 — Compile from Source

We’re left with compiling PHP8 from source. This isn’t horrible. It’s simply a bit more involved and slower than the prebuilt RPM route. Here’s the “easy path” to get this working.

You need to ask yourself if you’ll be needing Apache or not. For many PHP apps, they’re web-based and require a web server. Apache is just one web server that can be used (what I’m picking on in this article). Feel free to adapt to your specific needs!

With Apache

The PHP documentation has directions which this is based off of. Start by installing Apache:

sudo dnf install -y httpd httpd-devel
sudo systemctl start httpd

Now open up the needed port(s)/protocol(s):

sudo firewall-cmd --zone=public --add-port=80/tcp
sudo systemctl restart firewalld

Note that you will likely need different ports opened up. TCP/80 is typically used for unencrypted HTTP (which you really don’t want to use for anything beyond controlled, simple testing, if at all).

Get the latest PHP download URL from https://www.php.net/downloads and use it in place of the URL I’ve provided below (to make sure you get the latest-and-greatest version that you need):

wget https://www.php.net/distributions/php-8.1.4.tar.gz
tar -xzvf php-8.1.4.tar.gz
cd php-8.1.4

Obviously as the PHP URL changes, so will the filename. Your mileage may vary — make sure to reference the correct URL and filenames (as this article will become out-of-date)!

Next up let’s install the slew of development tools we’ll need to compile from source:

sudo dnf group install -y "Development Tools"
sudo dnf install -y re2c bison autoconf make libtool ccache libxml2-devel sqlite-devel libxml2 sqlite sqlite-devel

This will install a lot of packages. This could likely be trimmed down quite a bit, but unless you’re really tight on storage space and/or have a really lousy Internet connection, this shouldn’t be an issue.

Now we’re ready to compile and install:

./buildconf
./configure --with-apxs2=$(which apxs) --enable-opcache
make -j1 (should match number of cores - get via nproc)
make TEST_PHP_ARGS=-j4 test (set -j to # cores)
sudo make install

At this point we have PHP installed, but Apache doesn’t know. It’s time to tell Apache how to handle .php files. Edit (or create) a file /etc/httpd/conf.modules.d/00-php.conf (you’ll need to sudo to do this — something like sudo nano /etc/httpd/conf.modules.d/00-php.conf or similar) and place the following contents in it:

/etc/httpd/conf.modules.d/00-php.conf:

<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>

Next Apache needs to be restarted:

sudo systemctl restart httpd

It’s time to test things and make sure that it’s working! To do this, create a new file at /var/www/html/index.php and place the following contents in it:

/var/www/html/index.php:

<html>
<body>
<h1><?php print("Hello world!"); ?>
</body>
</html>

Before you say it… I know. The above HTML is horrible and incomplete (no head tag, etc.). While it’s short, it does render in the browser just fine!

To see it in action, go to your web browser and look at http://<ip>/index.php. It’s not fancy, but it does show us that PHP is working well with Apache (rendering the “Hello world!” text on the screen).

What if you just need PHP, but not Apache? I’m glad you asked. That’s what we’re talking about next…

Without Apache

Start by getting the latest PHP download URL from https://www.php.net/downloads and use it in place of the URL below (to make sure you get the latest-and-greatest version that you need):

wget https://www.php.net/distributions/php-8.1.4.tar.gz
tar -xzvf php-8.1.4.tar.gz
cd php-8.1.4

Just as with the Apache installation, you’ll need to use the correct (newer) URL and filenames!

Proceed by installing the many development tools needed to compile from source:

sudo dnf group install -y "Development Tools"
sudo dnf install -y re2c bison autoconf make libtool ccache libxml2-devel sqlite-devel libxml2 sqlite sqlite-devel

This is the “lazy” way of doing this. Gcc and friends (and other needed compilers) could be installed individually, probably saving a bit of time, storage and bandwidth. But as storage and bandwidth are relatively cheap and highly available, I’m going the easy/lazy route.

Let’s compile and install PHP:

./buildconf
./configure
make -j1 (should match number of cores - get via nproc)
make TEST_PHP_ARGS=-j4 test (set -j to # cores)
sudo make install

Voila! We have success. PHP is installed and ready for your scripting pleasure.

As a Container

There are times when we want to deploy a PHP8 app in a container. Here’s a Dockerfile that you might find helpful (again, make sure to update the URL and filenames for the latest):

ARG TARGETPLATFORM
FROM --platform=$TARGETPLATFORM oraclelinux:8-slim
RUN dnf update -y && dnf groupinstall "Development Tools" && dnf install -y sqlite sqlite-devel libxml2-devel libxml2 httpd httpd-devel
RUN cd /tmp && \
wget -O /tmp/php-8.1.5.tar.gz https://www.php.net/distributions/php-8.1.5.tar.gz && \
tar -xzvf /tmp/php-8.1.5.tar.gz -C /tmp && \
cd /tmp/php-8.1.5 && \
make clean && \
./buildconf && \
./configure --with-apxs2=$(which apxs) --enable-opcache && \
make && \
make install
RUN printf '<html>\n<body>\n<h1><?php print("Hello world!"); ?>\n</body>\n</html>' > /var/www/html/index.php && \
printf '<FilesMatch \.php$>\n SetHandler application/x-httpd-php\n</FilesMatch>' > /etc/httpd/conf.modules.d/00-php.conf
RUN systemctl restart httpd
RUN firewall-cmd --zone=public --add-port=80/tcp
RUN systemctl restart firewalld

EXPOSE 80/tcp
ENTRYPOINT []
CMD []

Notice how a build argument TARGETPLATFORM is used. This allows you to easily build a container for a different architecture. Say you’re on a amd64 (aka x86_64) system and would like to build a container image for arm64 (aka aarch64). This can be done with:

docker build --build-arg TARGETPLATFORM=linux/arm64 -t php8_ol8:latest .

How cool is that?! Yeah, I like it too!

Conclusion

Well, we’ve had a nice little journey. We now have a way to easily run PHP8 on Arm (A1) OCI instances running Oracle Linux 8. Having solutions for both instance and container workloads means that we have the flexibility to handle whatever comes our way! Keep the bits flowing until next time…

Want to discuss? Join our Slack channel and talk to other developers!

--

--

Tim Clegg
Oracle Developers

Polyglot skillset: software development, cloud/infrastructure architecture, IaC, DevSecOps and more. Employed at Oracle. Views and opinions are my own.