Setting up a server on Google Cloud

By Levi Clancy for לוי on

▶︎ View related▼︎ Tap to hide

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

Enter your domain name to get the commands you will need to run.

Enter your domain name, e.g. without any leading www or anything else

  • Linux Ubuntu operating system (version jessie)

  • Apache

  • mySQL

  • PHP

  • And: Configure common options, like redirecting from to

  • 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,

<i>in google cloud shell:</i> 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,

<i>in command line:</i> 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.

<i>in command line:</i> 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,

<i>in command line:</i> 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

Make the directory where you will store your website files,

mkdir -p /var/www/ chown -R $USER:$USER /var/www/ chmod -R 755 /var/www/

Create an index.php file,

nano /var/www/

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,

cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/

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

nano /etc/apache2/sites-available/

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,

<i>in the conf file:</i> ServerName ServerAlias DocumentRoot /var/www/

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

a2ensite service apache2 reload

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

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 jessie-backports main

Then save and go forward,

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


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.