top of page
Writer's pictureVaibhav Deshpande

Tutorial to run WordPress within Amazon Linux 2023 AMI Learn to set up & run WordPress within Amazon

Updated: Oct 12, 2023

Learning Objectives:

a) Learn to set up & run WordPress within Amazon Linux 2023 AMI

b) Learn to install & configure MariaDB Server

c) Learn to install, configure & run WordPress

d) Learn to configure Apache server

Prerequisites:

  • You must have an existing AWS Cloud9 EC2 development environment.

  • We strongly recommend that you associate an Elastic IP address (EIP) to the instance you are using to host a WordPress blog. This prevents the public DNS address for your instance from changing and breaking your installation. If you own a domain name and you want to use it for your blog, you can update the DNS record for the domain name to point to your EIP address (for help with this, contact your domain name registrar). You can have one EIP address associated with a running instance at no charge. For more information, see Elastic IP addresses.

  • You have an up to date EC2 instance with all the latest software packages. -y option to install updates without asking for confirmation. If you would like to examine the updates before installing, you can omit this option.

sudo yum update -y

  • While creating instance if you don’t have existing web server security group make sure enable HTTP & HTTPS options

Installation overview:

  • Installing WordPress on your environment's EC2 instance involves the following steps:

1. Installing and configuring MariaDB Server, which is an open-source relational database that stores information for WordPress installations

2. Installing and configuring WordPress, which includes editing the wordpress.conf configuration file

3. Configuring the Apache server that hosts the WordPress site

4. Previewing the WordPress web content that's hosted by the Apache server.

Step 1:

Installing and configuring WordPress

Connect to your instance, and download the WordPress installation package.

  • Download and install these packages using the following command.

sudo dnf install wget php-mysqlnd httpd php-fpm php-mysqli mariadb105-server php-json php php-devel -y

  • You may notice a warning displayed with similar verbiage in the output (the versions may vary over time):

As a best practice we recommend keeping the OS as up-to-date as possible, but you may want to iterate through each version to ensure there are no conflicts in your environment. If installation of the preceding packages noted in step 1 fail, you may need to update to one of the newer releases listed and retry

  • Download the latest WordPress installation package with the wget command. The following command should always download the latest release.

  • Unzip and unarchive the installation package. The installation folder is unzipped to a folder called WordPress.

tar -xzf latest.tar.gz

Step 2:

Secure the database server.

To create a database user and database for your WordPress installation.

Your WordPress installation needs to store information, such as blog posts and user comments, in a database. This procedure helps you create your blog's database and a user that is authorized to read and save information to it.

  • Log in to the database server as the root user. Enter your database root password when prompted; this may be different than your root system password, or it might even be empty if you have not secured your database server.

  • If you have not secured your database server yet, it is important that you do so.

  • The mysql_secure_installation command walks you through the process of setting a root password and removing the insecure features from your installation. Even if you are not planning on using the MariaDB server, we recommend performing this procedure.

  • To secure the MariaDB server

1. Start the database and web server.

sudo systemctl start mariadb

2. Run mysql_secure_installation.

sudo mysql_secure_installation

a When prompted, type a password for the root account.

  • Type the current root password. By default, the root account does not have a password set. Press Enter.

  • Type Y to set a password and type a secure password twice. Make sure to store this password in a safe place.

  • Setting a root password for MariaDB is only the most basic measure for securing your database. When you build or install a database-driven application, you typically create a database service user for that application and avoid using the root account for anything but database administration.

a Type N to unix_socket authentication

b Type Y to remove the anonymous user accounts.

c Type Y to disable the remote root login.

d Type Y to remove the test database.

e Type Y to reload the privilege tables and save your changes.

Step 3:

WordPress installation

  • Your WordPress installation needs to store information, such as blog posts and user comments, in a database. This procedure helps you create your blog's database and a user that is authorized to read and save information to it.

  • Start the database and web server.

sudo systemctl start mariadb httpd

  • Log in to the database server as the root user. Enter your database root password that we created in step 2. If you have not secured your database server yet, it is important that you do so. To secured it follow step 2.

