Blog Post

Tutorial: How to host a WordPress site on Amazon EC2

Warning: This post is highly technical! It is also a long tutorial, so be prepared!

Prerequisites

To complete this tutorial and launch your WordPress site, you’ll first need to have a new AWS EC2 instance, running the Amazon Linux AMI. Once your server is up and running, be sure to write down your Public DNS or Public IP, and then you’re ready to proceed with this tutorial.

Setting up your webserver

Now that your instance is ready to go, you can set up your web server and install WordPress. The instructions below are for users with Mac computers (or any other unix-based system), but if you’re a Windows guy or gal, you can use Putty to access your EC2 server, and once you’re in, the rest of the details will be the same.

Step 1: Connect to your server

Open Terminal, and navigate to the directory where your private key is saved. If it’s in your “Downloads” folder, then your command would look like:

$ cd Downloads/

Next, modify the file’s permissions so that you can use it to access the server:

$ chmod 400 MyKeyName.pem

Now, use the key to access the instance. You’ll need your instance’s Public DNS or Public to access the server:

$ ssh -i MyKeyName.pem ec2-user@public-IP-address

You’ll be told that the key’s authenticity cannot be found and asked if you want to contine; type yes and press enter. You should be connected to your instance now, but if not, retype the ssh command above, and you’ll get in.

Step 2: Apply any updates

Once you’re in, you may need to apply some updates to your server. If updates are available, you’ll see a message like “Run ‘sudo yum update’ to apply all updates.” If you see that message, run the following:

$ sudo yum update -y

Step 3: Install LAMP web server

Now it’s time to install the LAMP web server that will host your website. Do the following:

$ sudo yum install -y httpd24 php56 mysql55-server php56-mysqlnd

Step 4: Start the Apache web server

$ sudo service httpd start

You’ll also want to configure Apache to start automatically whenever the server boots up. (Running this command will not provide any confirmation message or response.)

$ sudo chkconfig httpd on

Step 5: Test your web server

Plug your Public IP or Public DNS in a browser, and you should see the the server’s test page:

Step 6: Set basic file permissions

Add a www group:

$ sudo groupadd www

Add the ec2-user to the www group:

$ sudo usermod -a -G www ec2-user

Now, log out and then log back in again so that the ec2-user picks up the new permissions:

$ sudo exit

$ ssh -i MyKeyName.pem ec2-user@public-IP-address

Now, change the ownership of the web root. Your website’s root folder is located in /var/www, so that’s the directory whose permissions you’ll be modifying. First, change the group ownership of /var/www:

$ sudo chown -R root:www /var/www

Then, change directory permissions for /var/www and its subdirectories to add write permissions for the www group:

$ sudo chmod 2775 /var/www

$ find /var/www -type d -exec sudo chmod 2775 {} \;

Finally, recursively change the file permissions of /var/www and its subdirectories to add group write permissions:

$ find /var/www -type f -exec sudo chmod 0664 {} \;

Step 7: Set up the MySQL server

The next task at hand is to set up the MySQL Server that will host your website’s database:

$ sudo service mysqld start

Then install MySQL:

$ sudo mysql_secure_installation

The MySQL installation is going to ask for some configuration details:

  • Press enter when asked for current root password (default is no password)
  • Type Y and then enter to create a secure password for your root user
  • Type in your password and then press enter
  • Re-enter the password to confirm it and then press enter
  • Type Y and press enter to remove anonymous users
  • Type Y and press enter to disable remote root login
  • Type Y and press enter to remove the test database
  • Type Y and press enter to reload the database privileges table

Finally, you want to use chkconfig to tell MySQL to start automatically when the server boots up:

$ sudo chkconfig mysqld on

Wahoo! You now have a functioning LAMP web server. All that’s left to do is install and set up WordPress, and then you’ll be good to go.

Installing and configuring WordPress

Step 1: Download WordPress

To install WordPress, use the wget command. First, navigate back to your ec2-user‘s home directory, and then download WordPress to it:

