Skip to content

Instantly share code, notes, and snippets.

@jdecode
Last active July 18, 2020 05:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdecode/4300189ee24e7d96a7e9fed3dc0b1c6c to your computer and use it in GitHub Desktop.
Save jdecode/4300189ee24e7d96a7e9fed3dc0b1c6c to your computer and use it in GitHub Desktop.
Steps to run PHP code with manual code deploy (i.e. via git clone/pull) at Google Cloud Platform [GCP/GCE] : Basics++

High level actions

  1. Create a VM (Virtual Machine) - with minimal hardware specs : probably a version with 1 vCPU and less than (or equal to) 1 GB RAM
  2. Use Ubuntu as the OS (you can choose any other OS, but you have to make sure that the respective commands for any follow up actions are known and you are well versed with those).
  3. Enable/configure SSH to access the system
  4. Clone git repo (Github/Bitbucket/Gitlab etc) - and install any dependencies on the VM
  5. Configure any additional tools/services that the code is using (DB/PDO/JSON etc)
  6. Map the IP/generic-url to a domain name (if you don't have a domain, then make sure you sign-up for that - free or paid - choice is yours) - DNS management

Good to have (If not here, then where? If not now, then when?):

  1. Logging
  2. Monitoring
  3. Error tracking

Specific steps

  1. Register + Login to GCP
  2. GCE > Create new VM (minimum specs, or next to minimum) > Ubuntu 18 (minimal version) > Allow HTTP/HTTPS traffic > Create
  3. SSH into the VM
  4. Apache installation

sudo apt-get update

sudo apt-get install apache2

  1. PHP installation

sudo apt-get install software-properties-common

sudo add-apt-repository ppa:ondrej/php

sudo apt-get update

sudo apt-get install php7.3

  1. Go to /var/www/html and create a file to run phpinfo (validating the apache-php setup) Note : If vim not installed, then you might need to install it using sudo apt-get install vim (or if you choose to use any other editor for creating the php file (e.g. phpinfo.php)

You can also create the file (and add content to that) by following commands (every command is sudo here, but depending on the permissions of file/folder, it may be omitted) :

sudo touch i.php

sudo echo "<?php phpinfo();" >> i.php (if this doesn't work, then change ownership of the file from root to logged in user first)

  1. Go to external IP (of VM) and you should be able to see the default page of Apache(you might need to switch to static IP from ephermal IP - if the page says "refused to connect"), and you can also check the custom file (phpinfo) created - HTTPS is not configured by default, so use HTTP to test the URL

  2. Install git using

sudo apt-get install git

  1. Install composer(and following missing extensions): php7.3-intl, php7.3-dom, php7.3-mbstring, php7.3-zip :

sudo apt-get install composer php7.3-intl php7.3-dom php7.3-mbstring php7.3-zip -y

  1. Run composer install

  2. Following issues to be taken care of:

    1. Make sure intl (or other PHP extensions) are enabled : sudo phpenmod intl
    2. Make sure Apache's mod_rewrite is enabled : sudo a2enmod rewrite
    3. For any other issues (500 errors) refer /var/log/apache2/error.log : sudo tail -f /var/log/apache2/error.log and then refresh the browser to allow live logging trail of the error log
    4. After any changes to Apache, remember to restart the service : sudo service apache2 restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment