How to optimize image compression for shared hosting sites
Reduce page load times by converting PNGs to WebP, resizing uploads, and configuring server-side compression rules for cPanel and Plesk shared environments.
Reducing image file sizes improves page load speed and lowers bandwidth usage on shared hosting plans. These steps target cPanel 118.x and Plesk Obsidian 18.x environments running on Ubuntu 24.04 or AlmaLinux 9.
Prerequisites
- Access to a shared hosting control panel (cPanel or Plesk).
- Root or reseller access to install server-side modules like mod_webp or LiteSpeed cache.
- A working website with existing images in /public_html or /var/www/vhosts.
- Basic knowledge of file paths and FTP/SFTP tools.
Step 1: Install and configure ImageMagick
ImageMagick is the standard tool for resizing and converting images on Linux servers. Install it via the package manager to enable local conversion of large files before they reach the browser.
sudo apt-get update
sudo apt-get install -y imagemagick
ImageMagick 6.9.11-60+deb12u2
Verify the installation by running the version command. You should see output indicating the installed version.
convert -version
Step 2: Create a WebP conversion script
Create a script that automatically converts PNG and JPEG files to WebP format while preserving quality. Place this script in the /home/<username>/public_html/scripts directory.
#!/bin/bash
INPUT_FILE=$1
OUTPUT_FILE=$2
if command -v convert &>/dev/null; then
convert -quality 85 -strip -define png:alpha=false $INPUT_FILE $OUTPUT_FILE.webp
echo "Converted $INPUT_FILE to WebP"
else
echo "ImageMagick not found."
fi
Make the script executable so the server can run it.
chmod +x /home/<username>/public_html/scripts/convert-to-webp.sh
Step 3: Configure cPanel to serve WebP automatically
In cPanel, enable the WebP module to serve optimized images to compatible browsers without manual conversion. Navigate to the Software section and select WebP to toggle it on.
Alternatively, add a .htaccess rule to force conversion for specific file types. This rule checks if the browser supports WebP and serves the .webp version if available.
# Add to .htaccess in public_html
RewriteEngine On
RewriteCond %{HTTP_ACCEPT} image/svg+xml,*
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME}\.webp -f
RewriteRule ^(.+)\.(png|jpg|jpeg)$ $1.webp [L]
Step 4: Set up server-side caching for images
Enable LiteSpeed cache or Varnish to serve compressed images from memory. In cPanel, go to Software > LiteSpeed Cache and enable image optimization.
For Nginx-based stacks, configure the fastcgi_cache directive to cache static assets. This reduces database queries and speeds up image delivery.
location ~* \.(jpg|jpeg|png|gif|webp|ico|svg)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
Step 5: Resize images on upload
Prevent large files from being uploaded by restricting max file size in /etc/php-fpm.d/www.conf. Set upload_max_filesize = 2M and post_max_size = 2M.
Create a cron job to scan and resize images daily. This ensures old images are optimized without manual intervention.
0 2 * * * /home/<username>/public_html/scripts/resize-images.sh
Verify the installation
Run a simple test to confirm WebP is being served. Use the curl command with the -H flag to check the Content-Type header.
curl -I https://yourdomain.com/image.png
You should see Content-Type: image/webp in the response headers if optimization is active.
Troubleshooting
Error: "convert: unable to open X server"
ImageMagick requires an X server for some operations. Install the X server package or use the -verbose flag to debug.
sudo apt-get install -y imagemagick-6.q16
Error: "403 Forbidden on .webp files"
Ensure the .webp file extension is allowed in /etc/nginx/nginx.conf or apache2.conf. Add the location block for static assets.
Error: "ImageMagick not found"
Reinstall ImageMagick using sudo apt-get install -y imagemagick. Ensure the path is correct in your script.