Hosting

Configuring and optimising LiteSpeed hosting

1. Introduction to LiteSpeed

LiteSpeed is a powerful web server that delivers a remarkable performance boost compared to Apache. It is fully backwards-compatible with Apache, so the server behaves exactly the same way.

LiteSpeed ships with an advanced caching system called LSCache, which caches the dynamic content of your websites and serves it as static content, dramatically improving load times: users browse the site and pages load instantly.

The secret behind this performance is the native integration between the different CMSs (WordPress, Prestashop, Magento, Drupal, Joomla...) and LiteSpeed itself.

Below we explain how to configure LiteSpeed to get the most out of your website.

2. Enabling LSCache with a plugin

Enabling LSCache is very simple: just install the plugin matching your CMS.

After installing the plugin you will notice load times drop drastically.

If you want to purge the LiteSpeed cache, cPanel provides an icon to do so.

The plugins are completely free. The LiteSpeed server itself does require a licence fee, but at GINERNET we cover that cost so you can use the plugins without paying anything.

3. Enabling LSCache without a plugin

If your website has no LiteSpeed module, you can apply the following directive at the top of your .htaccess file:

To cache all content:

RewriteRule ^(.*) - [E=Cache-Control:max-age=86400]

To cache a specific file:

RewriteRule ^file.php$ - [E=Cache-Control:max-age=86400]

The max-age variable specifies, in seconds, how long content stays cached; after that time the cache is purged. 86400 seconds = 1 day.

4. Configuring the LiteSpeed crawler for Prestashop or WordPress

The LiteSpeed crawler for Prestashop (it also works with WordPress) is a script that automatically and periodically visits every page of your website to load it into the cache. That way, when a user visits your site, it is served straight from the cache (faster).

  1. Generate a sitemap so the crawler knows which pages to cache. In Prestashop you can use the Google Sitemap tool.
  2. Download the crawler script and upload it via FTP (it does not need to live inside public_html).
  3. Go to cPanel and create a daily cron job, for example at 3:00 AM, with this command:
bash /home/USER/cachecrawler.sh -m SITEMAP_URL >/dev/null 2>&1

For example:

bash /home/ginernet/cachecrawler.sh -m https://ginernet.com/sitemap_index.xml >/dev/null 2>&1

You can contact us and we will verify that the cron job and the crawler are running correctly.

5. Optimising LSCache in WordPress

The WordPress LSCache plugin has a huge number of options and there is no universal configuration that suits every website. The following settings have been tested with most websites and deliver an outstanding level of optimisation:

General cache

Enable Cache             ON
Cache Logged-in Users    ON
Cache Commenters         ON
Cache REST API           ON
Cache Login Page         ON
Cache favicon.ico        OFF
Cache PHP Resources      OFF
Cache Mobile             OFF

Advanced cache

Instant Click            ON

Page Optimization > CSS Settings

CSS Minify                       OFF
CSS Combine                      OFF
Generate UCSS                    OFF
UCSS Inline                      OFF
CSS Combine External and Inline  OFF
Load CSS Asynchronously          ON
CCSS Per URL                     ON
Inline CSS Async Lib             ON
Font Display Optimization        Swap

Page Optimization > JS Settings

JS Minify                        OFF
JS Combine                       OFF
JS Combine External and Inline   OFF
Load JS Deferred                 OFF

Page Optimization > HTML Settings

HTML Minify                      ON
DNS Prefetch Control             ON
Remove Query Strings             OFF
Load Google Fonts Asynchronously OFF
Remove Google Fonts              OFF
Remove WordPress Emoji           OFF
Remove Noscript Tag              OFF

6. LiteSpeed ReCAPTCHA protection

On LiteSpeed servers you can protect access to your website with a reCAPTCHA verification. It is configured very simply via .htaccess:

Protect the whole website for specific countries (China and Russia in the example):

<IfModule LiteSpeed>
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(CN|RU)$
RewriteRule .* - [E=verifycaptcha:drop][L]
</IfModule>

Protect the whole website for everyone:

