Configure WordPress on OpenShift without Git: Part 2

Mark Berry May 5, 2015

In the Part 1 of this article, I explained how to set up an “empty” PHP app on OpenShift and how to connect to it using Putty, WinSCP, and FileZilla. In this part, we’ll focus on actually setting up WordPress. As mentioned in Part 1, I’m moving a WordPress site from a shared host, so I’ll actually be restoring a backup to create the site.

Figure Out Where to Load the Site

This is where WinSCP comes in handy. By poking around, I found that the OpenShift PHP gear is publishing the site from the app-root/repo folder:

WordPress on OpenShift 2

If you browse to the site in your browser (using the URL you defined in Part 1), you should see the contents of index.php from this folder.

Note If you restore your site to a subfolder under /repo named php, public, public_html, web, or www, that subfolder will be used as the document root the next time PHP is restarted. Avoid those names if you want to use /repo as the root. See Setting Your Document Root.

The simplest WordPress install will put several PHP files directly into the /repo directory, along with the standard wp-admin, wp-content, and wp-includes subdirectories. If you have a more complex structure, e.g. subdomains containing different WordPress sites, you may need to create a different structure.

Note Do not delete the hidden /repo/.openshift folder.

Set Up Admin Areas

If you need to keep static data outside the site, e.g. backup scripts and folders, create those under /app-root/data. This corresponds to $OPENSHIFT_DATA_DIR. See Persistant storage for OpenShift applications. I created and admin folder and some subfolders there.

Restore the SQL Database

On my old site, I use a script to do a SQL backup every day. After setting the proper environment variables, the backup script runs this command:

mysqldump --opt -h $DBHOST -u $DBUSER -p$DBPASS mysite > mysite.sql

I moved the latest .sql backup to my OpenShift /data/admin/backup_sql folder. When you created the SQL gear in Part 1, OpenShift created a SQL database and user for you. The login info is stored in environment variables. So to restore your database, you only need to run PuTTY and load the MySQL dump (note that mysql command wraps to two lines here; put it on one line):

cd ~/app-root/data/admin/backup_sql
mysql -u $OPENSHIFT_MYSQL_DB_USERNAME -p$OPENSHIFT_MYSQL_DB_PASSWORD -h $OPENSHIFT_MYSQL_DB_HOST $OPENSHIFT_APP_NAME < mysite.sql

Restore the Site Files

The old site uses this command to back up the site’s files:

tar -czf $SITEDIR/mysite-yyyymmdd.tar.gz .

So after uploading the most recent backup to my OpenShift /data/admin/backup_site folder, I used these commands to restore the site:

cd ~/app-root/repo
tar xvfz ~/app-root/data/admin/backup_site/mysite-yyyymmdd.tar.gz

Fix Permissions

Check the permissions on the subdomain directories and files. They must allow public read. Set the default permissions for directories (755) then files (644).
See Hardening WordPress:  File Permissions.

find ~/app-root/repo/ -type d -exec chmod 755 {} \;
find ~/app-root/repo/ -type f -exec chmod 644 {} \;

Configure the Database in wp_config.php

Edit ~/app-root/repo/wp-config.php with the new SQL DB info. Here are the lines from the OpenShift WordPress example:

define('DB_NAME', getenv('OPENSHIFT_APP_NAME'));
define('DB_USER', getenv('OPENSHIFT_MYSQL_DB_USERNAME'));
define('DB_PASSWORD', getenv('OPENSHIFT_MYSQL_DB_PASSWORD'));
define('DB_HOST', getenv('OPENSHIFT_MYSQL_DB_HOST') . ':' . getenv('OPENSHIFT_MYSQL_DB_PORT'));
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');

Note I had to modify this later. See “Troubleshooting” below.

Also in wp_config.php, you might want to update your Unique Keys. Follow the instructions in the comments.

Test the Site

Browse to your new site’s URL. If you get a 503 error, see “Troubleshooting” below.

If your new site has a different URL even if it is only temporary, you will need to replace instances of the old URL with the new URL in your database. See the official WordPress info on this under Moving WordPress:  Changing Your Domain Name and URLs. That article mentions the Search and Replace for WordPress Databases script (aka the interconnectit script), which I’ve found to be quite useful (even though you have to upload it manually rather than installing it as a plugin).

Troubleshooting

After loading the site, I got a 503 error whenever I tried to access it. I was struggling to understand why until I found a good tip in this thread: the 503 error is coming from Haproxy’s health check and means no healthy instances were found. That can happen if Apache is returning a 500 error. To check, log on with Putty and type:

curl -o - http://$OPENSHIFT_PHP_IP:$OPENSHIFT_PHP_PORT/

That showed that I had a database connection error. To get past the, I finally had to hard-code database logon instead of using environment variables. Not sure why the environment variables don’t work here.

Set Up Cron and Backups

In the OpenShift dashboard, open your application and choose “see the list of cartridges you can add.” Add cron 1.4.

Now you can drop a backup script into ~/app-root/repo/.openshift/cron/daily to run daily backups. Here is the backup_www.sh script I use (adapt and use at your own risk!):

cd $HOME/app-root/repo

