WordPress on Google Cloud Platform

Wordpress on Google Cloud Platform

I am going over hosting WordPress on Google Cloud Platform in this article. This is a step by step guide on how to set up and host a WordPress website using a fresh installation.

Step 1: Fresh VM

console.cloud.google.com
(Note: Be sure and install gcloud https://cloud.google.com/sdk/docs/downloads-apt-get)
Setup Project and VM (micro)
sudo apt update && sudo apt upgrade

Step 2: Install Swap

$ sudo fallocate -l 1G /swapfile
$ sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
$ sudo swapon /swapfile
$ sudo nano /etc/fstab
-- /swapfile swap swap defaults 0 0

Step 3: Install LAMP Stack (Linux, Apache, MySQL, and PHP)

$ sudo apt install tasksel
$ sudo tasksel install lamp-server
$ sudo apt install php-curl php-gd php-mbstring php-xml php-xmlrpc

Step 4: Configure Domain

Setup DNS
-Go to your web domain registrar and create A record pointing to your new server
Configure Apache conf for website
<Directory /var/www/example.com>
Require all granted
</Directory>
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/example.com
</VirtualHost>

$ mkdir -p /var/www/example.com

$ a2dissite 000-default.conf
$ a2ensite example.com.conf
$ systemctl reload apache2

Step 5: Prep Database and PHP

Create database
$ mysql -u root
> CREATE DATABASE wordpress;
> GRANT ALL ON wordpress.* TO 'wordpressuser' IDENTIFIED BY 'Secure1234!';
> quit
$ mysql_secure_installation

Edit /etc/php/7.2/apache2/php.ini
max_input_time = 30
upload_max_filesize = 20M
post_max_size = 21M

Wordpress on Google Cloud Platform

Step 6: Install WordPress on Google Cloud Platform

https://codex.wordpress.org/Installing_WordPress
$ wget https://wordpress.org/latest.tar.gz
$ tar -xzvf latest.tar.gz

Step 7: Tune the new install and MPM_Prefork.conf


https://raw.githubusercontent.com/richardforth/apache2buddy/master/apache2buddy.pl

Step 8: Troubleshooting

One important addition to setting up WordPress. Permission errors with the WordPress installation can be fixed with the following command.

$ chown -R www-data:www-data /var/www/html/*

This adds permissions for the Apache server user to use the files in the webpage directory.

Video Walkthrough

In Conclusion, you now have WordPress on Google Cloud Platform. Enjoy the experience and let me know in the comments if you have any issues.

Leave any Questions and Comments below and I will get back to you. I regularly publish on YouTube and christitus.com so if you’d like to see more videos and articles click the subscribe button in the top right.

18 Comments

  1. ayandebnath

    Hello, when I created my GC instance, I already have this entry in fstab –
    /.c2d-swap-file none swap sw 0 0

    Do I need to do something extra?

    1. Chris Titus

      I would verify that it seeing the swap partition and is using it by running an htop
      If it isn’t registering the swap, you probably need to run a swapon

  2. Arsen

    on other computers it will not open?

    1. Marcelo Pereira

      Thanks! One of the best step by step. Everything works fine.

  3. Paul

    Google says it will lock you out after a year. How is this overcoming that? (Would really like to understand, so anyone who knows please answer!)

    1. Paul

      Oops, forgot I already worked this out (getting older isn’t for cissies my dad used to say). The free service remains free, the “try out stuff for $300 or a year” paid services is what you lose.

    2. Gholastha Sibuea

      I dont really know about this, please correct me if this reason is fault.
      I think, google will only lock the dashboard without deleting all of your stuff if you not pay it. But with wordpress, you can still access and manage it from wp-admin.

  4. francesco

    Ciao Chris,
    when trying to use swapon I get the foolowing error:
    sudo swapon /swapfile
    swapon: /swapfile: swapon failed: Invalid argument

    What can I do?
    Thanks

  5. Juri

    After setting things up, one thing i haven’t figured out. If i want to update any of the wp plugins, it asks for the “connection information” in order for wordpress to access “my” web server Hostname, ftp username etc. does that refer to something on gcloud side and if so then how can i get that information / set up?
    Yes i am kind of a newb with this stuff, help would be appreciated alot ^^’

    1. Juri

      I did apply the last “troubleshooting command” afterwards, I think that resolved it as things work right now.

  6. Alex

    This was very helpful. Can you do a version for setting up a private MedisWiki w/ VisualEditor (and parsoid) running? Ive been struggling with getting that to work…

  7. John

    please chris help, i didn’t know how to do it .. had some problems .. pleaaaaasz

  8. Tony

    Chris Can you help me? I have followed your instructions, works well, though I have an issue with WordPress updates. It asks me for FTP credentials. Which I believe Ive set up in the Google cloud Instance…… But it just dont want to work!! Very frustrating….. Any help I would be very thankful. Tony

  9. Matthew Painter

    Thanks Chris,
    Your guide worked a treat.
    Thank you very much !!!

  10. yehuda

    wouldn’t you be able to go even cheaper if you choose ephemeral ip address. also you could get a similar deal at AWS by opening a different account every year with a new email address (it would be a pain to migrate)

  11. dotpavilion

    I ran in to some problems with settings in permalinks, giving the error “The response is not a valid JSON response.”
    and thru wordpress help, some say the solution is to whitelist the IP…
    how do i do this for on gcp?

    I’m a noob.

    thank you,

    James.

    1. Morh

      I ran into the same problem. The reason is that htaccess is not enabled. This can be enabled by adding “AllowOverride All” in step4 below “Require all granted”. And then running the command “sudo a2enmod rewrite” to enable the rewrite module. After that you might have to manually create the .htaccess file in the webroot if it was not created automatically (with the data wp gives you when enabling the permalink).

      1. Morph

        Or add the wordpress rewrite rules in the apache config file instead of using htaccess files that slows down performance. Add it below “Require all granted” and remove the “AllowOverride All” if it was added. The rewrite module must still be enabled though, so run the “sudo a2enmod rewrite” command to enable it, and restart apache.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: