Difference between revisions of "WebArea/Compat/Wordpress/En"

From TuxFamilyFAQ
Jump to navigationJump to search
(→‎Step #1: enable the required options: Add link to SSH section.)
Line 27: Line 27:
This 3rd step simply consists in moving your uploaded files from <code>wp-content/uploads</code> to <code>/data/repository/yourproject/dedicated-folder</code>.
This 3rd step simply consists in moving your uploaded files from <code>wp-content/uploads</code> to <code>/data/repository/yourproject/dedicated-folder</code>.
Although this can be done using a FTP/FTP/SFTP client, we recommend that your leverage TuxFamily's SSH access and the power of the command-line like this:
Although this can be done using a FTP/FTPS/SFTP client, we recommend that your leverage TuxFamily's SSH access and the power of the command-line like this:
cd /home/yourproject/
cd /home/yourproject/

Revision as of 19:26, 28 April 2018

Deport uploaded files to TuxFamily's download repositories

WordPress natively allows deporting uploaded files to an arbitrary folder associated with an equally arbitrary base URL. This perfectly suits the model chosen by TuxFamily to implement their download repositories. This section describes how to migrate the files you uploaded from their default location (namely wp-content/uploads) to TuxFamily's download repositories, thus sparing you small-ish project quota (typically 200M). Please note this approach may not be suitable if you somehow ended storing private files under wp-content/uploads (e.g. backups, database dumps, files which should be reachable only by authenticated users, etc.) as all files stored on TuxFamily's download repositories are considered 100% public.

Step #1: enable the required options

Since WordPress 3.5, the options required to configure your WordPress instance are hidden by default (presumably for the sake of simplicity and user-friendliness). To enable these options, run the following SQL query in your database:

UPDATE wp_options SET option_value = 'wp-content//uploads' WHERE option_name = 'upload_path' AND (option_value IS NULL OR option_value  = '');

You should not need to adjust this query unless you picked a custom prefix when installing your WordPress instance; if so, replace "wp_options" with "your_custom_prefix_options".

Depending on your skills, you can run this query using either phpMyAdmin or the mysql command-line interface over SSH; in both cases, use the settings and credentials stored in your wp-config.php -- the credentials you use for the panel, the forum or this FAQ will not work.

Step #2: set the adequate options

Log into your WordPress instance (wp-login.php) and go to Settings > Media (wp-admin/options-media.php). Set the following options:

  • Store uploads in this folder (aka upload_path): /data/repository/yourproject/dedicated-folder
  • Full URL path to files (aka upload_url_path): https://download.tuxfamily.org/yourproject/dedicated-folder

In both cases, replace yourproject and dedicated-folder with more appropriate values. Click "Save changes".

Step #3: move your files

This 3rd step simply consists in moving your uploaded files from wp-content/uploads to /data/repository/yourproject/dedicated-folder. Although this can be done using a FTP/FTPS/SFTP client, we recommend that your leverage TuxFamily's SSH access and the power of the command-line like this:

cd /home/yourproject/
mv your.web.area-web/htdocs/path/to/your/wordpress/wp-content/uploads yourproject-repository/dedicated-folder

If your WordPress is brand new, you are done. Otherwise, you probably want to ensure your database does not contain any obsolete URL, which is the point of step #4.

Step #4: replace old URLs throughout the database

As a relatively complex and very modular CMS, WordPress tends to store serialized data in its database; serialized data can be challenging for search and replace operations; this is why it is advised to:

  • backup your MySQL database (via phpMyAdmin, the mysql CLI or just the daily dump provided by TuxFamily);
  • install Search Replace DB; the pesky download form can be avoided by picking the latest release from https://github.com/interconnectit/Search-Replace-DB/releases;
  • leverage this tool to replace old URLs; typically, one should replace:
    • http://your.web.area/your/prefix/wp-content/uploads
    • https://your.web.area/your/prefix/wp-content/uploads
    • http://www.your.web.area/your/prefix/wp-content/uploads
    • https://www.your.web.area/your/prefix/wp-content/uploads
... with https://download.tuxfamily.org/yourproject/dedicated-folder
  • uninstall it once the operation has finished as leaving it within your web area is close to giving the keys to your entire website to attackers.

Safer approach: over SSH

For those of you who have enabled their TuxFamily SSH access and are not afraid to use it, the following commands should help you proceed with the replacements. Note: these are bash commands; zsh/tcsh aficionados will probably have to adjust them.

  1. Connect to TuxFamily over SSH
  2. Adjust and enter the following commands:
  3. # Go to your web area (this is important):
    cd yourproject/your.web.area-web
    # Enter the base path to your WordPress; it can be as
    # simple as "/" if your entire site is handled by WordPress:
    # Enter the folder that now hosts your uploaded files:
    # That's it; you shouldn't need to adjust the next commands.
  4. From that, we can automatically deduce a few things, like your project name, your web area and the credentials to connect to the database:
    # Project name:
    project=$(pwd | cut -d/ -f3)
    # Web area:
    webarea="$(basename "$(pwd)" | cut -d- -f1)"
    # Database name:
    wp_db="$((echo '<?php'; find htdocs/ -type f -name wp-config.php -print0 | xargs -0 -r grep DB_NAME; echo 'print DB_NAME;') | php)"
    # Putting your database password into a bash variable is better than using it directly as it will not end up in your shell history:
    wp_pw="$((echo '<?php'; find htdocs/ -type f -name wp-config.php -print0 | xargs -0 -r grep DB_PASSWORD; echo 'print DB_PASSWORD;') | php)"
  5. We can download and install Search-Replace-DB into a safe location:
    # The "php-include" directory is not exposed by the web server, making it a safer place than "htdocs":
    cd php-include
    wget https://github.com/interconnectit/Search-Replace-DB/archive/3.1.tar.gz
    [ "$(sha256sum < 3.1.tar.gz)" != '837389799a2884e3f77a3ab84c1a4161b6e2220041f91c9f149529111babe676  -' ] && echo "Something's phishy here..." && exit
    tar xzvf 3.1.tar.gz
    cd Search-Replace-DB-3.1
  6. We will call the tool several times, so it is more convenient to use a wrapper shell function:
    function wp_replace {
      # Yes, we have to pass the password on the command-line;
      # be reassured, though, the SSH access does not allow other
      # users to list your processes.
      ./srdb.cli.php --host 'sql' \
                     --user "${wp_db}" \
                     --pass "${wp_pw}" \
                     --name "${wp_db}" \
                     --replace "${target_url}" \
  7. You can now simulate the various replacements:
    wp_replace --search "http://${webarea}${prefix}/wp-content/uploads" --dry-run
    wp_replace --search "https://${webarea}${prefix}/wp-content/uploads" --dry-run
    wp_replace --search "http://www.${webarea}${prefix}/wp-content/uploads" --dry-run
    wp_replace --search "https://www.${webarea}${prefix}/wp-content/uploads" --dry-run
  8. If you are satisfied with the results of the simulations, proceed with the actual replacements:
    wp_replace --search "http://${webarea}${prefix}/wp-content/uploads"
    wp_replace --search "https://${webarea}${prefix}/wp-content/uploads"
    wp_replace --search "http://www.${webarea}${prefix}/wp-content/uploads"
    wp_replace --search "https://www.${webarea}${prefix}/wp-content/uploads"