SSH Private Key Authentication Tip


So easy to miss, but so important for SSH Private Key Authentication

I don’t allow password logins on any of my servers. Can only login via SSH key based authentication. No root login is allowed, and I specify every user that’s allowed to login via SSH, ie: me.

If you’re a regular here, you know I love DigitalOcean. They have a very nice tutorial on setting up SSH private key login, even walking you through creating SSH keys if you don’t already have one, and even adding that key to your DigitalOcean account.

None of that will be of interest to you if you already know how to generate SSH keys.

I’ll SSH into my new Droplet, only to be rejected. I immediately know why, because it’s happened so many times. It’s due to incorrect permissions on your Droplet, VPS, server, whatever.

For SSH private key authentication to work, the ~/.ssh/authorized_keys file and the ~/.ssh folder need specific permissions:

Run that, and then try logging in via SSH to your Droplet from your local machine. Should go this time.

If you still can’t login to your remote system, something else is likely wrong. If that’s the case, you’ll want to start at the top of the DigitalOcean post about setting up SSH private key authentication and just follow the steps.

After you’ve followed those steps, change permissions on the ~/.ssh/authorized_keys file again and on the ~/.ssh folder again. Like so from your terminal:

I’m curious…

Do you allow password logins over SSH?

View Results

Loading ... Loading ...

If you do allow password logins, I’d love to hear what scenario causes you to need to allow password logins. Let me know in the comments if you don’t mind.

Use Composer in Your WordPress Plugin or Theme


Simple Tutorial Showing How To Use Composer in Your WordPress Plugin or Theme

I love Composer. It just makes including libraries or scripts in your app incredibly easy. So easy that it’s stupid not to use it (in many, if not most cases).

The number of libraries/scripts available on Packagist is astounding, all of which can be included in your plugin with Composer. Packagist is the main Composer repository. It basically aggregates all types of PHP packages that can be installed via Composer.

I’d never used Composer with a proprietary WordPress plugin before. The plugin is for a client so it’ll never be available to the public.

Here’s the steps I took to make this WordPress plugin compatible with Composer so that I can easily bring in third-party libraries.

We’ll be using mailgun-php throughout this example, as the plugin that inspired this post uses Mailgun to send all sorts of emails.

1. First, install composer on your server.

I install Composer globally, like so:

2. Add Mailgun as a dependency.

3. Check your composer.json file.

We’re including Mailgun and guzzle from Packagist. Your composer.json file should look similar to the example below.

4. Tell composer to install Mailgun.

5. Autoload Our Mailgun Classes in Our Plugin.

The following should go in your plugin-name.php file, before any other PHP code.

You can now use Mailgun in your WordPress plugin or theme, some basic examples of using Mailgun can be found on GitHub and in their official documentation.

Fix StanleyWP WordPress Theme Portfolio Grid


Fix display of portfolio grid rows

Back in September of 2014 I wrote about using the StanleyWP WordPress theme for a portfolio site. After I added some projects, I noticed the grid on the Portfolio page template wasn’t displaying rows correctly. I even noted it in my original post, towards the end.

I’ve had a few people contact me about how to fix the StanleyWP portfolio grid issue, and earlier today Arun left a comment asking how to fix the grid issue.

You need to be using a child theme for this, it’s just good practice. If you don’t know how to create a child theme, read my post on creating a child theme. It’s really easy to do, but may require you to reset your menu or some widgets after changing to the child theme.

Anyway, Arun confirmed that this gist fixed the problem for him:

Just save that code as template-portfolio.php and put it in your child theme directory. Your portfolio should now show three projects per row. No CSS or anything else needs to be modified, just that one page template.

Let me know if you have any issues or questions.

Using Gmail SMTP Servers to Send Email From WordPress on DigitalOcean


Use Gmail SMTP Servers for Sending Emails from WordPress

After quite a bit of back and forth between sendmail, postfix, and exim, I’ve settled on using msmtp for sending emails from my servers/droplets at DigitalOcean (affiliate link).

MSMTP is very lightweight and has the ability to send emails via an existing SMTP server, like Gmail’s or Yahoo’s.

To get it working, there’s a few tricks. I’ve pieced this together from this post and this post. And when on DigitalOcean, there’s an IPv6 issue that causes major delays in sending the email, which there’s a fix for at the end of this post.

1. Install msmtp

2. Configure msmtp to use Gmail

Open up /etc/msmtprc as root: sudo nano /etc/msmtprc, and add the following, removing whatever else is there:

You’ll want to replace the user directive with a valid Gmail email address, a Gmail account or a Google Apps email address will work, too. Don’t forget to change enter-password-here! to the actual password for the Gmail account your using.

Save /etc/msmtprc.

3. Remove Sendmail

Run this:

4. Setup Some Aliases

Lots of software on Linux systems uses the sendmail command. Instead, we’re using msmtp, so we’re essentially invoking msmtp when the sendmail command is run.

5. Tell PHP About msmtp

First, locate your php.ini file that’s being used by Apache. It’s typically in /etc/php5/apache2/php.ini. If that’s not it, use PHP’s phpinfo() function to find the location of your php.ini file.

Find sendmail_path = in php.ini and replace it with this:

Now you should be able to send mail using PHP’s mail() function, which will use the Gmail SMTP server to send emails. Add this to a PHP file and access it through your browser to see if it works:

6. Disable IPv6 If You Experience Slowness

Open up /etc/gai.conf like so:

Now, look for a line that looks like this: #precedence ::ffff:0:0/96 100. Uncomment that line (remove the #) and save /etc/gai.conf. An explanation of why this helps can be found in this comment at the DigitalOcean article.

All Done

That should be it. If you run into any issues please do leave a comment, I’ll do my best to help you out. I may have missed a part, so no guarantees this will work for you. It does however work wonderfully on a DigitalOcean droplet that’s running Ubuntu 14.04 with a pretty standard LAMP stack.

You should now be able to send email from WordPress on DigitalOcean.