如何在Live Server的cPanel中安装Composer以使用PHP Spreadsheet包?
Hey there! I’ve walked many developers through getting Composer set up on cPanel live servers, so let’s break this down into simple, actionable steps—you’ll have it running in no time to install PHP Spreadsheet.
Step 1: Access the Server’s Command Line
First, log into your cPanel account. Look for the Terminal icon (usually under the "Advanced" section) and click it—this opens a command-line interface directly in your browser, no need for external SSH tools. If you don’t see Terminal, check with your hosting provider to ensure shell access is enabled for your account.
Step 2: Confirm/Set a Compatible PHP Version
Composer (and PHP Spreadsheet) requires PHP 7.2.5 or newer. Let’s check your current version:
php -v
If the version is too old, head back to cPanel’s main dashboard, open Select PHP Version, choose a version like 7.4 or 8.0 (both work great with PHP Spreadsheet), and click "Set as Current". Then refresh your Terminal window to apply the change.
Step 3: Download the Official Composer Installer
Run this command to grab the official Composer setup script:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
This pulls the latest, trusted installer directly from Composer’s official source.
Step 4: Verify the Installer’s Integrity (Recommended)
To make sure the installer wasn’t tampered with, run this hash check:
php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
If you see Installer verified, you’re good to go. If not, re-run the download command from Step 3.
Step 5: Install Composer
You have two options here, depending on your server permissions:
Option A: Local Install (Most Reliable for cPanel Users)
This installs Composer directly in your current directory (usually your home folder or project root):
php composer-setup.php
You’ll now have a composer.phar file in your directory. To use Composer, you’ll run commands like php composer.phar [command].
Option B: Global Install (If You Have Permissions)
If your hosting allows global installs, run this to make Composer available system-wide:
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
After this, you can use composer [command] directly from any directory.
Step 6: Confirm Installation Success
Test that Composer is working:
- For local install:
php composer.phar -V - For global install:
composer -V
You should see output with the Composer version number—this means it’s installed correctly!
Step 7: Install PHP Spreadsheet
Navigate to your project’s directory (use cd path/to/your/project in Terminal), then run:
- Local install:
php composer.phar require phpoffice/phpspreadsheet - Global install:
composer require phpoffice/phpspreadsheet
Composer will handle downloading and setting up the PHP Spreadsheet package for you automatically.
Troubleshooting Tips
- If you get permission errors, stick with the local install—cPanel user accounts typically have full permissions in their own directories.
- If the Terminal times out mid-install, try running the commands one at a time, or check with your host to see if they have any execution time limits in place.
内容的提问来源于stack exchange,提问作者user8814721

