I’m about to be released into the wild as a free roaming web developer. I won’t have the company hosting anymore and it’s damn well about time I got my own shit sorted. After a little reading I have decided to give Amazon Web Services a try. So far I am loving it.
I have oooooooodles of control. It’s super nerdy and more importantly, it’s super fast. Seriously – I had fun.
When I first set this up i tried following some third party tutorials to get a wordpress server setup and running on Amazon Web Services. After some trial and error I found the proper documentation and everything when much more smoothly (I know right)! The AWS docs are very detailed and easy to follow.
So, here’s my advice:
- Follow these instructions.
- Go through the setup, then follow all the getting started section.
- Once you have an EC2 instance all set up move onto the LAMPserver section.
- Then take a look at the wordpress documentation. It details how to setup your database through the command line.
- Finally follow these steps to attach your domain properly.
NB: During the Lamp Server Setup
Point 4 of the permissions setup section in the instructions above tells you to run this command:
sudo chown -R root:www /var/www. I found these permissions did not allow me to use the WordPress dash to properly update plugins etc. Do this instead:
sudo chown -R apache:apache /var/wwwand add your user to the apache group like this:
sudo usermod -a -G apache ec2-user.
And you’re done!
Snapshot your volume so you have a backup of the drive you can easily restore.
I’m really impressed and I can’t wait for an excuse to use more of it’s potential.
Using vhosts to run multiple sites on one instance
If a vhost conf file doesn’t exist, create one:
sudo vim /etc/httpd/conf.d/vhost.conf
Then set up as follows for multiple domains and/or subdomains:
RewriteRule ^/.* /var/www/html/domain.co.uk/index.html
RewriteRule ^(/sub/.*) /var/www/html/domain.co.uk$1
Restart apache to test. So far so good!
How to Enable Custom Permalink Structure
I like to setup my wordpress sites using %postname% for the permalinks. To enable the .htaccess edits required to make this work you’ll need ot make some changes to the default /etc/httpd/conf/httpd.conf.
AllowOverride None and change to
AllowOverride All. This was in 2 places for me. Once updated, restart apache and the permalinks should work perfectly 🙂
Setting up a git server
- Follow these instructions to install git and it’s dependencies.
- Then continue with this – Worked for me without the config fix 🙂
Stopping MYSQL from crashing
I’ve been using the setup above for a while now and I’ve been really happy with the speed and control. Everything was great until suddenly MySQL started crashing and my sites showed error pages becuae there were no databases! arg. The first time i noticed I saw it wasn’t running and just started mysql again. But, the next day it had crashed again! And this time it wouldn’t start. I had to reboot the whole instance to fix it. To fix it permenetly you need to set up swap space (nearly kicked myself). The size of your swap space should be 1MB for every 1G of RAM your server has. So adjust the 1st command accordingly.
$ sudo dd if=/dev/zero of=/swapfile bs=1M count=1024
$ sudo mkswap /swapfile
$ sudo chmod 600 /swapfile
$ sudo swapon /swapfile
$ sudo vim /etc/fstab
Add this line to fstab:
swap /swapfile swap defaults 0 0
This is now my first step when conecting to any new instance.
I’m also thinking it will be a good idea to put all my mysql data on a sepearte volume. See: https://forums.aws.amazon.com/thread.jspa?messageID=544552
All my attempts to use xfs format type failed. I used
sudo mkfs -t ext4 /dev/xvdf instead. This was also useful for reference http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-using-volumes.html
Another handy mysql command is:
mysqldump -u user -p database_name > database_name.sql – Once this auto scaling is done with i want to learn how i can automate this backup.
The swap space above made a big performance difference, but I’m quickly learning that 1 micro instance isn’t enough to run multiple dynamic websites, and I’m not entirely sure it will even do a good job of 1. My CPU keeps topping out and i’m concerned the server will fall over if my traffic peaks. So… I’ve done some more research. You can now get t2 micro instances which store CPU credits when your server isn’t doing much so these perform better thn the t1 i set up originally. I also need to look at auto scaling. Effectiviely I’ll have a cluster of free t2.micro instances working together to split the load on my site.
Using Auto Scaling
My plan for tommorrow is to give this guide part 1 and part 2 a read.
I’ve also happened across this article which is more specific to using AutoScaling with WordPress. I want to give this a good read before i continue.
The very first part of the procedure is to get an X.509 Security Certificate, but the interface for this in the console doesn’t quite match up with the screenshots. You need to provide your own certificate for this step, but it can be a self signed one! This is something I haven’t tried before, but I came across the free option when amazon directed me to this directory of Certificate Authorities. I decided to try this out using Simple Authority. This program lets you create self signed certificates wich are .p12 and .cert filetypes. I then used
openssl commands to convert them into the .pem formats I need for AWS.
openssl pkcs12 -nocerts -in *.p12 -out pk-whatever.pem
openssl pkcs12 -nokeys -clcerts -in *.p12 -out cert-whatever.pem
NB: I use Zsh instead of Bash, so i put the export path info in .zprofile instead of .bash_profile.
The guide then goes on to show you how to create your basic instance and your own AMI. All fine. But it suggests we use elastic IP’s. I’ve read here that this might not be such a good idea. So I’m going ot look into load balancers and find out what that’s all about.
Don’t give servers static/elastic IPs. For a typical web application, you should put things behind a load balancer, and balance them between AZs. There are a few cases where Elastic IPs will probably need to be used, but in order to make best use of auto-scaling you’ll want to use a load balancer instad of giving every instance their own unique IP.
Now after all of this, Mail sending really needs some attention.
1st note: Spamhaus.org lists amazons ec2 ip ranges as dynamic, thus many mail servers will reject emails from it. (Including other people using this howto.) But Spamhaus has a simple web page to remove ips, which they link to in rejection messages. Simple look in your logs, click on the link on follow the instructions: basically fill in your ip, email and state its for a mail server. Then Spamhaus will remove your IP from their database.
I’ve done some reading and it looks like I should setup postfix for my mailserver. For me it won’t be in heavy use as it’s just used to run the enquiry forms on my website. Anyway here are some links I’ll be looking into more:
plus the useful instructions i used for my localhost which may come in handy too…
And then what about a CDN?
I believe you can set this up too using Amazon’s CloudFront service. Maybe even in the free tier. Gonna test out my settings before making more notes.