Disable PHP execution in the WordPress uploads folder
wp-content/uploads is the one folder that the web server must be able to write to, which makes it the favourite place for a web shell: an attacker who gets a .php file in there through a vulnerable plugin can run it by opening its URL. Uploads never need to execute code, so the safest fix is to forbid PHP there at the server level.
Problem: A PHP file that ends up in the uploads folder can be executed from the browser, which turns any file upload bug into full control of the site.
Solution: On Apache, put an .htaccess file into wp-content/uploads/ that denies access to PHP files; on Nginx, add a location that returns 403 for PHP under /wp-content/uploads/ before the PHP handler. Then test with a harmless file.
Correct permissions for the rest of the installation are described in WordPress file and folder permissions.
Apache 2.4
# wp-content/uploads/.htaccess
<FilesMatch "\.(?i:php\d?|phtml|phar|pht)$">
Require all denied
</FilesMatch>
The pattern covers the extensions that Apache may hand to PHP depending on the configuration (.php, .php7, .phtml, .phar), in any letter case. This works when the server allows .htaccess overrides (AllowOverride All or at least AuthConfig/Limit), which is the case on most shared hosts. With access to the virtual host, the same block inside <Directory /var/www/example/wp-content/uploads> is better: it cannot be overwritten by an uploaded .htaccess.
Nginx
server {
# ...
location ~* ^/wp-content/uploads/.*\.(php\d?|phtml|phar|pht)$ {
deny all;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Nginx uses the first regular expression location that matches, in the order of the file, so the deny block must come before the \.php$ block. Reload with sudo nginx -t && sudo systemctl reload nginx.
Test it
echo '<?php echo "executed";' > wp-content/uploads/ha-test.php
curl -s -o /dev/null -w "%{http_code}\n" https://example.com/wp-content/uploads/ha-test.php
# 403 = blocked; 200 with "executed" = still runs
rm wp-content/uploads/ha-test.php
Look for files that are already there
find wp-content/uploads -type f \( -iname "*.php*" -o -iname "*.phtml" -o -iname "*.phar" \)
On a clean site this finds nothing, or only the index.php files some plugins place in their folders to prevent directory listing. Anything else is suspicious: compare it with a backup before deleting.
NOTE: Some plugins keep PHP files under uploads and call them directly (old cache and image tools did this). If a feature breaks after the change, check the error log for 403 responses and move that plugin’s folder out of the rule rather than removing the protection. Other protections against direct access: wp-config.php and xmlrpc.php via .htaccess.