PHP is a commonly used programming language in development of web applications and general dynamic web pages. PHP 8.0 is a major update from version 7.4 of PHP, with some improvements and new set of features. Most website content management systems like Joomla, WordPress, and Drupal are written in PHP language. If you’re interested in running PHP applications on FreeBSD system, then this article has been created for you.
PHP 8.0 release contains dozen of optimizations and new features which include:
- named arguments
- union types
- attributes
- constructor property promotion
- match expression
- nullsafe operator
- JIT,
- improvements in the type system, error handling, and consistency.
In this article, we shall cover all the necessary steps of installing and using PHP 8.0 on FreeBSD 13 / FreeBSD 12. For most web applications, PHP is used alongside database system and a web server such as nginx and apache. However, the installation of web servers/database systems is not covered in detail on this post. We’ll only include installation of Apache web server since we need it to demonstrate our PHP 8.0 installation on FreeBSD 13 / FreeBSD 12 is functional.
Step 1 – Update FreeBSD system
We’ll kickstart our installation with standard system upgrade:
$ su - root
# pkg update
Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
# pkg upgrade
Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
Checking for upgrades (21 candidates): 100%
Processing candidates (21 candidates): 100%
The following 1 package(s) will be affected (of 0 checked):
Installed packages to be REINSTALLED:
m4-1.4.19,1 (options changed)
Number of packages to be reinstalled: 1
253 KiB to be downloaded.
Proceed with this action? [y/N]: y
[1/1] Fetching m4-1.4.19,1.pkg: 100% 253 KiB 258.6kB/s 00:01
Checking integrity... done (0 conflicting)
[1/1] Reinstalling m4-1.4.19,1...
[1/1] Extracting m4-1.4.19,1: 100%
Alternative commands used to perform binary updates for the base system:
freebsd-update fetch
freebsd-update install
Step 2 – Install PHP 8.0 on FreeBSD 13 / FreeBSD 12
PHP 8.0 packages are available on FreeBSD binary distribution repositories with a number of modules. To install PHP 8.0 onFreeBSD 13 / FreeBSD 12, run the commands below:
[email protected]:~ # pkg install php80
Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
The following 4 package(s) will be affected (of 0 checked):
New packages to be INSTALLED:
libargon2: 20190702
libxml2: 2.9.12
pcre2: 10.39
php80: 8.0.13
Number of packages to be installed: 4
The process will require 40 MiB more space.
6 MiB to be downloaded.
Proceed with this action? [y/N]: y
[1/4] Fetching php80-8.0.13.pkg: 100% 4 MiB 4.3MB/s 00:01
[2/4] Fetching libxml2-2.9.12.pkg: 100% 830 KiB 849.6kB/s 00:01
[3/4] Fetching libargon2-20190702.pkg: 100% 65 KiB 67.1kB/s 00:01
[4/4] Fetching pcre2-10.39.pkg: 100% 1 MiB 1.3MB/s 00:01
Checking integrity... done (0 conflicting)
[1/4] Installing libxml2-2.9.12...
[1/4] Extracting libxml2-2.9.12: 100%
[2/4] Installing libargon2-20190702...
[2/4] Extracting libargon2-20190702: 100%
[3/4] Installing pcre2-10.39...
[3/4] Extracting pcre2-10.39: 100%
[4/4] Installing php80-8.0.13...
[4/4] Extracting php80-8.0.13: 100%
Copy the sample PHP config file:
cp -v /usr/local/etc/php.ini-production /usr/local/etc/php.ini
Install PHP 8.0 extensions on FreeBSD 13 / FreeBSD 12
To install commonly used PHP extensions, run the following commands:
pkg install vim php80-xml mod_php80 php80-zip php80-mbstring php80-zlib php80-curl php80-mysqli php80-gd php80-gd
Proceed with the installation by pressing y
....
The following 29 package(s) will be affected (of 0 checked):
New packages to be INSTALLED:
apache24: 2.4.52
apr: 1.7.0.1.6.1_1
ca_root_nss: 3.69_1
curl: 7.79.1
db5: 5.3.28_7
expat: 2.4.1
fontconfig: 2.13.94_1,1
freetype2: 2.11.0
gdbm: 1.21
giflib: 5.2.1
jansson: 2.14
jbigkit: 2.1_1
jpeg-turbo: 2.1.1
libgd: 2.3.1,1
libnghttp2: 1.44.0
libssh2: 1.9.0_3,3
libzip: 1.7.3
mod_php80: 8.0.13
oniguruma: 6.9.7.1
pcre: 8.45
php80-curl: 8.0.13
php80-gd: 8.0.13
php80-mbstring: 8.0.13
php80-xml: 8.0.13
php80-zip: 8.0.13
php80-zlib: 8.0.13
png: 1.6.37_1
tiff: 4.3.0
webp: 1.2.1
Number of packages to be installed: 29
The process will require 123 MiB more space.
28 MiB to be downloaded.
Proceed with this action? [y/N]: y
PHP extensions configuration files are usually located in below directory:
# ls /usr/local/etc/php
Step 3 – Install Apache web server
We’ll need Apache or any other web server to serve web pages to external users on a website. Let’s install the package using pkg command:
pkg install apache24
Getting details about installed package:
# pkg info apache24
apache24-2.4.52
Name : apache24
Version : 2.4.52
Installed on : Wed Dec 22 23:11:15 2021 EAT
Origin : www/apache24
Architecture : FreeBSD:13:amd64
Prefix : /usr/local
Categories : www
Licenses : APACHE20
Maintainer : [email protected]
WWW : https://httpd.apache.org/
Comment : Version 2.4.x of Apache web server
....
Start and enable Apache service after the installation:
[email protected]:~ # sysrc apache24_enable=yes
apache24_enable: -> yes
[email protected]:~ # service apache24 start
You might want to check the status of apache24 service:
[email protected]:~ # service apache24 status
apache24 is running as pid 546.
Step 4 – Test PHP 8.0 installation on FreeBSD 13/12
Let’s create a PHP code which will output details about the system and PHP information.
tee /usr/local/www/apache24/data/info.php<<EOF
<?php
phpinfo();
?>
EOF
Configure handler for PHP pages
# vim /usr/local/etc/apache24/Includes/php.conf
<IfModule dir_module>
DirectoryIndex index.php index.html
<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
</IfModule>
Save and close the file then restart apache service:
service apache24 restart
Access the php application on your web browser – http://serverip_or_hostname/info.php
. You should get a page that looks similar to below:
Hurray!, you now have a working installation of PHP 8.0 on FreeBSD 13 / FreeBSD 12 system. Other PHP applications can be hosted on this system. If you need a database server, check out the guide in the link provided.