Monday, 5 November 2012

Moving sub-domian on Wordpress

Wordpress has a nasty habit of keeling over and dying when it is moved from one directory, or server, from another. This is because it is not just static html and css pages and it has quite a lot of relevant links to where it was originally based. For example it requires a database which contains links to the site url and config files which link to the database. The use case for this was changing the theme of a codemeasandwich.com wordpress blog. Another test site was set up so themes and layout changes could be made without affecting the live site. From researching there seems to be plugins and services that help facilitate this better but these are more for server migration and also don't seem to be much simpler than this was for switching directories and such. Also there is more initial steps in this method but after this is done there will constantly be a test environment available for future wordpress development.
These are the steps taken to set up new.codemeasandwich.com:

  1. Add new sub-domain to gandi.net under codemeasandwich.com called "new"

  2. On DEV VM: Create new git repo called codemeasandwich_site using the create_repo.sh script

  3. On sites vm:

    sudo mkdir /var/www/testing.codemeasandwich.com

  4. Clone repo you made into the directory you created:

    git clone
    ssh://git@dev.codemeasandwich.com:2222/home/git/codemeasandwich_site.git testing.codemeasandwich

  5. Copy files from codemeasandwich directory into the testing.codemeasandwich directory:

    cd /var/www/codemeasandwich
    sudo cp -R * ../testing.codemeasandwich

  6. From within the testing/codemeasandwich directory push to master:

    sudo git add *
    sudo git commit
    sudo git push origin master

  7. Add a reference in /etc/apache2/sites-available/sites for the new sub-domain and restart apache:
    
            Use VHostSetup new.codemeasandwich.com testing.codemeasandwich
            ServerAlias www.new.codemeasandwich.com
    
    
    sudo service apache2 restart

  8. I also needed to chown -R the new testing.codemeasandwich directory to www-data, I will explain this in depth later:

    sudo chown www-data -R *

The remaining changes are to do with the database:

  1. Log into dev.codemeasandwich.com/phpmyadmin
  2. Export database codemeasandwich_wordpress to your local machine
  3. Create new empty database, in this case called newcodemeasandwich_wordpress
  4. Import the old database you downloaded to your local machine
  5. Add user "sandwich" and grant the user all privileges
  6. From inside the database click on wp-options on the list on the left
  7. From this table find the entries for siteurl and home, change them to point to your new sub-domain
  8. Lastly from the sites vm change the database being used for the wordpress:
    cd /var/www/testing.codemeasandwich
    sudo vim wp-config.php
The line:
/** The name of the database for WordPress */
define('DB_NAME', 'codemeasandwich_wordpress');
Should be changed to:
/** The name of the database for WordPress */
define('DB_NAME', 'newcodemeasandwich_wordpress');
The site should now be fully functioning from your new sub-domain.

Common errors with Wordpress

Uploaded file could not be moved to...
This happened after we finished moving the site and realised that images and such weren't able to be uploaded. The error that would appear is Uploaded file could not be moved to:/some/directory/uploads. From researching the problem online it seems to occur quite often and the reason it happens is that Wordpress does not have permission to place the file in the directory mentioned.

The top fix online was to  chmod 777  the directories but this is dangerous as someone could hack in and place their own images and we don't want that now do we?(Do we?)!? I noticed, by using  ls -la  on the directory that all the folders belonged to root but the original codemeasandwich directory had belonged to www-data. This was the problem and a simple  sudo chown www-data -R *  fixed the problem. I still don't know how the copying of the files changed the ownership but it did, so deal with it!
... continue reading!