FreeBSD 13: Apache, MySql, PHP

Server administration

FAMP is an acronym that stands for FreeBSD (operating system), Apache (web server), MySQL (database server), and PHP (to process dynamic PHP content). In this guide, we’ll get a FAMP stack installed on a FreeBSD 13.2 server using pkg, the FreeBSD package manager.

Explanations will be as short as possible because this is more like a step-by-step instruction and reminder for the future usage.

In all commands, I will omit the sudo part because I am logged in as root. If you are logged in as another user with sudo privileges, please add sudo to the beginning of each command.

1. Installation

Download the latest version of FreeBSD from the official website
https://www.freebsd.org/where/

Install FreeBSD following official documentation.
https://docs.freebsd.org/en/books/handbook/bsdinstall/
No need to explain OS installation in details as official documentation describes everything.
Just follow the on-screen instructions to select the language, keyboard layout, and other settings for your installation.

2. Configuration

2.1. Updating packages

It is generally not advised to use the Ports Collection in conjunction with the binary packages provided via pkg.
Run pkg command line:

pkg

Update the local package repositories catalogues

pkg update -f

2.2. Midnight Commander installation

By default the mc port is compiled with X11 support, this is excessive on the server. Let’s install Midnight Commander without X11 dependencies.

pkg install mc-nox11

2.3. Other misc configurations

Load coretemp driver at boot time automatically

vi /boot/loader.conf

Append the following for Intel CPUs:

coretemp_load="YES"

3. Apache 2.4 installation

pkg install apache24

To run apache www server from startup, add this line in your /etc/rc.conf

apache24_enable="YES"

Create new empty file /usr/local/ect/apache24/Includes/httpd-custom.conf
And following line:

ServerName localhost

Start Apache with the command

service apache24 start

To restart apache

service apache24 restart

4. MariaDB 10.6 installation

MariaDB Server is open source relational databases. It’s made by the original developers of MySQL and guaranteed to stay open source.

pkg install mariadb106-server

Add following line to /etc/rc.conf

mysql_enable="YES"

Start MariaDB

service mysql-server start

Run this interactive script to perform basic security hardening of the installation:

/usr/local/bin/mysql_secure_installation

Add new user within the MySQL shell:

CREATE USER 'developer'@'localhost' IDENTIFIED BY '[pass]';
GRANT ALL PRIVILEGES ON * . * TO 'developer'@'localhost';
FLUSH PRIVILEGES;

5. PHP 8.2, PHP 8.1, PHP8.0 installation

This is a crucial part of the article as our objective is to install multiple PHP versions that can coexist on the same server. By doing so, we can utilize different PHP versions for specific websites or applications.

To install multiple versions of PHP, you have two options: using jails or building PHP from ports with separate PREFIX and PHPBASE for each version. In this case, we will be utilizing the PREFIX approach.

5.1. PHP installation

cd /usr/ports/lang/php82
make PREFIX=/usr/local/php82 PHPBASE=/usr/local/php82 install clean BATCH=yes

cd /usr/ports/lang/php81
make PREFIX=/usr/local/php81 PHPBASE=/usr/local/php81 install clean BATCH=yes

cd /usr/ports/lang/php80
make PREFIX=/usr/local/php80 PHPBASE=/usr/local/php80 install clean BATCH=yes

When you install PHP with PREFIX, it’s always produces errors of missing libraries.
Need to fix these conflicts. If possible, to install libraries from packages. Or build from ports without PREFIX.

This helped me:

pkg install gettext-tools
pkg install gmake
pkg install help2man
pkg install texinfo
pkg install m4
pkg install autoconf-2.71
pkg install pkgconf

or

cd /usr/ports/devel/pkgconf/ && make reinstall clean
pkg install libargon2

or

cd /usr/ports/security/libargon2/ && make install clean

And then finish PHP installation.

Add symlink for default PHP version

ln -sfn /usr/local/php82/bin/php /usr/local/bin/php

5.2. PHP extensions installation

cd /usr/ports/lang/php82-extensions
make config
make PREFIX=/usr/local/php82 PHPBASE=/usr/local/php82 install clean BATCH=yes
cd /usr/ports/lang/php81-extensions
make config
make PREFIX=/usr/local/php81 PHPBASE=/usr/local/php81 install clean BATCH=yes
cd /usr/ports/lang/php80-extensions
make config
make PREFIX=/usr/local/php80 PHPBASE=/usr/local/php80 install clean BATCH=yes

