Deploy Laravel 12 to InfinityFree Using GitHub Actions and FTP
This guide walks you through preparing and deploying a Laravel 12 application to InfinityFree using GitHub Actions and lftp. Dependencies are installed and optimized directly on the GitHub Actions runner, and your production environment is securely generated from GitHub Secrets—meaning you never commit or push the vendor/ folder or sensitive .env file to Git.
Prerequisites
- Laravel 12 application running locally
- PHP, Composer, and Git installed
- Visual Studio Code or your preferred code editor
- GitHub account and repository for your project
- InfinityFree hosting account with FTP access
- Optional: MySQL database on InfinityFree if your app uses a database
1. Prepare Your Laravel Project Locally
If you don't have a project yet, create a fresh Laravel 12 application and verify it works:
composer create-project laravel/laravel laravel-app
cd laravel-app
php artisan serveVisit http://localhost:8000 to confirm your app loads. Keep your local .env configured for your local development environment—you do not need to change your local .env to production credentials.
2. Set Up MySQL Database on InfinityFree (If Needed)
If your application requires a database, create it directly in your InfinityFree panel:
- Log in to the InfinityFree Control Panel
- Navigate to MySQL Databases
- Create a new database and note down the details:
- Database Name: e.g.
epiz_XXXXXXXX_dbname - Host Name: e.g.
sqlXXX.epizy.com - Username: e.g.
epiz_XXXXXXXX - Password: your InfinityFree account password
- Port:
3306
- If you have existing local data or migrations, export your database and import it using InfinityFree’s phpMyAdmin under the Import tab.
Keep these database credentials handy—we will add them into GitHub Secrets in Step 5.
3. Add Root .htaccess for Routing & Security
Because InfinityFree fixes the document root strictly to htdocs/, you must place an .htaccess file directly in your project root (not inside public/). This file serves two critical purposes: it routes traffic into public/index.php and blocks web visitors from directly reading sensitive files like .env:
<IfModule mod_rewrite.c>
RewriteEngine On
# Prevent direct access to sensitive hidden files like .env
RewriteRule ^\.env - [F,L,NC]
RewriteRule ^\.(?!well-known).* - [F,L,NC]
# Route all traffic into the public/ directory
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>Save this file as .htaccess in your project root.
4. Add Your Domain on InfinityFree
InfinityFree organizes your web directories depending on whether you deploy to your primary domain or an addon domain:
| Domain Type | Server Directory Path |
|---|---|
| Main domain | htdocs/ (or /htdocs/) |
| Addon domain | yourdomain.com/htdocs/ (or /yourdomain.com/htdocs/) |
Make sure the domain is created in your control panel before running deployment. You don't need to hardcode this path in your repository—we'll define it as a secret named FTP_SERVER_DIR in the next step so the workflow is completely reusable without touching code.
5. Configure GitHub Repository Secrets
Your production environment configuration, FTP credentials, and target deployment path are stored safely in GitHub Secrets. Nothing sensitive is ever committed to Git.
- Go to your GitHub repository
- Navigate to Settings > Secrets and variables > Actions
- Click New repository secret and add the following secrets:
FTP_USERNAME→ Your InfinityFree FTP username (e.g.epiz_12345678)FTP_PASSWORD→ Your InfinityFree FTP passwordFTP_SERVER_DIR→ Your target server directory path. Set tohtdocs/for the main domain (or leave unset, as it defaults tohtdocs/), oryourdomain.com/htdocs/for an addon domain.PRODUCTION_ENV_FILE→ The entire contents of your production.envfile (containing your InfinityFree MySQL database credentials and generatedAPP_KEY)
Example for PRODUCTION_ENV_FILE:
APP_NAME="Laravel"
APP_ENV=production
APP_KEY=base64:YOUR_GENERATED_APP_KEY_HERE
APP_DEBUG=false
APP_URL=https://yourdomain.com
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=sqlXXX.epizy.com
DB_PORT=3306
DB_DATABASE=epiz_XXXXXXXX_dbname
DB_USERNAME=epiz_XXXXXXXX
DB_PASSWORD=your_infinityfree_password
SESSION_DRIVER=file
QUEUE_CONNECTION=sync
CACHE_STORE=fileDuring deployment, GitHub Actions reads PRODUCTION_ENV_FILE and writes it to .env on the runner before deploying.
6. Create the GitHub Actions Workflow
Create your workflow file in the repository at .github/workflows/infinity-free.yml:
mkdir -p .github/workflows
nano .github/workflows/infinity-free.ymlPaste the complete deployment configuration below:
name: Deploy to InfinityFree
# Run manually or on push to the main branch
on:
workflow_dispatch:
push:
branches:
- main
jobs:
deploy:
name: Deploy via lftp
runs-on: ubuntu-latest
steps:
# Step 1: Clone the latest code from GitHub
- name: Get latest code
uses: actions/checkout@v4
# Step 2: Set up PHP and Composer
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.4'
tools: composer
coverage: none
# Step 3: Cache Composer packages
- name: Get Composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> "$GITHUB_OUTPUT"
- name: Cache Composer dependencies
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
# Step 4: Generate production .env from GitHub Secret
- name: Create production .env file
env:
PRODUCTION_ENV_FILE: ${{ secrets.PRODUCTION_ENV_FILE }}
run: |
if [ -z "$PRODUCTION_ENV_FILE" ]; then
echo "ERROR: PRODUCTION_ENV_FILE secret is missing."
exit 1
fi
printf '%s' "$PRODUCTION_ENV_FILE" > .env
chmod 600 .env
# Step 5: Install production dependencies only
- name: Install Composer dependencies
run: composer install --no-dev --prefer-dist --optimize-autoloader --no-interaction --no-scripts
# Step 6: Strip unnecessary bloat from vendor to dramatically reduce file count
- name: Optimize vendor directory for upload
run: |
echo "Pruning non-production files from vendor..."
find vendor -type d \( -name "tests" -o -name "test" -o -name "docs" -o -name ".github" \) -exec rm -rf {} + 2>/dev/null || true
find vendor -type f \( -name "*.md" -o -name "*.txt" -o -name "phpunit.xml*" -o -name ".travis.yml" -o -name "changelog*" \) -delete 2>/dev/null || true
# Step 7: Run Laravel package discovery if artisan CLI exists
- name: Run Laravel package discovery
run: |
if [ -f artisan ]; then
php artisan package:discover --ansi
fi
# Step 8: Install lftp
- name: Install lftp
run: sudo apt-get update && sudo apt-get install -y lftp
# Step 9: Deploy to InfinityFree via lftp
- name: Deploy to InfinityFree via lftp
env:
FTP_SERVER: ftpupload.net
FTP_USERNAME: ${{ secrets.FTP_USERNAME }}
FTP_PASSWORD: ${{ secrets.FTP_PASSWORD }}
FTP_SERVER_DIR: ${{ secrets.FTP_SERVER_DIR }}
run: |
# Remove local git and github metadata so they are never scanned or uploaded
rm -rf .git .github
TARGET_DIR="${FTP_SERVER_DIR:-htdocs}"
echo "Deploying to remote directory: $TARGET_DIR"
lftp <<EOF
set net:timeout 30
set net:max-retries 3
set ftp:ssl-allow false
set ftp:passive-mode true
open -u "$FTP_USERNAME","$FTP_PASSWORD" "$FTP_SERVER"
cd "$TARGET_DIR"
mirror --reverse \
--only-newer \
--parallel=2 \
--no-perms \
--no-symlinks \
--verbose \
--exclude-glob tests/ \
--exclude-glob node_modules/ \
--exclude-glob *.backup \
--exclude README.md \
./ .
bye
EOF
# Step 10: Remove temporary .env from GitHub runner
- name: Clean up temporary environment file
if: always()
run: rm -f .envCompletely Config-Free: Because
FTP_SERVER_DIRis read from GitHub Secrets (defaulting tohtdocsif unset), you never have to edit the workflow file or touch the codebase when switching between main domains and addon domains.
7. Commit and Push to GitHub
Make sure your standard .gitignore is in place (ignoring /vendor and .env). Because dependencies are built in CI and .env is created from GitHub Secrets, your repository remains clean, lightweight, and completely free of sensitive secrets:
git init
git add .
git commit -m "feat: configure automated InfinityFree deployment with lftp"
git remote add origin https://github.com/your-username/your-repo.git
git branch -M main
git push -u origin main8. Automated Deployment & Verification
Every time you push to the main branch, GitHub Actions will:
- Check out your code.
- Generate your production
.envfile directly fromPRODUCTION_ENV_FILE. - Install production Composer dependencies and cache them for future runs.
- Prune vendor test files and documentation.
- Deploy updated files to InfinityFree with
lftpusing 2 parallel streams without setting restricted permissions.
You can monitor the deployment progress in the Actions tab of your GitHub repository.
Deployment Notes & Best Practices
- Never Commit
vendor/or.env: Your Git repository stays lightweight (typically under 2 MB), and sensitive database credentials are never stored in your git commit history. - Fast Incremental Deployments: The initial upload transfers the dependencies (~10–15 minutes due to InfinityFree's disk I/O rate limits). Once on the server, routine code changes take only 15–30 seconds because
lftp --only-newerskips all unchanged vendor files. - Inode Limit Protection: The vendor pruning step removes thousands of testing, documentation, and markdown files, easily preserving your account's 30,019 inode limit.
- Permission Handling: The
--no-permsflag preventslftpfrom issuingSITE CHMODcommands, avoiding550 Could not change permserrors on shared hosting.
Summary
| Step | Status |
|---|---|
| Laravel project created and tested locally | Done |
| MySQL database set up on InfinityFree | Done |
Root .htaccess added for routing & .env protection |
Done |
Standard .gitignore maintained (vendor & .env kept out of Git) |
Done |
| Domain configured on InfinityFree | Done |
FTP credentials, FTP_SERVER_DIR & PRODUCTION_ENV_FILE added to GitHub Secrets |
Done |
GitHub Actions workflow created with Composer & lftp |
Done |
| Lightweight code committed and pushed to GitHub | Done |
| Automated deployment verified in GitHub Actions | Done |