In this article we will learn how to configure multiple versions of PHP on Ubuntu. The first question that may cross our minds is: “But why do this? Why configure more than one version of PHP on the same server?” In a production environment, this may not make much sense. However, when working in a development environment, it might prove necessary.
When testing PHP 8, we can see this necessity. Imagine that we want to run tests on the different Alpha and Beta versions, even to compare the particularities of each. To avoid having to remove one version and install another every time, we can configure multiple versions that can run in parallel. This is what we will do in this article.
This installation will be performed without using apt-get or any other package manager. We will also not use Apache. The installation and configuration will take place from the PHP files we download from the source.
Installing Beta 1 version of PHP 8
We have already shown how to install the Alpha 3 version of PHP 8. You can check this content in the article PHP 8 - Installation and Configuration on Ubuntu 20.04 and Windows.
The goal of this article will be to install Beta 1 version of PHP 8, released last Thursday, and allow the older version to continue functioning. The download of the PHP 8 files and their extraction will occur in the same way as we taught in the article above. However, its configuration will be slightly different.
Configuring Beta 1 version
First, we will create a custom folder to store the settings for PHP Beta 1. We will use the following command:
sudo mkdir /usr/php8-beta1
We created this folder in the usr directory, but we could use another directory if we wanted.
Now let's proceed with its configuration. We will add the --prefix option in the configuration command, specifying another directory for it, namely, the same directory we just created. We will use the command below:
sudo ./configure --prefix=/usr/php8-beta1
After performing the configuration, we will execute the actual installation using the command sudo make && sudo make install.
With the installation complete, we can run the PHP commands directly by accessing the binary of this service. For example:
/usr/php8-beta1/bin/php --version
This command would return the following result:
Note that the version was returned, showing that PHP was installed successfully.
However, when working with the command line, we are used to providing only the program name, such as php --version for example. We don’t want to specify the absolute path to the binary every time. How could we resolve this issue?
One solution is to add the PHP binary in a custom way to the default directory that Ubuntu uses to execute the Operating System binaries. In Ubuntu 20.04, this directory is /usr/local/bin. So, we need to create a reference in this directory that will point to the original binary. A command we can use is ln, which creates links in Linux. We can say that these links work like “shortcuts to programs in Windows,” but applied in the Linux environment. To create the link, we will use this command:
sudo ln -sT /usr/php8-beta1/bin/php /usr/local/bin/php8-beta1
Now we have a new binary called php8-beta1. Since it is custom, we could specify any other name we wanted. Thus, when executing php8-beta1 --version, we will see the same result as the command we executed earlier, specifying the absolute path to the PHP binary.
If we go to the /var/www/html directory, execute sudo php8-beta1 -S localhost:80 and access http://localhost/info.php in the browser, we will see the following result:
Incredible! PHP is running correctly in our browser. ?
Making the same configuration with Alpha 3 version
If we wanted to do the same with Alpha 3 version, we would just need to repeat the process:
sudo mkdir /usr/php8-alpha3
sudo ./configure --prefix=/usr/php8-alpha3
sudo make && sudo make install
sudo ln -sT /usr/php8-alpha3/bin/php /usr/local/bin/php8-alpha3
When executing php8-alpha3 --version, we would see the following result:
Impressive, isn’t it?
Conclusion
In this article, we configured two versions of PHP on the same Ubuntu server. The best part is that we did this without using apt-get or any other tool, as we mentioned at the beginning. Now we are ready to configure as many versions as we want!!! ?
If you liked this post about PHP 8, be sure to check out the daily content we publish on our YouTube channel.
See you in the next article :)