3 minutes
Deploy Laravel to Google Cloud Run
This guide covers the complete process of deploying a Laravel application to Google Cloud Run, using Apache as the web server, and connecting it to a PostgreSQL database hosted on Google Cloud SQL.
Prerequisites
- Google Cloud Project: Ensure you have a Google Cloud project.
- Google Cloud SDK: Install the Google Cloud SDK.
- Docker: Install Docker on your local machine.
- Laravel Application: Have your Laravel application ready for deployment.
Steps to Deploy Laravel Application
1. Set Up Google Cloud SDK
- Initialize the Google Cloud SDK:
gcloud init
2. Create and Configure PostgreSQL on Google Cloud SQL
Create a PostgreSQL instance:
- Go to the Google Cloud Console.
- Navigate to
SQL
and create a new PostgreSQL instance. - Configure the instance settings, create a database, and a user with necessary permissions.
Enable the Cloud SQL Admin API:
- Go to the API Library.
- Search for
Cloud SQL Admin API
and enable it for your project.
3. Configure Cloud Run to Access Cloud SQL
- Obtain the connection name of your Cloud SQL instance, e.g.,
project-id:region:instance-id
.
4. Dockerize Laravel Application with Apache
Create a Dockerfile:
# Use the official PHP image with Apache as the base image FROM php:8.1-apache # Set working directory WORKDIR /var/www/html # Install system dependencies RUN apt-get update && apt-get install -y \ build-essential \ libpng-dev \ libjpeg62-turbo-dev \ libfreetype6-dev \ locales \ zip \ jpegoptim optipng pngquant gifsicle \ vim \ unzip \ git \ curl \ libpq-dev \ libonig-dev \ libzip-dev # Clear cache RUN apt-get clean && rm -rf /var/lib/apt/lists/* # Install PHP extensions RUN docker-php-ext-configure gd --with-freetype --with-jpeg \ && docker-php-ext-install pdo_pgsql mbstring exif pcntl bcmath gd zip # Enable Apache modules RUN a2enmod rewrite # Install Composer COPY --from=composer:latest /usr/bin/composer /usr/bin/composer # Copy existing application directory contents COPY . /var/www/html # Copy existing application directory permissions COPY --chown=www-data:www-data . /var/www/html # Set the correct permissions for Laravel directories RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache # Expose port 80 for Apache EXPOSE 80 # Start Apache CMD ["apache2-foreground"]
Build and Push Docker Image:
docker build -t gcr.io/my-project-87661/your-app-name . docker push gcr.io/my-project-87661/your-app-name
5. Deploy to Google Cloud Run
- Deploy the Docker image to Cloud Run and connect it to your Cloud SQL instance:
gcloud run deploy myapp-service \ --image gcr.io/my-project-87661/your-app-name \ --add-cloudsql-instances my-project-87661:region:instance-id \ --platform managed \ --region your-region \ --allow-unauthenticated \ --set-env-vars "APP_KEY=base64:generated_app_key,DB_CONNECTION=pgsql,DB_HOST=/cloudsql/my-project-87661:region:instance-id,DB_PORT=5432,DB_DATABASE=your-db-name,DB_USERNAME=your-db-username,DB_PASSWORD=your-db-password"
6. Configure Laravel for Cloud SQL
Database Configuration in .env:
DB_CONNECTION=pgsql DB_HOST=/cloudsql/my-project-87661:region:instance-id DB_PORT=5432 DB_DATABASE=your-db-name DB_USERNAME=your-db-username DB_PASSWORD=your-db-password
Database Configuration in config/database.php:
'pgsql' => [ 'driver' => 'pgsql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '5432'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8', 'prefix' => '', 'schema' => 'public', 'sslmode' => 'prefer', ],
Additional Laravel Configuration
Set Application Key: Ensure you have set the
APP_KEY
in your.env
file. You can generate one using:php artisan key:generate --show
Clear and Cache Configurations: Before building your Docker image, clear and cache Laravel configurations:
php artisan config:cache php artisan route:cache php artisan view:cache
Summary
This comprehensive guide walks you through deploying a Laravel application on Google Cloud Run with Apache and PostgreSQL. It includes setting up your environment, creating and pushing a Docker image, deploying the application to Cloud Run, and configuring Laravel to connect to a PostgreSQL database on Google Cloud SQL. By following these steps, your Laravel application will be correctly configured and ready for production deployment.