Install WordPress in AWS EC2 instance
When working with AWS, it’s super easy to setup an EC2 instance to run your WordPress installation. In this tutorial I am showing you how to set it up with services that are mainly available within the free tiers of AWS.
I assume you know how to use AWS in general.
First, log in to your AWS and navigate to EC2. Launch a new instance.
1. Choose an Amazon machine image
Make sure you select the Amazon Linux 2 AMI as it is a better configuration compared to Amazon Linux and has several amazon configurations in comparison to a „normal Ubuntu“ AMI.
2. Choose an Instance Type
For a normal WordPress installation without loads of traffic, the t2.micro instance within the free tier should be enough. The other types may generate costs. For the next step, make sure you navigate to „Configure Instance Details“ and not directly to Launch.
3. Configure Instance Details
For now, you can skip this step and leave the values to its defaults.
4. Add Storage
Skip storage as well. The predefined 8 GiB should be enough so far.
5. Add Tags
Here, click the link to add a name tag and enter a meaningful name. This name will later appear in your instance overview. I will enter „WordPress Tutorial“ here.
6. Configure Security Group
This step is the important one. AWS recommends to create a new security group for this instance and we’re going to do this. Apply the inbound rules as following:
The value for My IP is added automatically. With this configuration we’re allowing to connect to this instance via ssh from our computer and to access the instance via HTTP/HTTPS from everywhere.
Now head to „Review and Launch”
7. Review Instance Launch
You can quickly scan through your values and then click „Launch“ to create this instance. Next, create a new private key and give it a name. Don’t forget to immediately download it. You’ll need it later.
Press „Launch Instances“.
That’s it, you’re done and after the instance was successfully created we can connect to it via SSH.
Connect to Instance
In your terminal, navigate to the folder where your downloaded key pair is stored. In AWS select your initialized instance and click on „Connect“
In the upcoming overlay you’re getting instructions to change the permissions of the key file and copy the command to ssh into the instance.
$ chmod 400 wordpress-tutorial-key.pem$ ssh -i "wordpress-tutorial-key.pem" ec2-user@my-ec-2-instance.com
Install PHP and Apache
First we’ll check if there are any updates needed:
$ sudo yum update -y
Then we install PHP
$ sudo amazon-linux-extras install -y php7.2
Next, install Apache
$ sudo yum install -y httpd
Start the Apache Service
$ sudo systemctl start httpd
And make sure apache is always started when launching the instance
$ sudo systemctl enable httpd
Configure permissions for ec2-user
Add the ec2-user to the apache group:
$ sudo usermod -a -G apache ec2-user
Log out with $ exit and log back in with the ssh command from above.
Next, the permissions for the group must be changed for /var/www:
$ sudo chown -R ec2-user:apache /var/www
The write permissions and also all future directories must be set as well:
$ sudo chmod 2775 /var/www && find /var/www -type d -exec sudo chmod 2775 {} \;
And finally the same for the files:
$ find /var/www -type f -exec sudo chmod 0664 {} \;
And that’s it.
Install the WP CLI
I just copied the commands to download the CLI from https://wp-cli.org/de/#installing:
$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar$ php wp-cli.phar — info$ chmod +x wp-cli.phar$ sudo mv wp-cli.phar /usr/local/bin/wp
Now, check if the cli is available:
$ wp — info
WordPress Installation
Navigate to /var/www/html and run
$ wp core download.
If you’re now typing your IP address of the instance in your browser, you should see the WordPress installation guide.
Create a database
For the database setup you can easily follow the instructions of the AWS documentation here. https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_GettingStarted.CreatingConnecting.MySQL.html#CHAP_GettingStarted.Creating.MySQL
⚠️ But be aware that you should add an initial database in the „Additional Configuration“ section which will not only create a database server but also an empty database.
Create also an wp-mysql security group to control the access for this database with inbound rules to port 3306.
Now you’re done setting up WordPress in EC2. For completion, just follow the usual WordPress instructions.