Installing PHP 7.4 on FreeBSD 14.0 with the Legacy Ports Collection

PHP is a widely adopted server-side scripting language primarily used for developing dynamic websites and applications. Although PHP 7.4 reached end-of-life (EOL) on November 28, 2022, some older projects may still rely on it for compatibility reasons. The current FreeBSD Ports branches no longer include PHP 7.4, but it can still be installed through a legacy branch.

This guide outlines the process of setting up PHP 7.4 on FreeBSD 14.0 using the 2022Q4 branch of the FreeBSD Ports Collection. You will compile PHP from source, configure PHP-FPM, and integrate it with the Apache web server to maintain support for legacy workloads.

Warning

PHP 7.4 is deprecated and no longer receives security patches. Since it has reached EOL, continuing to use it may expose your system to vulnerabilities. It should only be deployed for legacy applications that require it, and you should plan a migration to a supported version as soon as possible.

Prerequisites

Before starting, ensure that you:

  • Have access to a FreeBSD 14.0 system as a non-root sudo user.

Step 1: Install the FreeBSD Ports Collection

The FreeBSD Ports Collection allows you to compile and install third-party software from source. To obtain PHP 7.4, you must rely on the older 2022Q4 branch.

Update the package information on your server:

Install Git:

Clone the legacy Ports tree using the 2022Q4 branch:

$ sudo git clone --depth 1 https://git.FreeBSD.org/ports.git -b 2022Q4 /usr/ports

Verify that the /usr/ports directory exists:

Step 2: Compile and Install PHP 7.4

Since FreeBSD’s current Ports tree no longer supports PHP 7.4, you must compile it using the legacy branch. Follow the steps below to install dependencies, rebuild Perl, and then compile PHP 7.4.

Install required build tools:

$ sudo pkg install -y curl re2c zip cmake gperf libzip html2text libxml2 gettext-tools

Remove the existing Perl binary to prevent conflicts:

$ sudo pkg remove -y perl5

Enter the Perl 5.36 source directory:

$ cd /usr/ports/lang/perl5.36

Compile and install Perl 5.36:

$ sudo make BATCH=yes install clean

Next, navigate to the PHP 7.4 source directory:

$ cd /usr/ports/lang/php74

Build and install PHP 7.4:

$ sudo make BATCH=yes install clean

Step 3: Verify PHP Installation

Confirm the installed version of PHP:

Copy the default production configuration file:

$ sudo cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini

Review the active variables in the php.ini file:

$ cat /usr/local/etc/php.ini | egrep -v '^;|^$'

This command outputs all enabled configuration options, such as memory limits, execution time, and file upload settings. Adjust these according to the needs of your application.

Install PHP 7.4 Extensions

PHP extensions expand the functionality of PHP by adding features such as database connectivity, advanced data processing, and more. Follow the steps below to compile and enable the required PHP 7.4 extensions on your server.

Navigate to the extensions source directory:

$ cd /usr/ports/lang/php74-extensions

Run the configuration utility:

Use the arrow keys to navigate and press Space to select the desired extensions. After making your selections, press Enter to save and exit.

Once configured, compile and install the chosen extensions:

$ sudo make BATCH=yes install

Verify the installed PHP modules:

The output should display a list of installed modules, including database support, compression, XML processing, and more.

Compile and Install PHP 7.4 FPM

PHP-FPM (FastCGI Process Manager) efficiently handles PHP requests and improves performance. When compiling PHP 7.4 from the Ports Collection, PHP-FPM is automatically included. Follow these steps to configure and run the service.

Check the installed PHP-FPM version:

Enable PHP-FPM to start automatically on boot:

$ sudo sysrc php_fpm_enable=YES

Start the PHP-FPM service:

$ sudo service php-fpm start

Verify the service status to confirm it is running:

$ sudo service php-fpm status

Configure PHP-FPM 7.4

Now adjust PHP-FPM settings by editing the default pool configuration. These adjustments help optimize resource usage and improve performance based on the server’s workload.

