PgBouncer: A Guide to Efficiently Managing Multiple Instances To effectively manage multiple PgBouncer instances, follow these steps Install Prerequisites Ensure PostgreSQL, PgBouncer, and systemd are installed and configured correctly on your Ubuntu 22.04 or later server. Create Systemd Templates Create template files for both the socket (/etc/systemd/system/
[email protected]) and service (/etc/systemd/system/
[email protected]) to define basic settings for each instance. Configure PgBouncer Create individual configuration files (e.g., pgbouncer-50001.ini, pgbouncer-50002.ini) based on the templates, specifying unique port numbers, log files, and PID files. Enable and Start Instances Use systemd commands to enable and start the newly created PgBouncer instances. Verify Instances Check the status of the running instances, including PID files, log files, and process information. We can effectively optimize your PostgreSQL database's performance and scalability through the use of mu...
Guide to Configuring PHP-FPM Slow Logs
To ensure
php-fpm-slowlog.shscript has data to analyze, we need to properly configure PHP-FPM to generate slow request logs. These logs capture details about PHP scripts that take longer than a specified duration to execute.https://gist.github.com/linuxmalaysia/f6d9ad909318eafd91804f78eacd112f
Here's how to set it up:
Step 1: Locate Your PHP-FPM Pool Configuration File
PHP-FPM pool configuration is typically found in a directory like
/etc/php/8.3/fpm/pool.d/. Fromlsoutput, your main pool configuration file iswww.conf./etc/php/8.3/fpm/pool.d/www.confStep 2: Edit the
www.confFileOpen this file using a text editor (like
nanoorvi):Step 3: Configure
request_slowlog_timeoutandslowlogInside the
www.conffile, we need to find or add the following two directives within the[www]pool section.request_slowlog_timeout: This directive defines the maximum execution time for a PHP script after which a log entry will be made to the slow log file. It's crucial to set this to a value lower than yourrequest_terminate_timeout(which is often 60s by default) so that slow requests are logged before they are forcefully terminated.5sor10s. This allows us to catch scripts that are slow but not necessarily timing out yet.slowlog: This directive specifies the path to the slow log file.Your current snippet shows:
slowlog = /var/log/$pool.log.slow$poolis a variable that PHP-FPM replaces with the name of the current pool (in your case,www). So, the actual path will resolve to/var/log/www.log.slow. This is the file we analysis script is designed to read.Locate these lines (they might be commented out with a semicolon
;) and uncomment/modify them as follows:Step 4: Ensure Log Directory Permissions
Make sure the directory where the slow log will be written (
/var/log/in this case) has appropriate permissions for the PHP-FPM user (oftenwww-dataornginx). If PHP-FPM cannot write to this directory, the slow log will not be generated.Check this with:
And adjust ownership if necessary (e.g.,
sudo chown www-data:www-data /var/log/orsudo chmod g+w /var/log/ifwww-datais in a group with write access).Step 5: Restart PHP-FPM
After making changes to the configuration file, we must restart the PHP-FPM service for the changes to take effect:
sudo systemctl restart php8.3-fpm # Or whatever command system uses for php-fpm (e.g., php-fpm, php-fpm8.3)Step 6: Monitor the Slow Log
Once PHP-FPM is restarted and the application receives traffic, we should start seeing entries in the slow log file (
/var/log/www.log.slow) whenever a script exceeds therequest_slowlog_timeout.WE can monitor it in real-time using:
Now, our
php-fpm-slowlog.shscript will have the necessary data to perform its analysis.