Step by Step Guide to Setup WordPress on Amazon EC2 (AWS) Linux Instance

Last updated
Kuldeep
Crunchify » WordPress Optimization and Tutorials » Step by Step Guide to Setup WordPress on Amazon EC2 (AWS) Linux Instance

Setup WordPress on Amazon EC2

Amazon Elastic Compute Cloud (Amazon EC2) is a cloud service that provides resizable compute capacity in the cloud. It is designed for developers to scale and monitor their web-application better way.

Amazon EC2 provides some of advanced features like elastic IPs, Load Balancing, various platforms like (linux, windows, etc.), scaling, monitoring etc. It is also easy for developers to manage their web-application in better way.

Hosting WordPress on AWS is smart choice because as compare to other VPS provider amazon have low pricing, more features and other related amazing services. Also they charge you per hours your instance run.

So, in this tutorial I’ll show you how you can setup LAMP environment on Linux platform and install WordPress blog manually on Amazon EC2 instance.

This step-by-step guide will help you to manage your host completely which is different than cPanel.

1. Create an AWS Account

First of all you need to create your AWS account. You can sign up by following this link. You will have to provide a credit card or international Debit card and a phone number on which you will be called as part of the online registration verification process as par their terms and policy.

Your credit card or debit card will be charge of minor value and it will be refunded once they verify it. Amazon offers a Free Usage Tier on which we will install WordPress, which is great to explore the services and even host real apps without being charged of single penny.

2. Create an Instance

After registration you have many options available and then you probably have this question in your mind that Which type of instance should I choose?

If you have new blog then you can choose EC2 micro instance which can handle around 200+ real-time traffic.

It has also attractive price structure but if you are migrating your existing blog and having traffic more than thousand per day then you must choose Small instance which can handle that traffic very easily.

To create a new instance, access the AWS Management Console and click the EC2 tab:

  • Choose an AMI in the classic instance wizard:
    • I chose the Basic 64-bit Ubuntu Server Amazon Linux AMI.

Choose an AMI in the classic instance wizard

  • Instance details:
    • Select the Instance Type you want to use. I chose Small (m5a.small).

Select the Instance Type you want to use. I chose Small (m5a.small)

  • Create a new key pair.
    • Enter a name for your key pair (i.e. crunchify) and download your key pair (i.e. crunchify.pem).
  • Select the quick start security group.
  • Launch your instance.

3. SSH into your Instance

Once your instance setup is complete and it shows instance is running, you can ssh into it.

  • First of all, you need to identify the IP Address (public DNS) of your instance:
  • Select the instance in the AWS Management Console.
  • Look for the Public DNS in the instance description (bottom part of the screen).

Use that address (and a path to your .pem file) to ssh into your instance:

ssh ec2-user@ec2-50-17-15-27.compute-1.amazonaws.com -i ~/crunchify.pem

If you are on windows system then you should use Putty for connect as SSH. You can connect with putty by following this article.

If you get an error message about your .pem file permissions being too open, chmod your .pem file as follows:

[ec2-user ~]$ chmod 600 ~/crunchify.pem

In this tutorial you need to perform many shell commands and most of command require root access. So, to avoid this we will prefix these all command with sudo by switching user once for all by this command.

[ec2-user ~]$ sudo su

4. Install the Apache Web Server to run PHP

To install the Apache Web Server, type in terminal:

[ec2-user ~]$ sudo yum -y install python-simplejson     # Install PHP latest version
[ec2-user ~]$ sudo yum update                           # System wide upgrade
[ec2-user ~]$ sudo yum install -y default-jre           # Install Java (just to be safe)
[ec2-user ~]$ sudo yum install httpd                    # Install HTTPD server

Start the Apache Web Server:

[ec2-user ~]$ service httpd start

After setup, to test your Web Server, open a browser and access your web site:

http://ec2-50-17-15-27.compute-1.amazonaws.com

(Use your actual public DNS name). You should see a standard Amazon place holder default page.

