Setting up a server on Google Cloud

By Levi Clancy for לוי on

This is to set up a LAMP server on Google Cloud, applicable to any Linux Ubuntu 8 (jessie) operating system.

  • Linux Ubuntu operating system (version jessie)

  • Apache

  • mySQL

  • PHP

  • And: Configure common options, like redirecting from www.levi.life to levi.life

  • Also: SSL support

  • Plus: Some helpful modifications for PHP

Create project

Tap to open

First set up the Google Cloud project. You will be required to set up a billing account for the project. New users may receive free credit to begin exploring.

Network access

Tap to open

By default, there is already a VPC network. Update it to 'global' dynamic routing mode. You will see the notification icon in the upper corner spinning while this saves.

When it finishes, then open Google Cloud shell and enter these commands,

in google cloud shell:
gcloud init

Choose [1] then choose the project you are working on — usually [1] — then [n] to move along quickly. You can run 'clear' as a command to clean out the window contents, which can prevent distraction.

Build instance

Tap to open

Now, go to Compute Engine and create an instance.

Change your boot disk to 'Debian GNU/Linux 8 (jessie)' and configure the system's memory and disk space to your needs and budget. Check 'Allow HTTP traffic' and 'Allow HTTPS traffic' for your instance to be accessible as a website.

Tap to open

Open the 'Management, Disks, Networking...' options and choose 'Networking.' Choose your network you just created, and make sure to reserve an external static IP address. Otherwise, the IP address will always change and you cannot point a web domain to it.

Tap to open

Now, enter the following in the google cloud shell at the bottom of the window,

in google cloud shell:
gcloud compute config-ssh

If prompted then choose [Y], then press 'enter' twice to skip setting a passphrase for the key pair.

Set up the system

In the list of instances, press 'SSH' to connect to your instance. The command line will look like this,

Now enter these commands,

in command line:
sudo -s
apt-get update
apt-get install apache2
apt-get install mysql-server

You may be asked along the way to type 'Y' to confirm the installations. If prompted for a mySQL password then leave it empty and hit 'return' because we will set it up in the next step.

in command line:
sudo mysql_secure_installation

You will be asked to set up a mySQL password. The default is no root password, which is very insecure. Choose [Y] for all the subsequent options.

in command line:
apt-get install php5 libapache2-mod-php5
apt-get install php5-mysql
apt-get install php5-gd
apt-get install zip
apt-get install unzip
a2enmod rewrite
service apache2 restart

Tap to open

I suggest allowing short PHP tags as a helpful way to make coding a little easier.

in command line:
nano /etc/php5/apache2/php.ini

Use CTRL+W and type 'short_open_tag' press enter. Then press CTRL+W and enter again. Change "Off" to "On". Then CTRL+O and enter and CTRL+X to save it and close it. Then restart Apache,

in command line:
service apache2 restart

Access your server

At this point you can access your server on the world wide web. The external IP listed by the instance will be the address. It hosts files from the /var/www/html directory by default.

Configure websites

Let us assume you have a domain name, in my case levi.life. Substitute levi.life for your own domain name anywhere below.

Make the directory where you will store your website files,

in command line:
mkdir -p /var/www/levi.life
chown -R $USER:$USER /var/www/levi.life
chmod -R 755 /var/www/levi.life

Create an index.php file,

in command line:
nano /var/www/levi.life/index.php

Populate the file with a simple PHP script,

in the index.php file:

Now we will set up the Apache configuration file so your site can be hosted,

in command line:
cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/levi.life.conf

This step copied the default configuration file and made a configuration file especially for you. Next we will edit it,

in command line:
nano /etc/apache2/sites-available/levi.life.conf

You'll want to set up the parameters like this. With the conf file open for editing, swap out any of the placeholders already there and make sure these are in place,

in the conf file:
ServerName levi.life
ServerAlias www.levi.life
DocumentRoot /var/www/levi.life

Note how the document root is the directory with the index.php file that you set up earlier? Now activate the conf file,

in command line:
a2ensite levi.life.conf
service apache2 reload

When you finish setting up the conf files for all domains names you want, then it is time to add SSL,

in command line:
mkdir -p /etc/apt/sources.list.d
nano /etc/apt/sources.list.d/sources.list

Add this line to the sources.list file,

in sources.list:
deb http://ftp.debian.org/debian jessie-backports main

Then save and go forward,

in command line:
apt-get -t stretch-backports install "package"
apt-get install python-certbot-apache -t jessie-backports
certbot --authenticator webroot --installer apache

Reinstalling

Sometimes something with Apache or somewhere else gets screwy. Definitely do not be afraid to reinstall,

sudo -s
apt-get autoremove --purge apache2
apt-get autoremove --purge letsencrypt
apt-get autoremove --purge certbot
apt-get autoremove --purge python-certbot-apache
apt-get install python-certbot-apache -t jessie-backports

Then you can install from there as normal.

Studies

cloud.google.com

digitalocean.com

certbof.eff.org

levi.life|1524566798_3MYKK3BVML
close
levi.life|1524566884_6FYGL5KCWQ
close
levi.life|1524567068_Y7LG3J6WD4
close
levi.life|1524567000_FSSL5DSZHW
close
levi.life|1524567214_B6LTNTWVHR
close
levi.life|1524567215_MHJZ2522JW
close