Configuring WordPress services revision 2

Introduction

This page describes how Alexis Huxley installed and configured WordPress.

Procedure

  1. Clean up any crud:
    apt-get --purge autoremove wordpress mysql-server apache2
    cd /etc && rm -fr php/7.0 mysql apache2 wordpress
    cd /var/lib && rm -fr wordpress* mysql*
    cd /etc/www/html && rm -fr wordpress*
  2. Install WordPress and its prerequisites with:
    apt-get install wordpress mysql-server apache2
  3. Create a database:
    mysql -u root 
    create database wordpress;
    create user wordpress@localhost identified by '<set-password-here>';
    grant all privileges on wordpress.* TO wordpress@localhost;
    flush privileges;
    exit
  4. Note that it is important that WordPress content appears to be somewhere under /var/www since /etc/apache2/apach2.conf only allows access to trees /usr/share and /var/www; although symlinking seems to be adequate. This is taken into consideration in the next two steps.
  5. Edit /etc/apache2/sites-available/wordpress.conf and add content:
    Alias /wordpress/wp-content /var/www/html/wordpress/wp-content
    <Directory /var/www/html/wordpress/wp-content>
        Options FollowSymLinks
        Order allow,deny
        Allow from all
    </Directory>
    Alias /wordpress /usr/share/wordpress
    <Directory /usr/share/wordpress>
        Options FollowSymLinks
        AllowOverride Limit Options FileInfo
        DirectoryIndex index.php
        Order allow,deny
        Allow from all
    </Directory>
    
  6. Run:
    ln -s ../../lib/wordpress /var/www/html/wordpress
  7. Run:
    a2ensite wordpress
    service apache2 reload
  8. At this point it is essential that the URL you enter into your browser to access the site is the URL that the site will have when it goes into production! (I tried installing with a different name and then renamed it and the HTML source of the pages still referred to the pre-production URL!) I suggest to this this by adding the following the the front-end proxying webserver:
    RewriteCond %{REMOTE_ADDR} ^<your-client-ip-with-dots-escaped>$
    RewriteRule ^/(wordpress/.*) http://<new-wordpress-server>/$1 [P]
    ProxyPassReverse / http://<new-wordpress-server>/
    
    RewriteCond %{REMOTE_ADDR} !^<your-client-ip-with-dots-escaped>$
    RewriteRule ^/(wordpress/.*) http://<old-wordpress-server>/$1 [P]
    ProxyPassReverse / http://<old-wordpress-server>/

    (Note the ‘!’ in the first line of the second stanza.)

  9. Edit /etc/wordpress/config-<public-name-of-your-server>.php and add content:
    <?php
    define('DB_NAME', 'wordpress');
    define('DB_USER', 'wordpress');
    define('DB_PASSWORD', '<set-password-here>');
    define('DB_HOST', 'localhost');
    define('WP_CONTENT_DIR', '/var/lib/wordpress/wp-content');
    
  10. Go to http://<your-server>/wordpress/ and fill in the fields as follows:
    • sitename: <your-internet-visible-hostname>
    • username: alexis
    • password: <something-strong>
    • your email: <publicly-valid-email-address>
  11. Press ‘Install WordPress’.
  12. Make the following general settings changes:
    1. To prevent the creation of month-based upload directories:
      1. Go to Dashboard–>Settings–>Media, disable ‘Organize my uploads into month- and year-based folders’
      2. Run:
        rm -fr /var/lib/wordpress/wp-content/uploads/201*
    2. To allow human-readable URLs:
      1. Run:
        a2enmod rewrite
        service apache2 restart
      2. Go to Dashboard–>Settings–>Permalinks, select Custom Structure and add ‘/about/’ in the box (I’m not sure if this is needed any more) and save settings.
      3. Click on the Permalinks link again and verify that the setting has stuck.
      4. Edit /etc/wordpress/htaccess to contain only:
        <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /wordpress/
        RewriteRule ^index\.php$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /wordpress/index.php [L]
        </IfModule>
    3. To allow plugins to be installed without FTP:
      1. Edit /etc/wordpress/config-<public-name-of-your-server>.php and add:
        define( 'FS_METHOD', 'direct' );
      2. Run:
        chown -R www-data:www-data /var/lib/wordpress/wp-content
        
  13. Allow existing themes to be upgraded by running:
    rm /var/lib/wordpress/wp-content/plugins/akismet   #  symlink pointing to read-only area prevents update
  14. Allow existing themes to be upgraded by running:
    rm /var/lib/wordpress/wp-content/themes/twentysixteen   #  symlink pointing to read-only area prevents update
  15. Download and activate the following plugins:
    • Akismet
    • Cleaner Gallery
    • Easy Fancybox
    • Embed OSM
    • Manage Upload Types
    • Media File Manager
    • W3 Total Cache
    • Tablepress
    • WordPress Importer
    • Slim Stat Analytics
    • Contact Form WpDevArt
    • Hide Title
  16. Configure those plugins as follows:
    • Easy Fancybox:
      1. Go to Dashboard–>Media–>Settings and enable handling Youtube links
    • Embed OSM:
      1. Set the image size to 900×400
      2. hide links tolarger maps
    • Manage Upload Types
      1. Scan any previous uploads directory for the largest file and filetypes by running:
        cd /var/lib/wordpress/wp-content/uploads
        find . -type f | sed 's/.*\.//' | sort -u
        du -ax * | sort -rn | grep '\t*.*\.' | head -1
      2. In the new installation, go to Dashboard–>Settings–>Media, for each filetype add it to the list. For me that was:
        gpx application/gpx+xml
        dsc text/dsc
        torrent application/x-bittorrent
        svg image/svg+xml
        iso application/iso-image
        img application/x-img
        vfd application/x-vfd
      3. Edit /etc/php/7.0/apache2/php.ini and set:
        post_max_size = <size-in-MB-of-largest-file-plus-50%-plus-1>M             #  E.g. post_max_size = 201M
        upload_max_filesize = <size-in-MB-of-largest-file-plus-50%-plus-2>M       #  E.g. upload_max_filesize = 202M
      4. and then run:
        service apache2 reload
    • W3 Total Cache
      1. Go to Dashboard–>Performance and, if any instructions are displayed about what to add to /usr/share/wordpress/.htaccess then edit /etc/wordpress/htaccess (which the former is symlinked to) and add them.
    • Contact Form WpDevArt
      1. Edit /var/lib/wordpress/wp-content/plugins/contact-forms-builder/frontend/skins/default.css and set:
        ...
        .wpdevart-forms.default-skin [type="button"]{
                ...
                /* font-size:16px; */
                /* font-weight:600; */
                ...
                color:#b12930 !important;
        }
        ...
        .success_message.reply_msg{
                background-color: #b12930;
                ...
        }
        ...
      2.  This will make forms more like the Reddle theme.
    •  Core
      1. Edit /etc/wordpress/config-<your-server>.php and add:
        define('WP_CACHE', true);
  17. If plagued by messages like:
    An automated WordPress update has failed to complete - please attempt the update again now

    then:

    1. Edit /etc/wordpress/config-<your-server>.php and set:
      define( 'WP_AUTO_UPDATE_CORE', false );
    2. Reload the dashboard and if the problem is solved them skip the next steps.
    3. Edit /usr/share/wordpress/wp-admin/includes/update.php locate the following code:
      function maintenance_nag() {
              include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
              global $upgrading;
              $nag = isset( $upgrading );

      and immediately after it add:

              delete_site_option('auto_core_update_failed');
    4. Reload the dashboard, verify the message is gone.
    5. Remove the just-added line (yes, really!).
    6. Reload the dashboard again, verify the message is still gone.
    7. See https://wordpress.stackexchange.com/questions/5468/an-automated-wordpress-update-has-failed-to-complete-please-attempt-the-updat for more information.
  18. Download and activate the following themes:
    • twentysixteen
    • twentyseventeen
    • Reddle
  19. Activate the Reddle theme.
  20. Modify the Reddle theme slightly:
    1. Edit /var/lib/wordpress/wp-content/themes/reddle/footer.php and change the site-info div to:
      <div id="site-info">
              <a href=/wordpress/>Home</a>
              <span> | </span>
              Powered by <a href="http://wordpress.org/">Wordpress</a>
              <span> | </span>
              Themed by <a href="http://wordpress.com/themes/reddle/">Reddle</a>
      </div>
    2. To get rid of horizontal scrollbars in preformatted blocks, go to Dashboard–>Appearance –>Editor and locate the CSS code:
      overflow-x: scroll;

      and change this to:

      overflow-x: auto;
    3. To remove the header text go to Dashboard–>Appearance–>Customize–>Site Identity, set the site title, remove the tagline and uncheck the ‘Display Header Text’ box. (The site title will appear on the browser title bar even if is prevented from appearing on the page.)
    4. To remove all the stuff on the right of the page, go to Dashboard–>Appearance–>Widgets and remove all widgets.
  21. Configure the logging of client IPs as described at Apache procedures.

Migrating content

  1. On the old WordPress server, install the Media File Manager plugin.
  2. On the new WordPress server, increase the timeout by editing /etc/apache2/apache2.conf and setting:
    Timeout 3600
  3. Increase the memory PHP may claim by editing /etc/wordpress/config-<your-server>.php and setting:
    define( 'WP_MAX_MEMORY_LIMIT' , '512M' );
  4. Apply the above changes by running:
    service apache2 restart
  5. Run:
    mysqldump --add-drop-table wordpress > ~/wordpress-empty.sql
  6. Prior to performing the actual migration, the content on the old WordPress server needs to be tweaked to allow some scripts to run. Do this as follows:
    1. Run:
      svn co https://svn.pasta.freemyip.com/main/webtools/trunk /tmp/webtools
      export SRC_UPLOADS_DIR=<path-to-uploads-dir>         #  e.g. export SRC_UPLOADS_DIR=/var/lib/wordpress/wp-content/uploads
      /tmp/webtools/bin/find-duplicately-named-uploads

      and for each file listed, use Media File Manager to rename one of the multiple occurrences of those files, in order to ensure there are no duplicately named uploads. You can call the script again to check you have renamed everything satisfactorily. (There is no need to edit the pages that reference the attachments; Media File Manager will do that automatically.)

    2. Use Media File Manager to move all banners to common/banners.
  7. On the old WordPress server, export content.
  8. On the new WordPress server, import content, being sure to tick the ‘ Expect some failures to be reported, as shown here:
    Failed to import Media “grosstiefental-map”
    Failed to import Media “5_20021124-1025_de-brauneck-lenggries_brauneck1”
    Failed to import Media “5_20021124-1028_de-brauneck-brauneckschneid_cross-michael1”
    Failed to import Media “5_20021124-1033_de-brauneck-brauneckhutte_risserkogel1”
    Failed to import Media “5_20021124-1034_de-brauneck-brauneckhutte_kotzen1”
    Failed to import Media “5_20040222-2482_de-brauneck-lenggries_hotel-alpenrose1”
    Failed to import Media “map1”
    Failed to import Media “x”
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Failed to import Media “bra2wie”
    Failed to import Media “no-ip-com-reset-button”
    Failed to import Media “no-ip-com-reset-butto2n”
    Failed to import Media “no-ip-com-update-hostnames”
    Media “directory-of-european-campsites” already exists.
    Media “” already exists.
    Media “” already exists.
    Media “” already exists.
    Failed to import Media “cz-sk-ku-track-annotated”
    Failed to import Media “cz-sk-hu-track-20150813TO20150904”
    Failed to import Media “pinion-gear-comparison”
    Failed to import “Hiking”: Invalid post type ec-template
    Failed to import “Software”: Invalid post type ec-template

    (I could not see any obvious cause of these failures.)

  9. On the new WordPress server, run:
    mysqldump --add-drop-table wordpress > ~/wordpress-post-import-pre-fix.sql
    rsync -a /var/lib/wordpress/wp-content/uploads/ /var/lib/wordpress/wp-content/uploads-pre-fix/
  10. On the old WordPress server, run:
    /tmp/webtools/bin/gen-retrieve-missing-uploads

    and follow the prompts regarding copying the just generated script across to the new WordPress server and running it there.

  11. To remove the header text go to Dashboard–>Appearance–>Customize–>Static Front Page, select ‘A static page’ and set the page to your intented top level page (for me this is the ‘Welcome!’ page). (This could not be done until the content had been migrated.
  12. On the old WordPress server, run:
    /tmp/webtools/bin/gen-relocate-uploads

    and follow the prompts regarding copying the just generated script across to the new WordPress server and running it there.

  13. Use the Media File Manager to move the remaining top level images to the right location.
  14. I do not use posts, so all example posts should be deleted.
  15. A lot of manual editing was required, especially for:
    • TablePress table indices needed offsetting (e.g. references to table 23 needed to be changed to table 21)
    • some banners were not displayed
    • thumbnails secondary lengths (i.e. the width or height that was not one of 150, 300 or 1024) changed by one pixel, with the result that the old thumbnails became part of the media library and needed deleting
    • galleries thumbnails (i.e. what was displayed prior to launching the fancybox window) changed from square to various rectangles and needed to be deleted and reuploaded
  16. Decrease the timeout in /etc/apache2/apache2.conf again.
  17. If the server is behind a front-end proxy then:
    1. Configure the logging of client IPs as explained in Apache procedures
    2. configure the proxying of requests to WordPress on the front-end server
  18. To support a Zabbix web scenario to check access to WordPress run:
    cd /etc/wordpress
    ln -s config-<your-server>.php config-$(uname -n).php

See also