5. Install PHP to run WordPress

To install PHP, type in terminal:

[ec2-user ~]$ yum install php php-mysql

After installing php successfully Restart the Apache Web Server:

[ec2-user ~]$ service httpd restart

Create a page to test your PHP installation:

[ec2-user ~]$ cd /var/www/html
[ec2-user ~]$ vi test.php
  • Type i to start the insert mode
  • Type <?php phpinfo() ?>
  • Type :wq to write the file and quit vi

Open a browser and access test.php to test your PHP installation:

http://ec2-50-17-15-27.compute-1.amazonaws.com/test.php

(Use your public DNS name)

6. Install MySQL for adding database

To install MySQL, type:

[ec2-user ~]$ yum install mysql-server

Start MySQL:

[ec2-user ~]$ service mysqld start

Create your “blog” database:

[ec2-user ~]$ mysqladmin -u root create blog

Secure your database:

[ec2-user ~]$ mysql_secure_installation

Answer the wizard questions as follows:

  • Enter current password for root: Press return for none
  • Change Root Password: Y
  • New Password: Enter your new password
  • Remove anonymous user: Y
  • Disallow root login remotely: Y
  • Remove test database and access to it: Y
  • Reload privilege tables now: Y

7. Install WordPress

To install WordPress, type:

[ec2-user ~]$ cd /var/www/html
[ec2-user ~]$ wget http://wordpress.org/latest.tar.gz

To uncompress tar.gz file type:

[ec2-user ~]$ tar -xzvf latest.tar.gzcd

This will uncompress WordPress in its own WordPress directory.

I like having WordPress in a separate directory, but would rather rename it to “blog” if you want to install it to subdomain like “http://your-site.com/blog”:

[ec2-user ~]$ mv wordpress blog

otherwise move all files to parent folder by typing:

[ec2-user ~]$ mv *.* ..

Create the WordPress wp-config.php file:

[ec2-user ~]$ cd blog
[ec2-user ~]$ mv wp-config-sample.php wp-config.php
[ec2-user ~]$ vi wp-config.php
  • Type i to start insert mode.

Modify the database connection parameters as follows:

define(‘DB_NAME’, ‘blog’);
define(‘DB_USER’, ‘root’);
define(‘DB_PASSWORD’, ‘YOUR_PASSWORD’);
define(‘DB_HOST’, ‘localhost’);
  • Press esc one time then
  • Type :wq to write the file and quit vi

Open a Browser and access your blog:

http://ec2-50-17-15-27.compute-1.amazonaws.com/blog (Use your public DNS name).

This should open the WordPress installation configuration process.

TIP: To allow WordPress to use permalinks

WordPress permalinks need to use Apache .htaccess files to work properly, but this is not enabled by default on Amazon Linux. Use this procedure to allow all overrides in the Apache document root.

Open the httpd.conf file with your favorite text editor (such as nano or vim). If you do not have a favorite text editor, nano is much easier for beginners to use.

[ec2-user wordpress]$ sudo vim /etc/httpd/conf/httpd.conf

Find the section that starts with <Directory “/var/www/html“>.

<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 None
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>

Change the AllowOverride None line in the above section to read AllowOverride All.

Note:

There are multiple AllowOverride lines in this file; be sure you change the line in the <Directory "/var/www/html"> section.

AllowOverride All
  • Save the file and exit your text editor.

8. Map IP Address and Domain Name

To use your blog in production, you will have to:

  • Associate an IP address to your instance
  • Map your domain name to that IP address
  • To associate an IP address to your instance:

