Set up Cherokee web server with optimized MySQL database connections, FastCGI configuration, and performance tuning for high-traffic web applications on Linux systems.
Prerequisites
- Root or sudo access
- Basic command line knowledge
- Understanding of web server concepts
What this solves
Cherokee is a high-performance web server that excels at serving dynamic content with low resource usage. This tutorial shows you how to configure Cherokee with MySQL database optimization, FastCGI support, and advanced caching for production web applications. You'll set up secure database connections, tune MySQL performance parameters, and configure Cherokee's built-in caching system for maximum throughput.
Step-by-step installation
Update system packages
Start by updating your package manager to ensure you get the latest versions of Cherokee and MySQL.
sudo apt update && sudo apt upgrade -y
Install Cherokee web server
Install Cherokee along with its admin interface for easy configuration management.
sudo apt install -y cherokee cherokee-admin libcherokee-mod-libssl libcherokee-mod-streaming
Install MySQL server and development libraries
Install MySQL server along with development headers needed for Cherokee's MySQL integration.
sudo apt install -y mysql-server mysql-client libmysqlclient-dev php-mysql php-fpm
Start and secure MySQL
Enable MySQL service and run the security script to set up initial authentication.
sudo systemctl enable --now mysql
sudo mysql_secure_installation
Follow the prompts to set a root password and remove test databases.
Create MySQL database and user
Create a dedicated database and user for your web application with optimized privileges.
sudo mysql -u root -p
CREATE DATABASE webapp_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'webapp_user'@'localhost' IDENTIFIED BY 'secure_password_here';
GRANT SELECT, INSERT, UPDATE, DELETE ON webapp_db.* TO 'webapp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Optimize MySQL configuration
Configure MySQL with performance optimizations for web applications and Cherokee integration.
[mysqld]
InnoDB Optimization
innodb_buffer_pool_size = 256M
innodb_log_file_size = 64M
innodb_flush_log_at_trx_commit = 2
innodb_flush_method = O_DIRECT
Query Cache (for read-heavy workloads)
query_cache_type = 1
query_cache_size = 64M
query_cache_limit = 2M
Connection Optimization
max_connections = 200
max_connect_errors = 10000
connect_timeout = 10
wait_timeout = 600
interactive_timeout = 600
Thread Cache
thread_cache_size = 16
thread_stack = 256K
Table Cache
table_open_cache = 4000
table_definition_cache = 2000
Slow Query Log
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 2
Binary Logging
log_bin = /var/log/mysql/mysql-bin.log
binlog_expire_logs_seconds = 259200
max_binlog_size = 100M
Restart MySQL and verify optimization
Apply the configuration changes and verify MySQL is running with optimized settings.
sudo systemctl restart mysql
sudo systemctl status mysql
mysql -u root -p -e "SHOW VARIABLES LIKE 'innodb_buffer_pool_size';"
Configure PHP-FPM for Cherokee
Set up PHP-FPM with optimized settings for Cherokee web server integration.
[www]
user = www-data
group = www-data
listen = /run/php/php8.3-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
pm = dynamic
pm.max_children = 20
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 6
pm.max_requests = 500
; Performance tuning
request_terminate_timeout = 60s
rlimit_files = 65536
rlimit_core = 0
; Security
security.limit_extensions = .php .php3 .php4 .php5 .php7 .php8
Optimize PHP configuration
Configure PHP with MySQL optimizations and performance settings for Cherokee.
; Memory and execution limits
memory_limit = 256M
max_execution_time = 60
max_input_time = 60
post_max_size = 32M
upload_max_filesize = 32M
max_file_uploads = 20
; MySQL/MySQLi optimization
mysql.connect_timeout = 10
mysql.default_socket = /var/run/mysqld/mysqld.sock
mysqli.default_socket = /var/run/mysqld/mysqld.sock
; PDO MySQL optimization
pdo_mysql.default_socket = /var/run/mysqld/mysqld.sock
pdo_mysql.cache_size = 2000
; OPcache configuration
opcache.enable = 1
opcache.memory_consumption = 128
opcache.max_accelerated_files = 4000
opcache.validate_timestamps = 1
opcache.revalidate_freq = 60
; Session optimization
session.save_handler = files
session.gc_maxlifetime = 1440
session.gc_probability = 1
session.gc_divisor = 100
Start PHP-FPM service
Enable and start PHP-FPM to handle dynamic content processing for Cherokee.
sudo systemctl enable --now php8.3-fpm
sudo systemctl status php8.3-fpm
Start Cherokee admin interface
Launch Cherokee's web-based admin interface for easy configuration management.
sudo cherokee-admin -b
sudo systemctl enable cherokee
The admin interface will be available at http://your-server-ip:9090. Note the one-time password displayed in the terminal.
Configure Cherokee FastCGI for PHP
Set up FastCGI to connect Cherokee with PHP-FPM for dynamic content processing.
In the Cherokee admin interface:
- Go to vServers → default → Behavior
- Click Rule Management → Add new rule
- Select Extensions and enter:
php - Set Handler to FastCGI
- Configure the FastCGI source:
Information Source: Local Interpreter
Host: localhost
Socket: /run/php/php8.3-fpm.sock
Spawn Server: No
Configure Cherokee caching
Enable Cherokee's built-in caching system for improved performance with database-driven content.
In the Cherokee admin interface:
- Go to vServers → default → Behavior
- Add a new rule for Directory:
/ - Set Handler to Static Content
- Enable Only allow GET and HEAD methods
- Configure caching in the Expiration tab:
Mode: Specify max-age
Time: 1h
No cache: Uncheck
Public: Check
Configure Cherokee compression
Enable GZIP compression to reduce bandwidth usage and improve load times.
In the Cherokee admin interface:
- Go to vServers → default → Encoding
- Check Allow x-gzip and Allow gzip
- Set compression level to 6 (balanced speed/ratio)
- Add MIME types:
text/plain
text/html
text/css
text/javascript
application/javascript
application/json
application/xml
text/xml
Create MySQL connection pool configuration
Configure connection pooling to optimize database performance under load.
host};dbname={$this->database};charset=utf8mb4";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_PERSISTENT => true,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4",
PDO::ATTR_TIMEOUT => 10,
];
$this->connection = new PDO($dsn, $this->username, $this->password, $options);
} catch (PDOException $e) {
error_log("Database connection failed: " . $e->getMessage());
throw $e;
}
}
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public function getConnection() {
return $this->connection;
}
}
?>
Set proper file permissions
Configure secure file permissions for Cherokee and web application files.
sudo chown -R www-data:www-data /var/www/html
sudo find /var/www/html -type d -exec chmod 755 {} \;
sudo find /var/www/html -type f -exec chmod 644 {} \;
sudo chmod 640 /var/www/html/config/database.php
Apply Cherokee configuration
Save and apply all configuration changes in the Cherokee admin interface.
In the Cherokee admin interface, click Save to apply all changes, then restart Cherokee:
sudo systemctl restart cherokee
sudo systemctl status cherokee
Verify your setup
Test that Cherokee is properly connected to MySQL and serving dynamic content.
sudo systemctl status cherokee
sudo systemctl status mysql
sudo systemctl status php8.3-fpm
curl -I http://localhost/
mysql -u webapp_user -p -e "SELECT 1 as test;"
Create a test PHP file to verify database connectivity:
getConnection();
$stmt = $db->query('SELECT VERSION() as mysql_version, NOW() as current_time');
$result = $stmt->fetch();
echo "Cherokee + MySQL Test
";
echo "MySQL Version: " . $result['mysql_version'] . "
";
echo "Current Time: " . $result['current_time'] . "
";
echo "Connection: SUCCESS
";
} catch (Exception $e) {
echo "Connection failed: " . $e->getMessage() . "
";
}
?>
Access http://your-server-ip/test_db.php to verify the connection works.
Performance monitoring and tuning
Enable Cherokee performance logging
Configure detailed logging to monitor Cherokee performance and identify bottlenecks.
In the Cherokee admin interface:
- Go to vServers → default → Logging
- Enable Access Log
- Set format to Combined Log Format
- Add custom log for timing:
%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-agent}i" %D
Monitor MySQL performance
Set up monitoring for MySQL query performance and connection usage.
mysql -u root -p -e "SHOW GLOBAL STATUS LIKE 'Connections';"
mysql -u root -p -e "SHOW GLOBAL STATUS LIKE 'Threads_connected';"
mysql -u root -p -e "SHOW GLOBAL STATUS LIKE 'Slow_queries';"
mysql -u root -p -e "SHOW PROCESSLIST;"
Create performance monitoring script
Set up automated monitoring for Cherokee and MySQL performance metrics.
#!/bin/bash
Cherokee + MySQL Performance Monitor
LOGFILE="/var/log/cherokee_mysql_performance.log"
DATE=$(date '+%Y-%m-%d %H:%M:%S')
echo "[$DATE] Performance Check" >> $LOGFILE
Cherokee process info
CHEROKEE_PROCS=$(ps aux | grep cherokee | grep -v grep | wc -l)
echo "Cherokee processes: $CHEROKEE_PROCS" >> $LOGFILE
MySQL connections
MYSQL_CONNECTIONS=$(mysql -u root -p"$MYSQL_ROOT_PASSWORD" -e "SHOW GLOBAL STATUS LIKE 'Threads_connected';" -s -N | awk '{print $2}')
echo "MySQL connections: $MYSQL_CONNECTIONS" >> $LOGFILE
PHP-FPM processes
PHP_FPM_PROCS=$(ps aux | grep php-fpm | grep -v grep | wc -l)
echo "PHP-FPM processes: $PHP_FPM_PROCS" >> $LOGFILE
Load average
LOAD_AVG=$(uptime | awk -F'load average:' '{print $2}')
echo "Load average: $LOAD_AVG" >> $LOGFILE
echo "---" >> $LOGFILE
sudo chmod +x /usr/local/bin/monitor_cherokee_mysql.sh
sudo crontab -e
Add this line to run monitoring every 5 minutes:
/5 * /usr/local/bin/monitor_cherokee_mysql.sh
For comprehensive monitoring of Cherokee and MySQL performance, consider integrating with Prometheus and Grafana dashboards for real-time metrics visualization.
Common issues
| Symptom | Cause | Fix |
|---|---|---|
| PHP files download instead of executing | FastCGI not configured properly | Check FastCGI socket path in Cherokee admin, verify PHP-FPM is running |
| Database connection timeouts | MySQL max_connections too low | Increase max_connections in MySQL config, restart MySQL |
| Cherokee won't start after config changes | Syntax error in configuration | Check sudo journalctl -u cherokee for error details |
| High memory usage | PHP-FPM pool size too high | Reduce pm.max_children in PHP-FPM pool config |
| Slow database queries | Missing indexes or poor queries | Enable slow query log, analyze with EXPLAIN statements |
| Permission denied errors | Incorrect file ownership | sudo chown -R www-data:www-data /var/www/html |
| FastCGI socket connection failed | PHP-FPM not listening on socket | Verify socket path in both Cherokee and PHP-FPM configs match |
Next steps
- Configure NGINX SSL termination with Redis session storage for advanced load balancing
- Monitor MySQL performance with Prometheus and Grafana dashboards for comprehensive monitoring
- Configure Cherokee SSL certificates and security hardening
- Set up Cherokee load balancing with multiple backend servers
- Implement Cherokee caching strategies for dynamic content
Running this in production?
Automated install script
Run this to automate the entire setup
#!/usr/bin/env bash
set -euo pipefail
# Cherokee Web Server with MySQL Optimization Install Script
# Supports Ubuntu, Debian, AlmaLinux, Rocky Linux, CentOS, RHEL
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# Configuration variables
DB_NAME="${1:-webapp_db}"
DB_USER="${2:-webapp_user}"
DB_PASS="${3:-$(openssl rand -base64 32)}"
DOMAIN="${4:-localhost}"
# Usage function
usage() {
echo "Usage: $0 [db_name] [db_user] [db_password] [domain]"
echo "Example: $0 myapp appuser mypassword example.com"
exit 1
}
# Logging functions
log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
# Cleanup function
cleanup() {
log_error "Installation failed. Cleaning up..."
systemctl stop cherokee 2>/dev/null || true
systemctl stop mysql 2>/dev/null || true
systemctl stop php*-fpm 2>/dev/null || true
}
# Set error trap
trap cleanup ERR
# Check if running as root
if [[ $EUID -ne 0 ]]; then
log_error "This script must be run as root or with sudo"
exit 1
fi
# Detect distribution
if [ ! -f /etc/os-release ]; then
log_error "Cannot detect distribution. /etc/os-release not found."
exit 1
fi
. /etc/os-release
case "$ID" in
ubuntu|debian)
PKG_MGR="apt"
PKG_UPDATE="apt update -y"
PKG_INSTALL="apt install -y"
PHP_VERSION="8.3"
PHP_FPM_SERVICE="php8.3-fpm"
PHP_FPM_CONF="/etc/php/8.3/fpm/pool.d/www.conf"
PHP_INI="/etc/php/8.3/fpm/php.ini"
MYSQL_CONF="/etc/mysql/mysql.conf.d/99-cherokee.cnf"
;;
almalinux|rocky|centos|rhel|ol)
PKG_MGR="dnf"
PKG_UPDATE="dnf update -y"
PKG_INSTALL="dnf install -y"
PHP_VERSION="8.1"
PHP_FPM_SERVICE="php-fpm"
PHP_FPM_CONF="/etc/php-fpm.d/www.conf"
PHP_INI="/etc/php.ini"
MYSQL_CONF="/etc/my.cnf.d/99-cherokee.cnf"
;;
fedora)
PKG_MGR="dnf"
PKG_UPDATE="dnf update -y"
PKG_INSTALL="dnf install -y"
PHP_VERSION="8.2"
PHP_FPM_SERVICE="php-fpm"
PHP_FPM_CONF="/etc/php-fpm.d/www.conf"
PHP_INI="/etc/php.ini"
MYSQL_CONF="/etc/my.cnf.d/99-cherokee.cnf"
;;
amzn)
PKG_MGR="yum"
PKG_UPDATE="yum update -y"
PKG_INSTALL="yum install -y"
PHP_VERSION="8.1"
PHP_FPM_SERVICE="php-fpm"
PHP_FPM_CONF="/etc/php-fpm.d/www.conf"
PHP_INI="/etc/php.ini"
MYSQL_CONF="/etc/my.cnf.d/99-cherokee.cnf"
;;
*)
log_error "Unsupported distribution: $ID"
exit 1
;;
esac
log_info "Detected distribution: $PRETTY_NAME"
log_info "Using package manager: $PKG_MGR"
# [1/10] Update system packages
echo "[1/10] Updating system packages..."
$PKG_UPDATE
# [2/10] Install Cherokee web server
echo "[2/10] Installing Cherokee web server..."
if [[ "$PKG_MGR" == "apt" ]]; then
$PKG_INSTALL cherokee cherokee-admin libcherokee-mod-libssl libcherokee-mod-streaming
else
# Enable EPEL for RHEL-based systems
$PKG_INSTALL epel-release
$PKG_INSTALL cherokee cherokee-admin
fi
# [3/10] Install MySQL server and PHP
echo "[3/10] Installing MySQL server and PHP..."
if [[ "$PKG_MGR" == "apt" ]]; then
$PKG_INSTALL mysql-server mysql-client libmysqlclient-dev php$PHP_VERSION-fpm php$PHP_VERSION-mysql php$PHP_VERSION-cli php$PHP_VERSION-common
else
$PKG_INSTALL mysql-server mysql-devel php php-fpm php-mysqlnd php-cli php-common
fi
# [4/10] Start and enable services
echo "[4/10] Starting and enabling services..."
systemctl enable --now mysql
systemctl enable --now $PHP_FPM_SERVICE
systemctl enable --now cherokee
# [5/10] Secure MySQL installation
echo "[5/10] Securing MySQL installation..."
mysql -e "DELETE FROM mysql.user WHERE User='';"
mysql -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');"
mysql -e "DROP DATABASE IF EXISTS test;"
mysql -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';"
mysql -e "FLUSH PRIVILEGES;"
# [6/10] Create database and user
echo "[6/10] Creating database and user..."
mysql -e "CREATE DATABASE IF NOT EXISTS $DB_NAME CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
mysql -e "CREATE USER IF NOT EXISTS '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PASS';"
mysql -e "GRANT SELECT, INSERT, UPDATE, DELETE ON $DB_NAME.* TO '$DB_USER'@'localhost';"
mysql -e "FLUSH PRIVILEGES;"
# [7/10] Configure MySQL optimization
echo "[7/10] Configuring MySQL optimization..."
cat > "$MYSQL_CONF" << 'EOF'
[mysqld]
# InnoDB Optimization
innodb_buffer_pool_size = 256M
innodb_log_file_size = 64M
innodb_flush_log_at_trx_commit = 2
innodb_flush_method = O_DIRECT
# Query Cache (for read-heavy workloads)
query_cache_type = 1
query_cache_size = 64M
query_cache_limit = 2M
# Connection Optimization
max_connections = 200
max_connect_errors = 10000
connect_timeout = 10
wait_timeout = 600
interactive_timeout = 600
# Thread Cache
thread_cache_size = 16
thread_stack = 256K
# Table Cache
table_open_cache = 4000
table_definition_cache = 2000
# Slow Query Log
slow_query_log = 1
long_query_time = 2
EOF
chown mysql:mysql "$MYSQL_CONF"
chmod 644 "$MYSQL_CONF"
# [8/10] Configure PHP-FPM
echo "[8/10] Configuring PHP-FPM..."
cp "$PHP_FPM_CONF" "$PHP_FPM_CONF.bak"
# Update PHP-FPM pool configuration
sed -i 's/^pm.max_children = .*/pm.max_children = 20/' "$PHP_FPM_CONF"
sed -i 's/^pm.start_servers = .*/pm.start_servers = 4/' "$PHP_FPM_CONF"
sed -i 's/^pm.min_spare_servers = .*/pm.min_spare_servers = 2/' "$PHP_FPM_CONF"
sed -i 's/^pm.max_spare_servers = .*/pm.max_spare_servers = 6/' "$PHP_FPM_CONF"
# Add performance settings to PHP-FPM config
cat >> "$PHP_FPM_CONF" << 'EOF'
; Performance tuning
pm.max_requests = 500
request_terminate_timeout = 60s
rlimit_files = 65536
rlimit_core = 0
EOF
# [9/10] Optimize PHP configuration
echo "[9/10] Optimizing PHP configuration..."
cp "$PHP_INI" "$PHP_INI.bak"
# Update PHP settings
sed -i 's/^memory_limit = .*/memory_limit = 256M/' "$PHP_INI"
sed -i 's/^max_execution_time = .*/max_execution_time = 60/' "$PHP_INI"
sed -i 's/^max_input_time = .*/max_input_time = 60/' "$PHP_INI"
sed -i 's/^post_max_size = .*/post_max_size = 32M/' "$PHP_INI"
sed -i 's/^upload_max_filesize = .*/upload_max_filesize = 32M/' "$PHP_INI"
# Restart services to apply configurations
systemctl restart mysql
systemctl restart $PHP_FPM_SERVICE
systemctl restart cherokee
# [10/10] Verification and final setup
echo "[10/10] Verifying installation and configuration..."
# Check service status
if ! systemctl is-active --quiet mysql; then
log_error "MySQL service is not running"
exit 1
fi
if ! systemctl is-active --quiet $PHP_FPM_SERVICE; then
log_error "PHP-FPM service is not running"
exit 1
fi
if ! systemctl is-active --quiet cherokee; then
log_error "Cherokee service is not running"
exit 1
fi
# Test database connection
if ! mysql -u "$DB_USER" -p"$DB_PASS" -e "USE $DB_NAME;" 2>/dev/null; then
log_error "Database connection test failed"
exit 1
fi
# Configure firewall
if command -v ufw >/dev/null 2>&1; then
ufw --force enable
ufw allow 80/tcp
ufw allow 443/tcp
ufw allow 9090/tcp # Cherokee admin
elif command -v firewall-cmd >/dev/null 2>&1; then
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --permanent --add-port=9090/tcp
firewall-cmd --reload
fi
log_info "Cherokee web server installation completed successfully!"
log_info "Database: $DB_NAME"
log_info "Database User: $DB_USER"
log_info "Database Password: $DB_PASS"
log_info "Cherokee Admin: http://$DOMAIN:9090"
log_info "To access Cherokee admin, run: cherokee-admin -b"
Review the script before running. Execute with: bash install.sh