---
title: "Tự động hóa deploy backend Laravel lên Cloud VPS bằng Ansible AWX"
description: "Hướng dẫn tự động hóa deploy backend Laravel (Composer, Artisan migrate, Queue worker, PHP-FPM/Octane) lên Cloud VPS bằng Ansible AWX zero-downtime."
canonical: "https://thuecloud.vn/tu-dong-hoa-deploy-backend-laravel-len-cloud-vps-bang-ansible-awx"
markdown_url: "https://thuecloud.vn/tu-dong-hoa-deploy-backend-laravel-len-cloud-vps-bang-ansible-awx.md"
locale: "vi"
kind: "article"
published_at: "2026-11-22T16:18:19.046Z"
updated_at: "2026-11-22T16:18:18.845Z"
site: "ThueCloud.Vn"
estimated_tokens: 2370
---
# Tự động hóa deploy backend Laravel lên Cloud VPS bằng Ansible AWX

> Hướng dẫn tự động hóa deploy backend Laravel (Composer, Artisan migrate, Queue worker, PHP-FPM/Octane) lên Cloud VPS bằng Ansible AWX zero-downtime.

- Canonical HTML: https://thuecloud.vn/tu-dong-hoa-deploy-backend-laravel-len-cloud-vps-bang-ansible-awx
- Markdown URL: https://thuecloud.vn/tu-dong-hoa-deploy-backend-laravel-len-cloud-vps-bang-ansible-awx.md
- Organization: ThueCloud.Vn
- Support Email: support@thuecloud.vn
- Hotline: 03333 499 48
- Published: 2026-11-22
- Updated: 2026-11-22

## Mục lục

- Cấu trúc thư mục chuẩn Zero-Downtime cho Laravel trên VPS
- Playbook Ansible AWX triển khai Laravel Backend
- Bảng lưu ý các lệnh Artisan dễ gây sự cố khi deploy
- Chạy Laravel Production siêu mượt trên VPS NVMe ThueCloud.Vn
- Câu hỏi thường gặp (FAQ)
  - Tại sao đã trỏ symlink current sang thư mục mới nhưng web vẫn chạy code cũ?
  - Nên giữ lại bao nhiêu thư mục cũ trong releases/?
  - Chạy composer install trực tiếp trên VPS nhỏ có bị thiếu RAM không?
  - Làm sao quản lý tiến trình php artisan queue:work tự khởi động lại sau queue:restart?

