> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/ask/flat/chapter-38-web-servers/di-38.4-jie-php.md).

# 38.4 PHP

PHP 8.x introduces a JIT compiler, with significant improvements to its type system and security mechanisms. This section covers installation via pkg, PHP-FPM configuration, and integration with Apache and MySQL.

## Installing PHP

Install using the pkg package manager:

```sh
# pkg install php84 php84-extensions mod_php84
```

> **Tip**
>
> Conflicts may exist between different PHP modules, which can cause Ports compilation to fail; therefore, enabling all PHP extensions is not recommended. Installing via pkg is recommended, as it handles inter-module dependencies better.

Alternatively, install PHP using Ports:

```sh
# cd /usr/ports/lang/php84/ && make install clean
# cd /usr/ports/lang/php84-extensions/ && make install clean
# cd /usr/ports/www/mod_php84 && make install clean
```

After installation, you can display the installed PHP version information with the following command:

```sh
# php -v
PHP 8.4.4 (cli) (built: Feb 15 2025 01:05:08) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.4.4, Copyright (c) Zend Technologies
```

> **Note**
>
> The number `84` may vary depending on the PHP version. You can check the currently available PHP versions before installing.
>
> ```sh
> # pkg search -o lang/php    # Search for packages starting with lang/php in FreeBSD Ports/Packages
> lang/php-mode.el               PHP mode for GNU Emacs
> lang/php81                     PHP Scripting Language (8.1.X branch)
> lang/php81-extensions          "meta-port" to install PHP extensions
> lang/php82                     PHP Scripting Language (8.2.X branch)
> lang/php82-extensions          "meta-port" to install PHP extensions
> lang/php83                     PHP Scripting Language (8.3.X branch)
> lang/php83-extensions          "meta-port" to install PHP extensions
> lang/php84                     PHP Scripting Language (8.4.X branch)
> lang/php84-extensions          "meta-port" to install PHP extensions (8.4.X branch)
> ```

## Configuring the PHP Daemon

After installation, you need to complete the basic PHP configuration, including copying the configuration file and starting the PHP-FPM (FastCGI Process Manager) service.

Directory structure:

```sh
/usr/local/
├── etc/
│   ├── php.ini                    # PHP main configuration file
│   ├── php.ini-production         # PHP production environment sample configuration file
│   ├── php-fpm.conf               # PHP-FPM main configuration file
│   └── apache24/
│       └── Includes/
│           └── php.conf           # Apache PHP configuration file
└── www/
    ├── apache24/
    │   └── data/
    │       └── info.php           # PHP info test file (Apache)
    └── nginx/
        └── info.php               # PHP info test file (Nginx)
```

The PHP sample configuration file is located at **/usr/local/etc/php.ini-production**, which contains recommended settings suitable for production environments.

Copy the production environment PHP configuration file as the default configuration file, and display the copy process:

```sh
# cp -v /usr/local/etc/php.ini-production /usr/local/etc/php.ini
/usr/local/etc/php.ini-production -> /usr/local/etc/php.ini
```

Set the PHP-FPM service to start at boot:

```sh
# service php_fpm enable
php_fpm enabled in /etc/rc.conf
```

Start the PHP-FPM service to apply the configuration; the system will automatically check the configuration file syntax before starting:

```sh
# service php_fpm start
Performing sanity check on php-fpm configuration:
[25-Feb-2025 20:28:32] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful
Starting php_fpm.
```

Check the current status of the PHP-FPM service to confirm whether it is running normally:

```sh
# service php_fpm status
php_fpm is running as pid 2592.
```

View post-installation information to understand the configuration instructions for mod\_php84:

```sh
# pkg info -D mod_php84
```

### References

