Setting up a Craft website on Digital Ocean
I recently changed the development environment for my website and moved it to a Digital Ocean droplet. It had been on my todo list for a while because I thought it'd take ages, so I was pleasantly surprised by how easy it was. This is largely due to the incredible number of posts in Digital Ocean's community and others, who explain the process really well.
This post explains how to launch a website powered by Craft CMS with a proper local development environment and workflow behind it. I have put the Craft theme and config files under version control with Git, created a new Digital Ocean droplet, installed Craft on it, and deployed the templates and configs to the live site. I haven't explained every part of the process because others have already done this really well. So in some cases, I have linked to the posts that helped me.
I've been using Craft CMS for a few years now and have never looked back. It's so simple, I can even write and publish blog posts on my walks to work.
Setting up a local environment
This is how I can work on my website and preview changes before deploying them to the live site. I have used MAMP for the local server environment and Sequel Pro to manage databases. Craft can be downloaded and installed by following the instructions on buildwithcraft.com. Una Kravets has written an excellent guide to setting up Craft with MAMP and Sequel Pro, so I recommend following that.
Check point: After following Una's post and the documentation for installing Craft, I had a working website with a database running on my local environment. It can be viewed here: http://localhost:8888/admin — or whatever has been set in MAMP preferences. If the page still shows a monkey, follow the steps to complete the website setup.
Once Craft was setup, I renamed the public directory to html. This is because the root document on a Digital Ocean droplet is located, by default, at /var/www/html/, so it's important that the development environment is the same. This is specifically the case for Digital Ocean and may not be for others; quite often the name public is correct—so it's best to check.
Before I started working on my site, I set up version control. First up, creating a blank repository on a Git hosting service—I used Github.
For security, I don't want to share my database credentials in craft/config/db.php. I also don't want the Craft theme files to be added to Github because I have no intention of changing them. So I created a .gitignore file in the root of the site to prevent certain files from being tracked and pushed to Github. Mine looks like this:
craft/app/ craft/plugins/ craft/storage/ craft/config/db.php
Using terminal, I pushed the changes to the repository and made an initial commit. There are also git GUI clients available if preferred.
1. Go to the website root:
cd ~/Sites/craft-website git init
2. Add and commit the files
git add . git commit -m "Initial commit"
3. Add a Git remote and push repository to it
git remote add origin https://github.com/username/craft-website.git git push -u origin master
Create a Digital Ocean droplet
Then it was time to go to DigitalOcean, sign up and create a new droplet. I gave the droplet a name, selected a size, and I added an SSH key. I chose LAMP on Ubuntu under applications, so that the droplet is preconfigured with Linux, Apache, MySQL & PHP. It also possible to set this up from scratch. Here's a tutorial for that: https://www.digitalocean.com/community/tutorials/h...
These two posts do a great job of illustrating the steps for creating a droplet:
The first time I connected to my droplet using ssh, I was prompted to change the root password in terminal.
Upload website files
Then I uploaded my website files to the droplet using Git. Connecting to the server like this:
ssh root@[IP address]
Move to the site root:
Then clone the website from Github. Make sure the .gitignore file is uploaded.
As mentioned earlier, I disallowed the Craft theme files and the config/db.php file, so they didn't get cloned into the live site using Github. I uploaded those files manually instead, using Transmit.
Create a database for the live site
In short, here is how I did it. First up, I opened terminal and accessed the MySQL shell by typing the following command:
mysql -u root -p
To create a new database:
CREATE DATABASES [db-name];
To see a list of the available databases, which should now show the newly created database:
Type exit to leave:
Here is a basic and more in-depth tutorial on MySQL: https://www.digitalocean.com/community/tutorials/a...
My local site already had a populated database, which I wanted to import to the live database. So I connected to the live database in Sequel Pro and imported the local one.
And that's pretty much everything, for now. I'm responsible for this server from now on, so i'll have to remember to keep it updated.
- Removing index.php from the url: http://buildwithcraft.com/help/remove-index.php
- Setting up a host name with Digital Ocean: https://www.digitalocean.com/community/tutorials/h...
- Basic My SQL tutorial https://www.digitalocean.com/community/tutorials/a...
- Setting up a DigitalOcean Droplet for ExpressionEngine or Craft: http://clearfirestudios.com/blog/setting-up-a-digi...
- The Absolute Beginners Guide to Setting Up Craft on Mac: http://una.im/2013/08/13/the-absolute-beginners-gu...