Learn how to use the gcloud CLI and startup scripts to deploy a LAMP server on Google Cloud Compute.
Even after more than a decade, a LAMP server (Linux, Apache, MySQL, PHP) is one of the most used server configurations on the Internet. Despite competition from newer languages and frameworks, PHP continues to power today's digital communities.
Getting Started
What's Included in this article
- Make a network with gcloud.
- Using gcloud, create a compute instance.
- WordPress database configuration
- Set up WordPress
Getting Ready to Provision
Choosing a Base Image
A compute instance is a virtual machine on which an operating system is installed. To deploy a compute instance, you must first choose an operating system by selecting a base image.
The gcloud compute images
list command returns a list of accessible images. Because an unfiltered list is rather long, it is recommended that you filter it.
glcoud compute images list
Output
NAME PROJECT FAMILY DEPRECATED STATUS
centos-6-v20200811 centos-cloud centos-6 READY
centos-7-v20200811 centos-cloud centos-7 READY
centos-8-v20200811 centos-cloud centos-8 READY
coreos-alpha-2514-1-0-v20200526 coreos-cloud coreos-alpha READY
coreos-beta-2513-2-0-v20200526 coreos-cloud coreos-beta READY
coreos-stable-2512-3-0-v20200526 coreos-cloud coreos-stable READY
cos-77-12371-1072-0 cos-cloud cos-77-lts READY
cos-81-12871-1185-0 cos-cloud cos-81-lts READY
cos-beta-81-12871-117-0 cos-cloud cos-beta READY
The -filter
flag, which allows regex, is used to apply a filter. The following command might be used to filter the list for Ubuntu images.
glcoud compute images --filter ubuntu
Output
NAME PROJECT FAMILY DEPRECATED STATUS
ubuntu-1604-xenial-v20200807 ubuntu-os-cloud ubuntu-1604-lts READY
ubuntu-1804-bionic-v20200807 ubuntu-os-cloud ubuntu-1804-lts READY
ubuntu-2004-focal-v20200810 ubuntu-os-cloud ubuntu-2004-lts READY
ubuntu-minimal-1604-xenial-v20200807 ubuntu-os-cloud ubuntu-minimal-1604-lts READY
ubuntu-minimal-1804-bionic-v20200806 ubuntu-os-cloud ubuntu-minimal-1804-lts READY
ubuntu-minimal-2004-focal-v20200729 ubuntu-os-cloud ubuntu-minimal-2004-lts READY
Machine type
Machine types are compute instance hardware templates. These templates decide how much memory and CPU power your compute instance will receive. The machine type you pick will determine your major costs for running a compute instance; machine types with more CPU and/or memory will have substantially higher operating costs.
Provisioning the Compute Instance
Create a new instance
When you create a new compute instance, you must give it a name and specify the machine type and image to be utilized. These are the two most crucial considerations to make when setting up a new instance to host a LAMP server.
The Machine Type specifies how much CPU and memory will be allocated to a compute instance. These parameters cannot be modified after they are established, so it is critical that you understand the basic requirements of your LAMP server before deploying a new instance. The machine type will determine whether or not the compute and memory are shared, with the latter being part of a higher-priced tier.
Image refers to the operating system image that was utilised to create your compute instance. The most popular image is one based on Ubuntu, but you can probably get any flavour of Linux you want.
Region is where the compute instance will be hosted.
The gcloud compute machine-types list
command displays a list of possible machine types. The list of machine kinds is somewhat extensive. This will most likely be used as a quick reference for the name and region.
gcloud compute instances create INSTANCE_NAMES \
--machine-type=<MACHINE-TYPE> \
--image-family=<OS-IMAGE-FAMILY> \
--image-project=<OS-IMAGE-PROJECT> \
--subnet=<SUBNET-NAME> \
--boot-disk-size=<SIZE|MB,GB,TB> \
--boot-disk-type=<BOOT-DISK-TYPE> \
--region=<GCP REGION>
For example, to set up a new compute instance for a WordPress blog named wordpress-lamp-1
on Ubuntu 20.04
with a 10GB boot disc in us-central-a, we would run the following command.
gcloud compute instances create wordpress-lamp-1 --machine-type=f1-micro --image-family=ubuntu-2004-lts --image-project=ubuntu-os-cloud --boot-disk-size=10GB --boot-disk-type=pd-standard --subnet=default --zone=us-central1-a
Output
Created [https://www.googleapis.com/compute/v1/projects/serverlab/zones/us-central1-a/instances/wordpress-lamp-1].
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
wordpress-lamp-1 us-central1-a f1-micro 10.128.0.32 35.223.143.236 RUNNING
Startup Scripts
The benefit of running your services in the cloud is the ease with which instances can be provisioned and decommissioned. Scaling on demand is another option that can help you when there is an increase in traffic or work. Manually configuring a freshly provided compute instance, on the other hand, is a tedious exercise that effectively disables auto scaling.
Startup scripts allow us to run commands during the deployment of a compute instance.
Make a new file called startup.sh
and fill it with the commands required to set up a basic LAMP server.
#! /bin/bash
# Update installed packages and install LAMP packages
sudo apt update
sudo apt upgrade -y
sudo apt install -y php php-mysql php-gd php-xml mysql-server apache2
# Download and install WordPress
wget https://wordpress.org/latest.tar.gz
tar xvf latest.tar.gz -C /var/www/html
Create a new compute instance with the startup script and configure it as a LAMP server. We utilize the --metadata-from-file
flag in conjunction with startup-script=<path/to/file>
to instruct gcloud
to use the startup script as part of the instance provisioning process.
Please note that local startup scripts are limited to 256 KB. If your startup scripts exceeds this limit, you will need to store the script in Cloud Storage, and specify the script URL. To use a remote file rather than a local file, use startup-script-url instead of startup-script.
For example, to utilize the startup.sh
script on your local system as the startup script, specify the flag to --metadata-from-file startup-script=startup.sh
.
gcloud compute instances create wordpress-lamp-2 --machine-type=f1-micro --image-family=ubuntu-2004-lts --image-project=ubuntu-os-cloud --boot-disk-size=10GB --boot-disk-type=pd-standard --subnet=default --zone=us-central1-a --metadata-from-file startup-script=startup.sh
Output
Created [https://www.googleapis.com/compute/v1/projects/serverlab/zones/us-central1-a/instances/wordpress-lamp-2].
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
wordpress-lamp-2 us-central1-a f1-micro 10.128.0.33 35.239.218.206 RUNNING
The startup script's actions will be reported to syslog. You can ssh
into the instance or inspect the logs in the Google Compute console to audit and debug your launch script.
An example of startup-script log entries is shown below.
Aug 18 02:15:41 wordpress-lamp-2 systemd[1]: Starting Google Compute Engine Startup Scripts...
Starting [0;1;39mGoogle Compute Engine Startup Scripts[0m...
Aug 19 02:15:42 wordpress-lamp-2 startup-script: INFO Starting startup scripts.
Aug 19 02:15:42 wordpress-lamp-2 startup-script: INFO Found startup-script in metadata.
Aug 19 02:15:42 wordpress-lamp-2 startup-script: INFO startup-script: WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
Aug 19 02:15:42 wordpress-lamp-2 systemd-resolved[415]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2023-0001, retrying transaction with reduced feature level UDP.
Aug 19 02:15:42 wordpress-lamp-2 systemd-resolved[415]: Server returned error NXDOMAIN, mitigating potential DNS violation DVE-2023-0001, retrying transaction with reduced feature level UDP.
Aug 19 02:15:42 wordpress-lamp-2 startup-script: INFO startup-script: Hit:1 http://us-central1.gce.archive.ubuntu.com/ubuntu focal InRelease
Aug 19 02:15:42 wordpress-lamp-2 startup-script: INFO startup-script: Get:2 http://us-central1.gce.archive.ubuntu.com/ubuntu focal-updates InRelease [111 kB]
Aug 19 02:15:42 wordpress-lamp-2 startup-script: INFO startup-script: Get:3 http://us-central1.gce.archive.ubuntu.com/ubuntu focal-backports InRelease [98.3 kB]
Aug 19 02:15:42 wordpress-lamp-2 startup-script: INFO startup-script: Get:4 http://archive.canonical.com/ubuntu focal InRelease [12.1 kB]
Aug 19 02:15:42 wordpress-lamp-2 startup-script: INFO startup-script: Get:5 http://us-central1.gce.archive.ubuntu.com/ubuntu focal/universe amd64 Packages [8628 kB]
Firewall Rules
If you did not indicate that HTTP and HTTPS traffic be allowed into your compute instance, you must do so immediately.
Allow HTTP Access
Run the following command to allow HTTP traffic to your newly provisioned compute instance.
gcloud compute firewall-rules create --network=default default-allow-http --allow=tcp:80
Allow HTTPS Access
Run the following command to allow HTTPS communication to your freshly provisioned compute instance.
gcloud compute firewall-rules create --network=default default-allow-https --allow=tcp:443
Allow SSH Access
If you intend to administer your server through SSH, you must also grant access to it.
gcloud compute firewall-rules create --network=default default-allow-ssh --allow=tcp:22
Using SSH with GCloud
When your server is completely deployed, use the gcloud compute ssh
command to SSH into it.
The command's basic syntax is shown below. The default user will be the one who has logged in using gcloud
. Unless you want to SSH access the server using a separate account, you don't need to specify a user.
gcloud compute ssh [USER@] INSTANCE [--zone=ZONE]
Let us log in to our wordpress-lamp-1
compute instance, for example.
gcloud compute ssh wordpress-lamp-1 --zone=us-central1-a
Alternatively, if you want to SSH into a different account, you can use the following syntax:
gcloud compute ssh operator1@wordpress-lamp-1 --zone=us-central1-a
Database
So far, we've covered provisioning the compute instance, which means we've launched a new instance and installed all of the essential packages. The next step is to configure those packages and create a database.
If you are not SSH’d into the server, do so now.
Creating a database
Log in to the MySQL server instance and create a new WordPress database.
- Connect to the database server instance
mysql -u root -p
- Set up a new database for your WordPress website.
create database wordpress_db;
Create a Database User
No application should use root credentials to connect to its backend database. An app should only be granted permissions to the databases with which it interacts, and only the permissions that it needed to function.
- Create a database user for WordPress
create user 'wordpress'@'localhost' identified by 'super-secret-password';
- Give the user access to the Wordpress Database
grant select,insert,delete on '*.wordpress' to 'wordpress'@'localhost';
- Finally, flush the cache of the database to apply the new user permisisons
flush;
WordPress Config
We're almost done! All that remains is to point our WordPress installation to our database.
- Navigate to your WordPress installation's root directory (the place where the contents of
latest.tar.tz
were extracted).
cd path\to\extracted\wordpress
- Next, copy the
wp_config.php
towp_config.php
cp wp_config.php wp_config.php
- Open the
wp_config.php
with the text editor of your choice.
vi wp_config.php
- Locate and edit the lines that configure WordPress's database connection.
Apache vhost
The final step is to host our WordPress site using Apache or any other webserver application.
YouAreAwesome #StayAwesome
Stay tuned and follow me for more updates. Don’t forget to give us your 👏 if you enjoy reading the article as a support to your author!!
Top comments (0)