How to install a WordPress site on ubuntu 20.04 on Google Cloud Platform

Diana Moraa
9 min readMar 30, 2022

--

Google cloud is one of the most sophisticated, reliable, and fastest cloud infrastructures. Website hosting on the Google cloud platform is a great idea because it ensures:

Uptime: With a promise of a 99.99% uptime SLA, and GCP guarantees high levels of availability. Resources span multiple zones and regions to avert any possibility of failure during disasters. In terms of performance, it’s hard to beat cloud hosting as dynamic requests are processed with consistently fast load times. This makes it ideal for websites that receive a high volume of traffic.

Speed: Cloud hosting allows websites to run faster. Also deploying cloud servers is very quick with the servers being available within seconds of launch.

Scalability: Google cloud engine servers are highly scalable and can handle unexpected traffic spikes and let you downgrade, or upgrade your server size without changing.

In this demo, I will deploy a LAMP (Linux, Apache, MYSQL, and PHP) stack on a Google VM Instance and install a WordPress application over there. Follow with me, lets’s get started:

STEP 1:LAUNCH A COMPUTE ENGINE VM INSTANCE IN THE CLOUD CONSOLE.

Go to the Navigation Menu > Compute Engine > VM Instances.

Then go to the create instance button to create your instance.

On the create an instance screen, choose the server configurations and other settings like server name of choice, server location, machine type, the boot disk, network interfaces, and many more.

Select the server location closest to the target audience. The server location is divided into 2 parts: Region and zone, where the region is the geographical location of your server and zone is the deployment area of a server within a region. It is best practice to choose the location nearest to users.

For machine configuration, when you choose a machine family, series and machine type, there are 4 options in the machine family section ,and am selecting General-purpose and E2 series with e2-micro machine type because it’s cost-effective for the purposes of this demo.

Next, in the boot disk section, choose an operating system you want to install on the google cloud server. I'm going with Ubuntu version 20.04, you can always change to your desired OS. For the boot disk type, I chose the standard persistent disk.

Check both the firewall rules i.e. allow HTTP/HTTPS traffic to allow specific networks from the internet. Then create the new server

STEP 2: DEPLOYING THE LAMP STACK(LINUX, APACHE2,MYSQL AND PHP)

After successfully launching the cloud server, it’s time to deploy the LAMP stack over there. Launch the server ssh by clicking SSH.

To update the system run:

sudo apt update

To upgrade the list of packages, run:

sudo apt upgrade -y

Install apache2 on the server

sudo apt install apache2

After the installation, copy the IP address of the server to confirm if the apache2 installation was successful. The apache2 default page was installed successfully as below:

To create an MYSQL database to store and manage data for my site and user for the WordPress site: run;

sudo apt install mysql-server

When the installation is complete, it is recommended to run a security script that comes pre-installed with MYSQL. The script will remove insecure default settings and lock down access to the database system. Run

sudo mysql_secure_installation

Then it will ask you to configure the validate the password plugin.

NOTE: Enabling this feature means that the passwords which don't match the specified criteria will be rejected by MYSQL with an error. But it is important because it is recommended to use strong unique passwords for database credentials.

Log in to the MYSQL console by typing

sudo mysql

Once you have access the mysql, create a database that WordPress will use:

Sign in to mysql using the following command

mysql -u root -p

Enter the administrative password entered during the mysql installation

Create the database by typing the command:

CREATE DATABASE test;

Next, create the user that will operate the database with the create user statement to specify the username, using the mysql_native_password as the default authentication method

CREATE USER ‘testuser’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘8K@]p7uvEpt5D#x)’;

Grant permissions to this user over the ‘test’ database created. This will will give the user privileges over the database

GRANT ALL ON test.* TO ‘testuser’@’%’;

or

GRANT ALL PRIVILEGES ON *.* TO ‘testuser’@’localhost’ WITH GRANT OPTION;

Flush privileges so that the current instance of mysql knows about the changes made to the database and exit the database.

FLUSH PRIVILEGES;

After installing Apache to serve content and MYSQL to store and manage my data. Install PHP to process code to display dynamic content to the users. The php-mysql will allow PHP to communicate with MYSQL-based databases and the libapache2-mod-php will handle php files

sudo apt install php libapache2-mod-php php-mysql

Confirm your PHP version by typing:

php -v

Then restart apache2 to load the new extensions .

sudo systemctl reload apache2

When using apache, you can create virtual hosts to encapsulate configuration details and host more than one domain from a single server. Apache has one server block enabled by default in the /var/www/html directory and it works well if you are hosting a single site. Instead of modifying /var/www/html, creating a directory structure is great within /var/www for the WordPress site leaving /var/www/html as the default directory .

Create the directory for tthe domainas follows:

sudo mkdir /var/www/wordpress

Next, assign ownership of the directory with the $USER environment variable, which will reference your current system user:

