In last article of the WordPress on Cloud series, we covered WordPress installation on a cloud server. Once WordPress is up & running, the next immediate step is to customize our website and add features to it. The most common way to approach this customization is with themes and plugins.
In our case, for using themes and plugins we have to come up with a way aligned to our infrastructure. In production we cannot install themes and plugins directly from the WordPress admin panel, as it will create inconsistency among host servers. We approach this issue by making use of the “Develop locally and Deploy globally” strategy. In this approach we develop and test on a local machine, and then we deploy that code to all of our production servers. This approach makes sure that each server has updated version of the website and there is no inconsistency. This approach also saves us hassle of making changes and testing in production environment, which is usually quite slow and does not include in ideal devops practices.
For getting started, install WordPress on your local machine. You can make use of our previous tutorial to install WordPress on your local machine. We will be using git for deploying our WordPress site, as it’s one of the simplest and easiest tools for deploying code.
Make sure that git is installed on your local machine. If you are not familiar with git, you can check out git documentation to learn and understand about git. Note that we will be using Ubuntu 12.04 LTS for this tutorial.
Setting up the Local Environment
In this tutorial we will be demonstrating installation of a new theme, procedure remains same for the plugins. We have WordPress installed and working on our local machine, let’s install a new theme!
Once the theme is installed and you are satisfied with the result, we can move on to deployment. Again, note that we are using “Develop locally, Deploy globally” strategy.
Change current directory to folder containing all the WordPress files[code language=”bash”]$ cd /var/www[/code]
Create git repository, add all the files and commit the changes[code language=”bash”]$ git init
$ git add -A
$ git commit -m “First commit | Added all the files”[/code]
Our local machine is now ready to deploy the code to Cloud.
Setting up the Server
We assume that you have installed LAMP stack on this server. If you need help with installing LAMP stack, please check out our previous blog post.
Change ownership of directory where WordPress will be installed.[code language=”bash”]$ sudo chown username:www-data /var/www -R
$ sudo chmod g+w /var/www -R[/code]
Install git[code language=”bash”]$ sudo apt-get install git-core[/code]
Create the directory where you want to store your git repository[code language=”bash”]$ mkdir wordpress.git
$ cd website.git[/code]
Initiate bare repository[code language=”bash”]$ git init –bare[/code]
The setup so far is enough to take care of our first deployment. We do the following setup to take care of updates we will make to our WordPress site on our local machine. To achieve this we will use git-hooks.[code language=”bash”]$ sudo nano hooks/post-update.sample[/code]
Add the following configuration to post-update hook[code language=”bash”]#!/bin/sh
GIT_WORK_TREE=/var/www git checkout -f[/code]
Rename the hook[code language=”bash”]$ sudo mv hooks/post-update.sample hooks/post-update[/code]
Give excute permission to post-update hook
[code language=”bash”]$ sudo chmod +x hooks/post-receive[/code]
You can user several other git-hooks according to your needs, you can learn more about git-hooks here.
Deploying WordPress to Production
Now we switch to our local machine to deploy the WordPress site.
Using git remote we will deploy our code to desired server[code language=”bash”]$ sudo git remote add wordpress ssh:[email protected]_ip/home/username/wordpress.git[/code]
This will push code to remote repository wordpress[code language=”bash”]$ sudo git push wordpress +master:refs/heads/master[/code]
Make sure that you add wp-config.php to .gitignore after first deployment. Now switch to production server and make changes to wp-config.php so that WordPress can connect to the production database[code language=”bash”]$ cd /var/www
$ sudo nano wp-config.php[/code]
Restart Apache to make sure that changes are implemented[code language=”bash”]$ sudo service apache2 restart[/code]
Visit http://server_ip to make sure everything is running properly. With this setup, you can deploy any changes/updates you make on your local machine. Whenever you add themes/plugins, just commit new files and deploy![code language=”bash”]$ git commit file1 file2
$ git push wordpress master[/code]
Note that after you deploy the code, you have to activate the theme/plugins you added, even if you activated them on the local machine.
Storing Media Assets
Similar to themes/plugin installation, media assets should not be stored on a single cloud server instance, as you may need to delete/add instance on go while scaling. Fortunately there are WordPress plugins to help us with assets, we will discuss them and role of Content Delivery Network(CDN) in further articles/tutorials.
So far we have successfully installed and deployed WordPress site, we have also learned about themes/plugin installation for WordPress sites on Cloud. Stay tuned as we dive deep into performance and monitoring of WordPress sites on Cloud.