Navigate to the configuration directory:

$ cd /usr/local/etc/php-fpm.d/

Ensure the www.conf file exists:

Edit the www.conf file with a text editor:

Adjust the following pool configuration values:

[www]
user = www
group = www
...
listen = /var/run/php-fpm.sock
listen.owner = www
listen.group = www
listen.mode = 0660
...
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
......

Explanation of key parameters:

  • [www]: Defines the pool name. Multiple pools can be managed independently.
  • user/group: Determines the system account PHP-FPM uses to execute scripts securely.
  • listen: Specifies the socket or TCP address used for FastCGI connections.
  • listen.owner / listen.group: Controls socket ownership to restrict access.
  • listen.mode: Defines permissions for the socket.
  • pm: Configures the process management strategy (dynamic scaling in this case).
  • pm.max_children: Limits maximum concurrent PHP workers.
  • pm.start_servers: Defines how many workers to preload at startup.
  • pm.min_spare_servers / pm.max_spare_servers: Manage idle worker thresholds to balance responsiveness and memory usage.

After editing, restart PHP-FPM to apply changes:

$ sudo service php-fpm restart

Check service status again to confirm it is running with the new configuration:

$ sudo service php-fpm status

Test and Use PHP 7.4

In this section, you will confirm that PHP 7.4 is installed and functioning properly by testing the PHP Command Line Interface (CLI), running a sample script, and configuring Apache to handle PHP-FPM requests via a UNIX socket. This ensures your server is running the intended PHP version.

Test PHP CLI

First, check the installed PHP version:

Navigate to your home directory:

Create a test file named greet.php:

Insert the following PHP code into the file:

Run the script using PHP CLI:

Configure Apache with PHP-FPM

Once PHP 7.4 works from the CLI, integrate it with Apache via PHP-FPM using a UNIX socket.

Install Apache:

$ sudo pkg install -y apache24

Enable Apache to start at boot:

$ sudo sysrc apache24_enable="YES"

Start the Apache service:

$ sudo service apache24 start

Check Apache’s status:

$ sudo service apache24 status

Edit the Apache configuration file:

$ sudo vim /usr/local/etc/apache24/httpd.conf

Uncomment the following modules by removing the # prefix:

LoadModule proxy_module libexec/apache24/mod_proxy.so
LoadModule proxy_fcgi_module libexec/apache24/mod_proxy_fcgi.so
LoadModule rewrite_module libexec/apache24/mod_rewrite.so

Create a virtual host configuration file:

$ sudo vim /usr/local/etc/apache24/Includes/php74.conf

Add the following configuration:

    ServerName localhost
    DocumentRoot "/usr/local/www/apache24/data"

    <Directory "/usr/local/www/apache24/data">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    

    
        SetHandler "proxy:unix:/var/run/php-fpm.sock|fcgi://localhost/"
    

    ErrorLog "/var/log/httpd-error.log"
    CustomLog "/var/log/httpd-access.log" combined

Create a test PHP file in the web root:

$ echo '<?php 
echo "<div style=\"text-align: center;\">
<h1>Greetings from centron!</h1>
</div>"; 
?>' | sudo tee /usr/local/www/apache24/data/test.php > /dev/null

Restart PHP-FPM and Apache:

$ sudo service php-fpm restart
$ sudo service apache24 restart

Check the services are running:

$ sudo service php-fpm status
$ sudo service apache24 status

Finally, open the following URL in your browser to verify the page:

http://your-server-ip/test.php

Conclusion

You have successfully installed PHP 7.4 on FreeBSD 14.0 using the legacy Ports Collection, configured PHP-FPM for performance optimization, and integrated it with Apache to handle FastCGI requests. You validated the installation through CLI tests and a sample script served over the web. While this setup enables legacy applications to run on PHP 7.4, keep in mind that it is deprecated and no longer receives security patches. It is strongly recommended to migrate to a supported version such as PHP 8.x for enhanced security, compatibility, and performance.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: