Archive | WordPress

WordPress 3.2 requirements and CentOS 5.6

Current requirements

  • PHP version 4.3 or greater
  • MySQL version 4.1.2 or greater

WordPress 3.2 requirements:

  • PHP version 5.2.4 or greater
  • MySQL version 5.0 or greater.

Default PHP on Cent0S 5.6 = PHP 5.1.6.

CRAP

Updated: See this post on how to install an updated PHP on CentOS 5.6

WordPress TwentyTen Custom Header setting in the db

When we move a WordPress site from development to production we update the URL in the following db values:

  • in the GUID value of each post in wp_posts
  • in the wp_options table, the option_name of home
  • in wp_options, the option_name of siteurl

In a recent move, we found that the custom header in the TwentyTen theme was not displaying correctly when we moved across servers. Seems that when you use a TwentyTen theme or child theme, a wp_option is added to the table – theme_mods_twentyten. The value of this contains all the theme mods including the the URL of the header image. The query below would update the URL in this value:

  • mysql –batch –skip-column-names -e “use $CURRENTDB;UPDATE wp_options SET option_value = replace(option_value, ‘”$OLDSITENAME”‘, ‘”http://$NEWSITENAME”‘) WHERE option_name = ‘ theme_mods_twentyten’;”

Note: When using a copy of the TwentyTen theme, the option_name value will be theme_mods_NameOfTheTheme.

MySQL replication of WordPress dbs over ssh

I wanted to setup MySQL replication over ssh for a small WordPress database. I have a VM that lives in my house, I wanted to be able to bring it up, make it current, disconnect, and then hack away. Here is my proceedure.

On the master:

Setup the replication user (I had to use 127.0.0.1 becasue % did not let me connect from ssh):

grant replication slave on *.* TO repl@"127.0.0.1" identified by '[repl password]';

edit /etc/my.cnf

server-id=1
log-bin=mysql-bin
replicate-do-db=DB_TO_REPLICATE   #the name of the db you want to replicate.

From the slave:

edit /etc/my.cnf

server-id = 2
master-host = 127.0.0.1
master-user = repl
master-password = password
master-port = 7777
replicate-do-db = DB_TO_REPLICATE   #the name of the db you want to replicate.

Next, tunnel a ssh connection from slave to the source, and then make sure you can see the source databases:

ssh -f -N -L7777:127.0.0.1:3306 [email protected]
mysql -h 127.0.0.1 -P 7777 -e "show databases;"

Next create a local database:

mysql -e "create database DB_TO_REPLICATE;"

Next we want to find where the master’s log is and its position:

MASTERLOGPOS=$(mysql -h 127.0.0.1 -P 7777 --batch --skip-column-names -e "SHOW MASTER STATUS;" | cut -f2)
MASTERLOGFILE=$(mysql -h 127.0.0.1 -P 7777 --batch --skip-column-names -e "SHOW MASTER STATUS;" | cut -f1)

next we want to seed the local db

mysqldump -h 127.0.0.1 -P 7777 DB_TO_REPLICATE | mysql -u root DB_TO_REPLICATE;

and finally, tell the db to use the source as the Master:

mysql -e "use DB_TO_REPLICATE;CHANGE MASTER TO MASTER_LOG_FILE='"$MASTERLOGFILE"',MASTER_LOG_POS=$MASTERLOGPOS;"
mysql -e "start slave;"

To test I run:

mysql -e "show slave status\G;"

and I compare the following on the master and the client:

mysql -e "select id,post_parent,post_modified,post_title from jbmurphy_com.wp_posts"

My “Copy WordPress site to local” script

I wanted to write a script that would copy a WordPress site to the local machine from a remote server (securely over ssh). This would allow me to set up a test environment or backup. First I need to make sure the site exists on the source server. Then I want to get the current wp-config settings, like the database name:

echo "Checking $SRCSERVER for a site named $SRCSITENAME"
CHECKCMD="ls /PATH/TO/$SRCSITENAME/wp-config.php | grep wp-config.php > /dev/null; echo \$?"
CHECKFILE=$(ssh $SRCSERVER $CHECKCMD)
if [ $CHECKFILE -eq 0 ]; then
        echo "Source Site $SRCSITENAME on $SRCSERVER exists"
        SRCMYSQLSITEUSER=$(ssh $SRCSERVER grep "DB_USER" /PATH/TO/$SRCSITENAME/wp-config.php | cut -f 2 -d " " | cut -f 2 -d "'")
        SRCMYSQLSITEPASS=$(ssh $SRCSERVER grep "DB_PASSWORD" /PATH/TO/$SRCSITENAME/wp-config.php | cut -f 2 -d " " | cut -f 2 -d "'")
        SRCMYSQLSITEDB=$(ssh $SRCSERVER grep "DB_NAME" /PATH/TO/$SRCSITENAME/wp-config.php | cut -f 2 -d " " | cut -f 2 -d "'")
        echo "Source mySQL Site Database:"$SRCMYSQLSITEDB
        echo "Source mySQL Site Username:"$SRCMYSQLSITEUSER
        echo "Source mySQL Site Password:"$SRCMYSQLSITEPASS
else
        echo "Source Site $SRCSITENAME on $SRCSERVER does not exist"
        exit;
fi

Next I provision a new site with a new virtual host, and I copy over the files:

echo "Copying site from $SRCSERVER:/PATH/TO/$SRCSITENAME/ to /PATH/TO/$DESTSITENAME"
rsync --archive $SRCSERVER:/PATH/TO/$SRCSITENAME/ /PATH/TO/$DESTSITENAME

Next I provision a new blank MySQL database with the name, username, and password received in the first part for code, then I copy over the db data via ssh:

echo "Copying mysql data from $SRCSERVER to local database name $DESTSITENAME"
ssh $SRCSERVER "mysqldump -u root $SRCMYSQLSITEDB" | mysql -u root $DESTSITENAME

Finally I run a couple of SQL queries to change the URL that WordPress has inside the DB.


echo "What is going to be the WordPress URL"
read NEWURL

OLDSITENAME=$(mysql --batch --skip-column-names -e "use $DESTSITENAME;select guid from wp_posts LIMIT 1;" | cut -d"/" -f3)
echo "Changing the wp_posts guids from $OLDSITENAME to $NewURL

mysql --batch --skip-column-names -e "use $DESTSITENAME;UPDATE wp_posts SET guid = REPLACE (guid, '"http://$OLDSITENAME"','"http://$NEWURL"');"

echo "Changing the home and siterurl to $NewURL"

mysql --batch --skip-column-names -e "use $DESTSITENAME;UPDATE wp_options SET option_value = replace(option_value, '"http://$OLDSITENAME"', '"http://$NEWURL"') WHERE option_name = 'home' OR option_name = 'siteurl';"

Check if a file exists on a remote server

I was working on a copy WordPress site to a local machine script, and I wanted to check if the remote path was a WordPress site.
Here is the code I used.

CHECKCMD="ls /path/to/wp-config.php | grep wp-config.php > /dev/null; echo \$?"
CHECKFILE=$(ssh $SRCSERVER $CHECKCMD)
if [ $CHECKFILE -eq 0 ]; then
	echo "file exists"
fi

Finding diffs between clean and modified versions of WordPress

We have an issue where our developers try to update the core WordPress files. I wanted to find a way to keep them honest. here is my script:


# get WordPress
cd ~/src/
rm -f ~/src/latest.tar.gz
rm -rf ~/src/wordpress/
wget -q http://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
CURRENTVERSION=$(grep "wp_version =" ~/src/wordpress/wp-includes/version.php | cut -f 3 -d " " | sed "s/;//" | sed "s/'//g")
# find WordPress installs and compare
for installpath in $(find $SEARCHPATH -name wp-config.php)
	do
		BASEDIR=$(dirname $installpath)
		INSTALLEDVERSION=$(grep "wp_version =" $BASEDIR/wp-includes/version.php | cut -f 3 -d " " | sed "s/;//" | sed "s/'//g")
		if [ $CURRENTVERSION == $INSTALLEDVERSION ]; then
		echo "====Comparing $BASEDIR to Source====" 
		diff -rq --exclude="wp-content" ~/src/wordpress  $BASEDIR #| grep differ
		fi
done

My current WordPress Update Script

Below is my current WordPress update script. First this script downloads the most recent version and determines which version it is:

cd ~/src/
rm -f ~/src/latest.tar.gz
rm -rf ~/src/wordpress/
wget -q http://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
CURRENTVERSION=$(grep "wp_version =" wordpress/wp-includes/version.php | cut -f 3 -d " " | sed "s/;//" | sed "s/'//g")
echo "Latest Version: $CURRENTVERSION"

Next it looks for all wp-config.php files in all the websites to identify which sites have WordPress installed. Then and finds the version from the versions.php script. If the version is not equal to the most recent downloaded version (from the code above), it copies the updated source to the website:

for installpath in $(find /webdir -name wp-config.php)
	do
	BASEDIR=$(dirname $installpath)
	INSTALLEDVERSION=$(grep "wp_version =" $BASEDIR/wp-includes/version.php | cut -f 3 -d " " | sed "s/;//" | sed "s/'//g")
	if [ ! $CURRENTVERSION == $INSTALLEDVERSION ]; then
		echo "updating" $BASEDIR from $INSTALLEDVERSION "to" $CURRENTVERSION
		cp -R  ~/src/wordpress/* $BASEDIR/
	else
		echo $BASEDIR "is already" $CURRENTVERSION
	fi
	done