Static WordPress with WP2static

Last Revised: October 2, 2021

If we have simple sites, simply with information, but in which users do not interact with anything, we could consider creating static sites in which everything is HTML, CSS, JavaScript and content such as images or documents, but without PHP intervening.

We can do this with an open source tool like WP2static that searches for all the URLs and contents of the site, and generates a static version of it in a few steps.

This tool is not created as a plugin directly, so we will have to create some previous steps, in addition to configuring two sites, the one that has the WordPress (which may be non-public) and the one that has the final static contents, which users will access.

This tutorial has been created on a Clouding.io VPS. You can create your own VPS from 3€/month.

In addition, you have the possibility to create your VPS with the WordPress image in one click.

PARTNERSHIP

Requirements

  • SSH access to your server account
  • WP-CLI (to make it work optimally)

About WP2static

Keep in mind that this plugin only has the basic code, but it is not “created as a plugin”, so we will have to generate it as new versions appear.

This will require several elements:

  • Download and update the plugin with Git
  • Update your components using Composer
  • Create the Plugin
  • Upload the plugin to a “non-public” site
  • Generate the static site publicly

The original WordPress should not be public (it may be, but it can be a protected site, but not for users to enter). You can put it in a subdomain, for example working.example.com even if we then have the site in www.example.com.

To begin with, we will need to have a “non-public” folder where we can work and create the plugin. If your hosting gives you folders of style a /home/example/public_html/, we can use something like /home/example/wp2static/. It is possible that your system allows the creation of subfolders as if they were subdomains.

First installation

The first thing we are going to do and get is to create the plugin that we will have to install in our WordPress. The initial developer has created the core of the plugin, but it requires certain dependencies, so we need to “create our plugin” before we can install it.

We will go to the base folder where we are going to place the Git.

cd /home/example/

We will clone for the first time the Git repository where the WP2Static is.

git clone https://github.com/leonstafford/wp2static.git

We will enter the newly created folder where the files will be.

cd wp2static/

By default, WP2Static, comes to work in development mode, but this version may contain bugs or problems. So we will switch to the stable main folder.

git checkout master

Now that we have the main code, we will download all the updates of the dependencies that are necessary for the plugin to work, using Composer.

composer install

At this moment we already have the software created and ready to create our plugin.

Upgrade and maintenance

If we have already created the plugin previously, or we want to validate that it is 100% updated to create a next version, we can perform some previous maintenance tasks before generating the plugin.

We will access the folder previously created by the Git.

cd /home/example/wp2static/

We will update the Git information.

git pull

We make sure we are in the master branch.

git checkout master

We perform an update of the components.

composer update

At this moment we already have the software updated and ready to create our plugin.

Creating the Plugin

Now that we have the software updated, complete, and with all its components, we can easily create the plugin.

This command will create a ZIP file that will be compatible with WordPress plugins, and that we can upload directly to the administration panel through the Add New (plugin).

composer build wp2static

This creates a ZIP file that is located in the Downloads.

cd ../Downloads/
ls -la

There we will have the file wp2static.zip, which we can download and use as a Plugin for our WordPress.

Installing/updating the plugin

We will go to our WordPress website, accessing the Administration working.example.com/wp-admin/ Panel and there we will access the Plugins area, where we will click on “Add new”.

We will upload the file and the installation or update process will begin. Once finished, we will validate that the plugin is active.

Once activated we will have the WP2Static option in the options menu.

In the Options section we will update the information of the URL that we are going to use https://www.example.com without the final bar, and we will configure an email account if we want to receive notifications every time an update of the site is published.

Once we have the configuration, we will confirm that in the Diagnostics area we have everything up to date and that everything has a green light.

If everything is correctly configured, we will move on to the Jobs section, where in principle everything will be activated. The part of WP-Cron will be left by default to “disable” since we will take advantage of WP-CLI for the execution of the tasks.

Running the processes

Before activating the entire default system, we will do a first test to check that everything works correctly. remember that for it to work, you need to have WP-CLI installed and running.

We will have as a basis that our WordPress site to run the plugin is in the domain working.example.com and that it is in the path /home/example/working/.

cd /home/example/working/

Once there, we will execute the 3 commands that generate different situations. We will start with the detection of the entire system and its validation.

wp wp2static detect

This will return a series of messages per screen

[2020-12-11T19:24:05+00:00] Starting to detect WordPress site URLs.
[2020-12-11T19:24:08+00:00] Detection complete. 623 URLs added to Crawl Queue.

The next step is to have all the URLs detected in the previous step reviewed and downloaded.

wp wp2static crawl

This will return a series of messages per screen

[2020-12-11T19:24:27+00:00] Starting crawling
[2020-12-11T19:24:27+00:00] Starting to crawl detected URLs.
[2020-12-11T19:24:27+00:00] Using CrawlCache.
[2020-12-11T19:24:28+00:00] 404 for URL /blog/page/1/
[2020-12-11T19:24:30+00:00] Crawling complete. 1 crawled, 622 skipped (cached).
[2020-12-11T19:24:30+00:00] Crawling completed

Finally, with this content, we will create the entire site ready to navigate, changing the URLs to the final addresses.

wp wp2static post_process

This will return a series of messages per screen

[2020-12-11T19:24:46+00:00] Processing crawled site.
[2020-12-11T19:24:48+00:00] Finished processing crawled site.

We can check that the entire site is available in the path it generates for storage.

cd wp-content/uploads/wp2static-processed-site/

Automating generation

If this whole manual creation process has worked, we can automate it by creating a cron that does all the steps for us.

*/5 * * * * WP_CLI_PHP=/usr/local/bin/php; SHELL=/bin/bash; /usr/local/bin/wp wp2static detect --path=/home/example/working/ && /usr/local/bin/wp wp2static crawl --path=/home/example/working/ && /usr/local/bin/wp wp2static --- --path=/home/example/working/ >/dev/null 2>&1 

This system will regenerate the website every 5 minutes.

Move to production

Now that we have the site created we have several options when it comes to putting it into production, and it will depend on the resources we have, if the site will be on the same machine …

Copying the contents

If the public website is on the same machine where our working WordPress is, we can do something as simple as copying the contents between different folders.

cp -rf /home/example/wp2static/wp-content/uploads/wp2static-processed-site/* /home/example/public_html/

In any case, we must automate this copy by adding it to the Cron that we have previously created, so that when all the creation of files is finished, they are automatically copied.

Another option, if we want to reserve resources and not have two copies of the site, can be to move the contents.

mv -f /home/example/wp2static/wp-content/uploads/wp2static-processed-site/* /home/example/public_html/

We must remember that at the end we must review the permissions of files and folders, both of the owners and the availability that they are legible.

Other options that we can use are the use of an SCP or an rsync to copy the files to other servers.


About this document

This document is regulated by the EUPL v1.2 license, published in WP SysAdmin and created by Javier Casares. Please, if you use this content in your website, your presentation or any material you distribute, remember to mention this site or its author, and having to put the material you create under EUPL license.