How to Install Apache, MySQL and PHP (FAMP Stack) on FreeBSD 15
Install Apache 2.4, MySQL 8.4 and PHP 8.5 on FreeBSD 15.1 with pkg, connect PHP to Apache and verify the stack with a test page.
A FAMP stack is the FreeBSD counterpart of LAMP: FreeBSD, the Apache HTTP server, MySQL and PHP. This guide builds one on FreeBSD 15.1-RELEASE with binary packages: Apache 2.4, MySQL 8.4 LTS and PHP 8.5 running as an Apache module. At the end, Apache serves PHP pages that can talk to a MySQL database. The same commands work on FreeBSD 14.5, the other supported release line. FreeBSD 12 reached end of life at the end of 2023, and the MySQL 8.0 and PHP 7.3 packages from that era are no longer maintained (the MySQL 8.0 port is marked deprecated and is scheduled for removal at the end of 2026).
Requirements
- A server running FreeBSD 15.1-RELEASE or 14.5-RELEASE.
- Root access. All commands in this guide are run as
root; if you work from a regular account such asdeploy, become root withsu -or prefix the commands withsudoordoasif you have installed one of them. - Ports 80 and 443 allowed in your provider's firewall, if it has one.
Update the system
Check the release and bring the base system and packages up to date:
freebsd-version
freebsd-update fetch install
pkg update
pkg upgrade -y
freebsd-update applies to systems installed from the traditional distribution sets. If your FreeBSD 15 system was installed with base packages (pkgbase), freebsd-update will tell you so; in that case pkg upgrade updates the base system as well. Reboot if a new kernel was installed.
Step 1: Install Apache
pkg install -y apache24
Enable the service at boot and start it:
sysrc apache24_enable=YES
service apache24 start
service apache24 status
Apache warns at startup when it cannot determine the server's name. Set it once in /usr/local/etc/apache24/httpd.conf by uncommenting and editing the ServerName line:
ServerName example.com:80
Open http://203.0.113.10/ (your server's address) in a browser. The default page, served from /usr/local/www/apache24/data, reads "It works!".
Step 2: Install MySQL 8.4
MySQL 8.4 is the long-term support series and the default MySQL version in the FreeBSD ports tree. The server package pulls in the matching client.
pkg install -y mysql84-server
sysrc mysql_enable=YES
service mysql-server start
service mysql-server status
The first start initializes the data directory with a root account that has no password. Fix that immediately with the hardening script, which sets the root password, removes anonymous accounts and the test database, and disables remote root logins:
mysql_secure_installation
Answer Y to the questions unless you have a reason not to. Then create a database and a dedicated account for your application instead of letting it connect as root:
mysql -u root -p
CREATE DATABASE appdb;
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'ChangeThisPassword';
GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost';
EXIT;
The package installs a default configuration at /usr/local/etc/mysql/my.cnf; that is the file to edit when you need to tune the server.
Step 3: Install PHP 8.5
Install the PHP interpreter, the Apache module and the MySQL extension:
pkg install -y php85 mod_php85 php85-mysqli
PHP runs without a configuration file until you provide one. Copy the production template into place:
cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini
Check the version and the loaded extensions:
php -v
php -m
Most applications need more extensions than the base package provides. Each one is a separate package named php85-<extension>. Search for them and install what your application's documentation lists, for example:
pkg search '^php85-'
pkg install -y php85-pdo_mysql php85-mbstring php85-curl php85-gd php85-zip php85-xml php85-intl php85-session php85-ctype
OPcache is built into PHP 8.5 itself, so there is no separate php85-opcache package as there was for earlier PHP versions. Restart Apache after installing extensions.
Step 4: Tell Apache to run PHP files
Installing mod_php85 adds the LoadModule php_module line to httpd.conf for you, but Apache still has to be told which files to hand to PHP. Apache on FreeBSD automatically includes files in modules.d whose names start with three digits and an underscore. Create /usr/local/etc/apache24/modules.d/001_mod-php.conf with this content:
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
Note the backslash before the dot in each pattern: a pattern written as "/.php$", as some older tutorials show, does not match and PHP files are then sent to the browser as plain text.
Check the syntax and restart Apache:
apachectl configtest
service apache24 restart
Confirm that the PHP module is loaded together with the prefork MPM, which mod_php requires:
httpd -M | grep -E 'php|mpm'
Step 5: Test PHP and the database connection
Create a test page in the document root:
echo '<?php phpinfo();' > /usr/local/www/apache24/data/info.php
Browse to http://203.0.113.10/info.php. You should see the PHP information page, with "Apache 2.0 Handler" as the server API and a mysqli section further down. To test the database account from PHP on the command line:
php -r '$db = new mysqli("localhost", "appuser", "ChangeThisPassword", "appdb"); echo $db->server_info, PHP_EOL;'
It prints the MySQL server version when the connection succeeds. Remove the information page afterwards, because it exposes details about your configuration:
rm /usr/local/www/apache24/data/info.php
Where things are
| Item | Location |
|---|---|
| Apache main configuration | /usr/local/etc/apache24/httpd.conf |
| Your own Apache snippets (virtual hosts) | /usr/local/etc/apache24/Includes/*.conf |
| Default document root | /usr/local/www/apache24/data |
| Apache logs | /var/log/httpd-access.log, /var/log/httpd-error.log |
| PHP configuration | /usr/local/etc/php.ini, /usr/local/etc/php/*.ini |
| MySQL configuration | /usr/local/etc/mysql/my.cnf |
| MySQL data | /var/db/mysql |
Files in Includes ending in .conf are loaded automatically, so that is the place for a virtual host per site. After changing Apache or PHP settings, run apachectl configtest and service apache24 reload.
Keep the stack updated
pkg update
pkg upgrade
pkg audit -F lists installed packages with known vulnerabilities. Restart Apache after PHP upgrades so the new module is loaded. MySQL 8.4 performs its own data upgrade at startup; there is no mysql_upgrade step any more. Add HTTPS before putting a site into production; the site has separate guides for TLS certificates.
Frequently asked questions
Should I use mod_php or PHP-FPM?
mod_php is the shortest path and is what this guide uses, but it ties Apache to the prefork MPM. The FreeBSD package itself suggests considering PHP-FPM, in line with the Apache project's recommendation. For that setup, skip mod_php85, enable php_fpm_enable=YES with sysrc, start php_fpm, and have Apache pass .php requests to it through mod_proxy_fcgi. Do not enable both at once.
Can I use MariaDB instead of MySQL?
Yes. MariaDB is packaged for FreeBSD under names such as mariadb118-server; run pkg search mariadb to see the versions currently offered. It uses the same mysql_enable setting and mysql-server service name. Install one or the other, not both.
Why does pkg offer older versions than the ones named here?
FreeBSD releases use the quarterly package branch by default, which only receives security and bug fixes between quarters. The package names (apache24, mysql84-server, php85) stay the same; only patch versions differ from the latest branch.
Do I need to open the firewall on the server?
A default FreeBSD installation has no packet filter enabled, so Apache is reachable as soon as it starts. If you have enabled PF or IPFW, add rules that allow TCP ports 80 and 443.