Install Apache2, MariaDB and PHP (FAMP stack) on FreeBSD
A FAMP stack (FreeBSD, Apache, MySQL/MariaDB, PHP) is a group of applications that can host dynamic websites and web applications, especially PHP-based applications. The FAMP Stack is similar to the LAMP Stack in the Linux world, based on the same applications, which include Apache as a web server, MySQL/MariaDB as the database, and PHP as a processing language for applications.
This tutorial will show you how to install and configure basic FAMP Stack using the new version, FreeBSD 12.0. We will install the Apache web server, install the MariaDB database and configure its root password, and then install the latest version of PHP 7.3.
Prerequisite
For this guide, we will install the FAMP Stack on a fresh FreeBSD 12.0 installation with 1GB of RAM and 2 CPUs.
What we will do:
- Update and Upgrade Packages
- Install Apache2 Web Server
- Install and Configure the MariaDB Database
- Install and Configure PHP 7.3
- Testing
Step 1 - Update and Upgrade Packages
Firstly, we will update the packages repository and upgrade all packages to the latest version using FreeBSD's pkg package management tool.
Update all available repositories and upgrade all packages to the latest version using the following command.
pkg update
pkg upgrade
Once all installation is complete, go to the next step.
Step 2 - Install Apache2 Web Server
Now we will install the Apache web server on our FreeBSD 12.0. We will install the Apache24 package, add it to the boot time, and start the service.
Install the Apache24 packages using the pkg command below.
pkg install apache24
Once the installation is complete, add the Apache service to the startup boot time.
sysrc apache24_enable=yes
Then start the Apache service using the following command.
service apache24 start
The Apache service is up and running on the FreeBSD system.
Now check the service status and the port used by the Apache service.
service apache24 status
sockstat -4 -l -P tcp
As a result, you will get the Apache service is running with the process ID '1506' and the HTTP port 80 is on the list. And you will be able to access the Apache web server from the web browser.
Open your web browser and type the server IP address on the address bar.
And you will get the default Apache index.html page as below.
Step 3 - Install and Configure MariaDB Database
After installing the Apache web server, we will install the MariaDB database to the FreeBSD 12.0 system and set up the MariaDB root user password.
By default, the FreeBSD repository provides multiple version of the MariaDB database. Check all available version of MariaDB using the following command.
pkg search mariadb
And you will get multiple version of it.
Install the latest version of MariaDB database using the following command.
pkg install mariadb103-server mariadb103-client
Once the installation is complete, add the MariaDB/MySQL service to the startup boot time.
sysrc mysql_enable="yes"
And now you will be able to start the MariaDB service.
Start the MariaDB service and check the service status using the command below.
service mysql-server start
service mysql-server status
As a result, the MariaDB service is up and running on FreeBSD 12.0 system.
Next, we will configure the MariaDB root password to improve database security. Run the 'mysql_secure_installation' command below.
/usr/local/bin/mysql_secure_installation
You will be asked for some questions, type 'Y' to yes for all.
Set root password? [Y/n] Y
TYPE YOUR ROOT PASSWORD
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y
And now the MariaDB root password has been configured, and you can log in to the MySQL shell using the password.
Login to the mysql system using the following command.
mysql -u root -p
TYPE YOUR PASSWORD
Now check the database and available user on the system using queries below.
show databases;
select User, Host, Password from mysql.user;
And you will get the result as below.
Step 4 - Install and Configure PHP 7.3
In this step, we're going to install and configure PHP 7.3 on our FreeBSD system. We will install PHP with some additional modules, and configure the PHP to make it work with Apache web server.
Install PHP 7.3 packages and basic modules using the pkg command below.
pkg install php73 php73-mysqli mod_php73 php73-mbstring php73-zlib php73-curl php73-gd php73-json
Once the installation is complete, you will get the result as below.
As a result, you will need to add the PHP module configuration to the Apache configuration.
Before adding the PHP configuration, you need to copy the sample of 'php.ini' file for production to the '/usr/local/etc' directory.
cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini
After that, go to the '/usr/local/apache24' configuration directory and edit the 'httpd.conf' file.
cd /usr/local/etc/apache24/
vim httpd.conf
Uncomment the 'ServerName' configuration and change the value with your server IP address.
ServerName 10.5.5.15:80
Save and close.
Next, create a new configuration file 'Includes/php.conf' using vim editor.
vim Includes/php.conf
Paste the configuration below.
<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 and close.
The PHP configuration has been added, and the Apache web server will be able to load the PHP file through the web browser.
Now test the Apache configuration and make sure there is no error, then restart the service.
apachectl configtest
service apache24 restart
The PHP 7.3 and Apache24 installation and configuration has been completed successfully.
Step 5 - Testing
In this step, we're going to test our FAMP Stack installation by creating the phpinfo file to the default web root directory '/usr/local/www/apache24'.
Goto the '/usr/local/www/apache24' directory and create a new file 'info.php'.
cd /usr/local/www/apache24/
vim data/info.php
Paste the PHP script below.
<?php
phpinfo();
?>
Save and close.
Now open your web browser and type the server IP address following by the 'info.php' file on the address bar.
http://10.5.5.15/info.php
And you will get details about your PHP settings.
And finally, the installation of FAMP (FreeBSD, Apache2, MySQL/MariaDB, and PHP) stack on FreeBSD 12.0 has been completed successfully. And for the next guide, we will set up the virtual host on the FreeBSD system.