Category Archives: Code

Rewrite rule to bulk redirect domains using Apache’s RewriteMap

A component of the server migration I’m currently working on is to move all of the existing virtual host files. Unfortunately, they’re not all consistently setup, which makes them difficult to overview, and from past experience, difficult to manage.

There is also no easy way to tell which of them represent real sites, and which are purely for redirection. Some even have a document root, implying there may actually be a site there, but often I found only a single .htaccess file that redirected the domain to another site.

In a previous blog post, I proposed several guidelines to effectively manage virtual host files going forward, and one aspect of that, which I’ll talk about more deeply in this blog post, is how to organize and manage the redirects in bulk.

Continue reading

Increase space on fixed VirtualBox VDI disk containing Windows 7 on Mac

I use VirtualBox on my Mac to have other operating systems at my disposal. One of my appliances contains a copy of Windows 7 with a fixed, 25GB disk. I’ve run out space and was looking for an easy way to increase it.

I came across the modifyhd command, which in theory let’s you increase the disk space, but it only works on dynamic disks. I opted for performance (fixed disk) when I set up the appliance, and this minor inconvenience is the price.

There are several tutorials out there, but many of them require third-party applications, either on the Mac or in Windows side. The thing is, even without those utilities, it’s fairly easy to increase your disk space.

What we’ll do is create a bigger disk, clone the contents from the small disk to the big disk, and then increase our Windows partition.

Continue reading

Managing and organizing Apache virtual host files

I’m in the midst of building new servers, inventorying the existing ones, and creating a migration plan to move over anything that needs to make it over. This is the perfect time to look at the big picture and assess whether everything is implemented in the best way possible.

One of the items I’m reviewing are the virtual host files, and let me tell you, they’re a mess.

The permissions are all over the place, there doesn’t seem to be a clear naming convention, some virtual host files are purely redirects, and there are so many includes containing additional rules and rewrites, that according to the error logs, they’ve started stepping on each other’s toes (“Request exceeded the limit of 10 internal redirects due to probable configuration error”).

Continue reading

WordPress Upgrade: Peer certificate cannot be authenticated with known CA certificates

I was trying to upgrade WordPress on one of my servers, but kept receiving an SSL-related error:

Peer certificate cannot be authenticated with known CA certificates.

I verified the SSL certificate on the server and there are no issues with it as far as I can tell, but I’d be curious to know if someone else figures out the actual cause.

That said, and as a quick work-around, you can prevent cURL from verifying the certificate just during WordPress upgrades:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
add_action(
    'load-upgrade.php',
    'my_load_upgrade_php'
);
 
function my_load_upgrade_php() {
    add_filter(
        'http_request_args',
        'my_http_request_args',
        10,
        2
    );
}
 
function my_http_request_args($args, $url) {
    $args['sslverify'] = false;
    return $args;
}
add_action(
	'load-upgrade.php',
	'my_load_upgrade_php'
);

function my_load_upgrade_php() {
	add_filter(
		'http_request_args',
		'my_http_request_args',
		10,
		2
	);
}

function my_http_request_args($args, $url) {
	$args['sslverify'] = false;
	return $args;
}

What this will do is set CURLOPT_SSL_VERIFYPEER to false:

1
curl_setopt( $handle, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $handle, CURLOPT_SSL_VERIFYPEER, false );

Git post-receive hook to deploy WordPress and plugins as submodules

In a previous blog post I discussed how best to manage file and folder permissions when deploying with Git, but today I’ll show a specific example of what that post-receive hook might look like for a WordPress project that uses submodules.

I have three servers that I can deploy to, but the post-receive hook only deploys a project when it encounters the specified branch as defined per server:

  1. QA waits for a release branch
  2. Staging waits for the master branch
  3. Production waits for the master branch

Other than this small difference, the post-receive hook is identical on all three servers to reduce maintenance.

Lastly, each server has two repositories per project:

  1. Bare repository – storage unit that uses a post-receive hook to deploy the project.
  2. Working repository – web root that will serve the project to the end-user.

This method has several benefits:

  1. View a combined list of all version-controlled projects via the /opt/repositories directory.
  2. Recover a corrupted web root by cloning a fresh copy from the origin.
  3. Prevent some issues that may occur during the deployment, since it will fail before post-receive hook is fired.

Continue reading