ClamAV RuntimeException: Cannot connect to "/var/run/clamav/clamd.ctl": No such file or directory (code 2)

Nextcloud version (eg, 20.0.5): 27.0.0
Operating system and version (eg, Ubuntu 20.04): Ubuntu 20.04.6 LTS
Apache or nginx version (eg, Apache 2.4.25): replace me
PHP version (eg, 7.4): 8.1.20

The issue you are facing:
ClamAV is not working, i.e. clamd does not run.

Is this the first time you’ve seen this error? Y

Steps to replicate it:

  1. Install ClamAV as described in the docs Antivirus scanner — Nextcloud latest Administration Manual latest documentation
  2. Nextcloud.log shows error message.

The output of your Nextcloud log in Admin > Logging:

[files_antivirus] Fehler: RuntimeException: Cannot connect to "/var/run/clamav/clamd.ctl": No such file or directory (code 2) at <<closure>>

0. /var/www/html/apps/files_antivirus/lib/Scanner/ScannerBase.php line 85
   OCA\Files_Antivirus\Scanner\ExternalClam->initScanner()
1. /var/www/html/apps/files_antivirus/lib/BackgroundJob/BackgroundScanner.php line 273
   OCA\Files_Antivirus\Scanner\ScannerBase->scan()
2. /var/www/html/apps/files_antivirus/lib/BackgroundJob/BackgroundScanner.php line 144
   OCA\Files_Antivirus\BackgroundJob\BackgroundScanner->scanOneFile()
3. /var/www/html/apps/files_antivirus/lib/BackgroundJob/BackgroundScanner.php line 97
   OCA\Files_Antivirus\BackgroundJob\BackgroundScanner->processFiles()
4. /var/www/html/apps/files_antivirus/lib/BackgroundJob/BackgroundScanner.php line 80
   OCA\Files_Antivirus\BackgroundJob\BackgroundScanner->scan()
5. /var/www/html/lib/public/BackgroundJob/Job.php line 86
   OCA\Files_Antivirus\BackgroundJob\BackgroundScanner->run()
6. /var/www/html/lib/public/BackgroundJob/TimedJob.php line 103
   OCP\BackgroundJob\Job->start()
7. /var/www/html/lib/public/BackgroundJob/TimedJob.php line 93
   OCP\BackgroundJob\TimedJob->start()
8. /var/www/html/cron.php line 152
   OCP\BackgroundJob\TimedJob->execute()

at 2023-07-06T12:55:04+02:00

[PHP] Fehler: stream_socket_client(): Unable to connect to unix:///var/run/clamav/clamd.ctl (No such file or directory) at /var/www/html/apps/files_antivirus/lib/Scanner/ExternalClam.php#38

at 2023-07-06T12:55:04+02:00

The output of your config.php file in /path/to/nextcloud (make sure you remove any identifiable information!):

<?php
$CONFIG = array (
  'passwordsalt' => '--removed--',
  'secret' => '--removed--',
  'trusted_domains' => 
  array (
    0 => '--removed--',
    1 => '--removed--',
  ),
  'datadirectory' => '/var/www/html/data',
  'dbtype' => 'mysql',
  'version' => '27.0.0.8',
  'overwrite.cli.url' => '--removed--',
  'dbname' => 'nextcloud',
  'dbhost' => 'localhost',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => 'nextcloud',
  'dbpassword' => '--removed--',
  'installed' => true,
  'instanceid' => '--removed--',
  'htaccess.RewriteBase' => '/',
  'mail_smtpmode' => 'smtp',
  'mail_smtpsecure' => 'tls',
  'mail_sendmailmode' => 'smtp',
  'mail_from_address' => '--removed--',
  'mail_domain' => '--removed--',
  'mail_smtpauthtype' => 'LOGIN',
  'mail_smtphost' => '--removed--',
  'mail_smtpauth' => 1,
  'mail_smtpname' => '--removed--',
  'mail_smtppassword' => '--removed--',
  'mail_smtpport' => '587',
  'logfile' => '/var/log/nextcloud.log',
  'memcache.local' => '\\OC\\Memcache\\APCu',
  'filelocking.enabled' => true,
  'memcache.locking' => '\\OC\\Memcache\\Redis',
  'redis' => 
  array (
    'host' => 'localhost',
    'port' => 6379,
    'timeout' => 0.0,
  ),
  'default_phone_region' => 'DE',
  'maintenance' => false,
  'theme' => '',
  'loglevel' => 2,
  'remember_login_cookie_lifetime' => 1296000,
  'session_lifetime' => 1800,
  'session_keepalive' => false,
  'auto_logout' => true,
  'overwriteprotocol' => 'https',
  'logtimezone' => 'Europe/Berlin',
);

