1080*80 ad

FreeBSD 12: Installing the FEMP Stack

Your Ultimate Guide to Building a FEMP Stack on FreeBSD 12

Powering modern web applications requires a robust and reliable server stack. For users of the FreeBSD operating system, the FEMP stack—FreeBSD, Nginx, MariaDB, and PHP—offers a high-performance, secure, and scalable solution. Nginx is renowned for its efficiency in handling concurrent connections, while MariaDB provides a powerful, open-source database system, and PHP remains the backbone of countless dynamic websites.

This comprehensive guide will walk you through the entire process of installing and configuring a fully functional FEMP stack on a fresh FreeBSD 12 server. Let’s dive in.

Prerequisites

Before we begin, ensure you have a clean installation of FreeBSD 12 and access to an account with root privileges or sudo capabilities.


Step 1: System Preparation and Updates

A crucial first step in any server setup is ensuring your system is up-to-date. This patches potential security vulnerabilities and updates the package repository information.

First, fetch and install the latest security patches for the FreeBSD base system:

freebsd-update fetch install

Next, update your package repository to ensure you are installing the latest versions of all software:

pkg update && pkg upgrade

With your system now current, we can proceed with installing the core components of our FEMP stack.


Step 2: Installing and Configuring the Nginx Web Server

Nginx (pronounced “Engine-X”) will serve as our web server. It’s known for its low memory footprint and high performance, making it an excellent choice for any modern application.

Install the Nginx package using pkg:

pkg install nginx

Once installed, we need to enable the Nginx service to ensure it starts automatically on boot. We do this by adding a line to the /etc/rc.conf file. The sysrc command is the safest way to manage this file.

sysrc nginx_enable=YES

Now, you can start the Nginx service for the first time:

service nginx start

Security Tip: Configure the Firewall

By default, FreeBSD’s packet filter firewall (pf) may block web traffic. You’ll need to allow incoming connections on port 80 (HTTP) and port 443 (HTTPS).

First, enable the firewall:

sysrc pf_enable=YES

Next, edit the firewall configuration file at /etc/pf.conf and add the following rule, typically near the other pass in rules:

pass in on vtnet0 proto tcp from any to any port { 80, 443 }

Note: Replace vtnet0 with your server’s public network interface name if it’s different.

Save the file and load the new ruleset:

service pf start
service pf reload

You should now be able to visit your server’s IP address in a web browser and see the default “Welcome to nginx!” page.


Step 3: Installing and Securing the MariaDB Database

MariaDB is a community-developed fork of MySQL and serves as a powerful relational database management system. It’s fully compatible with MySQL and is the preferred database choice for many modern systems.

Install the MariaDB server package:

pkg install mariadb105-server

(You can select a different version if your project requires it, but 10.5 is a stable, widely-used choice.)

Next, enable the MariaDB service to start on boot:

sysrc mysql_enable=YES

Start the service for the first time:

service mysql-server start

Crucially, you must secure your new MariaDB installation. The default settings are not suitable for a production environment. MariaDB includes a helpful interactive script for this purpose.

Run the security script and follow the prompts:

mysql_secure_installation

You will be asked to:

  • Set a root password (do this).
  • Remove anonymous users (yes).
  • Disallow root login remotely (yes, this is highly recommended).
  • Remove the test database and access to it (yes).
  • Reload privilege tables (yes).

Your database server is now installed and secured.


Step 4: Installing PHP and PHP-FPM

PHP will process the dynamic code for our applications. We will install PHP-FPM (FastCGI Process Manager), which is the standard way to integrate PHP with Nginx for optimal performance.

Install the main PHP package along with some common extensions needed by modern content management systems like WordPress or frameworks like Laravel.

pkg install php74 php74-mysqli php74-mbstring php74-zlib php74-curl php74-gd php74-json php74-zip

Note: Adjust the PHP version and extensions as needed for your specific application.

Next, enable the PHP-FPM service:

sysrc php_fpm_enable=YES

To use PHP-FPM, you’ll need to create a configuration file. Copy the production template to get started:

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

Finally, start the PHP-FPM service:

service php-fpm start

Step 5: Connecting Nginx and PHP

With all the components installed, the final step is to configure Nginx to pass PHP requests to the PHP-FPM service for processing. This is done by editing an Nginx server block configuration.

Open the main Nginx configuration file:

ee /usr/local/etc/nginx/nginx.conf

Find the server block within the file. You will need to make the following key changes:

  1. Add index.php to the index directive. This tells Nginx to look for an index.php file when a directory is requested.
  2. Uncomment or add the location ~ \.php$ block. This block tells Nginx how to handle files ending in .php.
  3. Ensure the fastcgi_pass directive points to the PHP-FPM socket.

Your modified server block should look similar to this:

server {
    listen       80;
    server_name  your_domain_or_ip;
    root         /usr/local/www/nginx;
    index        index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    # Pass PHP scripts to FastCGI server
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $request_filename;
        include        fastcgi_params;
    }
}

After saving your changes, check the Nginx configuration for syntax errors:

nginx -t

If it reports the syntax is okay, restart the Nginx service to apply the new configuration:

service nginx restart

Step 6: Testing Your Full FEMP Stack

To confirm that all components are working together correctly, we can create a simple PHP file that displays server information.

Create a new file called info.php in the Nginx web root directory:

ee /usr/local/www/nginx/info.php

Add the following single line of PHP code to this file:

<?php phpinfo(); ?>

Save and close the file. Now, navigate to http://your_domain_or_ip/info.php in your web browser.

If everything is configured correctly, you will see a detailed page with information about your PHP installation. This confirms that Nginx is successfully passing PHP requests to PHP-FPM, and PHP is running correctly.

Important Security Note: The phpinfo() page reveals sensitive information about your server environment. Be sure to delete this file after you have finished testing.

rm /usr/local/www/nginx/info.php

Conclusion

Congratulations! You have successfully deployed a high-performance FEMP stack on your FreeBSD 12 server. With Nginx, MariaDB, and PHP all working in harmony, you now have a powerful platform ready to host your websites and applications. From here, you can proceed with uploading your project files, creating databases, and building the next great thing on the web.

Source: https://kifarunix.com/install-nginx-mysql-php-femp-stack-on-freebsd-12/

900*80 ad

      1080*80 ad