
FreeBSD, Apache, MySQL, and PHP (FAMP stack) is a collection of open-source software applications that manage the runtime and development of dynamic web applications on a server. Apache works as a web server to delivers web application files while MySQL functions as the database backend, and PHP processes dynamic web application contents.
This article explains how to install the Apache, MySQL, and PHP (FAMP) stack on FreeBSD 14.0 and deliver dynamic web applications on your server.
Before you begin:
Deploy a FreeBSD 14.0 instance on Vultr.
Create a new domain A record pointing to the instance IP address. For example, app.example.com.
Access the server using SSH.
Create a non-root user with sudo privileges and switch to the user.
Apache is available in the default repositories on FreeBSD 14.0 with the latest package information. Follow the steps below to install the latest Apache web server package and enable the application to run on your server.
Update the server packages catalog.
Install Apache on your server.
Verify that Apache is installed on your server.
Output:
Enable Apache to automatically start at boot.
Start the Apache web server.
View the Apache service status and verify that the web server is running.
Output:
Access your server IP address using a web browser such as Chrome and verify that the default Apache virtual host web page displays.
MySQL is available in the default FreeBSD 14.0 repositories with multiple versions. Follow the steps below to install the latest MySQL database server version and enable it to run on your server.
Search all MySQL versions available in the default FreeBSD repositories.
Output:
Install the latest MySQL server and client packages.
View the installed MySQL version on your server.
Output:
Enable the MySQL server to automatically start at boot.
Output:
Start the MySQL database server.
View the MySQL server status and verify that it's running.
Output:
Start the MySQL secure installation script.
root database user.Restart the MySQL database server to apply changes.
PHP is available in the default FreeBSD repositories and includes the PHP-FPM package that manages connection requests using pool configurations on your server. Follow the steps below to install the latest PHP version on your server.
Search all available PHP packages in the default FreeBSD repositories.
Output:
Install the latest PHP package. For example, PHP version 8.3.
Install common PHP modules required to enable application functionalities.
View the installed PHP version on your server..
Output:
View the installed PHP-FPM version on your server.
Output:
Enable PHP-FPM to automatically start at boot.
View the PHP-FPM service status and verify that it's running.
Output:
Apache uses the mod_proxy and mod_proxy_fcgi modules to process FastCGI requests and connect to the PHP-FPM service. Follow the steps below to enable the required Apache modules and forward all PHP application requests to the PHP-FPM service port 9000.
Open the default PHP-FPM pool configuration file using a text editor such as the Easy Editor (ee).
user and group directives and verify that PHP-FPM runs with the web server user profile www.listen directive and verify that PHP-FPM listens for connection requests on the localhost port 9000.Save and close the file.
Run the following command to enable the mod_proxy and mod_proxy_fcgi Apache modules on your server.
The above command uncomments the following Apache configuration directives to enable the modules on your server.
Open the main Apache configuration file.
Add the following configurations at the end of the file to forward all PHP file requests to the PHP-FPM port 9000.
Save and close the file.
Test the Apache configuration for errors.
Your output should look like the one below when the test is successful:
Restart PHP-FPM to apply changes.
Restart Apache to apply the configuration changes.
Log in to the MySQL database server using the root user and password you set earlier.
Create a new sample database exampledb.
Create a new database user such as dbadmin with a strong password.
Grant the dbadmin user full privileges to the exampledb database.
Flush the MySQL privileges table to apply changes.
Switch to the sample database exampledb.
Create a new sample table exampletable with the following columns to store integer and string values.
The above SQL statement creates a new table with the columns: id that stores numeric data, and messages that stores mixed characters.
Insert sample data into the table. For example, Greetings from Vultr.
Exit the MySQL database console.
Create a new directory to store your web application files. For example, app.example.com in the default Apache web root path.
Create a new sample PHP application file in the directory. For example, index.php.
Add the following contents to the file.
Save the file and exit the text editor.
The above PHP application connects to the MySQL database server and queries the exampledb database to output values in the exampledata column within the messages table. The application displays a Greetings from Vultr heading when successful or No Data Found when unsuccessful.
Create a new Apache virtual host configuration file. For example, app.example.com.conf.
Add the following configurations to the file.
Save the file and exit the text editor.
The above Apache configuration creates a new virtual host that listens for connections using the domain app.example.com on the default HTTP port 80. All PHP requests are forwarded to PHP-FPM on the host port 9000 applied in your main Apache configuration.
Test the Apache configuration for errors.
Output:
Restart Apache to apply the new virtual host configuration.
Access your domain using a web browser and verify that you can access the PHP web application.
Install the Certbot Let's Encrypt client for Apache.
Run the following commands to enable the Apache mod_rewrite and mod_ssl modules to allow SSL configurations on your web server.
The above commands uncomment and enable the following directives in the /usr/local/etc/apache24/httpd.conf Apache configuration file:
Generate a new SSL certificate for your domain. Replace app.example.com with your actual domain.
Test the Certbot SSL certificate automatic renewal process.
Output:
Restart Apache to apply your SSL configuration changes.
Create a new Packet Filter (pf) firewall configuration.
Add the following configurations to the file. Replace vtnet0 with your public server interface name.
Save the file and exit the text editor.
The above firewall configuration enables network connections to the SSH port 22, HTTP port 80, and the HTTPS port 443. In addition, the firewall blocks any other network connections to other ports on your server.
Enable the firewall to automatically start at boot.
Output:
Load your firewall configuration.
Start the Packet Filter firewall.
Output:
View all firewall rules and verify that your SSH, HTTP, and HTTPS ports are available.
Output:
Access your domain using HTTPS to verify that your SSL certificate is valid and encrypted by the Apache web server.
You have successfully installed the Apache, MySQL, and PHP (FAMP) stack on your FreeBSD 14.0 server. All applications in the stack run collectively on the server to deliver dynamic web applications. You can configure the Apache web server and set up MySQL databases to securely deliver web applications or frameworks such as WordPress on your server.
0 Comments
Be the first to comment and share your perspective with the community.