In Part 1 of Linux for WordPress developers I covered the Apache web server, domain names, .htaccess and .htpasswd files, and extracting the WordPress files.
WordPress uses the MySQL database, which can be a little daunting if you’ve never set one up before. I use Ubuntu and I find the easiest way is to use phpMyAdmin, which if you’ve used cpanel as a WordPress developer you’ve probably used this to import the MySQL dump file.
You can install it on Ubuntu by following the phpMyAdmin community docs.
Creating a database for WordPress
Once you’ve installed phpMyAdmin just visit http://localhost/phpmyadmin/index.php and log in.
The easy way to create the database and user at the same time is to click the “User” tab.
Tick the “Create database with same name and grant all privileges” box, ignore all the stuff that really looks like it need to be ticked (that one checkbox field takes care of everything), and that it’s!
Version Control
I use git for version control. It’s designed for distributed change management but I like it basically to use as a backup, so I can roll back to an earlier version if I really stuff something up. That rarely happens so I usually need to look up my own quick reference.
Here’s the summary:
Git Quick Reference
Start using git on files in current directory:
> git init
> git add .
> git commit -a -m “initial”
Roll back:
via stackoverflow
> git reset –hard $REV
A slightly less scary way to do this than the git reset –hard method is to create a new branch. Let’s assume that you’re on the master branch and the commit you want to go back to is c2e7af2b51.
Rename your current master branch:
> git branch -m crazyexperiment
Check out your good commit:
> git checkout c2e7af2b51
Make your new master branch here:
> git checkout -b master
Now you still have your crazy experiment around if you want to look at it later, but your master branch is back at your last known good point, ready to be added to. If you really want to throw away your experiment, you can use:
> git branch -D crazyexperiment
Get a specific file
via stackoverflow
> git show $REV:$FILE
Change the previous commit log message
> git commit –amend
Go back to a previous commit, then switch to latest
> git checkout 1391785c3b946b526e3058da3902c41f03849a36
(now in “detached head” state)
> git checkout master
(back to latest commit)
git and MySQL
That takes care of the files, but we want to keep our database in version control too. I use this script, in /var/www/demo/mysql-backup.sh :
#!/bin/bash mysqldump -u wordpress_demo --password="e5aTpSCTzaS9FWJJ" wordpress_demo > wordpress_demo.sql
On a production system you definitely shouldn’t give the password in this script, because it’s possible for other users to see it by looking at the running processes on the system. But on a single-user development machine, it’s probably okay.
This gives us a wordpress_demo.sql file that can be added to git. This is great because it means that I can also make changes in WordPress and know that if anything goes badly wrong, or if the client requests a totally new direction, I can roll back and have all my settings appear in WordPress once I import the SQL file.
This is most easily done via phpMyAdmin, or via the command line with:
#!/bin/bash mysql -u wordpress_demo --password="e5aTpSCTzaS9FWJJ" wordpress_demo < wordpress_demo.sql
Now that we’ve got through all the tough stuff, please enjoy this related XKCD cartoon!