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.

Nagios – downtime on host/service from command line with curl

Sometimes deployment process or other havy task may cause some Nagios checks to rise below normal levels and bother admin. If this is expected and you want to add downtime on host/service during this task you may use this script:

#!/bin/bash

function die {
  echo $1;
  exit 1;
}

if [[ $# -eq 0 ]] ; then
    die "Give hostname and time in minutes as parameter!"
fi

if [[ $# -eq 1 ]] ; then
    MINUTES=15
else
    MINUTES=$2
fi

HOST=$1
NAGURL=http://nagios.example.com/nagios/cgi-bin/cmd.cgi
USER=nagiosuser
PASS=nagiospassword
SERVICENAME=someservice
COMMENT="Deploying new code"

export MINUTES

echo "Scheduling downtime on $HOST for $MINUTES minutes..."

# The following is urlencoded already
#STARTDATE=`date "+%Y-%m-%d %H:%M:%S"`
STARTDATE=`date "+%d-%m-%Y %H:%M:%S"`
# This gives us the date/time X minutes from now
#ENDDATE=`date "+%Y-%m-%d %H:%M:%S" -d "$MINUTES min"`
ENDDATE=`date "+%d-%m-%Y %H:%M:%S" -d "$MINUTES min"`
curl --silent --show-error \
    --data cmd_typ=56 \
    --data cmd_mod=2 \
    --data host=$HOST \
    --data-urlencode "service=$SERVICENAME" \
    --data-urlencode "com_data=$COMMENT" \
    --data trigger=0 \
    --data-urlencode "start_time=$STARTDATE" \
    --data-urlencode "end_time=$ENDDATE" \
    --data fixed=1 \
    --data hours=2 \
    --data minutes=0 \
    --data btnSubmit=Commit \
    --insecure \
    $NAGURL -u "$USER:$PASS"| grep -q "Your command request was successfully submitted to Nagios for processing." || die "Failed to con
tact nagios";

echo Scheduled downtime on nagios from $STARTDATE to $ENDDATE

Threat this script as template with some tips:

  • I you want to add downtime on service, then provide SERVICENAME and --data cmd_typ=56 \.
  • If you want downtime on whole host, just remove this line: --data-urlencode "service=$SERVICENAME" \ and --data cmd_typ=86 \
  • Another thing that in my example nagios page use basic auth for security, if your don’t use it, you may remove -u "$USER:$PASS" from parameters.
  • If you get Start or end time not valid, then you have to adapt dates to your formats of dates accepted by Nagios (probably this depends on Nagios version or timezone configuration).

Source:
http://stackoverflow.com/questions/6842683/how-to-set-downtime-for-any-specific-nagios-host-for-certain-time-from-commandli

Changing default php.ini file for PHP-CLI on CentOS

On Debian in default installation you have different configuration files for PHP in Apache, FPM, CLI, etc. But on CentOS you have only one php.ini for all of them. In case I have, I need to have different configuration file for scripts running in CLI mode (more memory, etc). I could run it like this:

php -c /etc/php-cli.ini script.php

But this a little burdensome. So I do it like this:

cat > /etc/profile.d/php-cli-ini.sh <<SCRIPT
#!/bin/bash
alias php="php -c /etc/php-cli.ini"
SCRIPT
cp /etc/php.ini /etc/php-cli.ini

Logout, login and now every user can run PHP scripts in CLI with different configuration – exactly what I need 🙂

Comparing two lists in bash

I had quite simple task – compare two lists of hosts and check if hosts from first one are also on the second one. I started with diff:

diff -u biglist.txt hosts_to_check.txt | grep -E "^\+"

It was fine but output needs some filtering to get what I want.

I’ve found another example with grep:

grep -Fxv -f biglist.txt hosts_to_check.txt | sort -n

This will search for all lines in hosts_to_check.txt which don’t match any line in biglist.txt. So after this I’ve got list of hosts that I have to check. That’s exactly what I need 🙂