Triển khai thủ công một dự án **Laravel** bằng cách SSH vào máy chủ chạy `git pull`, `composer install` rồi `php artisan migrate` thường tạo ra khoảng thời gian lỗi 500 trong lúc vendor đang tải dở hoặc cache route chưa được làm mới. Sử dụng **Ansible AWX** để tự động hóa quy trình deploy theo mô hình thư mục `releases/` + symlink `current/` lên [Cloud VPS Việt Nam](https://thuecloud.vn/vps-viet-nam) giúp ứng dụng chuyển phiên bản mượt mà gần như **zero-downtime**.

> **Tóm tắt nhanh (TL;DR):**
> - **Cấu trúc Atomic Symlink:** Mỗi lần deploy tạo thư mục mới `/var/www/app/releases/<timestamp>`, chạy xong hết các bước chuẩn bị mới trỏ symlink `current` sang.
> - **Chia sẻ dữ liệu bền vững:** Thư mục `storage/` và file `.env` nằm ở `/var/www/app/shared/` và được link vào từng bản release.
> - **Làm mới OPcache & Queue:** Bắt buộc reload `php-fpm` (hoặc `octane`) và chạy `php artisan queue:restart` sau khi đổi symlink để worker nhận code mới.
> - *Cập nhật: 11/2026*

## Cấu trúc thư mục chuẩn Zero-Downtime cho Laravel trên VPS

```text
/var/www/laravel-api/
├── current -> /var/www/laravel-api/releases/20261122103000
├── releases/
│   ├── 20261121090000/
│   └── 20261122103000/
└── shared/
    ├── .env
    └── storage/
```

Mô hình này giúp bạn rollback ngay lập tức chỉ bằng cách trỏ lại symlink `current` về thư mục release cũ nếu phát hiện lỗi sau khi lên phiên bản mới.

## Playbook Ansible AWX triển khai Laravel Backend

Tạo file `deploy-laravel.yml` để Ansible AWX thực hiện toàn bộ chu trình một cách nhất quán trên một hoặc nhiều [giải pháp hạ tầng](https://thuecloud.vn/giai-phap) máy chủ:

```yaml
---
- name: Zero-Downtime Deploy Laravel Backend
  hosts: laravel_prod
  become: true
  vars:
    base_dir: /var/www/laravel-api
    release_dir: "{{ base_dir }}/releases/{{ ansible_date_time.iso8601_basic_short }}"

  tasks:
    - name: Clone code repository into new release directory
      ansible.builtin.git:
        repo: "{{ git_repo_url }}"
        dest: "{{ release_dir }}"
        version: "{{ git_branch | default('main') }}"

    - name: Link shared .env and storage
      ansible.builtin.file:
        src: "{{ item.src }}"
        dest: "{{ release_dir }}/{{ item.dest }}"
        state: link
        force: true
      loop:
        - { src: "{{ base_dir }}/shared/.env", dest: ".env" }
        - { src: "{{ base_dir }}/shared/storage", dest: "storage" }

    - name: Install production Composer dependencies
      community.general.composer:
        command: install
        working_dir: "{{ release_dir }}"
        no_dev: true
        optimize_autoloader: true

    - name: Run database migrations and cache config
      ansible.builtin.shell: |
        php artisan migrate --force
        php artisan config:cache
        php artisan route:cache
        php artisan view:cache
      args:
        chdir: "{{ release_dir }}"

    - name: Switch current symlink atomically
      ansible.builtin.file:
        src: "{{ release_dir }}"
        dest: "{{ base_dir }}/current"
        state: link
        force: true

    - name: Reload PHP-FPM and gracefully restart Queue Workers
      ansible.builtin.shell: |
        systemctl reload php8.3-fpm
        php {{ base_dir }}/current/artisan queue:restart
```

## Bảng lưu ý các lệnh Artisan dễ gây sự cố khi deploy

| Lệnh Artisan | Rủi ro nếu chạy sai thứ tự | Cách xử lý chuẩn trong Playbook |
| :--- | :--- | :--- |
| `php artisan migrate --force` | Đổi cột DB trước khi code mới nhận traffic | Viết migration tương thích ngược (expand-and-contract) |
| `php artisan config:cache` | Chạy trước khi link `.env` làm cache rỗng | Luôn link `shared/.env` trước khi chạy `config:cache` |
| `php artisan queue:restart` | Quên chạy làm worker vẫn thực thi code cũ trong RAM | Đặt ở bước cuối cùng sau khi đổi symlink `current` |

Sau khi deploy backend Laravel thành công, bạn nên cấu hình [Cloudflare Origin Certificate 15 năm cho kết nối Full Strict](https://thuecloud.vn/cau-hinh-cloudflare-origin-certificate-15-nam-cho-ket-noi-full-strict-toi-vps), [triển khai AI Agent tóm tắt tài liệu PDF với chi phí dưới 100k/tháng](https://thuecloud.vn/ung-dung-ai-agent-va-automation-vao-tom-tat-tai-lieu-pdf-va-trich-xuat-du-lieu-bang-voi-chi-phi-duoi-100k-thang) cho các nghiệp vụ văn phòng, và thiết lập [sao lưu tự động cho hệ thống đặt lịch phòng khám](https://thuecloud.vn/thiet-lap-sao-luu-tu-dong-va-khoi-phuc-nhanh-cho-dat-lich-phong-kham).

## Chạy Laravel Production siêu mượt trên VPS NVMe ThueCloud.Vn

> **Tối ưu tốc độ `composer install` và phản hồi API Laravel với ổ cứng NVMe!**  
> Hạ tầng **KVM Enterprise NVMe** tại `vn-hn` của ThueCloud.Vn cam kết **SLA 99,9%**, **VAT 0%**, **hoàn tiền 07 ngày**.  
> - **VPS VN 2:** `78.000đ/tháng` (năm) / `90.000đ/tháng` — chạy tốt Laravel API + PostgreSQL/MySQL + Redis.  
> - **VPS VN 3:** `139.000đ/tháng` (năm) / `170.000đ/tháng` — dư sức chạy đa Queue Worker và Horizon.  
> 👉 Xem chi tiết tại [Bảng giá ThueCloud](https://thuecloud.vn/pricing) hoặc [Cloud VPS Việt Nam](https://thuecloud.vn/vps-viet-nam).

## Câu hỏi thường gặp (FAQ)

### Tại sao đã trỏ symlink `current` sang thư mục mới nhưng web vẫn chạy code cũ?
Do **PHP OPcache** lưu bytecode của file PHP trong bộ nhớ kèm đường dẫn đã resolve (realpath cache). Bạn bắt buộc phải chạy `systemctl reload php8.3-fpm` (hoặc restart Octane) ngay sau khi đổi symlink để xóa OPcache cũ.

### Nên giữ lại bao nhiêu thư mục cũ trong `releases/`?
Thông thường nên giữ lại 3 đến 5 bản release gần nhất để rollback nhanh khi cần, và thêm một task ở cuối Playbook tự động xóa các thư mục cũ hơn để không làm đầy dung lượng ổ NVMe.

### Chạy `composer install` trực tiếp trên VPS nhỏ có bị thiếu RAM không?
Với các dự án Laravel lớn, Composer có thể tốn 500MB - 1GB RAM. Bạn nên bật thêm Swap an toàn (1-2GB) trên VPS hoặc build sẵn thư mục `vendor` trên CI runner rồi đồng bộ artifact xuống VPS.

### Làm sao quản lý tiến trình `php artisan queue:work` tự khởi động lại sau `queue:restart`?
Lệnh `queue:restart` chỉ ra tín hiệu để worker thoát ra một cách êm đẹp sau khi xử lý xong job hiện tại. Bạn cần cấu hình **Supervisor** hoặc **systemd** để tự động khởi động lại tiến trình worker mới ngay khi tiến trình cũ vừa thoát.

## References / Sources

- https://laravel.com/docs/11.x/deployment
- https://ansible.readthedocs.io/projects/awx/en/latest/
- https://docs.ansible.com/ansible/latest/collections/community/general/composer_module.html

---

## Navigation & Machine-Readable Endpoints

- [Site Summary (llms.txt)](https://thuecloud.vn/llms.txt)
- [Full Technical Reference (llms-full.txt)](https://thuecloud.vn/llms-full.txt)
- [Pricing Catalog (Markdown)](https://thuecloud.vn/pricing.md)
- [Blog & Technical Guides (Markdown)](https://thuecloud.vn/blog.md)
- [XML Sitemap](https://thuecloud.vn/sitemap.xml)
