WordPress on AWS EC2 – part 8

Series summary

In this series my goal was to give you information sufficient to launch your own WordPress on AWS EC2 platform. I wanted to keep balance between detailed descriptions of what and why I do something, and length of the posts. Do i made it? I hope that you give me some feedback in comments below. I’ll do my best to take your opinion into account when I’ll be writing next posts.

What we have done?

  • We lunched new EC2 instance
  • We done some basic configuration of our system
  • We talked a bit about security
  • We installed Apache, PHP-FPM and MySQL
  • We talked a bit about WP-CLI, createed new MySQL database and installed WordPress
  • We investigated what and why not working correctly and we solve those issues

In fact it’s quite a lot! 🙂
However I must say, that this series omitted some things. We doesn’t talk about further optimization, backup strategies or versioning. There is also so much more to say in subject of security. If you want to use this system in production environment you should do some more work but this is out of scope of this series so i won’t dive into this topic. Below i present list of terms you can search for. As I wrote in introduction: if you know how to do something better or just have another opinion about something, please leave your comment. I’m constantly learning and improving my skill set so I’ll be happy to look on something from different point of view than mine.

Happy blogging! 🙂

Further reading:

Part 1
Part 2
Part 3
Part 4
Part 5
Part 6
Part 7

WordPress on AWS EC2 – part 7

What else need your WordPress?

WordPress use some additional modules for Apache and PHP. Unfortunately I haven’t found many information about what is actually needed and for what purpose. There are some articles on this subject, but they’re quite old. In this section I’ll show you couple of WordPress features which don’t work in our environment and how to fix it.

Pretty URLs

So let’s begin with turning on preety URLs. This feature help us make our URLs more readable and SEO friendly. In our WordPress Dashboard find tab Settings->Permalinks. There let’s choose one of option we like and save settings. Let’s go back to home page and try to click on post title. Whoops…

In this case the problem is disabled rewrite module in Apache. Let’s enable it:

Much better now. 🙂

ec2_wordpress_permalinks2

wp_mail

In previous part I wrote that our WordPress can’t send emails and I suggest installing plugin which allows us send messages through SMTP. Let’s install it then from dashboard. We can of course do it also using WP-CLI or upload files manually. Before we proceed to plugin installation, let’s install first another PHP module. It’s not necessary now because WordPress can work without it, but some plugins could not.

Okay, now we can install Easy WP SMTP (or any other with similar functionality). Configuration is very straightforward – just like configuration any e-mail client like Outlook or Thunderbird.

ec2_smtp

This plugin have feature which help us examine if everything is correct by sending test email. If you’re using another plugin which doesn’t have this kind of functionality you can for example change password for your user. WordPress should automatically send e-mail with information about password change to e-mail address attached to your account.

Images scaling and cropping

When we upload image to our WordPress media library, they are automatically processed and we can use different image sizes in our posts. Additionally we can use built-in image editor which help us crop image for example. So at least it should work that way but now if we try to put image into post we can use only full image size.

ec2_wordpress_image_size

This is because we don’t have PHP module called imagick. Let’s install it then. We’ll also install module called GD. In fact for WordPress itself imagick is sufficient, but some plugins could use GD for image manipulation.

Now if you upload new image it should be possible to use different image sizes and built-in image editor.

ec_wordpress_image_size2

Additional modules

Some plugins are using module called mcrypt so we can install it by the way:

This set of extensions should be sufficient in most cases.

So we actually done. In last part I’ll summarize everything what we’ve done and make some additional comments.

Part 1
Part 2
Part 3
Part 4
Part 5
Part 6
Part 8

WordPress on AWS EC2 – part 6

WordPress installation

In this part we install WordPress itself on our environment. I’ll use for this purpose WP-CLI. It’s very handy tool for managing WordPress from command line. If  you haven’t hear about WP-CLI I recommend to do some research on it.

In this part I assume that you own domain name for your WordPress. You should change A record for your domain to reflect IP address of your instance. Remember that such change could be visible after couple of hours. If you have any doubts about this process, contact with your service provider.

Apache Virtual Host

We’ll start from writing another piece of configuration. For now our server is configured to serve files from /var/www/html for every request. We can change it with virtual host configuration. We can then route users which are comes to our machine to different places based on some predefined criteria. The most common way of segregating users is domain name which user types in address bar of his browser. We can then add new configuration for our WordPress website. I will use domain wordpress.marchel.it. Let’s create configuration file. You can name it anything you want but file should ends with “.conf”.

sudo nano /etc/apache2/sites-available/wordpress-marchel-it.conf

What is going on here?
VirtualHost opens section where we’ll define all settings about our virtual host. Asterisk symbol means that we check for connections that are coming through any IP address that is attached to our server (in our case there’s only one public IP, but could be more). Colon i separator and number 80 is port number for HTTP service, where our server are pending connections.
ServerName is used for identification that this is the host for which browser is asking. It should be just your domain name.
ServerAlias gives us possibility of defining additional Server Names. In example above I’ve added www prefix so if someone will enter address with this prefix in his browser, server will know that following rules also applies here.
DocumentRoot is used to define root directory for our site. I came up with /var/www/wordpress-marchel-it/public_html. This path of course should exists so let’s create it and give it appropriate permissions.

