Learn how to Install Apache2, MariaDB and PHP (FAMP) on FreeBSD
Introduction
Requirements
The following is a list of items needed to complete the installation successfully:
1) A desktop or laptop with atleast 4GB RAM, a dual core processor and 25GB of free disk space
2) FreeBSD 14 or newer
3) An SSH client to access the server
4) Cloud servers: A domain and a static IPv4 or IPv6 address
5) A stable internet connection
Overview
The following is an overview of the steps covered in this guide:
1) Connect to the server and install updates
2) Download and install apache2
3) Download and install MariaDB server
4) Download and install PHP
5) Conclusion
Step 1: Connect to the server and install updates
freebsd-update fetchThis fetches details about available updates without actually installing them. Once the repositories are updated, use this command to install the available packages:
freebsd-update installThe system will display a list of updates and ask for your confirmation before proceeding. Carefully review the list and press y to accept and install the updates
Step 2: Download and install Apache2
To install the Apache web server on FreeBSD, you can use the default package repository. Install the Apache package using the command:
pkg install apache24
After installation, enable Apache to start at system boot time with the command
sysrc apache24_enable="YES"
To start the Apache service, enter the command
service apache24 start
Verify that Apache is running by running the command
service apache24 status
If Apache is running, you should see the output
apache24 is running as pid 1624
Step 3: Download and install MariaDB server
pkg search mariadbYou will see a list of available options, including the following:
mariadb-connector-c-3.3.4: MariaDB database connector for C mariadb-connector-odbc-3.1.17: MariaDB database connector for ODBC mariadb1011-client-10.11.5: Multithreaded SQL database (client) mariadb1011-server-10.11.5: Multithreaded SQL database (server) mariadb105-client-10.5.21: Multithreaded SQL database (client) mariadb105-server-10.5.21: Multithreaded SQL database (server) mariadb106-client-10.6.14: Multithreaded SQL database (client) mariadb106-server-10.6.14: Multithreaded SQL database (server) p5-DBD-MariaDB-1.21: MariaDB driver for the Perl5 Database Interface (DBI)After selecting the appropriate package for your system, install the MariaDB server and client package using the following command:
pkg install mariadb1011-server-10.11.5 mariadb1011-client-10.11.5Once the packages are installed, enable the MariaDB package to start at system reboot by executing the following command:
sysrc mysql_enable="yes"Next, start the MariaDB service:
service mysql-server startTo secure the installation, run the mysql_secure_installation script by executing the following command:
/usr/local/bin/mysql_secure_installationAnswer all the questions as shown below:
Enter current password for root (enter for none): Switch to unix_socket authentication [Y/n] Y Change the root password? [Y/n] Y New password: Re-enter new 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] YFinally, verify the MariaDB databases using the following command:
mysql -u root -p -e "show databases"You will see all default databases in the following output:
+--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+
Step 4: Download and install PHP
pkg install php80 mod_php80 php80-mbstring php80-zlib php80-curl php80-gd php80-mysqli nano-7.2After installing PHP, you need to create a PHP configuration file for the Apache web server. Use the following command to create it:
nano /usr/local/etc/apache24/Includes/php.confAdd the configurations given 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>Once you are done adding the configurations, save and close the file. Next, create an info.php file to test the web server using the following command:
nano /usr/local/www/apache24/data/info.phpAdd the following code to the file:
< ?php phpinfo(); ? >Save and close the file, and then restart the Apache service to apply the changes. Use the following command to restart the Apache service:
service apache24 restartFinally, open your web browser and access the info.php file using the URL http://example.com/info.php. This will display the PHP information page.
Conclusion
Congratulations! You have successfully installed the FAMP stack (FreeBSD, Apache, MariaDB, and PHP) on your server. This powerful combination allows you to host dynamic websites and web applications written in PHP.
This tutorial has guided you through the process of:
1. Updating your FreeBSD system
2. Installing and configuring Apache web server
3. Installing and securing MariaDB database server
4. Installing and configuring PHP to work with Apache
Now that you have the FAMP stack up and running, you can start developing and deploying your web applications. Remember to consult the official documentation for each component for further configuration and security best practices.