By the end of this chapter, the reader should be able to:
WordPress is a very popular open-source website builder that is also commonly used as an example to demonstrate cloud application deployments. We are going to follow the trend and use it as an example as well, with special emphasis on the architecture design approach that we described in chapter 3. The application is a very good example because its implementation includes several architectural patterns, and there are different ways to deploy it on the cloud, which allows us to compare the different options.
Since the application is already implemented, we don't need to decide on the architectural patterns. Still, it's important to understand the implementation architecture in order to make suitable decisions regarding its deployment on the cloud.
WordPress implements the multi-layered pattern in which the presentation layer is implemented using a web interface, the application layer is implemented using server-side PHP code, and the data-layer is implemented using a relational database (either MySql or MariaDB). In addition to that, there are two client-server pattern implementations within the application.
A client-server web pattern is implemented between the presentation-layer and the application-layer where the presentation-layer is accessible through the web-client (i.e., the browser), and the application-layer is contained in and accessible through a web server (e.g., Apache webserver). On the other hand, a client-server database pattern is implemented between the data-layer that includes a database server (e.g., MySql Server) and the application-layer which needs to access the database as a client. Figure 4.1 below summarizes the architectural patterns as well as the deployment plan that we are going to follow.
Figure 4.1: WordPress architectural patterns and deployment plan
As shown in the figure, the presentation layer doesn't need any deployment effort from our side since it's a web application, so the presentation layer would be generated automatically by the PHP code, and the web browser is (most likely) already installed on the client machine since it's already a standard.
For the application layer, we will go with the most basic deployment option (IaaS). So, we are going to provision a compute engine, and we'll be responsible for the installation and the management of both the environment and the application that we're going to deploy (including updating, maintaining, and securing).
As for the environment deployment, we need to install an Apache webserver to host the PHP application as well as the PHP interpreter. In addition to that, we'll need to install the MySql client to access the database server.
For the data layer, we can do the same thing and use the IaaS deployment option by provisioning a VM instance and installing the MySql server on it. However, since the provider already offers what we need as a PaaS, we'll go with this option instead since it would make our life easier if we delegate the responsibility of the environment to the provider, which would allow us to save our resources to focus on the application.
So, for the environment deployment, all that we need to do is to provision a cloud SQL instance that already has the MySql server environment installed in it. For the application deployment, we need to create a database(schema) within the server that will hold the tables needed for the application. In addition to that, we need to create a user with proper permissions to be used by the application layer to access the server.
In the coming sections, we'll execute the deployment plan step by step.
As mentioned in the deployment plan, the decision was to go with the PaaS deployment option and use the cloud SQL service.
The environment deployment here would involve provisioning the Cloud SQL instance.
Figure 4.2: Cloud SQL service in the navigation menu
Figure 4.3: Create or Migrate Cloud SQL instances dialog
Figure 4.4: Choose MySQL engine
Figure 4.5: SQL instance info
Figure 4.6: Instance public IP and connection name
Since the environment deployment is already taken care of, thanks to the PaaS deployment option, we only need to worry about the application deployment. Here, we'll need to create a database schema for the application and a user to be used in the application layer.
Figure 4.7: Create a new database schema
Figure 4.8: Set the new DB name
Figure 4.9: The new 'wordpress' DB now appears under the 'Databases' list
Figure 4.10: DB users list
Figure 4.11: New DB user settings
Figure 4.12: wordpress user listed under the instance 'Users'
Finally, we need to allow access to this instance from outside. For now, we'll open the instance to be accessible from anywhere, but we can change this later for better security after we are done with the installations.
Figure 4.13: Cloud SQL Connections page
Figure 4.14: Allowing any IP to access the DB instance
Now, we are ready for the deployment of the application layer. It would be a good idea to stop this instance to avoid being charged until we need it after we have the client instance ready.
Remember when we mentioned that the term server is used to refer to both the server software as well as the machine that hosts it. We'll first prepare/provision the machine (the virtual machine in this case), then we'll install the webserver software on it.
Figure 4.15: Allow HTTP traffic on the webserver instance
Run the following command to connect to SQL instance through the client we just installed
$ mysql -h <ip-of-the-sql-instance> -u <db-user-name> --password=<db-user-password>
use wordpress;
to access the database we created for the application. The prompt should be updated to show the database name.
MySQL [(none)]> use wordpress;
MySQL [(wordpress)]>
The command exit
ends the connection and stops the client.MySQL
[(wordpress)]> exit;
Figure 4.18: Testing the database client
As shown in figure 4.18 above, the client was installed successfully, the user credentials work, and the application database is accessible. So, the database client is ready; the final step in the environment deployment is to install PHP and its dependencies.
Let's start by installing wget,a free tool commonly used to retrieve files using web protocols (hence the name web-get) because we'll need it later.
$ sudo apt install wget
According to wordpress documentation here the latest version needs PHP7.4 or higher. Normally the command sudo apt -y install php7.4 would be sufficient to install it if the package existed in the local system. However, version 7.3 is the highest version available by default, so we needed to find a way to download the package before installing it. After a google search, I found this link with helpful instructions https://computingforgeeks.com/how-to-install-latest-php-on-debian/ so you can go there and follow the instructions up to step 3.
To test the installation, we can issue the following command which, executes the command line interface of PHP and prints the version.
$php -v
If the installation was successful, the output should look similar to the image shown in figure 4.19 below.
Figure 4.19: Testing PHP installation
$ sudo apt install php7.4-mysql
$ sudo apt install libapache2-mod-php7.4
$ sudo apt install php7.4-gdNow the environment is ready, and we are good to move to the application deployment.
Using wgetwe can retrieve the latest version of WordPress then extract it from the archive by issuing the following commands:
$ wget https://wordpress.org/latest.tar.gz
$ tar xzvf latest.tar.gz
If the download and extraction were successful, you should see a directory named 'wordpress' in your home directory.
The code inside wordpress knows how to access the database by reading the configuration set in a file called 'web-config.php', the directory that we just downloaded contains a sample for this configuration file named 'web-config-sample.php' as you can see in figure 4.20 below.
Figure 4.20: Sample Configuration file
We need to copy this file to a file with the name to web-config.php and edit it to reflect the settings of the database that we created. It's better not to rename the sample file so that we can have it as a back-up. the following two commands would copy the file and open it in the 'vi' editor
$ cp wp-config-sample.php wp-config.php
$ vi wp-config.php
Inside the vi editor, hit the letter 'i' on the keyboard to be able to make changes; this will start the 'insert' mode, which will be indicated at the bottom of the screen as highlighted by the red rectangle in figure 4.21 below:
Figure 4.21:The 'Insert' mode of the 'vi' editor
Using the arrow keys in the keyboard, navigate to the lines highlighted in figure 4.22 below and change the value for the database, name, username, password, and host IP to the values we had when we created the cloud SQL instance.
Figure 4.22: DB configurations for wordpress
When you're done editing, press 'Esc' exit from the insert mode, then type ':wq' to write (i.e., save) and quit the editor. (tip: you can quit the editor without saving by typing ':q!' in case you needed to avoid overwriting)
Notice that we still didn't perform the deployment yet. We were just preparing the application to be deployed. To actually deploy it to the webserver, we need to copy the entire wordpress directory to /var/www/html/ where the webserver can access it.
One last step before we test the installation is to let's restart the instance using the command
$ sudo reboot
Figure 4.23: WordPress Installation page
http://<vm-external-ip>
will open index.php, which is the home page of your site.As mentioned before, the current security settings are not secure enough. We need to enhance the application's security by allowing access to the database only through the webserver instance. First, let's give the webserver instance a static IP to avoid getting different external IPs every time we restart the instance. Follow the instructions here to promote the instance's IP to a static IP.Then go to the connections of the database instance and change the value of the network's CIDR citation to the value of the static IP of the webserver instance.
In this chapter, we deployed a real open-source web application using the IaaS and PaaS deployment options. We started by creating the deployment plan, then followed the plan step by step until we had the application up and running. The exercise was an opportunity to have a general sense on how cloud applications are deployed and to understand the difference between IaaS and PaaS as deployment options.
[GCP Screenshots] "Google and the Google logo are registered trademarks of Google LLC, used with permission."Unless otherwise stated, all Images in this chapter created by the author Shaimaa Ali using either MS PowerPoint or MS Visio or both.
© Shaimaa Ali 2022