Install Magento on Google Cloud with Ubuntu 18.04, Nginx, PHP 7.2, Cloud SQL and Let’s Encrypt SSL
Prerequisites
- Your Compute Engine Instance running and installed with Nginx and PHP.
- For setting up Compute Engine, see the Setting up Compute Engine Instance.
- For installing Nginx and PHP, see how to install LEMP in Compute Engine Instance.
- Domain name is pointed to your virtual machine.
- For setting up Cloud DNS, see the Setting up Google Cloud DNS for your domain.
- Google Cloud SQL Setup, see Setup Cloud SQL and connect with Compute Engine.
Install Required Magento Packages
sudo apt install php7.2-intl php7.2-bcmath unzip
Setup your website
Your website will be located in the home directory and have the following structure
Replace yourdomainname.com
with your original domain name.
home
-- yourdomainname.com
---- logs
---- public
The public
directory is your website’s root directory and logs
directory for your error logs
Now we create these directories and set correct permissions
You need to SSH into your VM Instance and run these commands
mkdir -p yourdomainname.com/logs yourdomainname.com/public
sudo chmod -R 755 yourdomainname.com
Download Magento
Navigate to your root directory and install Composer globally. You need composer to update dependencies before installing Magento
cd ~/yourdomainname.com/public
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/bin --filename=composer
Now create a new composer project using Magento Open Source
composer create-project --repository=https://repo.magento.com/ magento/project-community-edition <install-directory-name>
You will be prompted to enter username
and password
to download Magento
Go you your Magento account and grab your keys. Your public key is your username
; your private key is your password
.
Once done wait for the download to complete and set up permissions correctly
sudo cp -a ~/yourdomainname.com/public/<install-directory-name>/. ~/yourdomainname.com/public
sudo rm -r ~/yourdomainname.com/public/<install-directory-name>
find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
sudo chown -R :username .
sudo chmod u+x bin/magento
Setup Nginx for Magento
Copy the default Magento nginx.conf.sample
file to nginx.conf
cp ~/yourdomainname.com/public/nginx.conf.sample ~/yourdomainname.com/public/nginx.conf
Now create a new Nginx configuration for your website in the sites-available
directory
sudo nano /etc/nginx/sites-available/yourdomainname.com
Copy and paste the following configuration, ensure that you change the server_name, error_log and root directives to match your domain name. Hit CTRL+X
followed by Y
to save the changes.
upstream fastcgi_backend {
server unix:/run/php/php7.2-fpm.sock;
}
server {
listen 80;
listen [::]:80;
server_name yourdomainname.com www.yourdomainname.com;
error_log /home/username/yourdomainname.com/logs/error.log;
set $MAGE_ROOT /home/username/yourdomainname.com/public;
include /home/username/yourdomainname.com/public/nginx.conf;
}
To enable this newly created website configuration, symlink the file that you just created into the sites-enabled
directory.
sudo ln -s /etc/nginx/sites-available/yourdomainname.com /etc/nginx/sites-enabled/yourdomainname.com
Check your configuration and restart Nginx for the changes to take effect
sudo nginx -t
sudo service nginx restart
Create SSL certificate and enable HTTP/2
HTTPS
HTTPS is a protocol for secure communication between a server (instance) and a client (web browser). Due to the introduction of Let’s Encrypt, which provides free SSL certificates, HTTPS are adopted by everyone and also provides trust to your audiences.
HTTP/2
HTTP/2 is the latest version of the HTTP protocol and can provide a significant improvement to the load time of your sites. There really is no reason not to enable HTTP/2, the only requirement is that the site must use HTTPS.
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx
Now we have installed Certbot by Let’s Encrypt for Ubuntu 18.04, run this command to receive your certificates.
sudo certbot --nginx certonly
Enter your email
and agree to the terms and conditions, then you will receive the list of domains you need to generate SSL certificate.
To select all domains simply hit Enter
The Certbot client will automatically generate the new certificate for your domain. Now we need to update the Nginx config.
Redirect HTTP Traffic to HTTPS with www in Nginx
Open your site’s Nginx configuration file add replace everything with the following. Replacing the file path with the one you received when obtaining the SSL certificate. The ssl_certificate directive
should point to your fullchain.pem file, and the ssl_certificate_key
directive should point to your privkey.pem file.
upstream fastcgi_backend {
server unix:/run/php/php7.2-fpm.sock;
}
server {
listen [::]:80;
listen 80;
server_name yourdomainname.com www.yourdomainname.com;
# redirect http to https www
return 301 https://www.yourdomainname.com$request_uri;
}
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
server_name yourdomainname.com;
ssl_certificate /etc/letsencrypt/live/yourdomainname.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomainname.com/privkey.pem;
set $MAGE_ROOT /home/username/yourdomainname.com/public;
include /home/username/yourdomainname.com/public/nginx.conf;
# redirect https non-www to https www
return 301 https://www.yourdomainname.com$request_uri;
}
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
server_name www.yourdomainname.com;
ssl_certificate /etc/letsencrypt/live/yourdomainname.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomainname.com/privkey.pem;
error_log /home/username/yourdomainname.com/logs/error.log;
set $MAGE_ROOT /home/username/yourdomainname.com/public;
include /home/username/yourdomainname.com/nginx.conf;
}
The http2
value is all that is needed to enable the HTTP/2 protocol.
Hit CTRL+X
followed by Y
to save the changes.
Check your configuration and restart Nginx for the changes to take effect.
sudo nginx -t
sudo service nginx restart
Renewing SSL Certificate
Certificates provided by Let’s Encrypt are valid for 90 days only, so you need to renew them often. Now you set up a cronjob to check for the certificate which is due to expire in next 30 days and renew it automatically.
sudo crontab -e
Add this line at the end of the file
0 0,12 * * * certbot renew >/dev/null 2>&1
Hit CTRL+X
followed by Y
to save the changes.
This cronjob will attempt to check for renewing the certificate twice daily.
Install Magento
You need to set up Cloud SQL and create a database, user to install Magento. Learn to Setup Cloud SQL and connect with Compute Engine
Once Cloud SQL setup is ready and configured to allow connections from your VM Instance.
Visit your domain name in your web browser to install Magento
You should see the installation wizard to complete the install

Once the readiness check is completed, you can add the database you have created in Cloud SQL

Next, configure your admin

Continue the steps and complete the installation

Enjoy your Magento installation on Google Cloud with Ubuntu 18.04 LTS, Nginx, PHP 7.2, Let’s Encrypt SSL and Cloud SQL.
Set up WordPress on Google Cloud.
Hi,
Thank you so much for the detailed instructions. Ran into a problem though:
All was good until the section – Redirect HTTP Traffic to HTTPS with www in Nginx but when I type in sudo nginx -t it gives me an error as follows:
nginx: [emerg] open() “/home/username/mydomainname.com/nginx.conf” failed (2: No such file or directory) in /etc/nginx/sites-enabled/mydomainname.com:41
nginx: configuration file /etc/nginx/nginx.conf test failed
I checked the symlink between sites-available and sites-enabled and it is fine.
Not sure how to proceed. Any help would be appreciated.
Thanks
Hello there,
first of all thx you so much much for putting out such a great tutorial
amazing.
I followed along the tutorial installing magento on google cloud
so far so good, but when I came to the point of configuring my site’s Nginx configuration file, I ended up with the following error:
nginx: [emerg] “upstream” directive is not allowed here in /home/…
is there any chance you could point me in a direction why I am ending up with this error?
best regards
Thank you for using Cloudbooklet, can you please share your nginx configuration so that we can check.