Web Dev Blog

Looking for help with WordPress, Apache, Vim, Web Security and more. Here are a few tips.

Setting WordPress Permissions

I use the amazing wp-cli (http://wp-cli.org/) to install WordPress on my development machine, but for some reason the file and directory permissions are always wrong.

I have added this step as part of my installation procedure:

wp core download # Use wp-cli to download the latest WordPress
chown www-data:www-data -R * # Let apache be owner
find . -type d -exec chmod 755 {} \; # Change directory permissions rwxr-xr-x
find . -type f -exec chmod 644 {} \; # Change file permissions rw-r--r--

This sets all the permissions correctly so Apache no longer gives any errors.

Calibre Crashed with IOError: Errno 5 input/output error

I run Calibre on Kubuntu to manage my ebooks.
Calibre stopped working one day and crashed with this message

Traceback (most recent call last):
File "site.py", line 61, in main
File "site-packages/calibre/__init__.py", line 21, in
File "site-packages/calibre/startup.py", line 77, in
File "site-packages/calibre/utils/localization.py", line 129, in set_translators
File "site-packages/calibre/utils/localization.py", line 63, in get_lang
File "site-packages/calibre/utils/config_base.py", line 446, in
File "site-packages/calibre/utils/config_base.py", line 354, in __getitem__
File "site-packages/calibre/utils/config_base.py", line 364, in get
File "site-packages/calibre/utils/config_base.py", line 348, in refresh
File "site-packages/calibre/utils/config_base.py", line 277, in parse
IOError: [Errno 5] Input/output error
/opt/calibre/lib/python2.7/site-packages/calibre/ptempfile.py:27: RuntimeWarning: Parent module 'calibre' not found while handling absolute import

Hunted high an low to find the problem, re-installed, updated, upgraded to Ubuntu 14.10, still getting same error.

Finally ran an strace on calibre

strace -o ./logfile /opt/calibre/calibre

and found this

open("/home/user/.config/calibre/global.py", O_RDWR|O_CREAT|O_CLOEXEC, 0644) = 7
fstat(7, {st_mode=S_IFREG|0644, st_size=3498, ...}) = 0
fcntl(7, F_GETFL) = 0x8002 (flags O_RDWR|O_LARGEFILE)
fstat(7, {st_mode=S_IFREG|0644, st_size=3498, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fc610952000
lseek(7, 0, SEEK_CUR) = 0
lseek(7, 0, SEEK_SET) = 0
flock(7, LOCK_EX|LOCK_NB) = 0
fstat(7, {st_mode=S_IFREG|0644, st_size=3498, ...}) = 0
lseek(7, 0, SEEK_CUR) = 0
read(7, 0x7fc610952000, 4096) = -1 EIO (Input/output error)

Tried to open .config/calibre/global.py and sure enough, it was toast. Deleted global.py and restarted Calibre and everything is working.

www subdomain causing WordPress multisite redirection loop

WordPress Multisite Redirect from a www Subdomain in Cpanel

One of my clients in my multisite network has been having a reoccurring error.  From time to time the www.foo.com domain would stop working.  foo.com isn’t the primary, so the site would redirect to www.foo.com and fail.  Just a blank page would show.

Of course when a client’s site is down it’s panic mode it can sometimes be difficult to document the fix and then find it again a few months later when the problem reared it’s head.

This time was different, now the problem is solved and I’m documenting the issue.

Reviewing the Apache Logs

I started out seeing this error in my Apache error log

[Wed Apr 16 14:30:31 2014] [error] [client xx.xx.xx.xx] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

This indicated that it was a redirect issue.  .htaccess causes problems sometimes, the next step was to confirm that the .htaccess file matched what was in the Codex


Next step was to look at the Apache logs closer by setting LogLevel debug.  These lines were added to the Apache httpd.conf

RewriteLogLevel 3
RewriteLog "/var/log/apache2/rewrite.log"

(Several comments said this no longer works in newer versions of Apache, but it worked like a champ for me.)
These logs showed more evidence that the problem had to do with the www, but still weren’t helpful enough.  They showed a www directory under the primary domain.  A review showed there was an empty www directory.  This was part of the problem.  Deleting the www directory eliminated the problem, but didn’t explain why it kept coming back.

The httpd.conf directory still showed a VirtualHost listing for foo.com located in a www directory underneath primary.com, but the httpd.conf had warnings not to make edits there.

Back in cPanel, the only listing for foo.com was as a parked domain.  Attempting to delete the parked domain resulted in an error message.  Even stranger.

Finally checked the subdomains.  At some point a subdomain using www for foo.com had been created.  Attempted to delete this subdomain, but again, an error.

Hmm… so can’t remove the parked domain, and can’t remove the subdomain.  Maybe because both existed, back to Google for more searching and finally an answer came up in the cPanel forums.


cPanel won't allow users to create a "www" subdomain - and rightly so, because ww is handled in the main DNS as a CNAME...

However - some of our users have managed this (probably many years ago before the limitation was put in place).

This cause massive problems every apache recompile - because the system thinks the domain's document root is public_html/www

And the reply…

You should check for "www" subdomain entries at the following locations:
Remove entries within the files, and any individual files used purely for the "www" subdomain. Ensure you do not remove the "ServerAlias" itself, as you will still want the "www" alias for the domain name in the Apache configuration.

When you have finished, run the following commands:

Following these steps removed the subdomain from the system and should eliminate the problem permanently.

Shut Off Monitor With Nircmd

I have multiple computers on my work desk and sometimes I want to shut off a monitor.  Maybe I’m watching a video or playing a game with the lights down – whatever the reason, I haven’t found an easy way to blank out a monitor under Windows 7 until now.

Found this great article on howtogeek.com about disabling a monitor.

Create a Shortcut or Hotkey to Turn Off the Monitor


It uses the Nircmd.exe app to create a shortcut to disable the monitor.  Very cool.

M570 Mouse Button Click Problems – How Long Should They Last

m570-wireless-mouseI have a Logitech M570 Wireless Trackball.  I’ve had it for a few years, and love it.  Recently I’ve noticed the the right mouse button has click issues.  Sometimes it will right-click and act like a double click.  Sometimes it will stick.  This was a problem during gaming, working in Excel and while completing other tasks.

Finally, I was tired of this problem, so I did a search.  Amazingly, this is a huge problem.  The Logitech forums are littered with complaints.  What is the problem?  It seems to be the microswitches Logitech is using are not holding up.  Many users are able to contact Logitech and have the mouse replaced under warranty, but the Logitech hasn’t officially addressed the problem.

How to fix the M570 Wireless Trackball right click

There doesn’t seem to be a consensus on fixing this problem.  A site called Red Ferret has an article on repairing a switch in a Logitech mouse (not the M570) (http://www.redferret.net/?p=31008)

The symptoms are that the mouse click delivers two clicks for one press, which can be extremely annoying and renders the mouse unworkable for important tasks. This time, however, instead of consigning the device to the trash and buying another, we decided to see if we could solve the situation with a bit of DIY. And the answer is yes!

A quick search on Google revealed that other more intrepid souls had managed to fix their own meeces, and so we got to work on two ‘dead’ mice lying around in Ferret Towers, a Logitech M705 and an older MX Revolution. The first thing to note is that the culprit is tiny, a sliver of copper which can’t be worth more than £0.02p.

Their solution is to re-bend the spring or “sliver of copper” and they even have a video showing how to fix this.

Logitech Mouse Double Click Problem and How To Fix It

While this seems like a cheap fix, it does seem annoying to tear the whole thing apart just to tweak that spring. Why not replace the switch with a new one?

What switch is in the M570 and what do we replace it with? The consensus is Omron makes the best switches, but no one really made a distinction on what is the “best” switch for a mouse button. This forum lists the Omron switches and lists the D2F-01F as one of the better switches. Interestingly, it seems the Logitech uses Omron switches in many of their products, like the trackball in this post that clearly has a DF-01F-T switch.

Is the M570 mouse worth fixing or should we just buy a new one?

The M570 sells new on Amazon for about $40.  A new switch is only a couple dollars on ebay, but the labor to put it in is probably an hour and some soldering.  So, is it worth trying to fix?  That’s a tough question.  There are reports that some users are only getting a few months out of the M570 before experiencing the problem.  If that’s true, it might be worth fixing, but if Logitech will warranty the trackball, buying a new one may be a better option.

Finally, is Logitech to blame for this problem?  Is there something they should do?  It appears that Logitech is using the best switches available in their products, unless the M570 has a non-Omron switch.  Maybe these switches just won’t stand up to the kind of abuse the users are giving them.  Maybe a mouse only has a lifespan of a few months for some people and a couple years for others, at least until someone builds a better switch.

global.inc.php and pt_register from phpFormGenerator is broken and not working

There is a tool that used to come with the cpanel installation on some of the bigger hosts (hostgator, bluehost, etc…) call phpFormGenerator.  This tool would let you create forms in a GUI and then give you code to upload to your website.

Part of this code was a file called global.inc.php.  global.inc.php included code that read the session variables that were passed to the post.  At the time it was written PHP3 was in common use and a function called pt_register was included that made assigning the value from a form field to a variable a little easier (it was kind of clunky at that point).

Basically you would have something like


This would assign the contents of form field Foo to a variable $Foo.  The way this was accomplished was inherently unsafe and has been removed completely from current versions of PHP (PHP5.5).

If you have moved a site to a server running php 5.5 that uses this code the forms will be broken.

Thankfully there is an easy fix.  Form field contents are now put into the $_POST variable.  This means you can replace the pt_register call with this

$Foo = $_POST['Foo'];

Exact same thing is accomplished, just replace the pt_register lines in any form php files and the forms will work again.

Removing Special Characters From Linux Filenames

This goes with the previous post concerning converting files from flac to mp3. Sometimes filenames have characters beyond spaces that make it difficult to work with in a script. If you want to rename your files without annoying special characters, use the following command.

for f in *
mv "$f" $(echo $f | sed -e 's/[^A-Za-z0-9._-]/_/g')

This will replace every non-alphanumeric character in the filename with an underscore _

Converting FLAC to MP3 from the command line in Ubuntu

From time to time I have some flac files I want to convert to mp3 files so they will play on my car radio. This is simple enough to do,and with a Google search I found this command.

for f in *;do flac -cd $f |lame -b 128 - $f.mp3;done

The first problem with this is it doesn’t like spaces in the file names. This can be solved by adding quotation marks

for f in *;do flac -cd "$f" |lame -b 128 - "$f".mp3;done

The second problem is, it creates all the files with the name foo.flac.mp3. After a bit of research I found the rename command can quickly rename all of the files.

rename 's/\.flac.mp3$/.mp3/' *.mp3

You could probably add this to the end of the for loop with a && or something

Disabling the cache in Google Chrome

When working on website development in Chrome it’s useful to disable the cache. I have found three convenient ways to do this.

1. This isn’t really a disabling of the cache, but Ctl + Shift + R forces a reload of the page without accessing the cache.

2. The cache can be disabled by accessing the developer tools with a Ctl + Shift + I keystroke. Click on the gear in the bottom right corner and click the “Disable Cache” checkbox.

This method may be problematic. A tweet on July 12, 2012 mentions that the caching is only disabled while the devtools are open. This may not always bee what you want to see.

3. Ctl + Shift + N opens an incognito window. By design an incognito window does not have cache available, so anything opened in that window bypasses all caching. This seemed to work well.