Configuring Apache WebServer, PHP, And MySQL

The second part of this series on deploying a WordPress site on AWS EC2 will focus squarely on installing and configuring the following your EC2 instance;
Apache Webserver
MySQL
PHP Runtime
The first step in this second part is to SSH into the EC2 instance you created in part 1 using an SSH client.
Note: For this tutorial, I will be using Windows PowerShell. You can use the default terminal using a Linux operating system. If you use third-party software like Mobaxterm or Putty, SSHing into your EC2 instance should be straightforward.
To SSH into your EC2 instance, follow the steps below;
Copy your IP address (Your Elastic IPv4 address) and paste it into a notepad.
Locate the .pem key pair file you downloaded during the first part of this tutorial and copy it. This time, don’t paste it into a notepad.
Go to your terminal and use the “cd” command to move to the folder where your .pem file is saved, typically the Download folder. Then, enter this specified command below.
ssh -i your_keypair_file_name.pem ubuntu@your_ipv4_address
e.g.,
ssh -i my_ec2_keypairfile.pem ubuntu@192.168.0.1
Note: You can copy and paste the command, but don’t forget to change “your_file_name.pem” to the name of your keypair file and your_ipv4_address to your actual 1pv4 elastic IP.
After properly inputting the command above, hit enter and follow the next steps.
When you meet the message “Are you sure you want to continue connecting (yes/no/[fingerprint])?”, type in “yes” to proceed with connection to your EC2 instance.
After you successfully SSH into your EC2 instance, your command line prompt will change to this format;
ubuntu@your_ipv4_address:~$
And that is how you know you have successfully SSHed into your AWS EC2 instance.
The next step at this point is to update your Ubuntu operating system, which is, as a rule of thumb, the first thing to do when you launch an EC2 instance for the first time.
To update your EC2 instance, enter the following commands;
sudo apt update -y
The “-y” is to automatically accept a permission request that will occur during the update process.
Installing Apache Web Server
After the updates are complete, the next step is to install an Apache web server that will handle HTTP requests. Use the command below to install the Apache webserver.
sudo apt install apache2 -y
Installing PHP Runtime and PHP-MySQL Module
After the web server installs completely without any errors (which there should not be), the next step is to install the PHP runtime for WordPress and not just any PHP runtime, the type compatible with Apache.
At the same time, we can install the MySQL module for PHP.
This command below will do that for you.
sudo apt install php libapache2-mod-php php-mysql -y
The Role PHP is Playing
Think of PHP as the go-between in a WordPress website. Apache is like the person taking orders and handling incoming requests. PHP acts as the middleman, taking these orders from Apache and then talking to MySQL to fetch the requested information. Once PHP has the goods (HTML, CSS, JavaScript), it hands them over to Apache, who finally serves them up for you to see in your browser.
Installing MySQL
After the PHP installations are complete, the next step is to install the MySQL database that will hold data for the WordPress website. The command below will do just that.
sudo apt install mysql-server -y
With Apache, PHP, PHP-MySQL module, and MySQL installed, we can configure MySQL.
Configuring MySQL For WordPress Site on EC2
To complete the MySQL configuration, enter the following commands one after the order. Their explanations are in brackets; don’t input that in the terminal.
sudo mysql -u root
This command logs you into MySQL as a root user for the first time.
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'testpassowrd';
Changes the MySQL root user authentication type to mysql_native_password. Use a stronger password.
CREATE USER 'wp_demouser'@localhost IDENTIFIED BY 'testpassword';
Creates a new MySQL user for WordPress. Use a stronger password.
CREATE DATABASE wp_db;
Creates a new MySQL database for WordPress
GRANT ALL PRIVILEGES ON wp_db.* TO 'wp_demouser'@'localhost';
Grants the newly created user all privileges on the newly created MySQL database.
Press “Ctrl + D” to exit MySQL after finishing the last command.
If, for any reason, you want to re-authenticate into MySQL as a root user, use the following command;
sudo mysql -u root -p
After you enter the command above and hit enter, the terminal will allow you to type in your password. After typing in your password, press enter, and you are in MySQL.
Tip: You can change “root” to any user in MySQL to authenticate as that user.
Note: MySQL is case-sensitive.
Every MySQL command must end with a semi-colon because a semi-colon acts as a statement terminator.
If you don’t end each command with a semi-colon and press enter, it won’t execute because MySQL thinks you are not done with the command and continues a new line with a “->” prompt.
Type in the semi-colon “;” and press enter to execute the command where you missed the statement terminator (semi-colon).
That's it. Congratulations—you have done 70% of the work. The remaining 30% of the next tutorial will focus on installing WordPress and configuring Apache’s default virtual host configuration file.



