DEV Community

ahmet gedik
ahmet gedik

Posted on

How to Deploy PHP Applications to Multiple LiteSpeed Servers

LiteSpeed servers offer excellent PHP performance at affordable prices. Here's a complete guide to deploying PHP applications to multiple LiteSpeed shared hosting servers, based on our setup for TrendVidStream.

Understanding LiteSpeed

LiteSpeed is an Apache-compatible web server with built-in page caching. Key advantages for PHP:

  • LiteSpeed SAPI: Optimized PHP execution (faster than mod_php or PHP-FPM in many cases)
  • Built-in page cache: Serves cached pages without touching PHP
  • Apache-compatible .htaccess: Most Apache rules work unchanged
  • OPcache support: PHP bytecode caching for repeated executions

.htaccess Configuration

The biggest gotcha is LiteSpeed vs Apache .htaccess differences:

# Works on both Apache and LiteSpeed
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]

# LiteSpeed-specific caching (Apache skips this block)
<IfModule LiteSpeed>
    CacheEnable public /

    # Cache video pages for 6 hours
    RewriteRule ^watch/ - [E=Cache-Control:public\,max-age=21600]

    # Cache category pages for 3 hours
    RewriteRule ^category/ - [E=Cache-Control:public\,max-age=10800]

    # Don't cache admin pages
    RewriteRule ^admin/ - [E=Cache-Control:no-cache]
</IfModule>

# Cloudflare Flexible SSL compatibility
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
Enter fullscreen mode Exit fullscreen mode

Important: Commas inside [E=Cache-Control:public,max-age=21600] cause 500 errors on Apache because Apache parses commas as flag separators. Escape them with backslash or use the <IfModule LiteSpeed> block.

Deployment Script

#!/bin/bash
# deploy_litespeed.sh

LOCAL_DIR="./"
CONFIG="deploy_hosts.conf"

# Validate config
if grep -qP '\r' "$CONFIG"; then
    echo "Error: Config has Windows line endings. Fix: sed -i 's/\r$//' $CONFIG"
    exit 1
fi

deploy() {
    local alias=$1 host=$2 user=$3 pass=$4 path=$5

    echo "[$(date '+%H:%M:%S')] [$alias] Starting deploy"

    lftp -u "$user","$pass" "$host" << SCRIPT
        set ssl:verify-certificate no
        set net:timeout 30
        set net:max-retries 3
        mirror -R --verbose --ignore-time \
            --exclude .env \
            --exclude .git/ \
            --exclude data/ \
            --exclude '*.log' \
            --exclude '*.db' \
            $LOCAL_DIR $path
        # Clear LiteSpeed cache
        rm -rf ${path}/lscache
        quit
SCRIPT

    echo "[$(date '+%H:%M:%S')] [$alias] Deploy complete"
}

# Parallel deploy to all servers
while IFS='|' read -r alias host user pass path; do
    [[ -z "$alias" || "$alias" == \#* ]] && continue
    deploy "$alias" "$host" "$user" "$pass" "$path" &
done < "$CONFIG"

wait
echo "All deployments complete."
Enter fullscreen mode Exit fullscreen mode

LiteSpeed Cache Management

After deploying, always clear the LiteSpeed cache:

# Via FTP (in lftp)
rm -rf /htdocs/lscache

# Or via PHP if you have a cache management endpoint
file_put_contents('.lscache_purge', time());
Enter fullscreen mode Exit fullscreen mode

OPcache Considerations

<?php
// After deploy, reset OPcache if available
if (function_exists('opcache_reset')) {
    opcache_reset();
    echo "OPcache cleared\n";
}

// Check OPcache status
if (function_exists('opcache_get_status')) {
    $status = opcache_get_status();
    echo "Cached scripts: " . $status['opcache_statistics']['num_cached_scripts'] . "\n";
    echo "Memory used: " . round($status['memory_usage']['used_memory'] / 1024 / 1024, 1) . "MB\n";
}
Enter fullscreen mode Exit fullscreen mode

Monitoring

<?php
// Simple health check endpoint
// GET /health

header('Content-Type: application/json');

echo json_encode([
    'status' => 'ok',
    'php_version' => PHP_VERSION,
    'sapi' => php_sapi_name(),
    'opcache' => function_exists('opcache_get_status'),
    'sqlite_version' => (new PDO('sqlite::memory:'))->query('SELECT sqlite_version()')->fetchColumn(),
    'timestamp' => date('c'),
]);
Enter fullscreen mode Exit fullscreen mode

This deployment approach powers TrendVidStream across 4 LiteSpeed servers, each serving trending video content from different global regions.

LiteSpeed + PHP 8.3 + proper caching configuration delivers excellent performance at shared hosting prices.

Why LiteSpeed for PHP Applications

LiteSpeed deserves more attention in the PHP community. Its built-in page caching eliminates the need for a separate caching layer like Varnish or a CDN for HTML content. The LiteSpeed SAPI provides optimized PHP execution that outperforms traditional mod_php and php-fpm in many benchmarks.

For video platforms like TrendVidStream, LiteSpeed's caching means that the vast majority of page requests are served directly from the server cache without executing any PHP code. This reduces server load, improves response times, and allows modest shared hosting plans to handle significant traffic.

The cache management is straightforward: clear the lscache directory after deploy, and LiteSpeed rebuilds the cache from fresh PHP responses. Cache TTLs are controlled via .htaccess rules, giving fine-grained control over how long different page types are cached.

Combined with PHP's OPcache for bytecode caching, the LiteSpeed + PHP combination delivers enterprise-level performance at shared hosting prices. Our 4 LiteSpeed servers handling 8 global regions total under $50 per month, yet consistently achieve Mobile PageSpeed scores of 71-90.

For PHP developers evaluating hosting options, LiteSpeed shared hosting is the sweet spot between performance and cost. The deployment pattern described in this article makes it practical to manage multiple LiteSpeed servers reliably.

Top comments (0)