Longstreet Labs Blog

Tutorial: How to host a Wordpress site on Amazon EC2

Posted by Daniel Waltzer on Oct 14, 2016 3:27:36 PM

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. For step-by-step instructions on how to do that, follow the instructions in this tutorial. 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 Web Server

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 sever

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 Dowloads/

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: 

Apache 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:

$ 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/

Just 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 above for the MySQL root user and press enter.

Now, create the wordpress user:

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 "wordpress-user"@"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');

You'll also want to update Wordpress' Authentication Unique Keys and Salts:

aws8.png

Note: do NOT use these values...they are strictly examples!

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

  • Press ctrl+x
  • Type 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. It should look like this:

<Directory "/var/www/html">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

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 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 owernship of /var/www and its contents to the www group:

$ sudo chgrp -R www /var/www

Next, change the directory premission of /var/www and its subdectories 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:

Wordpress installation screen

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.

Topics: Wordpress, Tutorials, AWS

The blog for businesses with customers

Subscribe to Email Updates

Recent Posts