21 Tips WP Ebook EN
21 Tips WP Ebook EN
21 Tips WP Ebook EN
At SiteGround, we are aware of the many ways your site can fall prey
to attacks and we’re committed to helping site owners protect
themselves. Use this guide to increase your knowledge of WordPress
web security, implement new measures to secure your website, and
spread the word with those around you about the need to protect
their sites.
2
1
PROTECT
YOUR FILES
AND DATABASES
3
1. BEFORE INSTALLING WORDPRESS
When setting up a new WordPress installation, you should always
choose the latest stable version. Before installing it, follow these
two simple web security steps in the wp-config.php file:
By default, all WordPress installations use the prefix wp_ for their database. This
is consistent throughout WordPress, so it’s recommended to change the prefix
for each site to prevent possible attacks related to the database.
To alter the WordPress table prefix, change the following line in the
configuration file, wp-config.php, with the prefix that you would like to use:
$table_prefix = 'wp_';
For example:
$table_prefix = 'newsite_wp_';
This change will also allow you to have several WordPress installations on the
same database, as long as you do not repeat the prefix.
If your site is already installed and you didn’t change the default prefix during
the installation process, it’s not too late. Use a plugin such as Change Table
Prefix to make modifications. You can also do this manually, but I don’t
recommended it if you are not familiar with performing database changes.
WordPress has secret keys, called Keys and Salt, that are stored in the
wp-config.php file. They protect open sessions by encrypting the session data in
the browser’s cookie. Before beginning the installation, you should generate the
secret keys.
4
Like with the database prefix, you can change the secret keys on an existing site,
at any time, a task that I recommend you perform routinely in order to invalidate
active sessions and force all users to log in again.
Although you can generate your own keys manually, I recommend using the official
WordPress service found at https://api.wordpress.org/secret-key/1.1/salt/
and replace the keys with the ones in your wp-config.php file.
Before moving on to the next tip, I have one more piece of advice about WordPress
secret keys for live sites. In the unlikely scenario where you need to deny any type
of access to the admin panel, even with login credentials, you can configure keys to
$table_prefix = 'wp_';
invalidate every microsecond by replacing them in the wp-config.php with the
following:
define('AUTH_KEY', microtime());Lorem
define('AUTH_KEY', microtime());Lorem
define('AUTH_KEY', microtime());Lorem
define('AUTH_KEY', microtime());Lorem
define('AUTH_KEY', microtime());Lorem
define('AUTH_KEY', microtime());Lorem
define('AUTH_KEY', microtime());Lorem
define('AUTH_KEY', microtime());Lorem
5
2. AFTER INSTALLING WORDPRESS
Once you’ve finished installing your new WordPress site, you should
delete the admin profile used during installation and create a new
user with admin permissions, as well as any other necessary user
accounts.
Avoid weak usernames like admin or administrator which are common in all
WordPress installations and remember to use a strong password.
Protect files from attacks and intrusions by adding the following lines of code in
the htaccess file. Ideally, this should be done at the beginning of the file located
at the root directory of your site:
Options - Indexes
<files.htaccess>
Order allow,deny
</files>
<files wp-config.php>
Order allow,deny
</files>
6
You should block access to any unnecessary files by creating a new .htaccess file
in the /wp-admin directory and adding the following lines of code:
<files install.php>
Order allow,deny
</files>
<files setup-config.php>
Order allow,deny
</files>
I recommend reviewing the robots.txt file, which is located in the root directory
of your site. This file tells search bots what should and shouldn’t be analyzed on
your site, so be sure to check that it doesn’t show any sensitive information
about your WordPress installation, for example, your wp-admin folder.
You can change the permissions through an FTP client or through an admin
panel provided by your web host. With SiteGround, it’s easy to change file and
folder permissions in your cPanel.
7
Go to WordPress Tools > WordPress Toolkit > Select the installation >
Fix Permissions.
To restrict the access even further, you should protect these two files in your
WordPress configuration in the following way:
These permissions are referred to as View, Write, and Execute as defined in Unix
operating systems.
<Files *.php>
</Files>
Note: take into account that after every modification in a .htaccess file, you should check
it in your installation. Flush the cache to confirm that the added rules are working
properly.
8
5. DISABLE FILE EDITING IN WORDPRESS
This step focuses on adding a layer of security to the admin panel to
prevent unwanted intruders and limit mistakes made by authorized
users.
To disable the file edit option in the WordPress admin panel, use the following line of
code in the configuration file, wp-config.php:
You can add an additional layer of control for live sites if you don’t want users to install
themes and plugins on their own. To do this, add the following code to the configuration
file - wp-config.php:
Remember to deactivate it by changing the directive to false if you need to perform tasks
on the WordPress installation.
All modifications on the wp-config.php file above the following line of code:
9
It enables an active Firewall that is updated continuously against
malicious behaviour like massive connections, tracking ports, etc.
It prevents brute force attacks by using the distributed server
network of the provider which minimizes the impact and applies
blocking rules to detect these kinds of attacks, usually DoS or
DDoS.
It hides the real IP of your server which prevents direct attacks
against your site by masking the real IP where you are hosted.
You rarely need to restore a full site backup, but in case you do,
SiteGround has a tool for backups and easy restores developed
inhouse, independent of the web service infrastructure. You can
rest easy knowing we have copies of your files in case of any
incident, and you’ll be able to restore your site easily and quickly.
I recommend you follow the 3-2-1 rule as a strategy for backups that contain
important data.
Keep 3 backups
In 2 different formats (minimum)
1 of the backups should be in a different physical location
In case disaster strikes, it’s useless to have all your backups in the same format
or location. Remember to always generate a new backup after you make any
important changes to your WordPress installation.
10
2
SECURE
YOUR LOGIN
AND SESSIONS
11
8. ACTIVATE AND FORCE HTTPS
The HTTPS protocol creates a secure connection between users and
the server, eliminating possible Man-in-the-Middle (MITM) attacks.
HTTPS
These attacks happen when an intermediate service alters or
acquires information exchanged between two ends. That’s why we
use HTTPS encryption for all sensitive information.
To use the HTTPS protocol on your site, install an SSL certificate on your web server and
change the URL in the admin panel.
With SiteGround, all hosting plans include free Let’s Encrypt SSL certificates that can be
installed and configured with an easy tool in the control panel under Security section >
Let’s Encrypt.
There are several WordPress plugins that force an HTTPS connection on all your site
resources, to avoid warnings or errors when serving both HTTP and HTTPS content on
the same page.
Finally, you must force any new session in the admin panel of your site to be under SSL
protocol by adding the following code to the wp-config.php file:
define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);
Note: remember that you must have an active SSL in your installation, for example, the
one provided by Let’s Encrypt.
12
function no_wordpress_login_errors(){
There are several plugins in the WordPress repository that allow you to change the path
and location of your login page, for example, www.mydomain.com/newadminpanel
I recommend the WPS Hide Login plugin. However, other plugins exist and many
security plugins also include this functionality.
Security plugins like Wordfence normally include this feature, as do the following plugins:
13
Limit Login Attempts (miniorange)
Limit Login Attempts Reloaded
Loginizer
They normally include a Firewall WAF (Web Application Firewall), a tool that analyzes and
blocks attacks to the website in real time. At SiteGround, our customers have this service
by default. We analyze the types of connections and block attack attempts in a
completely transparent way for our customers.
14
Two-Factor Authentication to access the WordPress admin panel
Ability to block specific countries
A tool to check folder and file permissions
By adding the following line of code to your .htaccess file, you tell the browser that
frames can be only opened from the same domain or origin:
If your website includes services that can be embedded by third parties, you can specify
which domains are allowed and deny access the rest. For example:
Increase your site’s protection against XSS (cross-site scripting) attacks on older
browsers by adding the following line of code to your .htaccess file:
To reduce the risk of XSS, build on the following tip by using the content-security-policy
header or browser content security policy, which specifies what content from your site
or third parties is allowed to dynamically load.
15
For example, if you want your site to only accept content from the same domain, add
the following line of code to your .htaccess file:
To modify the variables for your specific project, for example, to allow scripts from third
parties like Google Analytics, use this line of code:
www.google-analytics.com;"
This header should be carefully implemented because it’s easy to block resources
without noticing. I recommend performing different tests with this header in a separate
browser tab to check for errors on the terminal.
Note: if you previously included the x-content-security-policy header in your server and
it’s outdated, you’ll need to delete it as it might cause issues if you use both headers at
the same time.
The fourth header you can use to boost your security is the X-content-type-options,
which protects you from unwanted styles and scripts to load when the expected MIME
types do not match what was declared on the page. To add this protection, add this line
to your .htaccess file:
16
You can completely deny access to the xmlrpc.php file by using rules in the .htaccess file
or by deleting it if you are sure that you don’t need it.
To deny access via .htaccess, add the following lines of code to the file:
<Files xmlrpc.php>
Order Deny,Allow
</Files>
You can also use plugins like Disable XML-RPC or iThemes Security, mentioned
in tip #14, to deny access to XMLRPC.
For those who absolutely need this API functionality, the best solution is to
enable it only from the IP where you need access and deny the rest. In this
specific case, add the following lines of code to the .htaccess file, modifying the
IP to the one requiring access:
<Files xmlrpc.php>
</Files>
17
If none of your plugins use the REST API, you can easily deactivate it for your installation.
Simply add the following lines of code to the functions.php file of your active theme or
resources plugin:
add_filter('json_enabled', '__return_false');
If you would rather not tinker with the code, you can use the plugin Disable REST API.
You can also use the iThemes Security plugin, mentioned in tip #12 about Firewall
Plugins, that will keep the REST API active but allow access only to users with exclusive
permissions.
18
3
MAINTAIN
A SECURE
WORDPRESS
INSTALLATION
19
16. CHOOSE REPUTABLE PLUGINS AND
THEMES
Plugins and themes are powerful third-party resources that can help
you increase the functionality of your WordPress site. There are
hundreds of thousands of them available both in the official
WordPress repository and elsewhere online. Since not all of them
are vetted, this represents a serious security problem. Most of us
don’t carry out exhaustive code and functionality reviews before
installing a plugin but a questionable plugin can cause security
breaches and conflicts.
Only download plugins and themes from the WordPress repository and reputable sites.
Before choosing your next plugin or theme, I recommend you:
Always perform a full website backup before installing a new plugin or theme.
You can delete the information from the HTML header and from the static files by
adding the following code to the functions.php file of your theme or in the utilities of
your plugin:
20
/*
*/
global $wp_version;
return $src;
/*
*/
function SG_remove_wp_generator() {
return '';
add_filter('the_generator', 'SG_remove_wp_generator');
21
You can also hide information about the current WordPress version by adding the
following line of code to the .htaccess file inside the WordPress root directory:
#Block WP info
<files readme.html>
Order allow,deny
</Files>
<files license.txt>
Order allow,deny
</files>
Note: although some WordPress security guides recommend deleting these files
altogether, my recommendation is to block their access since a new WordPress update
or re-installation might generate a new one.
In development environments, error reports are useful to validate your work and find
potential mistakes, however, on a live site, you should deactivate these records to hide
information such as paths, names, versions, and more.
To disable error reports in WordPress, simply add the following lines of code to the
wp-config.php file:
22
error_reporting( 0 );
ini_set( 'display_errors', 0 );
ServerSignature Off
There are two ways to hide the information about your site’s PHP version that some
servers send in the HTTP header. First, add the following code to the .htaccess file:
Note: normally you can add this line of code to your active php.ini through the server
admin panel, but this may be different depending on your hosting provider.
23
20. KEEP YOUR WORDPRESS UPDATED
To protect your website against known security vulnerabilities, you
should use the latest version of the WordPress core software, keep
any installed plugins updated, and update your themes.
I personally prefer to maintain my site manually, although it requires more attention and
time because it allows me to be aware of the features included in every update and the
reason for them. In terms of the update order, I always recommend updating the
WordPress core software first and then, without any specific order, updating plugins and
themes.
If, however, you want the WordPress core to be updated automatically, simply add the
following line of code to your wp-config.php file:
Note: the automatic update won’t run if you have disabled the WordPress cron.
You’ll receive an email to the address used by the platform admin account after every
update.
Updating WordPress core is only one part of the equation. Based on a report from
wpscan.org, 52% of vulnerabilities found in WordPress installations are due to plugins,
11% due to themes, and 37% due to the WordPress core software.
If you want to update plugins automatically, add the following line of code to the
functions.php file of your active theme or in your functionality plugin:
Before adding this code, delete all the unused plugins on your site. Simply deactivated
them is not enough to remove potential vulnerabilities. Delete them!
24
Adding the following line of code will automatically update themes:
Lastly, remember that keeping your WordPress site secure is great, but the computer
you use should also be protected from malicious software and viruses. Make sure you
use a reputable antivirus and your operating system is up to date.
Choosing the correct hosting provider will influence the success and security of your
WordPress project in a big way.
25
CONCLUSION
They say common sense isn’t so common, but in terms of
security, it’s your best ally. Use strong passwords, delete inactive
RESUME
users, assign the right roles to each user, do not save active
sessions on public computers, keep the server time updated, only
allow secure access, and actively monitor your website.
26
4
ABOUT
THE AUTHOR
27
FERNANDO PUENTE
fpuenteonline