Steps:

  • In the AWS Management Console, click Elastic IPs (left navigation bar)
  • Click Allocate New Address, and confirm by clicking the “Yes, Allocate” button
  • Right-click the newly allocated IP address and select “Associate” in the popup menu. Select the instance you just created and click “Yes, Associate”
  • To map your domain name to your IP address, you will have to use the tools provided by your domain registrar.
    • If you use GoDaddy, specify NS73.DOMAINCONTROL.COM and NS74.DOMAINCONTROL.COM as the name servers for your domain, and use the DNS Manager to modify the A record and point to your IP address.
  • Once everything is configured and mapped correctly, access the General Settings in the WordPress management console and make sure the WordPress Address and Site Address are specified correctly using your domain name

Other Method: To change your WordPress site URL with the wp-cli

Note the old site URL and the new site URL for your instance. The old site URL is likely the public DNS name for your EC2 instance when you installed WordPress. The new site URL is the current public DNS name for your EC2 instance.

If you are not sure of your old site URL, you can use curl to find it with the following command.

[ec2-user ~]$ curl localhost | grep wp-content

You should see references to your old public DNS name in the output, which will look like this (old site URL in red):

<script type='text/javascript' src= http://ec2-50-17-15-27.compute-1.amazonaws.com/blog/wp-content/themes/twentyfifteen/js/functions.js?ver=20150330'></script>

Download the wp-cli with the following command.

[ec2-user ~]$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Search and replace the old site URL in your WordPress installation with the following command. Substitute the old and new site URLs for your EC2 instance and the path to your WordPress installation (usually /var/www/html or /var/www/html/blog).

[ec2-user ~]$ php wp-cli.phar search-replace 'old_site_url' 'new_site_url' --path=/path/to/wordpress/installation --skip-columns=guid

In a web browser, enter the new site URL of your WordPress blog to verify that the site is working properly again.

That’s it.

You have successfully created LAMP environment and installed WordPress on Amazon EC2. If you get any type of error or got stuck in some of task please let us know by commenting here.

We will try our best to provide solution for your problem.

