Step 1: Connecting to an Instance
Before you can connect, you need to make a note of your server’s IP address (write it down!). Go back to your EC2 Control Panel and find your Public IP address. It’s the area that’s highlighted in the screenshot below…
Next, you will need to connect using the following command (note that you’ll need to replace my placeholder bits with your own)…
$ ssh -i ~/.ssh/your_key_name.pem ec2-user@your_ip_address
Let’s break this down… you are connecting via ssh, you are using the -i
modifier to specify a specific private key, and you are logging in using your username “@” your ip address.
Step 2: Set Your Timezone
To find your timezone using the interactive, step-by-step timezone utility, run the following command:
$ tzselect
Make a note of the timezone code (e.g. America/Los_Angeles
).
Edit your system clock settings…
$ sudo nano /etc/sysconfig/clock
In the clock file, change ZONE="UTC" to your own timezone and then set UTC to false. For example…
1 2 |
ZONE="America/Los_Angeles" UTC=false |
Finally, run a command to create a symlink to your timezone information (be sure to replace YOUR_TIMEZONE below with your own timezone code)!
$ sudo ln -sf /usr/share/zoneinfo/YOUR_TIMEZONE /etc/localtime
Step 3: Install Updates
Just because your server is new doesn’t mean you won’t need some updates. Before we do anything else, let’s make sure everything is up-to-date.
$ sudo yum -y update
You may be asked to confirm a download. If so, type y
to accept and hit enter. Then you just have to wait for the updates to finish.
Step 4: Install Apache, Php, MySQL, etc
Next, we need to install all of your web server packages: Apache, Php, MySQL, ImageMagick, etc. I won’t waste your time explaining what each of these are, they’re mostly self-explanatory-ish – you can Google the more mysterious ones if you are so inclined.
Run the following commands, in-order.
$ sudo yum install -y gcc make
$ sudo yum install -y httpd24 php56
$ sudo yum install -y php56-devel php56-mysqlnd php56-pdo php56-mbstring
$ sudo yum install -y php-pear
$ sudo pear install Log
$ sudo yum install -y pcre-devel
$ sudo yum install -y php56-opcache
$ sudo yum install -y re2c
$ sudo yum install -y mod24_ssl
$ sudo yum install -y memcached
$ sudo yum install -y php56-pecl-memcached
$ sudo yum install -y ImageMagick
$ sudo yum install -y ImageMagick-devel
$ sudo pecl install imagick
Step 5: Configure PHP
Next, we need to ensure that ImageMagick is loaded with PHP. This involves manually editing the php.ini
file.
To edit the file, type:
$ sudo nano /etc/php.ini
Scroll to the very end of the file and add the following line:
extension=imagick.so
Hit Ctrl + X to exit the Nano editor, and be sure to save when it asks you.
Step 6: Start Apache
Now we need to start the Apache web server. Type:
$ sudo service httpd start
If Apache is already running, restart it with:
$ sudo service httpd restart
Step 7: Set Apache to Auto-start
You don’t want to have to manually start Apache every time you have to restart your instance, so let’s configure it to autostart. Type…
$ sudo chkconfig httpd on
Step 8: Test Apache!
Before we go any further, let’s make sure that Apache is running and accessible in a web browser.
First, let’s create a phpinfo file…
$ sudo -i
$ echo "<?php phpinfo();" > /var/www/html/phpinfo.php
Now, type your instance’s ip address into your web browser followed by /phpinfo.php
– if you see a phpinfo page, you’re golden!
Finally, remove the phpinfo.php
file with…
$ rm -f /var/www/html/phpinfo.php
Step 9: Install MySQL
To install and start MySQL on your EC2 instance, type the following:
$ sudo yum install -y mysql-server
$ sudo service mysqld start
Step 10: Configure MySQL
Next we have to configure and secure MySQL. Run the following command to start the MySQL configuration process:
$ sudo mysql_secure_installation
Be sure you carefully read and follow the directions that the installer presents in your command line client.
Finally, let’s make sure that MySQL auto-starts when the instance boots (just like Apache). Type:
$ sudo chkconfig mysqld on
Step 11: Enable .htaccess Overrides
This is the last configuration step. If you want to be able to enable “Pretty URLs” in WordPress, you need to enable overrides in your httpd.conf
. Type the following…
$ sudo nano /etc/httpd/conf/httpd.conf
In here, you need to find the directory block for /var/www/html
. For me, it’s around line 130. It will look like this…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<span class="comment"># Further relax access to the default document root:<br /></span> <span class="keyword operator"><</span>Directory <span class="string">"/var/www/html"</span><span class="keyword operator">></span><span class="keyword operator"><</span>br /<span class="keyword operator">></span> <span class="comment">#<br /></span> <span class="comment"># Possible values for the Options directive are "None", "All",<br /></span> <span class="comment"># or any combination of:<br /></span> <span class="comment"># Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews<br /></span> <span class="comment">#<br /></span> <span class="comment"># Note that "MultiViews" must be named *explicitly* --- "Options All"<br /></span> <span class="comment"># doesn't give it to you.<br /></span> <span class="comment">#<br /></span> <span class="comment"># The Options directive is both complicated and important. Please see<br /></span> <span class="comment"># http://httpd.apache.org/docs/2.4/mod/core.html#options<br /></span> <span class="comment"># for more information.<br /></span> <span class="comment">#<br /></span> Options Indexes FollowSymLinks<span class="keyword operator"><</span>/p<span class="keyword operator">></span> <span class="keyword operator"><</span>p<span class="keyword operator">></span><span class="comment">#<br /></span> <span class="comment"># AllowOverride controls what directives may be placed in .htaccess files.<br /></span> <span class="comment"># It can be "All", "None", or any combination of the keywords:<br /></span> <span class="comment"># Options FileInfo AuthConfig Limit<br /></span> <span class="comment">#<br /></span> AllowOverride None<span class="keyword operator"><</span>/p<span class="keyword operator">></span> <span class="keyword operator"><</span>p<span class="keyword operator">></span><span class="comment">#<br /></span> <span class="comment"># Controls who can get stuff from this server.<br /></span> <span class="comment">#<br /></span> Require all granted<span class="keyword operator"><</span>br /<span class="keyword operator">></span> |
See the line in there for AllowOverride None
?
Simply change it to AllowOverride All
.
As with Nano before, press Ctrl+X to exit, and follow the directions to save the file.
Next, we just need to restart Apache one more time so that your htaccess files are being used. Type this at the command prompt:
$ sudo service httpd restart
Optional: Configure Virtual Hosts
If you want to host multiple websites on a single instance, now would be a good time to set up your vhosts (or virtual hosts), which enable this.
Click here for a guide to setting up vhosts on your AWS instance →
Deploying WordPress!
Congratulations, your server is fully configured! Now, we just need to connect to connect to it with our database software and install WordPress.
Connecting to MySQL
We need to do two things right now… connect to the MySQL server and create a new database for our WordPress install.
This part of the tutorial is client software agnostic, but I’m a huge fan of Sequel Pro for OSX, which is what I use on a day-to-day basis. If you’re on Windows or Linux, you’ll want to use MySQL Workbench instead.
To create the connection…
- Start by creating a new connection in your database client.
- For your connection method, you want to select the “SSH” option.
- For your SSH hostname, enter your instances public ip address.
- For SSH user, enter
ec2-user
(remember, that is always your default user). - For authentication, you will need to point your client to the private key you downloaded earlier (this is the file that ends in
.pem
). - Your MySQL host should be
127.0.0.1
and your port is3306
- Your MySQL username should be
root
. - Your MySQL password is whatever you personally specified earlier (when we ran the command: $
sudo mysql_secure_installation
)
With that done, you should now be able to connect to your MySQL server! Go ahead, try it.
Creating a New Database
Now that we’re connected, go ahead and create a new database/schema so that we can install WordPress.
You can name the database whatever you like (I like to prefix mine with wp_ to let me know it’s a WordPress schema), but your encoding should be utf8
and collation should be utf8_general_ci
.
Creating a New MySQL User
Next, we’ll want to create a new MySQL user whose access is limited to only the database you just created, and with limited privileges.
Why? Because, if anyone malicious were to gain access to your site’s file system, you wouldn’t want your root password readily visible in your wp-config.php
file. This is best-practices stuff.
You are free to skip this step and move on with your root login, but I reallydon’t recommend it.
The process is different depending on your client. In SequelPro, you will find the user options under Database > User Accounts. In MySQL Workbench, you’ll find this in the toolbar on the left under “Users and Privileges”.
In either case, you want to implement the following for this user:
- Only allow connections from
localhost
or127.0.0.1
- Only grant access to the single schema you just created
- Give the user full schema privileges
- Do not give the user any administrative privileges
And don’t forget to save that password someone safe, like an encrypted password locker (1Password or LastPass are good choices). I recommend using only extra-long (30+ character) machine-generated passwords.
Deploying WordPress
At this point, you have two options… you can copy WordPress onto your server via SFTP or you can deploy it via command line.
Uploading via SFTP
If you want to copy files via SFTP, you would configure your FTP client very closely to how we configured the MySQL client:
- Select SFTP as the connection method (sometimes called “Secure FTP” or “FTP over SSH”)
- Connect using your instances public IP address
- For user, enter
ec2-user
- For authentication, select your private key (the one that ends in
.pem
Your site files should be uploaded to /var/www/html
– so start uploading and knock yourself out!
Downloading via Command-line
I prefer this method. It’s faster and there’s no waiting for files to upload. Just enter the following commands…
$ cd /var/www/html
$ wget http://wordpress.org/latest.tar.gz
$ tar -xzvf latest.tar.gz
$ mv wordpress/* .
$ rm -rf wordpress
$ rm -f latest.tar.gz
Fix WordPress Permissions
At this point, you may need to fix your WordPress permissions to ensure that everything works as expected (media uploads, plugin installs and updates, etc). Run the following commands to repair your WordPress permissions…
First, we want to change the ownership of the html
folder…
$ sudo -i
$ cd /var/www
$ chown ec2-user:apache html
Next, we need to recursively change the ownership and permissions of WordPress’s files and folders…
$ cd /var/www/html
$ find . -type d -exec chmod 0755 {} \;
$ find . -type f -exec chmod 0644 {} \;
$ chown -R ec2-user:apache .
$ chmod -R g+w .
$ chmod g+s .
The idea here is this: by giving the ec2-user
user ownership, you can use SFTP to modify your files (since there are virtually no SFTP clients that support sudo
– not even Transmit); And by granting apache
group access, you can use media uploads, auto-updates, etc.
Wrap It Up!
Finally, visit your public ip address in your browser to complete that famoud 5-minute WordPress install! Don’t forget to use that special MySQL user we set up, when you do.
After that, you’re done. Mazel tov! You are now running a WordPress AWS website on EC2!