Select following extensions additionally to already selected:
BCMATH, CURL, EXIF, FILEINFO, GD, GETTEXT, MBSTRING, MYSQLI, PDO_MYSQL, SOAP, SOCKETS, ZIP, ZLIB

Errors may arise during installation because we use PREFIX approach. Need to fix them by installing packages which cause errors manually first.

This helped me:

pkg install devel/py-wheel
pkg install devel/meson
pkg install devel/jsoncpp
pkg install devel/libuv
pkg install freetype2
pkg install fontconfig
pkg install libdeflate
pkg install jpeg-turbo
pkg install oniguruma
pkg install libzip

cd /usr/ports/graphics/gd && make reinstall clean

Afterward, complete with the installation of PHP extensions. Typically, if you have successfully installed PHP 8.2 extensions, you should not encounter any issues when installing PHP 8.1 and PHP 8.2 extensions.

5.3. PHP configuration

Add symlinks

ln -sfn /usr/local/php80/etc/rc.d/php-fpm /usr/local/bin/php80-fpm
ln -sfn /usr/local/php81/etc/rc.d/php-fpm /usr/local/bin/php81-fpm
ln -sfn /usr/local/php82/etc/rc.d/php-fpm /usr/local/bin/php82-fpm

ln -sfn /usr/local/php80/etc/rc.d/php-fpm /usr/local/etc/rc.d/php80-fpm
ln -sfn /usr/local/php81/etc/rc.d/php-fpm /usr/local/etc/rc.d/php81-fpm
ln -sfn /usr/local/php82/etc/rc.d/php-fpm /usr/local/etc/rc.d/php82-fpm

Add following to /usr/local/ect/apache24/Includes/httpd-custom.conf

LoadModule proxy_module libexec/apache24/mod_proxy.so
LoadModule proxy_fcgi_module libexec/apache24/mod_proxy_fcgi.so
LoadModule proxy_http_module libexec/apache24/mod_proxy_http.so
LoadModule vhost_alias_module libexec/apache24/mod_vhost_alias.so
LoadModule rewrite_module libexec/apache24/mod_rewrite.so
LoadModule ssl_module libexec/apache24/mod_ssl.so

<Directory "/usr/local/www">
    AllowOverride All
    Allow from all
    Require all granted
    DirectoryIndex index.html index.htm index.php
</Directory>

<Directory "/usr/local/www/apache24/data">
    AllowOverride All
    Allow from all
</Directory>

Create /usr/local/ect/apache24/Includes/php-fpm.conf

<FilesMatch \.php$>
    #SetHandler proxy:unix:/tmp/php-fpm.sock|fcgi://localhost/
    # use the following line instead if you didn't set PHP-FPM to listen on a Unix socket
    SetHandler proxy:fcgi://127.0.0.1:9002
</FilesMatch>

Then perform interesting trick to have possibility to start, stop and restart different versions of PHP individually.
Update /usr/local/php8X/etc/rc.d/php-fpm files for each php version.
And replace php_fpm with php8X_fpm everywhere in this file
Also replace pidfile="/var/run/php-fpm.pid" with pidfile="/var/run/php8X-fpm.pid"

Add /usr/local/php8X/etc/php-fpm.d/php-fpm.conf

pid = run/php8X-fpm.pid
error_log = log/php8X-fpm.log
syslog.ident = php8X-fpm

Replace in /usr/local/php8X/etc/php-fpm.d/www.conf

listen = 127.0.0.1:900X

Where 900X is the port which php8X-fpm will use.

Add to /etc/rc.conf

php80_fpm_enable="YES"
php81_fpm_enable="YES"
php82_fpm_enable="YES"

And you can use now following commands:

php8X-fpm start
php8X-fpm stop

Conclusion

Now that you have FAMP stack installed, you can install PHP websites and application on your server.

In the upcoming article focused on FreeBSD, we will cover the following topics:

  • Installation and configuration of the Samba Server for convenient code updates in the Apache webserver folder.
  • Setting up the Unbound server for DNS caching and as a DNS server to manage the LAN zone.
  • Apache Virtual hosts configuration with VirtualDocumentRoot directive support for easy use of dynamic subdomains.

Leave a Comment

Your email address will not be published. Required fields are marked *

To top