DATE=`date +%Y%m%d`
#
# Site backup using tar with gzip
#
SITEDIR=$HOME/app-root/data/admin/backup_site
# Tar everything from root of the WordPress site.
# Use "." to include ALL files including .htaccess (see http://stackoverflow.com/a/1622401/550712)
tar -czf $SITEDIR/mysite-$DATE.tar.gz .
echo -e 'GZipped main site to '$SITEDIR'/mysite-'$DATE'.tar.gz\n'>&2
# Delete files older than 5 days
echo -e 'Deleting the following files over 5 days old:\n'>&2
find $SITEDIR/mysite-* -mtime +5 -print -delete >&2
#
# SQL backup using mysqldump and gzip
#
SQLDIR=$HOME/app-root/data/admin/backup_sql
DBHOST=$OPENSHIFT_MYSQL_DB_HOST
DBPORT=$OPENSHIFT_MYSQL_DB_PORT
DBNAME=$OPENSHIFT_APP_NAME
DBUSER=$OPENSHIFT_MYSQL_DB_USERNAME
DBPASS=$OPENSHIFT_MYSQL_DB_PASSWORD
mysqldump --opt -h $DBHOST -P $DBPORT -u $DBUSER -p$DBPASS $DBNAME > $SQLDIR/$DBNAME.sql
gzip $SQLDIR/$DBNAME.sql
mv $SQLDIR/$DBNAME.sql.gz $SQLDIR/$DBNAME-$DATE.sql.gz
echo -e '\nMysqldump of '$DBNAME' saved to '$SQLDIR'/'$DBNAME'-'$DATE'.sql.gz\n'>&2
# Delete files older than 5 days
echo -e 'Deleting the following files over 5 days old:\n'>&2
find $SQLDIR/$DBNAME-* -mtime +5 -print -delete >&2

Remember that space on OpenShift’s free tier is limited to 1GB per gear. That’s why this script deletes backups over five days old. To keep a local backup, you can set up a scheduled task on a Windows computer that uses PSCP to download the latest backup.

Other Tips

Review the 12 Tips article mentioned in Part 1 and consider its suggestions. In the U.S. and Canada, the free Bronze upgrade is a no-brainer to prevent gear idling; a credit card number is required but presumably won’t be charged if you stay within the free limits. Caching and external email are good suggestions for which my site already uses other solutions.



10 Comments

  1. Kiuhnm   |  May 25, 2015 at 2:52 am

    I think you missed a ‘\’:
    find ~/app-root/repo/ -type d -exec chmod 755 {} \ ;
    find ~/app-root/repo/ -type f -exec chmod 644 {} \ ;

  2. Mark Berry   |  May 25, 2015 at 7:13 am

    Kiuhnm – seemed to work without the backslash for me but I’m not a Linux guru so not sure what difference the makes.

  3. Kiuhnm   |  May 25, 2015 at 9:39 am

    I got the same error as this guy:
    http://stackoverflow.com/questions/19737525/find-type-f-exec-chmod-644

  4. Mark Berry   |  May 25, 2015 at 8:34 pm

    Kiuhnm – you are absolutely correct. I went back to the WordPress article linked just before the “find” statements, as well as to my internal notes, and both had that trailing backslash. Maybe my blogging software ate it but in any case I have corrected the article. Thanks for your help.

  5. Duy Trung   |  July 27, 2015 at 7:01 am

    I have a issue to want asking you, can you help me?
    I have created a wordpress site with scalable option. So, I can’t add phpmyAdmin to my cartridge. I downloaded the newest version phpmyadmin 4.4.12. Then I uploaded it to WordPress folder (in the same folder as wp-admin, wp-include,…). Then I access phpmyadmin vua my website domain http://www.duytrung.tk/phpmyadmin, but i can’t use username and password given by my app. Before, I have accessed config file of phpmyadmin, use enviroment variables as WordPress site.
    (Maybe I have upload phpmyadmin to wrong place?)
    Can you help me solve this problem?
    Thank you?

  6. Mark Berry   |  July 27, 2015 at 2:00 pm

    Duy, I did get phpMyAdmin working but I created a separate subdomain mysql.mydomain.com. That requires some advanced configuration in .htaccess. Then phpMyAdmin itself must be configured for login methods etc. I seem to remember this involved creating config.inc.php in the phpMyAmin main folder. Maybe this setup article will help: http://docs.phpmyadmin.net/en/latest/setup.html.

  7. Deepak   |  March 07, 2016 at 2:15 pm

    Hi,
    Thanks for the great article. I have moved over to Openshift, but always get 503 error when I open a few tabs for the various posts on my site. I think Openshift is unable to handle the load.

    Can you please tell me what you meant by – hard-code database logon instead of using environment variables?

  8. Mark Berry   |  March 07, 2016 at 2:27 pm

    Deepak – by “hard-code database logon” I mean I put the SQL username and password in my wp-config.php file.

  9. himesh   |  June 30, 2016 at 9:57 am

    Hey I want to migrate my site from openshift to new hosting. Please write a complete guide for dummies

  10. Mark Berry   |  June 30, 2016 at 10:29 am

    Himesh, if you set up WordPress as I suggested on OpenShift, you should be able to migrate it as you would any other WordPress site. There are lots of guides online for doing that.

Leave a Reply





*