LiteSpeed Cache (LSCache) for Drupal: Enhance Website Performance

LiteSpeed Cache (LSCache) for Drupal: Enhance Website Performance

Several clients who use Drupal versions 8, 9. 10 and 11 already taking advantage of the LiteSpeed ​​Cache for Drupal which is available on our hosting server (LiteSpeed-powered hosting).

What is LSCache?

LiteSpeed ​​Cache (also called LSCache) is LiteSpeed's more efficient and highly customizable answer to Apache mod_cache and Varnish.

LSCache is built from the ground up and integrated into all LiteSpeed ​​server products. It can: 

  • dramatically speed up dynamic website content (like PHP pages)
  • provide more efficient handling of static content (like images)
  • reduce server load

For the installation of the LSCache plugin on the website, you can see this page https://docs.litespeedtech.com/lscache/lscdrupal/installation/

One of the instructions is to add code to Drupal's .htaccess.

<IfModule LiteSpeed>
CacheLookup on
</IfModule>

For Drupal website installations without using Composer, changing .htaccess will not be a problem, but if we use Composer to install modules, it seems that whenever you update or install a module via Composer, it resets .htaccess to original.

Here I will explain the steps on how we can solve this problem.

To ensure that the additional code (<IfModule LiteSpeed>CacheLookup on</IfModule>) is not lost when running Composer, you can take one of the following approaches:

1. Use a .htaccess Patch Script

Create a script that will automatically re-apply the custom LiteSpeed code to the .htaccess file after every Composer update. This can be done by writing a simple shell or PHP script and executing it after Composer runs.

Steps:

  • Create a shell script (update-htaccess.sh) with the following content:
#!/bin/bash
HTACCESS_FILE="web/.htaccess" # Adjust path if needed
LITESPEED_CODE="<IfModule LiteSpeed>
CacheLookup on
</IfModule>"

# Check if LiteSpeed code is already present
if ! grep -q "LiteSpeed" "$HTACCESS_FILE"; then
  echo "$LITESPEED_CODE" >> "$HTACCESS_FILE"
  echo "LiteSpeed code added to .htaccess"
fi
  • Make the script executable:
chmod +x update-htaccess.sh
  • Add the script to Composer's post-update or post-install commands in the composer.json file:
"scripts": {
    "post-install-cmd": [
        "bash update-htaccess.sh"
    ],
    "post-update-cmd": [
        "bash update-htaccess.sh"
    ]
}

Let me break down the script and explain how it works step by step.

Script: `update-htaccess.sh`

#!/bin/bash
HTACCESS_FILE="web/.htaccess" # Adjust path if needed
LITESPEED_CODE="<IfModule LiteSpeed>
CacheLookup on
</IfModule>"
# Check if LiteSpeed code is already present
if ! grep -q "LiteSpeed" "$HTACCESS_FILE"; then
 echo "$LITESPEED_CODE" >> "$HTACCESS_FILE"
 echo "LiteSpeed code added to .htaccess"
fi

Explanation:

1. #!/bin/bash

  • This is the "shebang" line. It tells the system to execute the script using the Bash shell. It ensures that when you run the script, the system knows which interpreter to use.

2. HTACCESS_FILE="web/.htaccess"

  • This line defines a variable HTACCESS_FILE, which points to the location of the `.htaccess` file.
  • The value "web/.htaccess" is the file path to the .htaccess file in your project. Adjust the path as needed based on your Drupal project structure. If your .htaccess is in the root, you could change this to .htaccess.

3. LITESPEED_CODE="<IfModule LiteSpeed> CacheLookup on </IfModule>"

  • This line defines the variable LITESPEED_CODE, which stores the custom LiteSpeed directive that you want to add to the .htaccess file.
  • This block contains the code:    
<IfModule LiteSpeed>
CacheLookup on
</IfModule>

4. if ! grep -q "LiteSpeed" "$HTACCESS_FILE"; then

  • This is the beginning of an if statement.
  • grep -q "LiteSpeed" "$HTACCESS_FILE" checks if the .htaccess file already contains the text "LiteSpeed." grep is a command that searches for patterns within files.
  • The -q option makes grep run quietly, without printing any output. It simply returns true or false based on whether the text "LiteSpeed" is found.
  • The ! inverts the result, meaning if "LiteSpeed" is not found in the .htaccess file, the if block will run.

5. echo "$LITESPEED_CODE" >> "$HTACCESS_FILE"

  • If "LiteSpeed" is not found in the .htaccess file, this line executes.
  • The echo command outputs the content of the LITESPEED_CODE variable (the LiteSpeed block) and appends it to the .htaccess file.
  • The >> operator appends the text to the end of the .htaccess file. If you used >, it would overwrite the entire file, but >> ensures you’re just adding to the end.

6. echo "LiteSpeed code added to .htaccess"

  • This line simply prints a message to the terminal to let you know that the LiteSpeed code was successfully added to the .htaccess file.

7. fi

  • This marks the end of the if statement. If "LiteSpeed" is already present in the .htaccess file, the script skips the block inside the if and nothing is changed.

Purpose and Workflow:

1. Check if LiteSpeed Code Exists: 

  • The script first checks if the .htaccess file already has the LiteSpeed directive to avoid duplicating the code every time you run the script.

2. Append LiteSpeed Code if Missing:

  • If the LiteSpeed directive is not present, it appends the custom code to the end of the .htaccess file.

3. Post-Composer Execution:

  • The script is designed to be run after you execute Composer (composer install or composer update). Since running Composer may overwrite the .htaccess file, this script ensures that your custom LiteSpeed code is re-applied after each Composer run.

4. Automation with Composer:

  • You can automate running this script after Composer commands by adding it to your composer.json file as part of the post-install-cmd and post-update-cmd hooks. This means the script will run every time you install or update your dependencies via Composer, ensuring your custom LiteSpeed code is preserved.

  Here’s the relevant part in composer.json:

"scripts": {
    "post-install-cmd": [
        "bash update-htaccess.sh"
    ],
    "post-update-cmd": [
        "bash update-htaccess.sh"
    ]
}

This way, you don't have to remember to run the script manually each time you run Composer. The process becomes automatic.

Customization:

  • File Path Adjustment: Make sure the path to the .htaccess file (web/.htaccess) matches the structure of your project.
  • Additional Code: If you want to add more custom server configurations, simply update the LITESPEED_CODE variable.

This solution ensures that your custom LiteSpeed configuration is retained even when Composer modifies or replaces the .htaccess file.

2. Use a .htaccess Template

You can maintain a custom .htaccess template file that includes your custom LiteSpeed code and copy it over after Composer runs.

Steps:

  • Create a file called .htaccess.template that includes both the default Drupal .htaccess content and your custom LiteSpeed block.
  • After running Composer, copy this template back over the updated .htaccess:
cp .htaccess.template web/.htaccess

This can also be automated with a Composer post-update script.

3. Modify Composer Install Command to Preserve .htaccess

If you want to ensure that Composer doesn’t overwrite .htaccess, you can run:

composer install --no-scripts

This will prevent Composer from executing any scripts that might replace or update the .htaccess file.

By using one of these approaches, your custom LiteSpeed code will remain in the .htaccess file even after Composer runs.