
FreeBSD, Nginx, MySQL, and PHP (FEMP stack) is a collection of open-source applications that enable the development, hosting, and delivery of dynamic web applications on a server. FreeBSD works as the deployment operating system, Nginx as the web server, MySQL or MariaDB as the database backend, and PHP as the server-side scripting processor.
This article explains how to install Nginx, MySQL, and PHP (FEMP stack) on FreeBSD 14.0, and configure the packages to enable the delivery of dynamic web applications on your server.
Before you begin:
app.example.com.Nginx is available in the default package repositories on FreeBSD 14.0 with the latest version information. Follow the steps below to install Nginx and manage the application service to run on your server.
Update the pkg repository catalog.
Install Nginx.
Enable Nginx to automatically start at boot time.
Start the Nginx service.
View the Nginx service status and verify that it's running.
Output:
MySQL is available in the default package repositories on FreeBSD 14.0 with multiple package versions. Follow the steps below to install the latest MySQL database server package on your server using the default pkg package manager.
Search all MySQL packages available in the default pkg repositories.
Output:
Based on the above output, MySQL 8.1 is the latest version you can install on your server. Use the FreeBSD ports collection to compile and install any other versions that are unavailable in the default repositories on your server.
Install the latest MySQL database server package.
Enable the MySQL service to automatically start at boot.
Start the MySQL service.
View the MySQL service status and verify that it's running.
Output:
Start the MySQL secure installation script.
Reply to the following MySQL prompts with your desired selection to set up authentication and disable insecure defaults on your server.
root database user.root database user password.root database user account.Restart the MySQL database server to apply your configuration changes.
PHP is available in the default repositories on your FreeBSD server and includes the PHP-FPM package that manages connections to the PHP service. Follow the steps below to install the latest PHP version and configure PHP-FPM on your server.
Search all available PHP packages in the FreeBSD pkg catalog.
Install the latest PHP version and PHP-FPM. For example, PHP version 8.3.
View the installed PHP version on your server.
Output:
Enable PHP-FPM to automatically start at boot time.
Start the PHP-FPM service.
View the PHP-FPM and verify that it's running.
Output:
Install common PHP modules required by most web applications.
Open the default PHP-FPM pool configuration www.conf using a text editor such as vi.
Find the following user and group directives.
Find the following listen directive and verify that it's set to the localhost address 127.0.0.1.
Modify your PHP-FPM memory configurations to match your FreeBSD server memory needs.
Save and close the file.
Restart the PHP-FPM service to apply your configuration changes.
Nginx delivers dynamic web applications using the PHP-FPM service that processes PHP requests on your server using the default pool configuration. Follow the steps below to create a new Nginx virtual host and enable the PHP-FPM service to process dynamic web application requests on your server.
Create a new virtual hosts directory on your server. For example, /usr/local/etc/nginx/vhosts/
Open the main Nginx configuration file.
Add the following directive at the end of the file before the closing http context tag }.
Save and close the file.
Create a new Nginx virtual host configuration file in the /usr/local/etc/nginx/vhosts/ directory. For example, app.example.com.conf.
Add the following contents to the file.
Save and close the file.
The Nginx configuration above listens for incoming HTTP connections using your domain app.example.com and serves files from the /usr/local/www/app.example.com web root directory. The location ~ \.php$ block matches all PHP file requests on the domain and forwards them to the PHP-FPM port 127.0.0.1:9000 for processing.
Create the virtual host web root directory to deliver your web application files.
Test the Nginx configuration for errors.
Output:
Restart Nginx to apply the configuration changes.
Nginx works with PHP-FPM to deliver dynamic web applications on your virtual host domain app.example.com while MySQL works as the database backend that stores the application data. Follow the steps below to test the FEMP stack installation and create a sample PHP application that connects to the MySQL database server and displays a Greetings from Vultr message.
Log in to the MySQL database server as root.
Enter the root database user password you set earlier when prompted to access the database console.
Create a new MySQL sample database to use with your application. For example, example_db.
View all available databases and verify that the new database is available.
Output:
Switch to the database.
Create a new sample database user test_user with a strong password. Replace Strong)P@ssword123 with your desired password.
Create a new sample table greeting with two columns, id and message.
Insert a new Greetings from Vultr record into the greetings table.
Exit the MySQL console.
Create a new index.php file in your web root directory /usr/local/www/app.example.com.
Add the following contents to the file.
Save and close the file.
The above PHP application connects to the MySQL database server using your database user details and retrieves the message Greetings from Vultr when successful. A No message found error displays in the web browser when the database table does not return any records.
Restart the Nginx web server to apply changes.
Access your domain using a web browser such as Chrome and verify that a Greetings from Vultr message displays in your browser.
Nginx delivers web applications on your server using the insecure HTTP port 80 by default. SSL certificates enable encryption of network connections between a user's web browser and your web server using HTTPS. Follow the steps below to generate trusted Let's Encrypt SSL certificates and enable encrypted HTTPS connections on your server
Install the Certbot Let's Encrypt client plugin for Nginx.
Generate a new SSL certificate for your domain. Replace app.example.com with your actual domain and admin@example.com with your active email address.
Restart the Nginx web server to apply the SSL configuration changes.
Access your domain using the HTTPS scheme in a new web browser window and verify that your PHP web application loads correctly.
You have installed Nginx, MySQL, and PHP (FEMP stack) on your FreeBSD server and set up a sample web application to deliver dynamic web content on your server. You can use the FEMP stack to securely develop and deliver high-performance web applications on your server using virtual host configurations. For more information and configuration options for each application in the stack, please visit the following documentation resources:
0 Comments
Be the first to comment and share your perspective with the community.