Laravel
Laravel Laravel is a Web application framework known for its expressive and elegant syntax. Laravel is committed to providing an excellent development experience while offering powerful features, such as full dependency injection, an expressive database abstraction layer, queues and scheduled tasks, unit and integration testing, etc.
This guide describes how to deploy the Laravel application on Tencent CloudBase through multiple approaches:
Create a Laravel Application
Note: If you already have a Laravel application and have ensured that the Dockerfile is in the root directory, you can skip this step.
Create a Laravel application. Ensure that PHP and Composer are installed locally.
After installing PHP and Composer, you can create a new Laravel project using Composer's create-project command:
composer create-project laravel/laravel cloudrun-laravel
Start the php service
Execute php-fpm to start the php service. The service access address is 127.0.0.1:9000.
Configure the Nginx default.conf
server {
listen 80;
index index.php index.html;
server_name localhost;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /\.ht {
deny all;
}
}
Place the default.conf file in the Nginx conf.d directory, execute nginx -s reload, start the Nginx service, then you can access the php service via http://127.0.0.1.
Deploying from Dockerfile
- Create an nginx directory under the
cloudrun-laravelproject. Then, under the nginx directory, add thedefault.confandsupervisord.conffiles respectively, as follows:
default.conf
server {
listen 80;
index index.php index.html;
server_name localhost;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /\.ht {
deny all;
}
}
supervisord.conf
[supervisord]
nodaemon=true
[program:php-fpm]
command=/usr/local/sbin/php-fpm
[program:nginx]
command=/usr/sbin/nginx -g "daemon off;"
- Under the
cloudrun-laravelproject, create a Dockerfile with the following content:
# 1. Build stage: Install dependencies using composer install
FROM php:8.2-fpm-alpine AS build
# Install system dependencies and PHP extensions
RUN apk add --no-cache \
bash \
git \
unzip \
libzip-dev \
oniguruma-dev \
curl \
&& docker-php-ext-install pdo_mysql mbstring zip bcmath
# Install composer
COPY /usr/bin/composer /usr/bin/composer
WORKDIR /var/www/html
# Copy the project code
COPY . .
RUN composer install --no-dev --optimize-autoloader --no-interaction
# Generate optimized autoload files
RUN composer dump-autoload -o
# 2. Run stage: PHP-FPM image
FROM php:8.2-fpm-alpine
# Install PHP extensions
RUN apk add --no-cache libzip oniguruma-dev libpng libjpeg-turbo libwebp freetype \
&& apk add --no-cache --virtual .build-deps $PHPIZE_DEPS libzip-dev oniguruma-dev \
&& docker-php-ext-configure zip \
&& docker-php-ext-install pdo_mysql mbstring zip bcmath gd \
&& apk del .build-deps
WORKDIR /var/www/html
# Copy code and dependencies from the build stage
COPY /var/www/html /var/www/html
# Set permissions (adjust as needed)
RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache
# 3. Install and configure supervisord to start both php-fpm and nginx
RUN apk add --no-cache supervisor nginx
# Copy the nginx configuration
COPY nginx/default.conf /etc/nginx/conf.d/default.conf
# Copy the supervisord configuration
COPY nginx/supervisord.conf /etc/supervisord.conf
# Install composer
EXPOSE 80
# Start supervisord to manage php-fpm and nginx
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"]
With the above changes, your laravel application will be deployable to Tencent Cloud Hosting!
Deployment to Cloud Hosting
Cloud Hosting provides multiple deployment methods to deploy your application:
Deploy via Console
Open Tencent CloudBase, click Deploy via local code -> enter the service name -> select Upload code package for the deployment method -> select Folder for the code package type -> select the cloudrun-laravel directory for upload -> enter 8080 for the port -> click Create and wait for the creation to complete.
Deploying via cli
If you have installed CloudBase CLI, you can use the following command in the project directory to deploy the application to CloudBase Cloud Hosting:
tcb cloudrun deploy
After entering the environment and service names, the CLI will automatically package the application image and deploy it to Cloud Hosting.
Besides manual deployment, you can also install the aforementioned applications with one click: