How to Host Up A PHP Website With Google Cloud
In this step-by-step tutorial, you will learn how to install & set up a PHP website with Google Cloud. We’ll install LAMP Stack, transfer files with FileZilla, as well as encrypt your site for free with Let’s Encrypt.
Before starting this tutorial, you should have created a Google Cloud account, and have a fresh new project to put your instance on.
Installing LAMP Stack
We can use Google Cloud’s marketplace feature to set up LAMP Stack with ease. First, go to console.cloud.google.com and in the top-left menu, click on the button. Next up, search “Lamp Stack” in the search bar, select the first result, and select “Launch.”
Now, we need to configure our LAMP Stack deployment. At the top, under “Deployment name,” you can change the name of the deployment- but we can just leave it at the default “lamp-1” for this tutorial. Next up, we need to choose what location the VM Instance should be located in. You should choose the one closest to where most of your customers will be. For example, if you’re hosting a Japanese gaming website, you shouldn’t set the location to Central America- South Asia would be a much better option. (In most cases, you can just leave the location to the default value.)
Now for the machine type. Depending on how many people use the site, you may need to change it. Most likely, you’ll be just fine setting it to the smallest option, “Micro (1 shared vCPU.)” Of course, you can change the size of the server later on, so you might as well leave it at the lowest and increase it as traffic increases.
Next, make sure “Install PhpMyAdmin” is checked. For the Boot Disk option, you can leave it at the default “Standard Persistent Disk”, however it is important to note that setting it to an SSD, while increasing the monthly cost, will speed up data transfer rates. Before deploying, we’ll need to ensure the “Allow HTTPS traffic from the Internet” checkbox is ticked. With our VM Instance all set up, you can hit the button.
Once LAMP Stack has deployed, you can visit the site by clicking the button. After a redirect notice, it should bring you to the Apache Default Page:
Transer Files With FileZilla
Now that we’ve got LAMP Stack set up, it’s time to transfer our site’s files over. To do this, we’re going to use FileZilla, but you really can use any FTP solution. If you don’t have the FileZilla client installed, go to https://filezilla-project.org/download.php and install it. (It’s free!)
First, we need to create some SSH keys. In the Command Prompt, type in “ssh-keygen.” When prompted to enter where to save the keys, you can enter any name, or leave it blank. Now we need to enter a passphrase. There is an option to enter no passphrase, but it’s not recommended to do so, because if anyone gets a hold of the key files, they have free range of your site. This just adds an essential extra-layer of security. After entering your passphrase, your keys should be complete! On Windows computers, they should be located in
C:\Users\username/.ssh/, and on Mac computers, it should be located in
In the directory, you should now have the two key files. The public and the private key. For now, we need to copy the public key so that we can add it to the VM Instance’s SSH keys list.
After copying the public key, navigate to and select “VM Instances.” In the list of VM instances, select the instance you just created (if you left it to it’s default name, it should be called ‘lamp-1.’) Now, click the button. Scroll down to the SSH keys section, and click “Show and edit.” In the newly created text box, paste your public key.
Before saving, though, we must edit the network interfaces so that we get a static IP. Right now, we have an ephemeral IP address. The problem with ephemeral IP addresses is that they’re rather short lived- they will change if the VM instance stops or restarts. That means that if your VM instance ever restarts, you’d have to go through your DNS settings all over again and update the IP. A static IP address won’t ever change- even if your VM instance restarts, stops- or anything else. To register a static IP address, (still in the edit page), scroll up to the “Network interfaces” section. Click on the dropdown under “External IP”, and select “Create IP address.” Type in a name, and click .
Now, with your SSH keys saved and your static IP reserved, click (saving may take a few minutes.) Now, in the FileZilla site manager, click the . First, ensure that the protocol is set to “SFTP (secure file transfer protocol.)” In the “Host” field, enter your site’s IP, and leave “port” blank. For the logon type, select “Key file.” For the user field, enter your computer’s user name. And finally, for the key file, click “Browse,” navigate to the private key file, and open the file. Now that everything’s set up, click “Connect.” Your may be notified that the host key is unknown, simply check “Always trust this host” then click “OK.”
You may also be prompted for a password. Use the passphrase from earlier when you were creating the key files. Before adding files, however, we need to change some permissions so that we’re actually able to add or remove files. To do this, click on the button in the Google Cloud Platform. This should open up a pop-up window with the Apache command line. First, navigate to
/var/www, then type
sudo chmod 777 html, then navigate to the “html” folder with
cd html, and then type
sudo chmod 777 index.html. Now you are able to freely transfer files to the site. Note that after transferring files, it’s best practice to lock the files up again with
chmod 755. (Don’t close the command line yet- we’ll use it to get an SSL certificate!)
SSL Certificate With Let’s Encrypt
Now that we’ve got our site all ready to go, we should probably encrypt it. If you don’t, you run the risk of exposing user data, as well as getting massively demoted by the Google search engine (Google heavily prefers encrypted sites over unencrypted sites.) Don’t worry, though! Encrypting your site these days is easy and free, thanks to Let’s Encrypt! Note that you’ll need your site connected to a domain first in order to do this.
In the command line, navigate to your root directory with
cd /. Next, type
sudo apt-get install certbot python-certbot-apache and hit enter, type “yes” when prompted to install it to your site. Now that Certbot is installed, type
sudo certbot --apache to start the certificate process. Simply provide the information it asks from you- and boom- you have a certificate, and your site is encrypted! Renewal should be automatic, but in case of any problems, they’ll write to you at the email address you provided.
And it’s as simple as that. If you’ve followed this tutorial properly, you should now have a PHP and Apache website, with an SSL certificate provided by Let’s Encrypt. In case of any problems, please do leave a comment in the comment section below. Happy coding!