WordPress: Installation and Performance

This article describes some of the basics on installing and running WordPress to maximize its performance. The following scripts and text generally assume Debian/Ubuntu, so substitute Red Hat, Arch, etc. as needed.

Installation

If you’re less lazy than I am, you could combine the whole install routine into a single Bash script, and make it compatible with multiple distros, something like:

#!/bin/bash
REDHAT="/etc/redhat-release";
ARCH="/etc/arch-release";
APACHED=apache2;
WPDIR=/var/www/wpdir;
if [[ -e "$REDHAT" ]] || [[ -e "$ARCH" ]]
   then echo "Using the httpd daemon for Apache.";
   APACHED=httpd;
else
   echo "Using the Apache2 service for Apache.";
fi
if [[ -e "$ARCH" ]]
   then echo "Using the Arch Apache DocumentRoot (/srv/http/wpdir)."
   WPDIR=/srv/http/wpdir;
else
   echo "Using /var/www as the Apache DocumentRoot.";
fi

(Of course, there are other ways to determine which distro you’re running: /etc/issue, /etc/release, lsb_release -a, uname -a in some cases, etc.)

Anyway, get the latest version of WordPress, untar it, set permissions:

#!/bin/bash
# Set sitename and directory for WordPress
WPDIR=/var/www/wordpress;
SITENAME=mydomain.com;
EMAIL=myemail@somewhere.com;

# Download latest WordPress and untar it
wget https://wordpress.org/latest.zip;
unzip latest.zip;
mv wordpress/* $WPDIR;

# Set permissions on /var/www/wpdir to web user
# Assumes Debian/Ubuntu; for Fedora/CentOS use apache:apache
chown -hR www-data:www-data $WPDIR;

# Write an Apache virtual host file for
# Again assumes Debian/Ubuntu, for Fedora/CentOS use httpd instead of apache2, etc.
echo -e "<VirtualHost *:80>ntServerAdmin $EMAILntServerName $SITENAMEntDocumentRoot 
$WPDIRn</VirtualHost>" > /etc/apache2/sites-available/$SITENAME.conf;
a2ensite $SITENAME.conf;
service apache2 restart;

# Set up database
mysql -uroot -p -e "CREATE DATABASE wordpress DEFAULT CHARACTER SET='utf8';  
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, LOCK TABLES, 
CREATE TEMPORARY TABLES ON wordpress.* TO 'wp-user'@'localhost' IDENTIFIED BY 'wp-password'; 
FLUSH PRIVILEGES;"
mysql> SHOW GRANTS FOR 'wp-user'@'localhost';
+--------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for wp-user@localhost                                                                                                                     |
+--------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'wp-user'@'localhost' IDENTIFIED BY PASSWORD '*934823094823AB0498230498234DA0938'                                   |
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, 
| LOCK TABLES ON `wordpress`.* TO 'wp-user'@'localhost' |
+--------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

Finally, go to mydomain.com in your web browser and specify the first WP user.

WordPress Performance Concerns

As a PHP application, WordPress benefits considerably from the usual caching systems and performance accelerators, like APC or Zend OPcache. If you run OPcache, you may also want to install APCu as a data store/user cache.

Otherwise, the usual performance tips apply:

  • If you’re pushing a lot of large files, graphics, and interactive scripts, consider moving them to a CDN with a highly distributed network like CloudFlare or Amazon CloudFront.
  • Minimize the number of HTTP requests by combining or eliminating as many .js and .css files as possible, or install a plugin like Better WordPress Minify to compress them. Depending on your theme or environment, you can use WordPress’ wp_register_style(), wp_enqueue_style(), wp_register_script(), and wp_enqueue_script() functions to eliminate CSS and JavaScript you don’t need.
  • Run your web server behind a general HTTP accelerator like Varnish.
  • Run an event-driven web server like Nginx — generally better for high concurrency — or if you’re running Apache, consider using its Worker or Event multi-processing modules with PHP-FPM instead of Prefork with mod_php. (Now that Apache ships with Event as its default MPM, many developers believe the difference between Apache and Nginx to be neglible.)
  • Run Memcached to reduce the load on your MySQL database.
  • Use a lightweight, text-based adaptive/responsive theme like the appropriately-named Responsive. What matters most is content; the most heavily trafficked web site in the world is plain old Google, after all.
  • For customizations, use a minimal child theme, so your base theme can be updated and secured without overwriting your changes.
  • For WordPress performance plugins, I’ve long used Mark Jaquith’s APC Object Cache Backend, which is an unintrusive and quiet caching mechanism but assumes the existence of APC on your server. Alternatively I’d consider Super Cache or BatCache.
  • Watch carefully how plugins interact with your site. For example, I’ve used the Yoast Google Analytics plugin for a long time, but with some themes I noticed the .js tracking code was loading before the CSS, which was locking up Firefox and causing the page text to show for a second without styling. Moving Yoast’s code below the call to wp_head() smoothed things out; apparently IE and Chrome render JavaScript fast enough to avoid this problem.
  • If you use a social bookmarking system like AddThis, consider adding only the social networks you want (Facebook, Twitter, Google+, LinkedIn, perhaps) for each the_permalink() value returned by the main loop. You can use something like this to better control the inserted code:
    <div class="addthis_toolbox addthis_default_style"
        addthis_url="<?php the_permalink(); ?>"
        addthis_title="<?php the_title(); ?>"
        addthis_description="<?php the_excerpt(); ?>">
      <a class="addthis_button_preferred_1"></a>
      <a class="addthis_button_preferred_2"></a>
      <a class="addthis_button_preferred_3"></a>
      <a class="addthis_button_preferred_4"></a>
      <a class="addthis_button_compact"></a>
      <a class="addthis_counter addthis_bubble_style"></a>
    </div>
    <script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=xa-51c5e0e92c8c44a9"></script>
    

    When plugins insert JavaScript code, you can usually override them in your theme’s functions.php to move the scripts to wp_footer() instead of wp_head(). Because downloading scripts tends to lock things up, this typically permits faster page loading.

  • To get an idea of your page loading time, you can either install a plugin designed for this purpose, or add a timer start/stop script to your header and footer.

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *