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 5

Apache, PHP, MySQL

To launch WordPress we need to have some additional tools. First of all we need to install server which will pending requests from users browsers and sending them responses. WordPress is written in PHP so we also need to install interpreter of this language. Last but not least we need database server because our blog can’t work without it. WordPress is designed to work with MySQL so we need to install this one.

Apache installation

To install apache you need to SSH your instance and run following command:

sudo apt-get update && sudo apt-get install apache2

As I mention in previous part sudo apt-get update will fetch information about software available in Ubuntu repository. The && operator stands as: “if command on the left side will ran successfully, run command on the right side”. Command sudo apt-get install apache2 launch installation of package apache2.

After this operation when you type IP address of your instance to browser address bar, following page should appears. This indicates that our server works. 🙂

ec2_apache

Before we continue it’s worth to look if apache is using mpm-events module instead of mpm-prefork. It’s about performance. My installation has this module installed and activated by default. You can check this with command which lists all loaded modules:

sudo apache2ctl -M

If you don’t have it you can install it manually:

sudo apt-get install apache2-mpm-event
sudo a2enmod mpm_event
sudo service apache2 restart

PHP Installation

In Ubuntu 14.04 repository newest PHP version is 5.5.9. If you want to install newer version like 5.6.x you should use additional repositories.

As I mention at the beginning of the series I want to use PHP5-FPM. It’s mainly because of performance. Here you can read comparison of mod_php and php-fpm. To install run the command:

sudo apt-get install php5-fpm

Żeby taka instalacja PHP chciała współpracować z apachem musimy doinstalować do niego dodatkowy moduł. Ze względu na zawiłości licencyjne, jest on dostępny w repozytorium multiverse, które domyślnie jest wyłączone, więc najpierw musimy je włączyć. O różnicach między repozytoriami można poczytać tutaj. Otwórzmy w ulubionym edytorze tekstowym plik /etc/apt/sources.list (ja korzystam z nano)…

To get this installation to work with apache we should install another apache module. Because of license incompatibilities this module is available in multiverse repository which is disabled by default so we need to enable it. About differences between types of repositories you can read here. Please open file /etc/apt/sources.list in your favorite text editor (I will use nano).

sudo nano /etc/apt/sources.list

We should uncomment appropriate lines (URLs can be different for different regions):

ec2_sources

Now we can run command

sudo apt-get update
sudo apt-get install libapache2-mod-fastcgi

After installation module should be enabled automatically and apache should be restarted.

ec2_fcgi

If that is not true in your case, you can anytime enable module and restart server yourself running following commands:

sudo a2enmod fastcgi
sudo service apache2 restart

CAUTION!
If you have had installed mod_php previously you should turn it off. You can do this similarly:

sudo a2dismod php5
sudo service apache2 restart

Now we’ll configure apache a bit. I assume that only one site will be running on our instance. Configuration provided below is very simple then. Let’s enable another two modules which we’ll need:

sudo a2enmod alias actions
sudo service apache2 restart

Now we should create configuration file which tells apache what should be done with PHP files. All of apache configuration files are stored in /etc/apache2/conf-available directory so we put our file here also. I will name it php5-fpm.conf. We can do this for example with nano:

sudo nano /etc/apache2/conf-available/php5-fpm.conf

Next we put this content to the file:

Couple words of explanation:
IfModule checks if module is active and if yes performs instructions in block.
AddHandler tells apache which action it should take for described files – in this case for files with php extension, action php5-fcgi will be fired
Action defines program to which request will be passed when action is fired – in this case for action php5-fcgi request will be routed to /php5-fcgi path
Alias is used to map paths – in this case we define that path /php5-fcgi from previous line is actually /usr/lib/cgi-bin/php5-fcgi
FastCgiExternalServer indicates how to handle file which we catch with the above lines – in this case it will be run by server pending on unix socket on /var/run/php5-fpm.sock path. This path is defined in php-fpm configuration file. You can find it here: /etc/php5/fpm/pool.d/www.conf. Option -pass-header gives us ability to pass to the script http headers which won’t be passed by default. For example Authorization Header.
Directory defines path in which following settings will apply.
Require all granted gives permission to read localization by all – we need to add this line because otherwise we’ll see “Access denied” instead of results of any PHP script.

Nie pozostało nam nic innego, jak zapisać konfigurację, włączyć ją i przetestować czy wszystko działa. Zapisujemy plik, wychodzimy z edytora, wydajemy polecenia:

There’s nothing else to do except saving configuration. Now we should enable it and test if everything works.

sudo a2enconf php5-fpm
sudo service apache2 reload

Teraz utwórzmy plik php, dzięki któremu przetestujemy to, co do tej pory zrobiliśmy. Domyślna konfiguracja apache-a kieruje nas na ścieżkę /var/www/html, więc tam też utworzymy nasz plik:

Let’s create PHP file so we can test our work so far:

sudo nano /var/www/html/info.php

Save, and navigate to your server in browser. In my case URL will be: http://52.29.70.252/info.php. We should see well known PHP Info page. 🙂

ec2_phpinfo

MySQL Installation

This should be easy. Let’s run MySQL installation. We additionally install php module which will be used for communication with database.

sudo apt-get install mysql-server php5-mysql

During installation we’ll be asked about creating password for root user of our database. We can create it now or leave it blank. We’ll back to this step in a moment.

When installation is complete we should run script which prepares our database to work which means for example creating appropriate directory structure.

sudo mysql_install_db

Last step is to run script which help us secure our server a bit.

sudo mysql_secure_installation

In first step we’ll be asked about current root password. If we set this during installation we need to provide it. If not we just hit enter.

Second step is to set root password. If you already set it you can change it in this step.

Third step gives us ability to delete anonymous user. In other words there will be no possibility to log in to database without having an account. Of course we confirm.

In fourth step we can define if root can or cannot login from another computer than this where server is running. Let’s disable remote login.

W fifth step we can delete “test” database which is created during installation and is available for everyone. Confirm deletion.

Last step is to flush privileges table. Confirm.

So… that’s all! We have almost everything which we need to launch our WordPress. See you in next part. 🙂

Part 1
Part 2
Part 3
Part 4
Part 6
Part 7
Part 8