Posted in

FreeBSD Server – Part 3: PHP and SQL

freebsd server php mariadb



 
In the first part of the “Your Own FreeBSD Server” series, we installed a web server using Apache, and in the second part, we installed it using Nginx. In this tutorial, we’ll install the PHP interpreter and a database system based on MariaDB.

Installing PHP

FreeBSD 14 includes PHP versions 8.1, 8.2, 8.3, 8.4, and 8.5.
For this tutorial, I installed version 8.4. pkg install php84 php84-xml php84-mbstring php84-mysqli php84-gd php84-gettext php84-gd php84-ftp

Next, copy the sample PHP configuration file to the same location:

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

To enable PHP-FPM to run as a service at system startup, add the following line:

sysrc php_fpm_enable=”YES”

to the file:

nano /etc/rc.conf

And restart the service & check the PHP version:

service php-fpm start
service php-fpm status
php-fpm -v

Now configure Apache to work with PHP.

nano /usr/local/etc/apache24/modules.d/001_mod-php.conf

by adding the following entry:

<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>

Restart Apache:

apachectl configtest
apachectl restart

You can check your PHP configuration and running modules by adding to the file:

nano /usr/local/www/apache24/data/info.php

the contents into it:

<?php phpinfo(); ?>

Check its contents in a web browser at:

your-ip/info.php

or

your-domain/info.php

Installing SQL

FreeBSD includes MySQL 8.4 LTS and MariaDB 11.8 packages, which I personally recommend.

pkg install mariadb118-server

Add the line:

sysrc mysql_enable=”YES”

to the file:

nano /etc/rc.conf

Then restart the SQL service:

service mysql-server start
service mysql-server status

After installation, MariaDB does not include a password for the root user, so you need to set one immediately. Execute the command:

mysql_secure_installation

When prompted for the root password, simply press ENTER:
Enter current password for root (enter for none):

Now you can define the MariaDB authentication protocol:

If you already have your root account protected, you can safely answer ‘n’.

Switch to unix_socket authentication [Y/n]

By entering Y (Yes), you can change the root password:

Change the root password? [Y/n]
New password:

Re-enter new password:
Password updated successfully!

A series of questions about MariaDB configuration and security are then displayed. You can answer each one with Y:

Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

The script will then complete, and MariaDB will be ready for use.

First login as root:
mysql -u root -p
Enter password:

MariaDB [(none)]>

Create a user named ‘pawel’:

CREATE USER ‘pawel’@’localhost’ IDENTIFIED BY ‘mypassword’;

Create a new database named database1:

CREATE DATABASE database1;

Grant user ‘pawel’ access to database ‘database1’:

GRANT ALL PRIVILEGES ON database1.* TO ‘pawel’@’localhost’;

To exit MariaDB:

exit;

To check the MariaDB version:

mysql -v
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection ID is 14
Server version: 11.8.3-MariaDB FreeBSD Ports

The MariaDB database system and PHP interpreter have been installed correctly. Next time we will discuss further configuration of the FreeBSD 14 system.

Click to rate this post!
[Total: 0 Average: 0]
Share

Leave a Reply

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

Accessibility Toolbar