<IfModule LiteSpeed>
RewriteRule .* - [E=verifycaptcha:drop]
</IfModule>

Be careful with this last rule: payment gateways will not be able to deliver notifications because they cannot solve the reCAPTCHA.

Show the ReCaptcha to every visitor NOT coming from Spain, while allowing search engines and payment gateways:

<IfModule LiteSpeed>
RewriteCond %{HTTP_USER_AGENT} !(Paypal|Stripe|Googlebot|Bingbot|Slurp|DuckDuckBot|Baiduspider|YandexBot|facebookexternalhit|Twitterbot|LinkedInBot) [NC]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} !^ES$
RewriteRule .* - [E=verifycaptcha:drop][L]
</IfModule>

Protect the whole website except some specific directories/files:

<IfModule LiteSpeed>
RewriteCond %{REQUEST_URI} !^/excluded/directory/
RewriteCond %{REQUEST_URI} !^/file/exclude.php
RewriteRule .* - [E=verifycaptcha:drop]
</IfModule>

Protect only some specific directories/files:

<IfModule LiteSpeed>
RewriteRule file.php - [E=verifycaptcha:drop]
RewriteRule ^directory - [E=verifycaptcha:drop]
RewriteRule ^directory/allowed - [E=verifycaptcha:drop]
RewriteRule ^directory/file.php - [E=verifycaptcha:drop]
</IfModule>

Completely block one or more countries, returning a 403 error to visitors:

<IfModule LiteSpeed>
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(CN|RU)$
RewriteRule ^(.*)$ - [F,L]
</IfModule>

7. Setting custom php.ini values in LiteSpeed

LiteSpeed lets you modify certain PHP parameters via .htaccess. Edit your .htaccess file and define the values you need; here is a syntax example:

php_value max_execution_time 300
php_value memory_limit 2048M
php_value upload_max_filesize 2048M
php_value post_max_size 2048M

To verify which values the server is actually returning, create a test PHP page that runs the phpinfo() function:

<?php
phpinfo();
?>

8. Removing the LiteSpeed server timeout

The directives in this section prevent LiteSpeed processes from ever being killed, which can saturate the server and make your website inaccessible. Make sure you know what you are doing.

Every LiteSpeed process is configured to be killed automatically 300 seconds after it starts. Normally, when an action takes longer than that, something is wrong, which is why the process is killed to avoid saturating the server with zombie processes. However, if you need to remove this limit, here is how.

1. For specific URLs:

<IfModule Litespeed>
RewriteEngine On
RewriteRule ^file\.php$ - [E=noabort:1, E=noconntimeout:1]
RewriteRule ^.*/file\.php$ - [E=noabort:1, E=noconntimeout:1]
RewriteRule ^directory/ - [E=noabort:1, E=noconntimeout:1]
RewriteRule ^directory/file\.php$ - [E=noabort:1, E=noconntimeout:1]
</IfModule>
  • ^file.php$ applies to the file file.php in the root: ^ ensures the URL starts with "file.php" and $ guarantees nothing comes after ".php".
  • ^.*/file.php$ applies to any file called file.php, no matter which directory it lives in.
  • ^directory/ applies to any URL starting with "directory/": it affects every file and subdirectory inside /directory.
  • ^directory/file.php$ applies specifically to the file file.php inside directory.

If you do not know which file needs a longer execution time, you can find out by running this command in the cPanel Terminal:

top -c

2. For all URLs:

We do not recommend removing the timeout for every page: in 99% of cases you only need it for one specific file (use method 1). If you still need to remove it globally, these are the directives:

<IfModule Litespeed>
RewriteEngine On
RewriteRule .* - [E=noabort:1, E=noconntimeout:1]
</IfModule>
<IfModule Litespeed>
SetEnv noabort 1
SetEnv noconntimeout 1
</IfModule>

If your server is saturated and your website is inaccessible

Go to the cPanel of the affected domain and click the "Terminal" button.

cPanel Terminal button

A terminal will open where you can kill all processes by running:

kill -9 -1