mysql -u root -p

  • Create a user and password for your MySQL database. Your WordPress installation uses these values to communicate with your MySQL database. Enter the following command, substituting a unique username and password.. Replace the red part with your unique username and password.

CREATE USER 'wordpress-user'@'localhost' IDENTIFIED BY 'your_strong_password';

  • Make sure that you create a strong password for your user. Do not use the single quote character ( ' ) in your password, because this will break the preceding command.

  • Create your database. Give your database a descriptive, meaningful name, such as ‘wordpress-db’.

CREATE DATABASE `wordpress-db`;

  • Grant full privileges for your database to the WordPress user that you created earlier.

GRANT ALL PRIVILEGES ON `wordpress-db`.* TO "wordpress-user"@"localhost";

  • Flush the database privileges to pick up all of your changes.

FLUSH PRIVILEGES;

  • Exit the mysql client.

exit


Step 4:

Create and edit the wp-config.php file.

  • The WordPress installation folder contains a sample configuration file called wp-config-sample.php. In this procedure, you copy this file and edit it to fit your specific configuration.

  • Copy the wp-config-sample.php file to a file called wp-config.php. This creates a new configuration file and keeps the original sample file intact as a backup.

cp wordpress/wp-config-sample.php wordpress/wp-config.php

  • Edit the wp-config.php file with your favorite text editor (such as nano or vim) and enter values for your installation. If you do not have a favorite text editor, nano is suitable for beginners.

nano wordpress/wp-config.php

a. Find the line that defines DB_NAME and change database_name_here to the database name that you created in Step 4 of to create a database user and database for your WordPress installation.

b. Find the line that defines DB_USER and change username_here to the database user that you created in Step 3 of to create a database user and database for your WordPress installation.

c. Find the line that defines DB_PASSWORD and change password_here to the strong password that you created in Step 3 of to create a database user and database for your WordPress installation.

d. Find the section called Authentication Unique Keys and Salts. These KEY and SALT values provide a layer of encryption to the browser cookies that WordPress users store on their local machines. Basically, adding long, random values here makes your site more secure.

define('AUTH_KEY', ' #U$$+[RXN8:b^-L 0(WU_+ c+WFkI~c]o]-bHw+)/Aj[wTwSiZ<Qb[mghEXcRh-');

define('SECURE_AUTH_KEY', 'Zsz._P=l/|y.Lq)XjlkwS1y5NJ76E6EJ.AV0pCKZZB,*~*r ?6OP$eJT@;+(ndLg');

define('LOGGED_IN_KEY', 'ju}qwre3V*+8f_zOWf?{LlGsQ]Ye@2Jh^,8x>)Y |;(^[Iw]Pi+LG#A4R?7N`YB3');

define('NONCE_KEY', 'P(g62HeZxEes|LnI^i=H,[XwK9I&[2s|:?0N}VJM%?;v2v]v+;+^9eXUahg@::Cj');

define('AUTH_SALT', 'C$DpB4Hj[JK:?{ql`sRVa:{:7yShy(9A@5wg+`JJVb1fk%_-Bx*M4(qc[Qg%JT!h');

define('SECURE_AUTH_SALT', 'd!uRu#}+q#{f$Z?Z9uFPG.${+S{n~1M&%@~gL>U>NV<zpD-@2-Es7Q1O-bp28EKv');

define('LOGGED_IN_SALT', ';j{00P*owZf)kVD+FVLn-~ >.|Y%Ug4#I^*LVd9QeZ^&XmK|e(76miC+&W&+^0P/');

define('NONCE_SALT', '-97r*V/cgxLmp?Zy4zUU4r99QQ_rGs2LTd%P;|_e1tS)8_B/,.6[=UK<J_y9?JWG');

e. Save the file and exit your text editor. The command is Ctrl+x followed by Y and then Enter.

Step 5:

Install your WordPress files under the Apache document root

  • Now that you've unzipped the installation folder, created a MySQL database and user, and customized the WordPress configuration file, you are ready to copy your installation files to your web server document root so you can run the installation script that completes your installation. The location of these files depends on whether you want your WordPress blog to be available at the actual root of your web server (for example, my.public.dns.amazonaws.com) or in a subdirectory or folder under the root (for example, my.public.dns.amazonaws.com/blog).

  • If you want WordPress to run at your document root, copy the contents of the wordpress installation directory (but not the directory itself) as follows:

sudo cp -r wordpress/* /var/www/html/

  • If you want WordPress to run in an alternative directory under the document root, first create that directory, and then copy the files to it. In this example, WordPress will run from the directory blog:

sudo mkdir /var/www/html/blog

sudo cp -r wordpress/* /var/www/html/blog/

Step 6:

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 suitable for beginners.

sudo vim /etc/httpd/conf/httpd.conf

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

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

  • Save the file and exit your text editor. The command is Esc followed by :wq and Enter.

Step 7:

Install the PHP graphics drawing library on Amazon Linux 2023

  • The GD library for PHP enables you to modify images. Install this library if you need to crop the header image for your blog. The version of phpMyAdmin that you install might require a specific minimum version of this library (for example, version 8.1)

  • Use the following command to install the PHP graphics drawing library on Amazon Linux 2023. For example, if you installed php8.1 from source as part of installing the LAMP stack, this command installs version 8.1 of the PHP graphics drawing library.

sudo dnf install php-gd

  • To verify the installed version, use the following command:

sudo dnf list installed | grep php-gd

  • The following is example output:

Step 8:

Install the PHP graphics drawing library on the Amazon Linux AMI

The GD library for PHP enables you to modify images. Install this library if you need to crop the header image for your blog. The version of phpMyAdmin that you install might require a specific minimum version of this library (for example, version 8.1).

  • To verify which versions are available, use the following command:

dnf list | grep php

  • The following is example lines from the output for the PHP graphics drawing library (version 8.1):

Step 9:

Fix file permissions for the Apache web server

  • Some of the available features in WordPress require write access to the Apache document root (such as uploading media though the Administration screens). If you have not already done so, apply the following group memberships and permissions (as described in greater detail in the LAMP web server tutorial).

1. Grant file ownership of /var/www and its contents to the apache user.

sudo chown -R apache /var/www

2. Grant group ownership of /var/www and its contents to the apache group

sudo chgrp -R apache /var/www

3. Change the directory permissions of /var/www and its subdirectories to add group write permissions and to set the group ID on future subdirectories.

sudo chmod 2775 /var/www

find /var/www -type d -exec sudo chmod 2775 {} \;

4. Recursively change the file permissions of /var/www and its subdirectories.

find /var/www -type f -exec sudo chmod 0644 {} \;

5. Restart the Apache web server to pick up the new group and permissions.

Amazon Linux 2023

sudo systemctl restart httpd

Step 10:

Run the WordPress installation script with Amazon Linux 2023

You are ready to install WordPress. The commands that you use depend on the operating system. The commands in this procedure are for use with Amazon Linux 2023. Use the procedure that follows this one with Amazon Linux 2023 AMI.

  • Use the systemctl command to ensure that the httpd and database services start at every system boot.

sudo systemctl enable httpd && sudo systemctl enable mariadb

  • Verify that the database server is running.

sudo systemctl status mariadb

  • If the database service is not running, start it.

sudo systemctl start mariadb

  • Verify that your Apache web server (httpd) is running.

sudo systemctl status httpd

  • If the httpd service is not running, start it.

sudo systemctl start httpd


Note: If you no longer need this instance make sure to terminate the instance. Click on the drop-down menu beside the actions button. Select the Instance State and click on Terminate. This will terminate your instance.


Was this document helpful? How can we make this document better. Please provide your insights. You can download PDF version for reference.




We provide the best AWS training from Pune, India. For aws certification contact us now.

Recent Posts

See All

13 Comments


Well explained!!

Like

Good content sir

Like

very informative

Like

Yogaraj P
Yogaraj P
Jan 31

Very well explained


Like

nice blog good content

Like
bottom of page