When automating server configuration through bash scripts, it's crucial to validate configurations before applying changes. For Apache, we commonly use apachectl configtest
or apachectl graceful
to test configurations. But what about PHP-FPM?
PHP-FPM provides a similar testing mechanism through the -t
or --test
flag:
php-fpm -t
# or for specific versions
php5-fpm -t
php7.4-fpm -t
php8.2-fpm -t
Here's how you can integrate this check in your vhost creation script:
#!/bin/bash
# Your existing vhost creation commands...
# Test PHP-FPM configuration
if php5-fpm -t; then
echo "PHP-FPM configuration test successful, restarting..."
systemctl restart php5-fpm
else
echo "PHP-FPM configuration test failed!" >&2
exit 1
fi
For production environments, you might want more robust error handling:
#!/bin/bash
CONFIG_TEST=$(php5-fpm -t 2>&1)
TEST_STATUS=$?
if [ $TEST_STATUS -eq 0 ]; then
echo "$(date) - Config test passed" >> /var/log/vhost_creator.log
systemctl restart php5-fpm
echo "$(date) - PHP-FPM restarted successfully" >> /var/log/vhost_creator.log
else
echo "$(date) - Config test failed: $CONFIG_TEST" >> /var/log/vhost_creator.log
exit 1
fi
For systems with multiple PHP versions, you can make the script more flexible:
PHP_VERSION="5.6" # Can be parameterized
FPM_SERVICE="php${PHP_VERSION}-fpm"
FPM_BIN="/usr/sbin/php-fpm${PHP_VERSION}"
if $FPM_BIN -t; then
systemctl restart $FPM_SERVICE
else
echo "Configuration test failed for PHP ${PHP_VERSION}"
exit 1
fi
Another method is to perform a dry run that doesn't actually start the service:
php5-fpm --nodaemonize --fpm-config /etc/php/5.6/fpm/php-fpm.conf --test
This provides more detailed output about the configuration check process.
When automating Nginx virtual host deployments through bash scripts, it's crucial to validate configurations before service restarts. Unlike Apache's apachectl graceful
, PHP-FPM requires a different approach for configuration testing.
PHP-FPM provides several ways to check configuration validity:
# Standard syntax check
sudo php-fpm5.6 -t
# Alternative with full path
sudo /usr/sbin/php-fpm5.6 -t
# For systemd systems
sudo php-fpm5.6 -t -y /etc/php/5.6/fpm/pool.d/your_pool.conf
Here's how to integrate this check in your vhost creation script:
#!/bin/bash
# Your existing vhost setup code here...
# Validate PHP-FPM config before restart
echo "Validating PHP-FPM configuration..."
if ! php-fpm5.6 -t &> /dev/null; then
echo "ERROR: PHP-FPM configuration test failed"
echo "Details:"
php-fpm5.6 -t
exit 1
fi
# Restart PHP-FPM if config is valid
echo "Configuration valid, restarting PHP-FPM..."
systemctl restart php5.6-fpm
For more robust scripts, consider this enhanced version that checks both Nginx and PHP-FPM configs:
#!/bin/bash
function validate_services() {
# Check Nginx
if ! nginx -t; then
echo "Nginx configuration test failed"
return 1
fi
# Check PHP-FPM
if ! php-fpm5.6 -t; then
echo "PHP-FPM configuration test failed"
return 1
fi
return 0
}
# Usage in script
if validate_services; then
systemctl reload nginx
systemctl restart php5.6-fpm
echo "Services reloaded successfully"
else
echo "Aborting due to configuration errors"
exit 1
fi
- Always test configurations before applying them to production
- Consider adding log rotation for PHP-FPM when creating new pools
- For Docker environments, use the appropriate PHP-FPM binary path
- When supporting multiple PHP versions, parameterize the PHP-FPM binary name
If php-fpm5.6 -t
fails:
- Verify the PHP-FPM binary path with
which php-fpm5.6
- Check file permissions on pool configurations
- Ensure no syntax errors in pool files (missing semicolons, brackets)
- Validate include paths in php-fpm.conf