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

From TuxFamilyFAQ
Jump to navigationJump to search
m (Correction des fautes de français.)
(Copy/paste from the English version.)
Line 1: Line 1:
Wordpress permet nativement de déporter le dossier d'upload.  Pour cela, il suffit de remplir les deux champs ''Store uploads in this folder'' et ''Full URL path to files'' dans ''Settings → Media''.
 
  
* '''Store uploads in this folder''': chemin local où WordPress va enregistrer les fichiers.  Il faut qu'il soit accessible par PHP avec des droits appropriés.  En pratique, il faut le définir à quelque chose comme ''/data/repository/<nom du groupe>/.../'' (voir [[WebArea/Fr#Copier des fichiers vers les espaces de téléchargements]]).
+
== Deport uploaded files to TuxFamily's download repositories ==
* '''Full URL path to files''' : base d'URL fournie aux visiteurs.  C'est le début de l'URL que WordPress utilisera pour afficher les médias.  En pratique, il faut qu'elle pointe vers ''<nowiki>http://download.tuxfamily.org/<nom du groupe>/.../</nowiki>'', comme indiqué dans la [[WebArea/Fr#Copier des fichiers vers les espaces de téléchargements|doc générique]]
 
  
 +
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.
  
== Pour WordPress 3.5 ==
+
=== Step #1: enable the required options ===
  
Dans WordPress 3.5 [https://codex.wordpress.org/Settings_Media_Screen#Changelog les options ont disparu de la page].  Cependant, il est possible avec quelques efforts non seulement de s'en passer, mais aussi de les faire réapparaître.
+
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:
  
Pour ce faire, il va falloir modifier la base de données à la main.  Connectez-vous à votre base de données WordPress (par exemple via PhpMyAdmin, cf [[DbMySQL/Fr]]), et sélectionnez la table ''wp_options''.  Trouvez les champs ''upload_path'' (ID 54 sur mon installation fraîche) et ''upload_url_path'' (ID 61 sur mon installation), qui correspondent respectivement au chemin local et à l'URL décrits plus haut.
+
<pre>UPDATE wp_options SET option_value = 'wp-content//uploads' WHERE option_name = 'upload_path' AND (option_value IS NULL OR option_value  = '');</pre>
  
Remplissez ces champs avec les valeurs appropriées comme expliqué plus haut.  Une fois les champs remplis, les deux options apparaissent dans la page de configuration ''Settings → Media'' et fonctionnent comme dans les anciennes versions.
+
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 [https://phpmyadmin.tuxfamily.org/ phpMyAdmin] or the ''mysql'' command-line interface over [[User/En#SSH_access|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.
  
== Sources ==
+
=== Step #2: set the adequate options ===
  
* http://www.ampercent.com/wordpress-store-blog-post-images-different-folder-subdomain/908/
+
Log into your WordPress instance (wp-login.php) and go to Settings > Media (wp-admin/options-media.php). Set the following options:
* http://programepc.net/media-image-in-subdomeniu-store-uploads-in-this-folder-wordpress-3-5/
+
* '''Store uploads in this folder''' (aka upload_path): <code>/data/repository/yourproject/dedicated-folder</code>
 +
* '''Full URL path to files''' (aka upload_url_path): <code><nowiki>https://download.tuxfamily.org/yourproject/dedicated-folder</nowiki></code>
 +
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 <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:
 +
<pre>
 +
cd /home/yourproject/
 +
mv your.web.area-web/htdocs/path/to/your/wordpress/wp-content/uploads yourproject-repository/dedicated-folder
 +
</pre>
 +
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 [https://interconnectit.com/products/search-and-replace-for-wordpress-databases/ 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:
 +
** <code><nowiki>http://your.web.area/your/prefix/wp-content/uploads</nowiki></code>
 +
** <code><nowiki>https://your.web.area/your/prefix/wp-content/uploads</nowiki></code>
 +
** <code><nowiki>http://www.your.web.area/your/prefix/wp-content/uploads</nowiki></code>
 +
** <code><nowiki>https://www.your.web.area/your/prefix/wp-content/uploads</nowiki></code>
 +
: ... with <code><nowiki>https://download.tuxfamily.org/yourproject/dedicated-folder</nowiki></code>
 +
* 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.
 +
<ol>
 +
<li>Connect to TuxFamily over SSH</li>
 +
<li>'''Adjust''' and enter the following commands:</li>
 +
<pre># 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:
 +
prefix=/wordpress
 +
 
 +
# Enter the folder that now hosts your uploaded files:
 +
dest_dir='dedicated-folder'
 +
 
 +
# That's it; you shouldn't need to adjust the next commands.
 +
</pre>
 +
<li>From that, we can automatically deduce a few things, like your project name, your web area and the credentials to connect to the database:
 +
<pre># 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)"</pre>
 +
</li>
 +
<li>We can download and install Search-Replace-DB into a safe location:
 +
<pre>
 +
# 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</pre>
 +
</li>
 +
<li>We will call the tool several times, so it is more convenient to use a wrapper shell function:
 +
<pre>function wp_replace {
 +
  target_url="https://download.tuxfamily.org/${project}/${dest_dir}"
 +
  # 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}" \
 +
                "$@"
 +
}</pre>
 +
</li>
 +
<li>You can now simulate the various replacements:
 +
<pre>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</pre>
 +
</li>
 +
<li>If you are satisfied with the results of the simulations, proceed with the actual replacements:
 +
<pre>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"</pre>
 +
</li>
 +
</ol>

Revision as of 18:56, 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/FTP/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:
    prefix=/wordpress
    
    # Enter the folder that now hosts your uploaded files:
    dest_dir='dedicated-folder'
    
    # 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 {
      target_url="https://download.tuxfamily.org/${project}/${dest_dir}"
      # 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"