Install PHP on FreeBSD

This is a tutorial on how to use PHP on FreeBSD.

PHP, which stands for “PHP: Hypertext Preprocessor”, is a widely used open source, general-purpose scripting language that is particularly suitable for web development and can be embedded in HTML. The syntax is based on C, Java, and Perl, and is easy to learn.

This is a tutorial on how to use PHP on FreeBSD. We will install the following packages:

For this, we will run the following command:

$: pkg install php80 php80-dom php80-gd php80-phar php80-ctype php80-filter php80-iconv php80-json php80-curl php80-mysqli php80-pdo_mysql php80-sqlite3 php80-pdo_sqlite php80-tokenizer php80-readline php80-session php80-simplexml php80-xml php80-zip php80-zlib php80-bcmath php80-xmlwriter php80-posix php80-openssl php80-pecl-redis php80-fileinfo php80-soap openssl

Then we copy the php.ini-production template:

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

We will next edit the www conf:

$: nano /usr/local/etc/php-fpm.d/www.conf ⇒

listen = /var/run/php-fpm.sock
listen.owner = www
listen.group = www
listen.mode = 0660

In the /usr/local/etc/php.ini file, we need to find a section that configures the behavior of cgi.fix_pathinfo. It is commented out and set to “1” by default. We have to comment this out and set it to “0”. This prevents PHP from attempting to execute parts of the path if the file passed to process is not found. This could be used by malicious users to execute arbitrary code.

$: nano /usr/local/etc/php.ini ⇒

cgi.fix_pathinfo=0

Finally, we will activate PHP and start the PHP service:

$: service php-fpm enable
$: service php-fpm start

PHPUnit

PHPUnit is a free framework written in PHP for testing PHP scripts, which is particularly suitable for automated tests of individual units.

So that we can use PHPUnit under FreeBSD, we install the package phpunit8 with the following command:

$: pkg install phpunit8-php80

Composer

Composer is an application-oriented package manager for the PHP programming language. Composer executed via the command line and installs dependencies of a PHP program.

We install php-composer2 with the following command:

$: pkg install php80-composer2

Xdebug

Xdebug helps us debug our PHP scripts by providing a lot of valuable debug information.

We installed the pecl-xdebug extension with:

$: pkg install php80-pecl-xdebug

Then we add the following instructions to php.ini:

$: nano /usr/local/etc/php.ini ⇒

xdebug.remote_enable=1
xdebug.remote_port=9000

Finally, we restart the PHP service:

$: service php-fpm restart

Discuss...