Creating an AMI of WordPress and MySQL
In this article, I am going to create an Amazon Machine Image (AMI) of WordPress and MySQL. This setup is typically called LAMP (Linux Apache MySQL PHP).
I have used Amazon Linux AMI for my base instance. I will launch two instances using the same AMI and then, I will install WordPress on the top of 1st one and MySQL on the top of the second one.
The very first step is to launch the two instances.
Now, let’s create a setup for WordPress first. Log in into the instance in which you want to set up WordPress.
Then, log in through root user using “sudo su - root” command.
And in the next step, we have to download the following software using yum: httpd, php, mysql-server, and php-mysqlnd. I have also mentioned the compatible versions of the above software:
In the next step, we have to start the services of httpd, and enable the services permanently. And then, we will download the WordPress tar file from the link (in the screenshot):
Now, we have to extract the files from the downloaded tar file. And then, the content of the WordPress directory is to be copied inside /var/www/html/ directory.
And thus, WordPress has been configured:
Now, let us configure the Database in the other instance. For this, we have to login inside our instance first. Then, we have to change your user to the root user. And finally, install some similar software using yum:
In the next step, we have to start the mysqld services and make it permanent:
Then, we have to set up the password for the root user to login into the mysql. I have used the above command to do so:
“mysqladmin -u root password [your_password]
And in the next step, I have created a database, a user, set the password for that user and granted permissions to that user so that it can use the created as well as the other databases.
- user: this is the username I created
- pass: the password for the user
- mydatabase: database name
- Here, user@% means that the ‘user’ named user can be accessed from any host.
Then, we have to provide the bind-address. Here, I bind to 0.0.0.0 which means I can connect to the server using all existing accounts. For simplicity, you can understand the bind-address as: it tells MySQL on which networks it can listen for connections.
MySQL Bind-Address - how/from where MySQL listens for connections
We have to add the bind-address in the configuration file of mysql. For this, we have to go to:
“vi /etc/my.cnf”
And in the above file, you just have to add the following line:
Then restart the MySQL server and it will be configured successfully:
Now, let’s connect the WordPress server with the database server:
And WordPress is successfully connected to the database.
If you just want to create an AMI then stop here only. Then, create ‘snapshots’ from the volumes of these two instances. And from those snapshots, you can create the AMIs of the respective applications.
However, if you want to save the complete setup and launch the WordPress, then you need to copy the code from the above screenshot and paste it in a file named wp-config.php. You have to create this file inside /var/www/html directory.
This is a one-time process.
Here is the reason why we have to copy the code manually: WordPress creates and uses an Apache user behind the scene to do certain things. Apache is a service user and it is the one that copies the code inside the wp-config file at /var/www/html location by creating it first. But here, we have not given any permissions to the apache user to do any changes in the /var/www/html location. Because, it is not always recommended to give permissions to the apache user.
Even though if you want to give permissions to the apache user, just run the below code while creating the AMI inside the WordPress instance:
"chown -R apache:apache /var/www/html/"
And in case, if any issue of SELinux appears, just run the following command:
"chcon -t httpd_sys_rw_content_t /var/www/html/ -R"
After doing the same, these are the results:
I hope you liked the above article. Thanks for reading! ;)
Have a good day!!
Infoscion | DevOps - Cloud enthusiast
4 年Superb
Senior DevOps Engineer at Moveworks
4 年Great job ? Keep learning !!
Actively Looking for Change | DevOps Engineer | CKA | AWS SAA-C02 | RHCSA | RHCE | Azure | Docker | Kubernetes | Terraform | Ansible
4 年Great work brother!