$ cd /home/ec2-user

$ wget https://wordpress.org/latest.tar.gz

Now, unzip the file:

$ tar -xzf latest.tar.gz

You’ll now have a wordpress directory that contains all of the WP files. Since your website’s root directory is in /var/www/html/, you’ll have to move the WP files there:

$ mv wordpress/* /var/www/html/

ust to confirm, you can move to the /var/www/html/ directory, run an ls command to list the directory’s contents, and verify that the WordPress files and folders are there:

$ cd /var/www/html

$ ls

Step 2: Set up the MySQL database

Next, create a database user for WordPress to use. Start MySQL:

$ mysql -u root -p

Enter the password you created in the last tutorial for the MySQL root user and press enter.

mysql> CREATE USER 'wordpress-user'@'localhost' IDENTIFIED BY 'your_password';

Next, create a database for WordPress to use:

mysql> CREATE DATABASE `wordpress-db`;

And then grant full database privileges to the wordpress user you created:

mysql> GRANT ALL PRIVILEGES ON `wordpress-db`.* TO "wordpressuser"@"localhost";

Finally, flush MySQL privileges and exit the MySQL client:

mysql> FLUSH PRIVILEGES;

mysql> exit

Step 3: Set up WordPress’ wp-config.php file

First, navigate back to your website directory:

$ cd /var/www/html

The standard WordPress installation includes a sample wp-config-sample.php file, so copy that to the live wp-config.php file:

$ cp wp-config-sample.php wp-config.php

Now you can use the nano editor to configure WordPress:

$ sudo nano wp-config.php

This will open the file editor, and you should replace the details below with the database and user credentials you created in the steps above:

define('DB_NAME', 'wordpress-db');

define('DB_USER', 'wordpress-user');

define('DB_PASSWORD', 'your_password');

ou’ll also want to update WordPress’ Authentication Unique Keys and Salts:


Note: do not use these sample values

Next, you should save the file and then exit the editor:

  • Press ctrl+x
  • Type Y and then press enter

Step 4: Modify your Apache configuration to allow WordPress to use permalinks

For WordPress to properly manage your URL structure, you’ll need to override some Apache default settings. First, pull up the Apache’s httpd.conf file in the nano editor:

$ sudo nano /etc/httpd/conf/httpd.conf

Now, find the section that starts with <Directory "/var/www/html"> and make some adjustments. In particular, change the AllowOverride None to read AllowOverride All.

Make sure you only made this change in the <Directory "/var/www/html"> section, and then save the file and exit:

  • Press ctrl+x
  • Type Y and then press enter

Step 5: Further refine file permissions

You just need to make a few final permission changes so that WordPress can function as intended. Since WordPress uses the apache user to perform certain functions, make sure that apache is granted the proper permissions. First, add apache to the www group:

$ sudo usermod -a -G www apache

Next, change the file ownership of /var/www and its contents to the apache user:

$ sudo chown -R apache /var/www

Then, change the group ownership of /var/www and its contents to the www group:

$ sudo chgrp -R www /var/www

Next, change the directory permission of /var/www and its subdirectories to add group write permissions for current and future directories:

$ sudo chmod 2775 /var/www

$ find /var/www -type d -exec sudo chmod 2775 {} \;

Finally, recursively change the file permissions of /var/www and its subdirectories to include group write permissions:

$ find /var/www -type f -exec sudo chmod 0664 {} \;

Now, all you need to do is restart Apache, and you’re good to go:

$ sudo service httpd restart

That’s it…you’re ready to go!

To finish setting up your blog, just navigate to your Public IP or Public DNS and follow WordPress’ on-screen setup instructions:

Final Thoughts

PHEW! That was a long process, but now you’ve got your own EC2 server for your WordPress site. WordPress can be finicky, and it’s important to keep up with server, WordPress, and plugin updates.

Only made it a few sentences in before we lost you? That’s OK – drop us a line and we’ll lend you a hand in setting up your site.

Related Posts

Write a Comment