Posts

How to Build WordPress on AWS

Updated: Mar 25



This article is about how to build a LAMP environment using AWS services and eventually be able to post to WordPress. Let's get started!


AWS

-VPC Name: wp-test-vpc CIDR: 10.0.0.0/16

-Internet Gateway Tag Name: wp-test-igw Key Name Value: wp-test-igw

Once created, select wp-test-igw and select "Attach to VPC" from Actions.

Available VPC: wp-test-vpc

-Root Table Name Tag: wp-test-rt VPC: wp-test-vpc After creation, select wp-test-rt⇒Edit Root⇒Add Root Sender: 0.0.0.0/0 Target: wp-test-igw

-Subnet Name: wp-test-public-subnet-1a VPC: wp-test-vpc CIDR: 10.0.0.0/24

-Security Group Security Group Name: wp-test-sg Description: wp-test-sg VPC: wp-test-vpc

Inbound Rules Type: SSH Protocol: TCP Port Range: 22 Souce: MyIP Type: HTTP Protocol: TCP Port Range: 80 Souce: Custom 0.0.0.0/0

-EC2 Amazon Linux2 t2.micro VPC: wp-test-VPC Subnet: wp-test-public-subnet-1a Auto-assigned public IP: Enabled Strage: Default Tag Key: Name Value: wp-test-ec2 Security Group: wp-test-sg Key Pair: Create a new key pair Key Pair Name: wp-test-key



Now that we have completed the work done in the AWS console, we will install and configure the middleware in the server.

Connect to the server via ssh using any platform such as Command Prompt or Windows Powershell, specifying the key, user name, and public IP as shown below.


ssh -i wp-test.pem ec2-user@{Public IP}



Apache


The first step is to install Apache.

# yum install httpd -y

You are good to go if the last word is "Complete!"


Next, let's boot up Apache.

# systemctl start httpd

You might wanna set up an automatic startup as follows so that the instance will start automatically when you restart it.

# systemctl enable httpd

Then check if it's enabled.

# systemctl is-enabled httpd

Create a VirtualHost with the following settings.

Pass: /etc/httpd/conf.d/wp-test.conf

Server Name: wp-test

Document Root: /var/www/html/wp-test

Accesslog: /var/log/httpd/wp-test-access_log

Errorlog: /var/log/httpd/wp-test-error_log


Create the document root and confirmation files first.

# vi /var/www/html/wp-test/index.html
test
# vi /etc/httpd/conf.d/wp-test.conf
<VirtualHost *:80>
ServerName wp-test
DocumentRoot "/var/www/html/wp-test"
CustomLog "/var/log/httpd/wptest-access_log" combined
ErrorLog "/var/log/httpd/wptest-error_log"
</VirtualHost>

Restart Apache to reflect the changes made to the conf file.

# systemctl restart httpd

Search for the public IP in your browser to see if "test" will be displayed.

And then delete the index.html file.

# rm -f /var/www/html/wp-test/index.html


PHP

Install the epel and remi repositories first since only the 5.4 series is included by default.

# yum info php
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Available Packages
Name : php
Arch : x86_64
Version : 5.4.16
Release : 46.amzn2.0.2
Size : 1.4 M
Repo : amzn2-core/2/x86_64
Summary : PHP scripting language for creating dynamic web sites
URL : http://www.php.net/
License : PHP and Zend and BSD
Description : PHP is an HTML-embedded scripting language. PHP attempts to make it
: easy for developers to write dynamically generated web pages. PHP also
: offers built-in database integration for several commercial and
: non-commercial database management systems, so writing a
: database-enabled webpage with PHP is fairly simple. The most common
: use of PHP coding is probably as a replacement for CGI scripts.
:
: The php package contains the module (often referred to as mod_php)
: which adds support for the PHP language to Apache HTTP Server.

Install epel repository.

# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

Install remi repository.

# yum install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

Check to see if the 7.3 series will fit.

# yum info php --enablerepzo=remi-php73
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
remi-php73 | 3.0 kB 00:00:00
remi-php73/primary_db | 234 kB 00:00:01
261 packages excluded due to repository priority protections
Available Packages
Name : php
Arch : x86_64
Version : 5.4.16
Release : 46.amzn2.0.2
Size : 1.4 M
Repo : amzn2-core/2/x86_64
Summary : PHP scripting language for creating dynamic web sites
URL : http://www.php.net/
License : PHP and Zend and BSD
Description : PHP is an HTML-embedded scripting language. PHP attempts to make it
: easy for developers to write dynamically generated web pages. PHP also
: offers built-in database integration for several commercial and
: non-commercial database management systems, so writing a
: database-enabled webpage with PHP is fairly simple. The most common
: use of PHP coding is probably as a replacement for CGI scripts.
:
: The php package contains the module (often referred to as mod_php)
: which adds support for the PHP language to Apache HTTP Server.

It is still the 5.4 series due to the high priority of the amzn2-core repository.


# less /etc/yum.repos.d/amazon-2-core.repo
[amzn2-core]
name=Amazon Linux 2 core repository
mirrorlist=$awsproto://$amazonlinux.$awsregion.$awsdomain/$releasever/$product/$target/$basearch/mirror.list
priority=10
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-amazon-linux-2
enabled=1
metadata_expire=300
mirrorlist_expire=300
report_instanceid=yes
.....

Since the current priority is 10, set "priority=99" to lower the priority.


