1080*80 ad

Install phpMyAdmin on FreeBSD 12 with Nginx

A Step-by-Step Guide to Installing and Securing phpMyAdmin on FreeBSD with Nginx

Managing a MySQL or MariaDB database from the command line is powerful, but for many developers and system administrators, a graphical user interface (GUI) is indispensable for daily tasks. This is where phpMyAdmin shines, offering a comprehensive web-based tool for database administration.

This guide will walk you through the complete process of installing and, more importantly, securing phpMyAdmin on a FreeBSD server running an Nginx web server. Following these steps will give you a robust and secure environment for managing your databases.

Prerequisites

Before you begin, ensure you have a fully functional LEMP (Linux/FreeBSD, Nginx, MySQL/MariaDB, PHP) stack installed and configured on your FreeBSD server. Specifically, you will need:

  • FreeBSD 12 or a later version.
  • Nginx installed and running.
  • PHP-FPM installed and configured to work with Nginx.
  • MySQL or MariaDB installed and running.

Step 1: Install phpMyAdmin with FreeBSD’s Package Manager

The simplest way to install phpMyAdmin is by using FreeBSD’s built-in package manager, pkg. This ensures you get a version that is optimized for your operating system.

Open your terminal and run the following command:

sudo pkg install phpmyadmin

The package manager will handle all dependencies. Once the installation is complete, the phpMyAdmin files will be located in /usr/local/www/phpMyAdmin. It is crucial to note this path, as you will need it to configure Nginx.

Step 2: Configure PHP for phpMyAdmin

phpMyAdmin relies on several PHP extensions to function correctly. While your base PHP installation might have some of them, we need to ensure the critical ones are enabled. The most important extensions are mysqli (for connecting to the database), mbstring (for handling multi-byte character strings), and session (for managing user logins).

You can enable these by creating a dedicated configuration file for phpMyAdmin.

  1. Create a new configuration file in your PHP directory:
    bash
    sudo touch /usr/local/etc/php/ext-20-phpmyadmin.ini
  2. Open the file with your preferred text editor (like ee or vim) and add the following lines to enable the necessary extensions:
    ini
    extension=mysqli.so
    extension=mbstring.so
    extension=session.so
  3. Save and close the file. To apply these changes, you must restart the PHP-FPM service:
    bash
    sudo service php-fpm restart

Step 3: Configure the Nginx Server Block

Next, you need to tell Nginx how to serve the phpMyAdmin files. This is done by creating a new server block (often called a virtual host).

  1. Create a new Nginx configuration file. We recommend placing it in a sites-available directory for better organization.
    bash
    sudo touch /usr/local/etc/nginx/sites-available/phpmyadmin.conf

  2. Open this new file and paste the following configuration. Be sure to replace pma.your-domain.com with the subdomain you intend to use and update the root path if it differs.

    server {
        listen 80;
        server_name pma.your-domain.com;
        root /usr/local/www/phpMyAdmin;
        index index.php;
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        include fastcgi_params;
    }
    
    # Deny access to sensitive files
    location ~ /\.ht {
        deny all;
    }
    

    }

  3. To enable this site, create a symbolic link from sites-available to sites-enabled.
    bash
    sudo ln -s /usr/local/etc/nginx/sites-available/phpmyadmin.conf /usr/local/etc/nginx/sites-enabled/

  4. Finally, test your Nginx configuration for syntax errors and reload the service to apply the changes.
    bash
    sudo nginx -t
    sudo service nginx reload

Step 4: Secure Your phpMyAdmin Installation (Crucial!)

An unsecured phpMyAdmin instance is one of the biggest security risks a web server can have. It is a prime target for automated bots and attackers. Do not skip these security steps.

1. Create the phpMyAdmin Configuration and Blowfish Secret

phpMyAdmin uses a config.inc.php file for its core settings. We will create this from the provided sample file.

cd /usr/local/www/phpMyAdmin
sudo cp config.sample.inc.php config.inc.php

Next, you must set a blowfish secret, which is a random string of characters used to encrypt cookies. A weak or default secret compromises your security.

  1. Generate a strong, random 32-character string. You can use the following command:
    bash
    openssl rand -base64 32
  2. Open the configuration file: sudo ee /usr/local/www/phpMyAdmin/config.inc.php
  3. Find the following line:
    php
    $cfg['blowfish_secret'] = ''; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
  4. Paste your generated secret between the single quotes.
    php
    $cfg['blowfish_secret'] = 'Your-32-Character-Random-String-Goes-Here';
  5. Save and close the file.

2. Restrict Access with HTTP Basic Authentication

Adding a server-level password prompt provides an excellent extra layer of security. This forces users to authenticate before they can even see the phpMyAdmin login page.

  1. Install the htpasswd utility if you don’t already have it (it’s part of the apache24 package but can be installed standalone).
    bash
    sudo pkg install apache24

  2. Create a password file. The command below will create a new file and prompt you to create a password for the user admin.
    bash
    sudo htpasswd -c /usr/local/etc/nginx/.pma_pass admin

  3. Now, modify your Nginx server block for phpMyAdmin to enable basic authentication. Add the two auth_basic lines inside the server block:

    server {
        listen 80;
        server_name pma.your-domain.com;
        root /usr/local/www/phpMyAdmin;
    # Add these two lines for security
    auth_basic "Admin Login";
    auth_basic_user_file /usr/local/etc/nginx/.pma_pass;
    
    # ... rest of the configuration
    

    }

  4. Test and reload Nginx again.
    bash
    sudo nginx -t
    sudo service nginx reload

Step 5: Accessing Your phpMyAdmin Dashboard

You’re all set! Open your web browser and navigate to the subdomain you configured (e.g., http://pma.your-domain.com).

You should immediately be greeted by a pop-up authentication box from your browser. This is the HTTP Basic Authentication you just set up. Enter the username (admin) and password you created with htpasswd.

After successfully authenticating, you will see the standard phpMyAdmin login page. Here, you can log in with your MySQL/MariaDB root user or any other database user credentials.

By following this guide, you have not only installed a powerful database management tool but also implemented essential security measures to protect your server and its data from unauthorized access.

Source: https://kifarunix.com/install-phpmyadmin-with-nginx-on-freebsd-12/

900*80 ad

      1080*80 ad