Hello, friends. Every day more and more people make the jump from Linux to FreeBSD and it is a good time to help them with this post where you will learn how to install PHP 8 on FreeBSD 12 along with Apache.
Recently a new version of PHP has been released which is a major release promising many things like improved performance. This makes it an attractive release and already many open source applications have started to support it. So it is also the case of FreeBSD which is already leaping.
Install PHP 8 on FreeBSD 12
In this post, we will use Apache so that together with FreeBSD we have a small server where we will be able to test the operation of the same one. Also, we will run the commands with the root user but if your user has sudo access, you can use it too.
So, let’s get started.
Install Apache on FreeBSD
First of all, you have to install the Apache webserver from the official repositories, just run:
pkg install apache24 Updating FreeBSD repository catalogue... FreeBSD repository is up to date. All repositories are up to date. The following 13 package(s) will be affected (of 0 checked): New packages to be INSTALLED: apache24: 2.4.46_2 apr: 1.7.0.1.6.1_1 ca_root_nss: 3.58 curl: 7.74.0 db5: 5.3.28_7 expat: 2.2.10 gdbm: 1.18.1_1 jansson: 2.13.1 libnghttp2: 1.42.0 libxml2: 2.9.10_2 pcre: 8.44 perl5: 5.32.1_1 readline: 8.0.4 Number of packages to be installed: 13 The process will require 157 MiB more space. 36 MiB to be downloaded. Proceed with this action? [y/N]:
The command will also install several dependencies that are necessary to install Apache.
After installing it, make it start with the system, this is recommended.
sysrc apache24_enable=yes
After that, start the service:
service apache24 start
This way we will have Apache on FreeBSD. Do not forget to open ports 80
and 443
of your Firewall so that it can run correctly.
Install PHP 8 on FreeBSD
Now it is the turn of PHP 8, which fortunately is already in the official repositories. So we will be able to install it without any problems – some of the PHP modules will have to be installed from ports or by compiling the source code but they are few.
So run the following command to install PHP 8 and some of its main modules
pkg install php80 mod_php80 php80-mbstring php80-zlib php80-curl php80-gd php80-gd Updating FreeBSD repository catalogue... FreeBSD repository is up to date. All repositories are up to date. The following 18 package(s) will be affected (of 0 checked): New packages to be INSTALLED: fontconfig: 2.13.92_2,1 freetype2: 2.10.4 giflib: 5.2.1 jbigkit: 2.1_1 jpeg-turbo: 2.0.6 libargon2: 20190702 libgd: 2.3.1,1 mod_php80: 8.0.3 oniguruma: 6.9.6 pcre2: 10.36 php80: 8.0.3 php80-curl: 8.0.3 php80-gd: 8.0.3 php80-mbstring: 8.0.3 php80-zlib: 8.0.3 png: 1.6.37 tiff: 4.2.0 webp: 1.1.0 Number of packages to be installed: 18 The process will require 66 MiB more space. 12 MiB to be downloaded. Proceed with this action? [y/N]:
With Apache and PHP installed, we have to make some configurations to get them working.
Configuring Apache to work with PHP
The configuration we have to do is quite simple. In the /usr/local/etc/apache24/Includes/
directory we have to add a file where PHP support is added.
In my case, I like nano so I have installed it.
pkg install nano
And with it create and edit the file:
nano /usr/local/etc/apache24/Includes/php.conf
And add the following content:
<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 the changes and close the editor.
To verify if everything went well, it is necessary to make a sample file with some PHP code in the default Apache DocumentRoot.
nano /usr/local/www/apache24/data/info.php
And add
<?php phpinfo(); ?>
Save the changes and close the editor. To apply all changes made, restart Apache.
service apache24 restart
Now open your web browser and go to http://your-server/info.php
and you will see the following
So, PHP 8 is ready for work.
Before I added the apache configuration, my php was running as text.
After adding the configuration, my server keeps making me download the file instead of displaying html and php files.