# vi /etc/yum.repos.d/amazon-2-core.repo
[amzn2-core]
name=Amazon Linux 2 core repository
mirrorlist=$awsproto://$amazonlinux.$awsregion.$awsdomain/$releasever/$product/$target/$basearch/mirror.list
priority=99
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-amazon-linux-2
enabled=1
metadata_expire=300
mirrorlist_expire=300
report_instanceid=yes
.....

Check the change you made.

# yum info php --enablerepo=remi-php73
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
1 packages excluded due to repository priority protections
Available Packages
Name : php
Arch : x86_64
Version : 7.3.27
Release : 1.el7.remi
Size : 3.2 M
Repo : remi-php73
Summary : PHP scripting language for creating dynamic web sites
URL : http://www.php.net/
License : PHP and Zend and BSD and MIT and ASL 1.0 and NCSA
Description : PHP is an HTML-embedded scripting language. PHP attempts to make it
: easy for developers to write dynamically generated web pages. PHP also
: offers built-in database integration for several commercial and
: non-commercial database management systems, so writing a
: database-enabled webpage with PHP is fairly simple. The most common
: use of PHP coding is probably as a replacement for CGI scripts.
:
: The php package contains the module (often referred to as mod_php)
: which adds support for the PHP language to Apache HTTP Server.

You see it became the 7.3 series.


Then install modules too.

# yum install php php-mbstring php-xml php-xmlrpc php-gd php-pdo php-pecl-mcrypt php-mysqlnd php-pecl-mysql --enablerepo=remi-php73

Create a file named index.php under the document root.

# vi /var/www/html/index.php
<?php
phpinfo();
?>

Restart Apache, then search for the public IP in your browser. If you see a page like the one below, you are good to go.




MySQL


Install MySQL

# yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

If you check the mysql version with the following command, you will find that the series 8 is enabled.


# yum repolist all
repo id repo name status
.....
mysql57-community/x86_64 MySQL 5.7 Community Server disabled
mysql80-community/x86_64 MySQL 8.0 Community Server enabled: 229
.....

We want to install series 5.7, so we disable series 8 and activate series 5.7.

# yum-config-manager --disable mysql80-community
# yum-config-manager --enable mysql57-community

Confirm that series 5.7 is enabled.

# yum repolist all
.....
mysql57-community/x86_64 MySQL 5.7 Community Server enabled: 484
mysql80-community/x86_64 MySQL 8.0 Community Server disabled
.....

Then install series 5.7.

# yum install mysql-community-server

Launch MySQL.

# systemctl start mysqld

As with Apache, enable automatic launch.

# systemctl enable mysqld

Confirm it.

# systemctl is-enabled mysqld
enabled

Before entering MySQL, change your password first.


Confirm your current password.

# grep password /var/log/mysqld.log
[Note] A temporary password is generated for root@localhost: xxxxxxxxx

Password is the "xxxxxxxxx" part.


Change the password.


# mysql_secure_installation
Securing the MySQL server deployment.
 
Enter password for user root:{Old Password}
 
The existing password for the user account root has expired. Please set a new password.
 
New password:{New Password}
 
Re-enter new password:{New Password}
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.
 
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y
 
New password:{New Password}
 
Re-enter new password:{New Password}
 
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
 
#Delete existing user
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
 
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
 
#Prohibit root login remotely
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
 
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
 
#Delete a test database
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.
 
- Removing privileges on test database...
Success.
 
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
 
#Reload privilege table (reflect settings)
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
 
All done!

Finally login to MySQL to create a database for WordPress.


Login.

# mysql -u root -p
Enter password:{The password you just changed in the previous step}

Create a new database.

mysql >database wp_test_db;

Create a new user.

mysql >create user 'wp_user'@'localhost' identified with mysql_native_password by '{Password}';

Grant permissions for the created user (wp_user) to allow operating the database (wp_test_db).

mysql >grant all privileges on wp_test_db.* to wp_user@'localhost';


WordPress


Finally, it is time to install WordPress.

The installation location can be anywhere, but this time we will use the home directory of ec2-user.

# cd /home/ec2-user

Install WordPress packages.

# wget https://ja.wordpress.org/latest-ja.tar.gz

Check if it is properly installed.

# ls
latest-ja.tar.gz

Since the extension "gz" means compressed, use the tar command to expand it.

# tar -zxvf latest-ja.tar.gz

Confirm that it has been expanded by using the ls command.


You can see a directory "wordpress" is created.

# ls
latest-ja.tar.gz wordpress

Move all directories and files under the wordpress directory to the document root.

# mv ./wordpress/* /var/www/html/wp-test/


Edit wp-config-sample.php file as below.

# vi wp-config-sample.php
/** A database name for WordPress */
define( 'DB_NAME', 'wp_test_db' );
 
/** A user name for MySQL database */
define( 'DB_USER', 'wp_user' );
 
/** A password for MySQL database */
define( 'DB_PASSWORD', '-GrrLr5A7ymN' );


When you search for a public IP in your browser, you will see a screen like the one below.


Click "Let's go!" and enter the information you entered in wp-config-sample.php.

You will then be asked to create a wp-config.php file and paste the contents as shown in the image, so follow the instructions.


# vi /var/www/html/wp-test/wp-config.php
{Paste what you copied in the previous step}

Return to the browser and continue.


As the last step, decide on the title of the webpage and user name, store your user name and password, and press "Install WordPress".


Log in to WordPress, post a test content, reflect, and you're done!





This blog post is translated from a blog post written by Koki Yagitani on our Japanese website Beyond Co..