Wordpress on Heroku

Sep 23, 2015 | |

Wordpress on Heroku is great, but difficult to setup. You will need to sign up for Amazon Web Services and Heroku. You’ll need to install the Heroku toolbelt on your machine using “wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh”. I’ll assume you’re running Linux for this, but I’ll see about a Windows tutorial as well later on. Setting up AWS is also not going to be covered right now in this tutorial.

  1. The first thing to do is setup LAMP on your Linux machine.
  2. Next get the latest version of Wordpress. Download it and unzip the file into your "/var/www/html/" folder.
  3. Download Amazon Web Services and Amazon S3 and Cloudfront.
  4. Unzip both of Amazon Web Services and Amazon S3 and Cloudfront into the wp-content/plugins folder.
  5. You should have been given access keys for your AWS user. Add them to your wp-config.php file like this:

    // AWS Access Keys //
    define( 'AWS_ACCESS_KEY_ID', 'your_aws_access_key_id_here' );
    define( 'AWS_SECRET_ACCESS_KEY', 'your_aws_secret_access_key_here' );

  6. Go here and add your Unique Keys and Salts to your wp-config.php file like below:

    define('AUTH_KEY', 'put your unique phrase here');
    define('SECURE_AUTH_KEY', 'put your unique phrase here');
    define('LOGGED_IN_KEY', 'put your unique phrase here');
    define('NONCE_KEY', 'put your unique phrase here');
    define('AUTH_SALT', 'put your unique phrase here');
    define('SECURE_AUTH_SALT', 'put your unique phrase here');
    define('LOGGED_IN_SALT', 'put your unique phrase here');
    define('NONCE_SALT', 'put your unique phrase here');

  7. Go to localhost/wordpress/ and run through the installation using the MySQL credentials you setup while setting up your LAMP stack. Check here if you're not sure what to do.
  8. Go to your dashboard, and click on the plugins tab.
  9. Activate both the Amazon Web Services and Amazon S3 and Cloudfront plugins. There should now be another tab at the bottom that says "AWS". If your AWS User is setup correctly then they should both work immediately and allow you to connect to a bucket.
  10. Go into your wordpress folder and open a terminal session there.
  11. In your terminal type $ git init That'll initialize the whole thing as a git repository.
  12. In your terminal type git add . to get all the WP files ready to be committed. This will take a second or two.
  13. Now type git commit -m 'Add your own commit message here' Wait for the wall of text to stop running.
  14. Now type heroku create That'll create a new Heroku app from that directory.
  15. Type heroku addons:add cleardb If all goes well then you're almost ready to push your installation to Heroku.
  16. Copy the code below with the Unique Keys and Salts you got earlier:

    heroku config:set AUTH_KEY='put your unique phrase here'
    SECURE_AUTH_KEY='put your unique phrase here'
    LOGGED_IN_KEY='put your unique phrase here'
    NONCE_KEY='put your unique phrase here'
    AUTH_SALT='put your unique phrase here'
    SECURE_AUTH_SALT='put your unique phrase here'
    LOGGED_IN_SALT='put your unique phrase here'
    NONCE_SALT='put your unique phrase here'

  17. Again in the terminal type git checkout -b production You'll store your production environment variables here.
  18. Add this line to your wp-config.php file:

    $db = parse_url($_ENV["CLEARDB_DATABASE_URL"]);

  19. Change your database information in wp-config.php to the code below:

    define('DB_NAME', trim($db["path"],"/"));
    define('DB_USER', $db["user"]);
    define('DB_PASSWORD', $db["pass"]);
    define('DB_HOST', $db["host"]);

  20. Change your Unthentication Unique Keys and Salts to the below:

    define('AUTH_KEY', getenv('AUTH_KEY'));
    define('SECURE_AUTH_KEY', getenv('SECURE_AUTH_KEY'));
    define('LOGGED_IN_KEY', getenv('LOGGED_IN_KEY'));
    define('NONCE_KEY', getenv('NONCE_KEY'));
    define('AUTH_SALT', getenv('AUTH_SALT'));
    define('SECURE_AUTH_SALT', getenv('SECURE_AUTH_SALT'));
    define('LOGGED_IN_SALT', getenv('LOGGED_IN_SALT'));
    define('NONCE_SALT', getenv('NONCE_SALT'));
    define('AWS_ACCESS_KEY_ID', getenv('AWS_ACCESS_KEY_ID'));
    define('AWS_SECRET_ACCESS_KEY', getenv('AWS_SECRET_ACCESS_KEY'));

  21. Now you're all set. Type git push heroku production:master and let it finish. When that's done you can go to "http://yourwordpressapp.heroku.com" and complete your remote installation.
    1. In order to check out your remote installation simply switch back to the master branch with git checkout master

Updating Wordpress

If you need to update Wordpress (mine updated twice while I was working on this) you’ll need to take a few steps:

  1. In a terminal in the html folder run sudo chown -R www-data:www-data wordpress/ That'll let you update wordpress locally through the app.
  2. Make sure you're on the master branch and update WP via the dashboard.
  3. Once Wordpress is finished updating run sudo chown -R your_user:your_user wordpress/ Now you'll be able to edit the folder yourself again.
  4. Commit the changes made by the update.
  5. Switch to the production branch and merge with master by running git merge master This shouldn't create any conflicts (it didn't with me).
  6. The wp-config file should be untouched, but check it just in case. If it doesn't have your production settings, simply edit the file again, and commit changes.
  7. Now run git push heroku production:master again, wait and your remote installation will be updated.
    • Remember to switch back to the master branch to get your local installation working again.

Adding/updating themes and plugins

Adding/updating themes and plugins is easy.

  1. Download the theme or plugin.
  2. Make sure you're in your master branch. Unzip the theme/folder into it's respective folder.
  3. In your local installation active the theme or plugin.
  4. Commit the changes, then switch to production.
  5. Run git merge master This should not create any conflicts if the plugin/theme is new.
    • If you're updating a conflict might be caused. If so, then in the terminal type git reset --hard
    • Delete the older plugin/theme and replace with the new one, then merge again.
  6. The wp-config file should be untouched, but check it just in case. If it doesn't have your production settings, simply edit the file again, and commit changes.
  7. Now run git push heroku production:master again, wait and your remote installation should now have the plugin/theme.
    • Go to "http://yourwordpressapp.heroku.com/wp-admin" and Wordpress might ask you to update the database. Simply go ahead and everything should work fine.