How can I activate CORS for my VPS with Cyber Panel?

I’ve already tried enabling CORS with the <Header Set Access-Control-Allow-Origin “*”> entry in the .htaccess file, but it doesn’t work.

Whenever I want to test it with this page cors-test.codehappy.dev I get the message:

image

I already found the solution, but I still want to share it. You have to edit the vhost.conf.

→ see list of vhosts
cd /usr/local/lsws/conf/vhosts/
la -la

→ go to your Domain
cd yourdomain.xyz
la -la

→ do a backup
cp vhost.conf vhost.conf.bak

→ edit vhost.conf
nano vhost.conf

→ add the following

context / {
  location                $DOC_ROOT/
  allowBrowse             1
  extraHeaders            Access-Control-Allow-Origin *

  rewrite  {
RewriteFile .htaccess
  }
  addDefaultCharset       off

  phpIniOverride  {

  }
}

→ restart LiteSpeed Web Server
systemctl restart lsws

Now it should work and you will see:

image

Addendum: If you only want CORS to be activated for a certain directory, then you can specify the path that should be allowed in vhost.conf after context /. Here e.g. the path /public_html/.well-known`.

context /public_html/.well-known {
  location                $DOC_ROOT/
  allowBrowse             1
  extraHeaders            Access-Control-Allow-Origin *
..