Last active
September 24, 2025 14:15
-
-
Save stefanpejcic/9b22bd6508395509d0d3da4151d843ca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| # Function to print error and exit | |
| die() { | |
| echo "ERROR: $*" >&2 | |
| exit 1 | |
| } | |
| # Check if running as root | |
| if [[ $EUID -ne 0 ]]; then | |
| die "This script must be run as root" | |
| fi | |
| # (Optional) check that php CLI exists | |
| if ! command -v php &> /dev/null; then | |
| die "php CLI not found. Please install or enable php CLI." | |
| fi | |
| # 1. Check allow_url_fopen is enabled in the default PHP version | |
| # 2. Download composer installer | |
| echo "Downloading Composer installer..." | |
| php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" | |
| # 3. Download the signature for verification | |
| echo "Downloading installer signature..." | |
| php -r "copy('https://composer.github.io/installer.sig', 'composer.sig');" | |
| # 4. Verify the installer | |
| echo "Verifying installer..." | |
| php -r "if (hash_file('sha384', 'composer-setup.php') === trim(file_get_contents('composer.sig'))) { echo 'Installer verified' . PHP_EOL; } else { echo 'Installer corrupt' . PHP_EOL; unlink('composer-setup.php'); exit(1); }" | |
| # 5. Run installer | |
| echo "Running installer..." | |
| php composer-setup.php | |
| # 6. Move the resulting phar to a system‐wide location | |
| echo "Installing composer to /usr/local/bin/composer ..." | |
| mv -v composer.phar /usr/local/bin/composer | |
| # 7. Clean up installer files | |
| echo "Cleaning up..." | |
| php -r "unlink('composer-setup.php'); unlink('composer.sig');" | |
| echo "Composer has been installed successfully." | |
| # Extra: for CloudLinux servers with CageFS, add composer to CageFS map | |
| if command -v cagefsctl &> /dev/null; then | |
| echo "Detected cagefsctl — assuming CloudLinux with CageFS. Configuring composer in CageFS..." | |
| # create composer.cfg | |
| cat <<EOF > /etc/cagefs/conf.d/composer.cfg | |
| [composer] | |
| comment=Composer https://getcomposer.org/ | |
| paths=/usr/local/bin/composer | |
| EOF | |
| # rebuild cagefs | |
| echo "Rebuilding CageFS..." | |
| cagefsctl --force-update | |
| echo "CageFS configuration updated." | |
| fi | |
| echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment