Prefer IPv4 over IPv6

I try to use IPv6 where it’s available but it’s sometimes so hard… It happen quite often that I can’t download packages from repos because they weren’t configured on IPv6 vhosts even when host is available via IPv6 address. For APT you may use this trick to force IPv4 connections only:

echo 'Acquire::ForceIPv4 "true";' > /etc/apt/apt.conf.d/99force-ipv4

If you need more than that, then gai.conf will allow you to filter where you will be connecting via IPv4 and where via IPv6 – in example bellow you will prefer IPv4 whenever it’s available:

echo 'precedence ::ffff:0:0/96  100' >> /etc/gai.conf

WordPress with HyperDB on PHP 7.0

I was configuring WordPress with HyperDB plugin on PHP 7.0 but the only I get were constant 500 errors. As I found here PHP 7.0 is not supported by HyperDB for now – it rely on mysql php extension but in PHP 7.0 there is only mysqli. Helpfully few folks fixed it and it’s possible to use it.

curl -O https://raw.githubusercontent.com/soulseekah/hyperdb-mysqli/master/db.php
mv db.php /var/www/wordpress/wp-content/

And configure it ex. like this:

cat <<DBCONFIG > /var/www/wordpress/db-config.php
<?php
\$wpdb->save_queries = false;
\$wpdb->persistent = false;
\$wpdb->max_connections = 10;
\$wpdb->check_tcp_responsiveness = true;

\$wpdb->add_database(array(
'host'     => "master.db.host",
'user'     => "wordpress",
'password' => "random_password",
'name'     => "wordpress",
'write'    => 1,
'read'     => 1,
));
\$wpdb->add_database(array(
'host'     => "slave.db.host",
'user'     => "wordpress",
'password' => "random_password",
'name'     => "wordpress",
'write'    => 0,
'read'     => 1,
));
DBCONFIG

Now WordPress could handle crash of master database.

Source:
https://www.digitalocean.com/community/tutorials/how-to-optimize-wordpress-performance-with-mysql-replication-on-ubuntu-14-04

List octal file permissions in bash

Sometimes it’s easier to use octal file permissions but they’re not so easy to list. I caught myself few times that I didn’t remember how to list them – so this is a reason for that note.

$ stat -c "%a %n" *
755 bin
755 games
755 include

Yes, it’s that easy 🙂
And here also with human readable attributes:

$ stat -c '%A %a %n' *
drwxr-xr-x 755 bin
drwxr-xr-x 755 games
drwxr-xr-x 755 include

Automatically build after file change

I’m playing a lot with Docker lately. Building images, and then rebuilding, and then building again… It’s pretty boring. To automate this task a little I used inotify to build automatically after I changed any file. This trick could be used in many different situations.

You will need inotify-tools package:

    sudo apt-get install -y inotify-tools

Then run something like this:

while inotifywait -e modify -r .; do docker-compose build; done

This commands will rebuild my Docker images after any file change in current directory. Use Ctrl+c to exit from loop.

Install WordPress from command-line

I never tried it before but today I needed to install WordPress… From command line only. And there is a way to do this with wp-cli.

WP-CLI installation

First some requirements (as root):

apt-get install php5-cli php5-mysql mysql-client curl

And now installation of wp-cli (as root too):

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
mv wp-cli.phar /usr/local/bin/wp

Check if it’s working:

$ wp --version
WP-CLI 0.22.0

WordPress installation

Now you should switch to user of your web application, ex. like this:

su - www-data -s /bin/bash

And install WP:

wp core download --path=/var/www/wordpress

wp core config --path=/var/www/wordpress \
  --dbname=wordpress \
  --dbuser=wordpress \
  --dbpass=wordpresspass \
  --dbhost=localhost \
  --locale=pl_PL

wp core install --path=/var/www/wordpress \
  --url="http://example.com" \
  --title="Example blog" \
  --admin_user=never_use_admin_here \
  --admin_password=admin_pass \
  --admin_email=admin@example.com \
  --skip-email

Here you may find more about wp-cli configuration and commands.