clamd.conf:

Automatically Generated by clamav-daemon postinst
#To reconfigure clamd run #dpkg-reconfigure clamav-daemon
#Please read /usr/share/doc/clamav-daemon/README.Debian.gz for details
LocalSocket /var/run/clamav/clamd.ctl
FixStaleSocket true
LocalSocketGroup clamav
LocalSocketMode 666
# TemporaryDirectory is not set to its default /tmp here to make overriding
# the default with environment variables TMPDIR/TMP/TEMP possible
User clamav
ScanMail true
ScanArchive true
ArchiveBlockEncrypted false
MaxDirectoryRecursion 15
FollowDirectorySymlinks false
FollowFileSymlinks false
ReadTimeout 180
MaxThreads 12
MaxConnectionQueueLength 15
LogSyslog false
LogRotate true
LogFacility LOG_LOCAL6
LogClean false
LogVerbose false
PreludeEnable no
PreludeAnalyzerName ClamAV
DatabaseDirectory /var/lib/clamav
OfficialDatabaseOnly false
SelfCheck 3600
Foreground false
Debug false
ScanPE true
MaxEmbeddedPE 10M
ScanOLE2 true
ScanPDF true
ScanHTML true
MaxHTMLNormalize 10M
MaxHTMLNoTags 2M
MaxScriptNormalize 5M
MaxZipTypeRcg 1M
ScanSWF true
ExitOnOOM false
LeaveTemporaryFiles false
AlgorithmicDetection true
ScanELF true
IdleTimeout 30
CrossFilesystems true
PhishingSignatures true
PhishingScanURLs true
PhishingAlwaysBlockSSLMismatch false
PhishingAlwaysBlockCloak false
PartitionIntersection false
DetectPUA false
ScanPartialMessages false
HeuristicScanPrecedence false
StructuredDataDetection false
CommandReadTimeout 30
SendBufTimeout 200
MaxQueue 100
ExtendedDetectionInfo true
OLE2BlockMacros false
AllowAllMatchScan true
ForceToDisk false
DisableCertCheck false
DisableCache false
MaxScanTime 120000
MaxScanSize 100M
MaxFileSize 25M
MaxRecursion 16
MaxFiles 10000
MaxPartitions 50

clamd NOK:

root@nextcloudw86:/run/clamav# systemctl status clamav-daemon.service
● clamav-daemon.service - Clam AntiVirus userspace daemon
     Loaded: loaded (/lib/systemd/system/clamav-daemon.service; enabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/clamav-daemon.service.d
             └─extend.conf
     Active: failed (Result: signal) since Thu 2023-07-06 13:27:55 CEST; 8s ago
       Docs: man:clamd(8)
             man:clamd.conf(5)
             https://docs.clamav.net/
    Process: 4547 ExecStartPre=/bin/mkdir -p /run/clamav (code=exited, status=0/SUCCESS)
    Process: 4552 ExecStartPre=/bin/chown clamav /run/clamav (code=exited, status=0/SUCCESS)
    Process: 4553 ExecStart=/usr/sbin/clamd --foreground=true (code=killed, signal=KILL)
   Main PID: 4553 (code=killed, signal=KILL)

Jul 06 13:27:24 nextcloudw86 systemd[1]: Starting Clam AntiVirus userspace daemon...
Jul 06 13:27:24 nextcloudw86 systemd[1]: Started Clam AntiVirus userspace daemon.
Jul 06 13:27:55 nextcloudw86 systemd[1]: clamav-daemon.service: Main process exited, code=killed, status=9/KILL
Jul 06 13:27:55 nextcloudw86 systemd[1]: clamav-daemon.service: Failed with result 'signal'.