36 thoughts on “Step by Step Guide to Setup WordPress on Amazon EC2 (AWS) Linux Instance”

  1. Hey kuldeep i successfully installed apache2 server and mysql server and fallow ur all steps but at the end when i hit my host name or my domain name i get error established database connection have u any idea about it?

    Reply
  2. Hi Kuldeep, I ran into trouble when I tried the WordPress installation configuration process, it showed ‘Your server is running PHP version 5.4.16 but WordPress 5.2 requires at least 5.6.20’, I upgraded php to version 8.0.2 and now when I give the domain name followed by wordpress folder, it ends up showing the directory listing of WordPress. What do I do? Thanks.

    Reply
  3. Hi Kuldeep, thanks for the tutorial..
    I ran into trouble when I tried initiating WordPress, I immediately got the following message ‘Your server is running PHP version 5.4.16 but WordPress 5.7.2 requires at least 5.6.20.’. Tired quite a bit of things but am at square one, can you please help? Thanks.

    Reply
  4. Hello

    I have gone through the document and I am getting a error trying to bring up the wp-admin site. The error that I am getting is Error establishing a database connection. I am assuming this is because the DB is probably blank and not intialized…. Any idea on how to correct this?

    Reply
    • Hi Bryan – thanks for posting your concern. Interesting. Can you please share screenshot and error message?

      Reply
  5. Website not secure? I have found that many corporations are blocking insecure websites and thus without a valid ssl certificate, installing wordpress by itself is just 1 step of perhaps a larger process. I have struggled getting a valid certificate working with AWS and if you have any guidance on fixing that, i’m all ears, thanks

    Reply
    • Hi Darrin – thanks for your comment and feedback. I’ve not yet personally installed SSL cert on Amazon AWS instance. Have you try importing let’s encrypt cert?

      I’ll give this a try in a week or so and will update.

      Reply
    • Hi Sanket – I would go with Amazon EC2’s Linux m5.xlarge instance. It’s medium size with 4 vCPU, 16 GB RAM quota.

      Reply
  6. Hi Brother,
    This is an wonderful tutorial for AWS beginners like me. I am new to cloud hosting and not familiar with the tech. words, as confused that whether I will be billed or not, hence asking You for help to provide guidance that if I go with AWS EC2 free tier, hosting 1 WP site with about daily traffic of 1000 unique visitors with an average of 5 minutes per visit, then will I get any bill?
    Any help / suggestions will be very helpful, Once again thank You very much for this wonderful tutorial.

    Reply
  7. Hello after completed all of the steps and going to my instances Public DNS address, I got a message saying WordPress cannot run on an outdated PHP. Any ideas how to fix this?

    Reply
    • Hi Connor –

      I’ve just updated steps above. Could you retry using step-4 and let me know if that fixes your issue. Let me know.

      Reply
      • Hi App Shah,

        So we just finished exporting our Lightsail instance to EC2.

        What is the next best step?

        I assume we just need to make changes to the GoDaddy and CloudFlare settings?

        Please advise.

        Thanks

        CP

        Reply
        • Hi Crebs Park – that’s right. You need to point your Name Server to IP address of your Amazon EC2 instance.

          Hope this helps.

          Reply
      • The same happened to me 🙁
        “Your server is running PHP version 5.3.29 but WordPress 5.4 requires at least 5.6.20.”

        Repeated step 4, answer is: “No packages marked for update”

        The error persists.

        (By the way, the command sudo “yum install -y default-jre” returns “No package default-jre available”)

        Reply
        • Hi Rodrigo – sorry to hear that. Could you please try running command

          root@ip-172-31-22-237:~# apt-get install -y default-jre

          root@ip-172-31-22-237:~# apt-get install -y default-jre
          Reading package lists... Done
          Building dependency tree
          Reading state information... Done
          The following additional packages will be installed:
          at-spi2-core ca-certificates-java default-jre-headless fontconfig-config fonts-dejavu-core fonts-dejavu-extra java-common libatk-bridge2.0-0
          libatk-wrapper-java libatk-wrapper-java-jni libatk1.0-0 libatk1.0-data libatspi2.0-0 libavahi-client3 libavahi-common-data libavahi-common3 libcups2
          libdrm-amdgpu1 libdrm-intel1 libdrm-nouveau2 libdrm-radeon1 libfontconfig1 libfontenc1 libgif7 libgl1 libgl1-mesa-dri libglapi-mesa libglvnd0 libglx-mesa0
          libglx0 libice6 libjpeg-turbo8 libjpeg8 liblcms2-2 libllvm9 libnspr4 libnss3 libpciaccess0 libpcsclite1 libsensors-config libsensors5 libsm6 libvulkan1
          libwayland-client0 libx11-xcb1 libxaw7 libxcb-dri2-0 libxcb-dri3-0 libxcb-glx0 libxcb-present0 libxcb-randr0 libxcb-shape0 libxcb-sync1 libxcomposite1
          libxdamage1 libxfixes3 libxft2 libxi6 libxinerama1 libxkbfile1 libxmu6 libxpm4 libxrandr2 libxrender1 libxshmfence1 libxt6 libxtst6 libxv1 libxxf86dga1
          libxxf86vm1 mesa-vulkan-drivers openjdk-11-jre openjdk-11-jre-headless x11-common x11-utils
          Suggested packages:

          It should work.

          Reply
  8. After setup, to test your Web Server, open a browser and access your web site:

    I get a ERR_CONNECTION_TIMED_OUT.

    Reply
    • Hi there – there won’t be any changes. Above steps works without any issue and will install latest version of PHP.

      Reply
  9. Thank you so much for making the deployment to AWS easier! I noticed that AWS has a best practices whitepaper on how to improve performance through Cloudfront, Amazon S3, Amazon Elasticache and Amazon EFS, and Amazon Aurora. Can these improvement be done after following your instructions OR does this need to be done during initial deployment (using instructions in this blog entry)?

    Reply
  10. You have a typo that might be confusing. You have written

    mysql_secure_Installation

    instead of

    mysql_secure_installation

    with a lower case i.

    Reply

Leave a Comment