如何通过Bitbucket Pipeline将Laravel项目部署至无Composer的共享主机?
Yes, this approach works perfectly!
You absolutely can build your full Laravel project in Bitbucket Pipelines (handling all dependencies, migrations, and asset compilation there) then deploy the ready-to-run code to your server via git-ftp—this is actually a great solution for servers that don't have Composer, Node.js, or other build tools installed. Let's fix up your pipeline configuration and cover key details to make this work smoothly.
First, adjust your bitbucket-pipelines.yml
Here's a refined version of your config with critical fixes and improvements:
image: php:7.2-fpm pipelines: branches: master: - step: caches: - composer - node script: # Install system dependencies (including git-ftp upfront) - apt-get update && apt-get install -y unzip gnupg ssh git-ftp # Install Composer globally - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer # Install required PHP extensions for Laravel (critical missing step!) - docker-php-ext-install mbstring pdo_mysql # Install Laravel dependencies for production (skip dev packages) - composer install --no-dev --optimize-autoloader # Set up Node.js and compile frontend assets - curl -sL https://deb.nodesource.com/setup_8.x | bash - - apt-get install -y nodejs - npm install - npm run production # Configure environment file - php -r "file_exists('.env') || copy('.env.pipelines', '.env');" - php artisan key:generate # Run database migrations (force to avoid interactive prompts) - php artisan migrate --force # Deploy via git-ftp: use "init" for first deploy, switch to "push" afterward # --all ensures we upload ignored files like vendor/ and compiled assets - git ftp init --user $FTP_USERNAME --passwd $FTP_PASSWORD --all $FTP_HOST_PATH
Key notes to avoid pitfalls:
- PHP Extensions: Your original config was missing Laravel-required extensions like
pdo_mysqlandmbstring—without these, Composer and Artisan commands will fail. Thedocker-php-ext-installline fixes this. - git-ftp
--allFlag: By default, git-ftp follows your.gitignorerules, which exclude critical directories likevendor/and compiled frontend assets. The--allflag overrides this to upload every file from your built project. - Migrate with
--force: Production migrations require the--forceflag to skip the interactive confirmation prompt (Pipelines can't respond to user input). Important: Ensure your Pipeline can access your database—if your DB is locked down to server-only access, you may need to whitelist Bitbucket's IPs or use an SSH tunnel to connect. - First Deploy vs. Subsequent Deploys: Use
git ftp initonly for your first deployment (it uploads all files). After that, replace it withgit ftp pushto only upload changed files—this is much faster. - Cache Optimization: Added the
nodecache to speed up repeatednpm installruns, just like the existingcomposercache.
Additional critical tips:
- Secure Environment Variables: Don't hardcode sensitive values (like DB passwords) in
.env.pipelines. Instead, use placeholders (e.g.,DB_PASSWORD=${DB_PROD_PASSWORD}) and add these variables to your Bitbucket repo's Settings > Repository Variables—they'll automatically inject into the Pipeline. - Server File Permissions: After deployment, make sure your server's
storage/andbootstrap/cache/directories have write permissions. You can add a command to the Pipeline (before deploy) likechmod -R 775 storage bootstrap/cache, or set permissions manually on the server. - Backup First: Before your first deploy, back up all existing files on your server to avoid accidental data loss.
内容的提问来源于stack exchange,提问作者Tomjesch
相关产品推荐
相关产品推荐

