An arbitrary-file-upload bug in a WordPress plugin is only a vulnerability report. It becomes remote code execution the moment your web server is willing to hand a file under wp-content/uploads/ to a PHP interpreter. Block that one thing and a large class of plugin bugs degrades from “site compromised” to “junk file on disk”. The rule itself is small: on Apache and LiteSpeed Enterprise it is a FilesMatch block that denies .php, .phtml, .phar and the numbered .php7-style variants; on nginx it is a location that has to be written so the generic \.php$ block never sees the request. What matters more than the rule is putting it where the server actually reads it — an .htaccess dropped into the uploads folder does nothing on nginx, usually nothing on OpenLiteSpeed, and nothing on Apache when AllowOverride is restricted.
What you’ll accomplish
- Confirm, with a real request rather than a config read, whether your uploads directory currently executes PHP.
- Install a deny rule in the one config file your server actually parses, chosen from Apache, LiteSpeed Enterprise, OpenLiteSpeed and nginx.
- On cPanel, place the rule in an include directory so EasyApache runs and account restores do not silently erase it.
- Find the plugins that legitimately write PHP into uploads before the rule breaks them, not after.
- Prove the block works end to end: upload a benign PHP file, request it, get a 403 and no source disclosure.
Prerequisites
- Root or sudo on the server, or a hosting control panel that exposes a per-site include file. Shared hosting without config access limits you to
.htaccess, which only helps on Apache and LiteSpeed Enterprise. - Any WordPress version — the rule lives below WordPress entirely — but a standard layout, with
wp-content/uploads/under the document root. - Shell access to the account so you can write a test file and read logs. A file manager works, but you lose the log analysis.
- Know which server binary answers requests. A proxy in front (Cloudflare, a load balancer, Varnish) does not change that; what matters is the origin process that maps a URL to a PHP handler.
- On cPanel, version 110 or newer is assumed for the
2_4userdata paths. On older EasyApache 3 systems the include layout differs and these paths will not exist.
How it works
A PHP file is not executable because of a filesystem permission bit. It is executable because the web server has a mapping that says “requests for this extension go to an interpreter.” Apache does that with AddHandler, AddType or SetHandler; LiteSpeed does it with an LSAPI script handler; nginx does it with a location that proxies to a PHP-FPM socket. Remove or override that mapping for one directory tree and the same file is served as an inert blob, or refused outright.
Two details in that chain are where most hardening attempts go wrong.
Matching only the final extension is not enough. The classic Apache misconfiguration is AddHandler application/x-httpd-php .php. AddHandler associates a handler with any dot-separated component of the filename, not just the last one, so evil.php.jpg is handed to PHP on a server configured that way. SetHandler inside a FilesMatch "\.php$" block does not have this behaviour, which is why modern EasyApache profiles use it — but plenty of inherited stacks, and plenty of copied-from-a-forum vhost snippets, still use AddHandler. A deny rule anchored with \.php$ will therefore protect a correctly configured server and miss a badly configured one. You want both an anchored match and a middle-component match. The same reasoning covers .phtml, which is a conventional PHP extension that a \.php$ pattern misses entirely.
Changing permissions does not change the mapping. Removing the execute bit with chmod 644 has no effect: the interpreter is not exec’ing the file, the web server is reading its bytes and feeding them to a parser. Mounting the uploads directory noexec has no effect for the same reason. Removing the read bit would work and would also stop your images from loading. Permissions are a useful control for who can write into uploads; they are not a control for whether uploads runs code.
The third detail is where the rule is read from. .htaccess is a per-directory override file that only Apache and LiteSpeed Enterprise consult, and Apache only consults it for directives the surrounding AllowOverride permits. Require all denied inside FilesMatch needs at least AllowOverride Limit; php_flag engine off needs AllowOverride Options. If the parent Directory block says AllowOverride None, nobody reads your file. nginx has no equivalent mechanism at all and never reads the file. OpenLiteSpeed can load rewrite rules from .htaccess when explicitly configured to, but does not honour Apache access-control containers like FilesMatch, so the block silently does nothing there too.
Step-by-step
1. Identify which server answers the request. Do this on the box, not from the Server response header, which a CDN or proxy will rewrite.
ps -eo comm= | grep -Ei 'httpd|apache2|nginx|litespeed|lshttpd|php-fpm|lsphp' | sort -u
Expect something like httpd plus php-fpm, or lshttpd plus lsphp, or nginx plus php-fpm. If you see lshttpd, find out which LiteSpeed you have, because the branches diverge:
/usr/local/lsws/bin/lshttpd -v
Output contains either LiteSpeed/6.x Enterprise or openlitespeed/1.7.x. Enterprise reads .htaccess; OpenLiteSpeed does not, for this purpose.
2. Establish the current behaviour with a real request. Write a harmless marker file, request it, then remove it.
DOCROOT=/home/<account>/public_html
printf '<?php echo "sh-uploads-exec-marker"; ' > "$DOCROOT/wp-content/uploads/sh-exec-test.php"
curl -sS -o /tmp/sh-exec-test.out -w 'status=%{http_code}\n' \
https://example.com/wp-content/uploads/sh-exec-test.php
cat /tmp/sh-exec-test.out; echo
Before hardening you will see status=200 and the body sh-uploads-exec-marker. That is the vulnerability, demonstrated. Leave the file in place for now — you will re-run this after the rule lands.
3. Find the legitimate PHP-in-uploads users before you break them. Some cache plugins and a handful of gallery and form builders generate PHP under the uploads tree, usually to sidestep a permissions problem in wp-content/cache/. Two checks catch almost all of them. First, look for non-trivial PHP files that already exist:
find "$DOCROOT/wp-content/uploads" -type f \
\( -iname '*.php' -o -iname '*.phtml' -o -iname '*.phar' -o -iname '*.php[0-9]' \) \
-size +32c -printf '%TY-%Tm-%Td %s %p\n' | sort
WordPress core drops empty or near-empty index.php stubs in upload subdirectories; the -size +32c filter skips those. Anything else that appears is either a plugin artefact or a webshell, and you need to know which before you continue.
Second, look at whether anything has actually been requesting those files successfully:
grep -Eo 'GET /wp-content/uploads/[^ "?]*\.ph(p[0-9]?|tml|ar)' \
/home/<account>/access-logs/example.com | sort | uniq -c | sort -rn | head -20
An empty result means nothing on the live site depends on executing PHP from uploads, so deploy immediately. Hits on a consistent path such as uploads/cache/… point at a plugin that will break; reconfigure its cache directory or accept the breakage before you ship. Scattered random filenames are scanners probing for known webshells.
4. Apache and LiteSpeed Enterprise: write the deny rule. This is the content of the rule regardless of where you place it. In an .htaccess inside wp-content/uploads/:
# Deny any request whose path ends in a PHP-family extension
<FilesMatch "\.(php|phtml|phar|php[0-9])$">
Require all denied
</FilesMatch>
# Deny double extensions such as evil.php.jpg, which AddHandler-based
# configurations will still route to the interpreter
<FilesMatch "\.(php|phtml|phar|php[0-9])\.">
Require all denied
</FilesMatch>
# Only meaningful under mod_php; guarded so PHP-FPM and LSAPI
# servers do not fail with "Invalid command 'php_flag'"
<IfModule mod_php.c>
php_flag engine off
</IfModule>
The IfModule guard is not optional. php_flag is a mod_php directive; on a PHP-FPM or suPHP stack an unguarded php_flag in .htaccess produces a 500 on every request to that directory.
5. cPanel: put the rule somewhere a rebuild will not eat it. Editing /etc/apache2/conf/httpd.conf is pointless — cPanel regenerates it. Editing the uploads .htaccess survives rebuilds, but not a plugin that regenerates it and not a restore from an older backup. The durable location is the userdata include tree, which cPanel merges into the generated vhost:
install -d /etc/apache2/conf.d/userdata/std/2_4/<user>/<domain>
install -d /etc/apache2/conf.d/userdata/ssl/2_4/<user>/<domain>
cat > /etc/apache2/conf.d/userdata/std/2_4/<user>/<domain>/uploads-noexec.conf <<'EOF'
<Directory "/home/<user>/public_html/wp-content/uploads">
<FilesMatch "\.(php|phtml|phar|php[0-9])$">
Require all denied
</FilesMatch>
<FilesMatch "\.(php|phtml|phar|php[0-9])\.">
Require all denied
</FilesMatch>
</Directory>
EOF
cp /etc/apache2/conf.d/userdata/std/2_4/<user>/<domain>/uploads-noexec.conf \
/etc/apache2/conf.d/userdata/ssl/2_4/<user>/<domain>/uploads-noexec.conf
/scripts/ensure_vhost_includes --user=<user>
ensure_vhost_includes prints the vhosts it updated and restarts Apache. Copy the file into the ssl tree as well — cPanel keeps separate include directories for the plain and TLS vhosts, and a rule present only in std leaves the HTTPS vhost, which is the one everybody actually reaches, unprotected. On a server where you want this for every account, /etc/apache2/conf.d/includes/post_virtualhost_global.conf with a DirectoryMatch covering ^/home/[^/]+/public_html/.*/wp-content/uploads applies globally; follow it with /scripts/rebuildhttpdconf and /scripts/restartsrv_httpd. The include layout is documented at docs.cpanel.net.
Confirm the include actually landed in the generated config:
grep -n 'uploads-noexec' /etc/apache2/conf/httpd.conf
apachectl configtest
The first command should print two matching Include lines, one for the plain vhost and one for the SSL vhost. The second must print Syntax OK before you restart anything.
6. nginx: write the location block and respect the ordering rule. nginx location matching is not top-to-bottom for everything. Exact = matches win first, then the longest matching ^~ prefix — and a ^~ match stops regular-expression locations from being evaluated at all. Regular-expression locations are only tried if no ^~ prefix matched, and among them the first one in file order wins. That last point is the trap: if your vhost already contains location ~ \.php$ { fastcgi_pass … } above your uploads rule, the uploads rule is never reached and the file executes despite being “blocked”. The order-independent way to write it is a nested location inside a ^~ prefix:
# In the server block of /etc/nginx/conf.d/example.com.conf
location ^~ /wp-content/uploads/ {
try_files $uri =404;
location ~* \.(php|phtml|phar|php[0-9])$ {
deny all;
}
location ~* \.(php|phtml|phar|php[0-9])\. {
deny all;
}
}
The outer ^~ /wp-content/uploads/ wins over any regex location for that path prefix, so nothing in the rest of the file can route an upload to FPM. try_files $uri =404; keeps normal media serving working inside that block. If you would rather keep a flat regex location, it works too — but it must appear before the \.php$ FastCGI location in the same file:
location ~* ^/wp-content/uploads/.*\.(php|phtml|phar|php[0-9])(\.|$) {
deny all;
}
Then test and reload:
nginx -t && systemctl reload nginx
Expect nginx: configuration file /etc/nginx/nginx.conf test is successful. Confirm the rule is in the running configuration and in the right order:
nginx -T | grep -n -E 'location .*(uploads|\\.php)'
The uploads line number must be lower than the generic PHP line number, or you used the nested form and the ordering does not matter.
7. Add the companion hardening, and be clear about what it does not do. disable_functions in php.ini narrows what a webshell can accomplish if one ever does run, on any path, not only through uploads:
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,proc_nice,pcntl_exec
WordPress core does not call any of those. Some backup, image-optimisation and server-status plugins do, so roll this out on a staging copy first. It is a damage limiter, not a substitute for the execution block. And to restate: chmod -R 644, an SELinux relabel, or a noexec mount will not stop the PHP handler. Only removing the mapping or denying the request will.
Validate it works
The proof is a request, not a config file. The test file from step 2 should still be in place:
curl -sS -o /tmp/sh-exec-test.out -w 'status=%{http_code}\n' \
https://example.com/wp-content/uploads/sh-exec-test.php
grep -c 'sh-uploads-exec-marker' /tmp/sh-exec-test.out
You want status=403 and a grep count of 0. A count of 1 means the file still executed. A status=200 whose body contains the literal text <?php is a different and also bad outcome: the handler was unmapped but nothing denies the request, so you have turned code execution into source disclosure. Add the deny or Require all denied directive rather than only removing the handler.
Repeat with the double-extension case, and over HTTPS specifically:
cp "$DOCROOT/wp-content/uploads/sh-exec-test.php" \
"$DOCROOT/wp-content/uploads/sh-exec-test.php.jpg"
curl -sS -o /dev/null -w 'jpg=%{http_code}\n' \
https://example.com/wp-content/uploads/sh-exec-test.php.jpg
curl -sS -o /dev/null -w 'phtml=%{http_code}\n' \
https://example.com/wp-content/uploads/sh-exec-test.phtml
Both should report 403. Then confirm you have not broken ordinary media by fetching a real image and a real PDF from the same tree and checking for 200. Finally, clean up:
rm -f "$DOCROOT"/wp-content/uploads/sh-exec-test.php*
Log in to WordPress and upload an image through the media library, then view a page that uses it. If that renders, the block is scoped correctly.
Troubleshooting
Every page under uploads returns 500 after adding .htaccess. Almost always an unguarded php_flag or php_value on a PHP-FPM, suPHP or LSAPI stack. Check the error log for Invalid command 'php_flag'. Wrap it in IfModule mod_php.c as shown, or drop it — the FilesMatch deny is doing the real work.
The rule is in the file and the test file still executes. Work through the three usual causes in order. On Apache, check that overrides are permitted for that path — grep -R 'AllowOverride' /etc/apache2/conf/httpd.conf and look at the Directory block covering the docroot; AllowOverride None makes the file inert. On nginx, you almost certainly hit the ordering rule from step 6: a location ~ \.php$ earlier in the file matched first. On LiteSpeed, verify with /usr/local/lsws/bin/lshttpd -v that you are on Enterprise rather than OpenLiteSpeed, and remember LiteSpeed caches configuration — /usr/local/lsws/bin/lswsctrl restart is required, a graceful reload is not always enough. Handler selection on LiteSpeed is its own subject, covered in switching the LSPHP handler.
Legitimate media now returns 403. Your pattern is too broad. A FilesMatch "php" without the escaped dot and anchor matches photo.jpg; \.(php|phtml|phar|php[0-9])$ does not. Also check you did not put Require all denied outside the FilesMatch container, which denies the entire directory.
The rule disappeared after an EasyApache run or an account restore. That is the expected outcome for an edited stock config or a docroot .htaccess. Userdata includes survive EasyApache; they do not survive a restore of an account backup created before the file existed, because the restore rewrites the account’s files but the include lives outside the account. Re-run /scripts/ensure_vhost_includes --user=<user> after any restore, and add a check for the include file to whatever configuration drift monitoring you run.
A site was already compromised before the block went in. The block stops new execution; it does not remove what is already there or clean up what a shell already did. Quarantine rather than delete, because the files are evidence, and treat the whole account as suspect until you have checked the database and the theme functions file. The pattern for that cleanup is in the Japanese keyword hack walkthrough.
When this is the wrong approach
If a plugin your business genuinely depends on writes and executes PHP inside uploads, this rule breaks it, and the honest fix is to move that plugin’s generated code out of the uploads tree rather than punching a hole in the block. A per-file exception inside the denied directory is a standing invitation for an attacker to guess the exempt filename.
The block is also not a substitute for keeping plugins current. It converts one vulnerability class into a non-event; it does nothing about SQL injection, stored cross-site scripting, privilege escalation through a broken capability check, or code executed from the database rather than from disk. Pair it with a standing inventory of what you actually run, and with the upload validation filters documented in the WordPress developer handbook.
Finally, on managed platforms where you cannot reach the web server configuration and .htaccess is ignored, there is no way to implement this correctly. A security plugin claiming to block uploads execution there is either writing an .htaccess nothing reads, or filtering inside PHP — which runs only after WordPress boots, and never for a request that goes straight to the uploaded file. Treat that as a reason to move the site, not as a solved problem.
Running WordPress that earns real money deserves an operator — Stack Harbor runs that stack under Enterprise WordPress; book an infrastructure consult.