* ComputingForGeeks. Install PHP 8.0 on FreeBSD 13 / FreeBSD 12\[EB/OL]. (2021-12-13)\[2026-03-25]. <https://computingforgeeks.com/how-to-install-php-8-on-freebsd-system/>. Detailed explanation of PHP 8 installation steps and dependency handling on FreeBSD.
* FreeBSD Project. PHP-FPM(8)\[EB/OL]. \[2026-03-25]. <https://man.freebsd.org/cgi/man.cgi?query=php-fpm&sektion=8>. Systematic description of the PHP-FPM process management mechanism and configuration parameters.
* FreeBSD Forums. Apache and PHP\[EB/OL]. \[2026-03-25]. <https://forums.freebsd.org/threads/apache-and-php.80625/>. Provides practical experience for Apache and PHP integration configuration, suggesting installation of mod\_php84.

## PHP Configuration for Apache

If using Apache as the web server, corresponding configuration is needed so that Apache can correctly handle PHP files.

Edit the **/usr/local/etc/apache24/Includes/php.conf** file and add

```apache
<FilesMatch "\.php$">                           # Match files ending with .php
    SetHandler application/x-httpd-php          # Pass matched files to PHP for processing
</FilesMatch>
<FilesMatch "\.phps$">                          # Match files ending with .phps
    SetHandler application/x-httpd-php-source   # Display matched files as PHP source code
</FilesMatch>
```

Edit the **/usr/local/www/apache24/data/info.php** file and add:

```php
<?php
    phpinfo();
?>
```

This outputs the current PHP configuration information and runtime environment.

Restart the services:

```sh
# service php_fpm restart   # Restart the PHP-FPM service
# service apache24 restart  # Restart the Apache 24 service
```

Access `ip/info.php`, for example `http://192.168.179.150/info.php`:

> **Tip**
>
> The **192.168.179.150** in the above example is a placeholder and needs to be replaced with the actual value.

![Apache PHP8 FreeBSD](/files/PS0WUpH3p8oP7rHEzpQV)

## PHP Configuration for Nginx

If using Nginx as the web server, corresponding configuration is also needed so that Nginx can handle PHP files through PHP-FPM.

Edit the **/usr/local/etc/nginx/nginx.conf** file with the following modifications:

Remove all comment symbols `#` from the following lines:

```nginx
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
```

Modify as follows:

```nginx
location ~ \.php$ {                                 # Match .php file requests
    root           /usr/local/www/nginx;           # Website root directory, modify according to actual path
    fastcgi_pass   127.0.0.1:9000;                 # FastCGI service address
    fastcgi_index  index.php;                       # Default FastCGI index file
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  # Full PHP script path, where $document_root represents the website root directory
    include        fastcgi_params;                 # Include FastCGI parameters file
}
```

Edit the **/usr/local/www/nginx/info.php** file and add test code to display PHP configuration information:

```php
<?php
    phpinfo();
?>
```

This displays the current PHP configuration information and runtime environment.

Restart the services to apply the configuration changes:

```sh
# service php_fpm restart    # Restart the PHP-FPM service to apply configuration changes
Performing sanity check on php-fpm configuration:
[25-Feb-2025 20:59:12] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful
Starting php_fpm.
# service nginx restart    # Restart the Nginx service to apply configuration changes
Performing sanity check on nginx configuration:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
Stopping nginx.
Waiting for PIDS: 1153.
Performing sanity check on nginx configuration:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
Starting nginx.
```

Access `IP/info.php`, for example `http://192.168.179.150/info.php`:

![Nginx PHP8 FreeBSD](/files/0DDOgocTZf10lItwLjbG)

### References

* Vultr. How to Install PHP and PHP-FPM on FreeBSD 14.0\[EB/OL]. (2025-04-01)\[2026-03-25]. <https://docs.vultr.com/how-to-install-php-and-php-fpm-on-freebsd-14-0>. This article was previously the primary reference, providing detailed steps for Nginx and PHP-FPM integration.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.bsdcn.org/ask/flat/chapter-38-web-servers/di-38.4-jie-php.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