sudo chown -R $USER:$USER /var/www/wordpress

Then, open a new configuration file in Apache’s sites-available directory using your preferred command-line editor. Here, we’ll use nano:

Go to cd /etc/apache2/sites-available

Its possible to use the 000-default.conf default configuration with (/var/www/html as my web root if am going to only host one site on my server. If not it is better to split the configuration into junks: one file per site.

In this directory, we have the default conf file 000-default we are using as a base for our new sub-domains. We are going to copy it and modify it based on our new sub-domain.

sudo cp 000-default.conf wordpress.conf

Then, open a new configuration file in Apache’s sites-available directory using your preferred command-line editor. Here, we’ll use nano:

sudo nano /etc/apache2/sites-available/wordpress.conf

We will paste the configurations

<VirtualHost *:80>
ServerName my_domain
ServerAlias www.my_domain
ServerAdmin webmaster@localhost
DocumentRoot /var/www/wordpress
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Here we are telling apache to serve our domain using the /var/www/wordpress as the web root directory

Then allow .htaccess files by setting the AllowOverride directive within a directory block pointing to our root document.

Save and close the file when you’re done. If you’re using nano, you can do that by pressing CTRL+X, then Y and ENTER.

Then enable the new virtual host with the a2ensite flag

sudo a2ensite wordpress

Then disable the default website that comes installed with apache to avoid overwriting the virtual host.

sudo a2dissite 000-default

Enable mod_rewrite to allow us to have more human-readable permalinks to my posts.

sudo a2enmod rewrite

Then make sure that our configuration files does not have any errors by running:

sudo apache2ctl configtest

Finally reload apache to effect the changes

sudo systemctl reload apache2

STEP 3: DOWNLOADING WORDPRESS

Now that our server software is configured, we can download and set up WordPress. For security reasons in particular, it is always recommended to get the latest version of WordPress from their site.

Change into a writable directory (we recommend a temporary one like /tmp) and download the compressed release.

cd /tmp
curl -O https://wordpress.org/latest.tar.gz

Extract the compressed file to create the WordPress directory structure:

tar xzvf latest.tar.gz

We will be moving these files into our document root momentarily. Before we do, we can add a dummy .htaccess file so that this will be available for WordPress to use later.

Create the file by typing: touch /tmp/wordpress/.htaccess

We’ll also copy over the sample configuration file to the filename that WordPress reads:

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

We can also create the upgrade directory, so that WordPress won’t run into permissions issues when trying to do this on its own following an update to its software:

mkdir /tmp/wordpress/wp-content/upgrade

Now, we can copy the entire contents of the directory into our document root. We are using a dot at the end of our source directory to indicate that everything within the directory should be copied, including hidden files (like the .htaccess file we created):

sudo cp -a /tmp/wordpress/. /var/www/wordpress

STEP 4: CONFIGURING THE WORDPRESS DIRECTORY

Before we do the web-based WordPress setup, we need to adjust some items in our WordPress directory.This includes changing the ownership of files to the www-data user and group. This is the user that the apache web server runs as and Apache will need to be able to read and write wordpress files in order to serve the website content and perform automatic updates.

Update the ownership with the chown command which allows you to modify file ownership. Be sure to point to your server’s relevant directory.

sudo chown -R www-data:www-data /var/www/wordpress

Next we’ll run two find commands to set the correct permissions on the WordPress directories and files:

sudo find /var/www/wordpress/ -type d -exec chmod 750 {} \;
sudo find /var/www/wordpress/ -type f -exec chmod 640 {} \;

These permissions should get you working effectively with WordPress, but note that some plugins and procedures may require additional tweaks.

STEP 5: SET UP THE WORDPRESS CONFIGURATION FILE

Now, we need to make some changes to the main WordPress configuration file.

Open the WordPress configuration file by navigating to:

sudo nano /var/www/wordpress/wp-config.php

Save and close the file when you are finished.

STEP 6: COMPLETING THE INSTALLATION THROUGH THE WEB INTERFACE

Now that the server configuration is complete, we can complete the installation through the web interface.

In your web browser, navigate to your server’s domain name or public IP address:

https://server_domain_or_IP

Next, you will come to the main setup page.

Select a name for your WordPress site and choose a username. It is recommended to choose something unique and avoid common usernames like “admin” for security purposes. A strong password is generated automatically. Save this password or select an alternative strong password.

Enter your email address and select whether you want to discourage search engines from indexing your site:

When you click ahead, you will be taken to a page that prompts you to log in:

At this point, you can begin to design your WordPress website! If this is your first time using WordPress, explore the interface a bit to get acquainted with your new CMS.

Thanks for staying to the end!

--

--

Diana Moraa
Diana Moraa

Written by Diana Moraa

Passionate and motivated about Cloud Computing technology because it continues to allow us to modernize, consolidate IT infrastructure and automate workloads.

Responses (1)