First two lines obviously creates directories. In third line we changed owner of public_html directory for www-data user. We also assigned this directory to www-data group. Owner of files should be user with which privileges all PHP scripts will be running. In our case this user is www-data. Without this settings we won’t be able for example to install plugins directly from dashboard.

Warning!
If you running more than one site on your server this kind of configuration can by dangerous! In this case you should consider creating user for every application which owns this application files and with which privileges application will be running.

In last line we changed permissions for public_html directory. It’s available for read, write and execute for owner and group.

Directory opens section where we can configure some options for provided path.
Options give us ability to turn on or off some additional server features. In this case we turn off directory listing – if some folder doesn’t contain index file, it won’t be listed and instead of that error 403 will be returned – access forbidden. Additionally we set ability to follow symlinks which will be needed for pretty urls in WordPress.
AllowOverride All give us ability to overwrite server settings with .htaccess files which WordPress use.
Require All Granted permit anyone to have read access to folder which is obviously needed if we want to publish our website.

We can save this file and we’ll back to it in a while.

PHP-CLI and WP-CLI installation

Installed previously PHP version works only with Apache server. We can’t manually run PHP script from terminal. Because WP-CLI is written in PHP we should provide environment in which we can use it. This time there won’t be painful configuration. Just one line:

sudo apt-get install php5-cli

Great! Let’s download WP-CLI following instructions on project website. It’s worth to change directory to your home directory if you are somewhere else (you can do it by running command cd ~/).

Check it it works:

Now it’s good to make WP-CLI globally accessible so we need to give it execution permission and move it to directory which is included in PATH variable.

Now if you run command wp --info you should see the same result.

Creating new MySQL database.

Last step before WordPress installation is to create empty database where our blog will store all of it’s data. Let’s do this then. We should log in as root to our MySQL server. We can do this like this:

This means that we are logging in as user root and we’ll be asked for password which we are previously defined during installation process.

Then we run 3 SQL Queries:

First one creates database named db_name with utf8mb4 charset. You can find very good explanation of character encoding subject here.

Second grants all permissions to manage database called “db_name” to user called “user_name” which authenticates with password “password” . If this user doesn’t exists it will be created.

Third query reload privileges. Just for sure.

Next we exit MySQL console with simple command exit.

Time for WordPress

Finally we came to what this series is all about so to the WordPress installation. As I mention before we use WP-CLI for this purpose. We have already create directory where we want to install our blog, so change our working directory to this folder. In my case it will be:

Now we need to download WordPress files:

If you want to look closer to some WP-CLI command you can add word help at the beginning, for example  wp help core, or wp help core download. Above command will download newest version of WordPress with en_US language pack. If you want to download another language version (Polish for instance) you can add parameter –locale=pl_PL

Now we need to generate wp-config.php file, where among others database credentials will be stored. We should provide at least database name, database user and password. By default for all tables prefix wp_ will be used but we can of course change this by adding appropriate arguments.

Obviously you should provide values which you used for creating database instead of “wordpress” and “password”.

Last thing to do is to run installation process which creates tables in database and set up some values such as site title for us.

In URL filed  we should provide address where our blog will be publicly available. For me it’s wordpress.marchel.it.
title is simply title of our site. If you have spaces in it you should enclose it with double quotes.
admin_user is field where we can set name for our user
admin_password will set up password for our user
admin_email is email address which will be used for administrative purposes

During installation probably you’ve seen this information:

sh: 1: /usr/sbin/sendmail: not found

It means only that software used by WordPress to sending emails is not installed in our system. I won’t cover in this tutorial configuration of sendmail which is default transport for WordPress emails. Instead of that I suggest to use one of available plugins which enable WordPress to send emails through SMTP. I’m using Easy WP SMTP. Configuration of this plugin is very easy – you just need to provide login and password for your email account and SMTP server address along with encryption settings. You are doing exactly the same when you configure Outlook or Thunderbird.

So everything is ready. Now we should enable configuration we prepared in first section of this part and just visit our site. 🙂

ec2_wordpress

Perfect! We just launched our WordPress! In next part I’ll show you that however everything looks good, some features won’t work as it should. We also fix all of the problems of course.

Part 1
Part 2
Part 3
Part 4
Part 5
Part 7
Part 8

WordPress on AWS EC2 – part 1

Introduction

With this post I’m starting the series about deploying WordPress on Amazon AWS EC2. I’ll try to explain step by step what am I doing and why. If you know how to do something better or faster, please leave your comment on specific post. I’ll be happy to learn some new things or best practices. 🙂

Assumptions

Many people probably have set of tools which they like to use. Someone like this Linux distribution, someone else prefer that. Someone like Apache server and someone prefer Nginx. I won’t argue with that and I don’t think that one approach is better than another in every single situation. In many cases it’s just matter of preference. I’ll use following setup:

  • Ubuntu 14.04 Server (in time of writing this text it’s newest LTS version)
  • Apache2
  • PHP-FPM
  • MySQL

I’m assuming that you registered your Amazon AWS account so I’ll proceed straight to the first step – launching your EC2 instance.

Part 2
Part 3
Part 4
Part 5
Part 6
Part 7
Part 8