Laravel is a popular web framework for making custom applications. It runs on PHP and is entirely free and open source. In this guide, we will see how to install and configure a Laravel application on Google Cloud Compute Engine. Compute Engine is an IaaS offering of Google Cloud where the users can create and configure their own virtual machine instance.

Step 1 – Create Compute Instance

  1. Go to your Google Cloud Console and take Compute Engine Page.

2. Click Create Instance Menu from the top.

Configure the instance as your requirement. Here I’m creating an instance with 2vCPU, 2GB 20GB SSD with Ubuntu 20.04 LTS Operating System. Don’t forget to check to allow HTTP and HTTPS tick boxes to configure a firewall policy.

Once you click Create button. Google Cloud will spin up a new instance / VM with a specified configuration.

Step 2 – Install Required Softwares

SSH Login to the newly created instance you created in the previous step via your preferred connection method (Browser / gcloud CLI/ Third-party tools).

Since this is our first time using apt for this session, start off by updating your server’s package index.

sudo apt update

Install Nginx

sudo apt install nginx

Install MySQL

sudo apt install mysql-server
sudo mysql_secure_installation

Install PHP and Modules

sudo apt install php-fpm php-mysql php-mbstring php-xml php-bcmath

Install Composer

cd ~
sudo apt install php-cli unzip
curl -sS https://getcomposer.org/installer -o composer-setup.php
php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

Step 3 – Create Database for our Laravel Application

  1. Login to MySQL Console
sudo mysql

2. Create a new database and user. Don’t forget to replace SECURE with a secure password of your choice

CREATE DATABASE laravel_db;
CREATE USER 'laravel_dbadmin'@'%' IDENTIFIED WITH mysql_native_password BY 'SECURE';
GRANT ALL ON laravel_db.* TO 'laravel_dbadmin'@'%';
exit

Step 4 – Install Laravel Application

  1. Create a staging directory in user home.
mkdir staging

2. Upload Laravel application to this folder via git/SCP/wget.

3. Install composer dependencies for the project.

cd staging
composer install --no-dev --optimize-autoloader

4. Edit .env file and change database credentials and other configs as your requirement.

nano .env

Step 5- Configure Nginx

  1. First we need to move our application files to standard web location location.
sudo mv ~/staging /var/www/application

2. Give write permission to Nginx to Laravel storage and cache folders.

sudo chown -R www-data.www-data /var/www/application/storage
sudo chown -R www-data.www-data /var/www/application/bootstrap/cache

3. Link Laravel public storage to the public folder.

php artisan storage:link

4. Run Laravel database migrations.

php artisan migrate

4. Create virtual host configuration for our application.

sudo nano /etc/nginx/sites-available/example.com
server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/application/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

5. Activate virtual host.

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

6. Test Nginx configuration and apply it.

sudo nginx -t
sudo systemctl reload nginx

Assuming you’ve followed all the steps in this guide so far, you should have a working Laravel application accessible at example.com

By Adarsh Vengarathodi

Hey there! I'm the founder and the CEO of MistLayer, Inc. In this role, I oversee the company’s overall product direction and development and leads the engineering, product and infrastructure teams. I have more than 12 years of software, systems and product development experience, including hardware and software engineering, product management, quality assurance and support.