Skip to content
Skip to main content
Free .com domain on yearly Starter and up 30-day money-back guarantee Free website migration Live chat with real people, 7 days a week
Hostking
  • Pricing
  • Hosting
    Web hostingNVMe hosting for any website, from $2.50/mo WordPress hostingInstalled, cached and backed up for you Reseller hostingHost your own clients under your brand Free migrationWe move your website from another host
  • Websites
    AI website builderSee your website in about a minute, free Website CareWe build it and look after it, from $19.99/mo Recent designsWebsites we built for customers
  • Domains
    Register a domain.com, .net and more, from $22/year Transfer a domainBring the domain you already have
  • Email
  • Help
Chat with us
USD

Choose your country

South AfricaZARUAEAEDUKGBPNigeriaNGNKenyaKESIndiaINRGlobalUSDCanadaCADEuropeEURMalaysiaUSDPhilippinesPHP
Web hostingWordPress hostingAI website builderWebsite CareDomainsEmailPricingHelpLog in
Server Administration

How To block WordPress Login attacks to 100s of websites on a Shared Server

  • Hostking Team

  • 2015-07-02

custom wordpress login page 1
#image_title
Share on Social Media
xfacebook

The script is complete! Currently compatible with cPanel servers.

It creates a new random alphanumeric username and math equation every time it runs. The user is asked to enter the random username and the sum of the two random numbers when accessing any wp-login.php on the server.

Instructions:
1) Run this command in SSH:

Code:
touch /usr/local/apache/conf/includes/wordpressprotect.conf

2) Open /usr/local/apache/conf/includes/pre_virtualhost_global.conf
Remove any other code which you may have added for wp-login.php HTTP protection and add this line at the beginning of the file:

Code:
Include "/usr/local/apache/conf/includes/wordpressprotect.conf"

Now save /usr/local/apache/conf/includes/pre_virtualhost_global.conf

3) Create a shell script, with any name, for example wp-login.sh and add this code:

Code:
echo "Generating random username (6 alphanumeric characters)..."
username=`cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 6 | head -n 1`

echo "Generating two random numbers for math..."
n=$RANDOM
n1=$((RANDOM%20+10))
n2=$((RANDOM%9+1))
result=$(($n1 + $n2))

echo "Creating and saving configuration file..."

CONFFILE="\n
<LocationMatch \"wp-login.php\">\n
AuthType basic\n
AuthName \"WordPress attack protection. Please verify that you are a legitimate user and not an attack bot which attempts to hack this site. DO NOT ENTER HERE YOUR WP-ADMIN USERNAME AND PASSWORD. Enter username: $username Password: The result of math $n1+$n2\"\n
AuthUserFile /home/wp-admin-attack-htpasswd-file\n
Require valid-user\n
</LocationMatch>\n
ErrorDocument 401 \"Authentication required\"\n
\n"


echo -e $CONFFILE > /usr/local/apache/conf/includes/wordpressprotect.conf
/bin/chmod 0755 /usr/local/apache/conf/includes/wordpressprotect.conf
/bin/rm -f /home/wp-admin-attack-htpasswd-file
/usr/local/apache/bin/htpasswd -bc /home/wp-admin-attack-htpasswd-file $username $result
/bin/chmod 0755 /home/wp-admin-attack-htpasswd-file

echo "Restarting Apache (and nginx if exists)..."
/etc/init.d/httpd restart
echo "All done!"

Save it and make it executable:

Code:
chmod +x wp-login.sh

Now, every time you want to create new HTTP authentication credentials for all wp-login.php files on the server, just run this new script.

Code:
./wp-login.sh

Users (or bots  ) visiting any wp-login.php file will be seeing the message in the attached screenshot.

You can create a cron job which runs this script as often as you want, to set a new random username and math equation.
For example, for every 15 minutes:

Code:
*/15 * * * * /path/to/wplogin.sh

Thanks to NetworkPanda on WebHostingTalk.com
Follow us on Social Media
xfacebookinstagram

Hosting set up for WordPress

NVMe SSD, free SSL, daily backups and a team that fixes WordPress problems with you. Free migration from any host.

See WordPress HostingOr let us build and look after your site ›

Keep Reading

image 18Server AdministrationSecuring a Cpanel Web Serverimage 17Server AdministrationCloudLinux and CageFSimage 15Server AdministrationWhat is AlmaLinux? The Best Enterprise-Grade Linux Distro
bruteforceloginWordPress
Website Care

Need a website for your business?

We build it, a designer finishes it, and we look after it. Hosting, domain and email included.

$19.99/month · cancel any time

See My Website FreeWhat's included ›

Topics

  • Blogging (7)
  • Business Tips (19)
  • Domains & DNS (16)
  • Email (11)
  • Hostking News (4)
  • Online Shops (15)
  • Security (28)
  • SEO & Marketing (15)
  • Web Hosting (25)
  • Website Design (10)
  • WordPress (60)

Latest Guides

  • Isometric illustration of a laptop login screen with a padlock and a database icon representing WordPress password recovery
    Can’t Login to WordPress? Recover Admin Access Without a Plugin2026-10-23
  • Illustration of an email envelope with a padlock representing email login authentication errors
    Fix “Unable to Verify Account Name or Password” Email Errors2026-10-19
  • Isometric illustration of a laptop, notebook and checklist representing writing blog posts with AI assistance
    How to Write Blog Posts Faster Using AI Without Losing Your Voice2026-10-16
  • Person troubleshooting a website issue on a laptop screen
    Why Is My Website Down? How to Check and What to Do2026-10-16
  • Illustration of a blog dashboard or website editor on a computer screen
    Joomla vs WordPress for a Small Business Blog: Which Fits You?2026-10-16

Just need hosting?

  • NVMe SSD and free SSL
  • Daily backups
  • Free migration from any host
  • 30-day money-back guarantee

From $5/month

See Hosting PlansWordPress hosting ›

Stuck on something?

Our support team is on live chat every day, 05:00 to 20:00 UTC. Ask us before you buy, too.

Chat with usBrowse the Help Centre ›
Hostking

Hosting, websites, domains and email for small businesses, since 2013.

4.3/5 on Trustpilot
HostingWeb hostingWordPress hostingReseller hostingcPanel reseller hosting
WebsitesAI website builderWebsite CareRecent designs
Domains & EmailRegister a domainTransfer a domainBusiness email
CompanyAbout usContact usGuidesBlog
Our regions:South AfricaUAEUKNigeriaKenyaIndiaCanadaEuropeMalaysiaPhilippines
© 2013–2026 Hostking. Prices in US dollars.TermsPrivacyAcceptable use
imunify-bot-check
Share on Social Media
xfacebook
Follow us on Social Media
xfacebookinstagram