Nest Nepal Blogs

Web Hosting Blog by Nest Nepal | Domain & Hosting Tips

4 Simple Steps to Set Up PHP Composer on cPanel Shared Hosting

php composer

PHP Composer in shared hosting helps in dependency management of modern PHP applications much easier and efficiently. Composer lets you install and manage packages on the server without uploading a large vendor folder every time you update your project – with simple terminal commands.

With Laravel, Symfony, WordPress plugins, or custom PHP projects, PHP Composer can help you automate the installation of libraries, update the packages, and ensure that you use the same version of your package in production as in development. This will reduce upload time, bandwidth usage, and reduce file corruption problems if any while deploying on a shared server.

The version control is another great benefit of Composer when working with shared hosting. Composer ensures that you get the right versions of your packages installed each time, avoiding compatibility problems and broken apps, by using composer.json and composer.lock.

What You Need Before Starting

Before installing make sure your hosting environment supports the required tools and configurations.

Enabling SSH Access in cPanel

To install composer on shared hosting, your cPanel account must enable SSH access. SSH enables you to directly connect to your server through command line and securely run Composer commands.

To enable SSH access, most hosting providers have cPanel settings which make it easier to be done. If SSH isn’t enabled, get in touch with your provider and request that they do so. 

Checking Your Current PHP Version

Check that the above PHP version is compatible before installing PHP Composer on your server.

Run the following command:

php -v

Latest version of composer usually needs PHP 8.2+, if you use the modern versions. Compatibility problems with many contemporary frameworks and packages or dependency installation failures can result from an out-of-date version of PHP.

Ensuring allow_url_fopen is Enabled

Composer downloads packages from external repositories, so you need to have allow_url_fopen  enabled on your shared hosting account.

Check the setting using: 

php -i | grep allow_url_fopen

If it displays ‘On’ you are good to proceed to installing Composer on your server. 

Step-by-Step Guide: Installing Composer 

Installation of PHP Composer in the shared hosting server requires some terminal commands. 

Step 1: Connecting to Your Server via Terminal

Use terminal and connect to the hosting server via SSH:

ssh [email protected]

Enter username and domain with your hosting details.

Step 2: Downloading the Composer Installer

Download the Composer installer directly from the source:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

This will download the latest version of composer installer from the official composer website

Step 3: Verifying the Installation File 

You can check the installer script before execution, for safety reasons.

php -r "if (hash_file('sha384', 'composer-setup.php') === 'c8b085408188070d5f52bcfe4ecfbee5f727afa458b2573b8eaaf77b3419b0bf2768dc67c86944da1544f06fa544fd47') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

Note: The hash above is for the current version. If it fails, copy the latest ‘Installation Verification’ code from the official Composer download page

Step 4: Running the Installer Script

Install PHP Composer by running:

mkdir -p ~/bin
php composer-setup.php --install-dir=$HOME/bin --filename=composer

This command installs Composer directly into your local bin directory and renames it to composer for easy access.

Finally, remove the installer script:

rm composer-setup.php

Configuring Composer  for Global Use

For making Composer global on your shared hosting account, install Composer into your executable path. 

Updating Your .bashrc or .bash_profile

Add your local bin directory to the PATH variable:

For .bash_profile

nano ~/.bash_profile

For .bashrc

nano ~/.bashrc

Add this line:

export PATH="$HOME/bin:$PATH"

Save and close the file.

Refreshing the Shell Environment

Apply the changes immediately: 

source ~/.bashrc
source ~/.bash_profile

The composer command is now recognized account-wide on your shared hosting account.

If the command still isn’t recognized, try logging out of your SSH session and logging back in.

Testing Your Setup

Testing is a wonderful way to make sure everything is functioning.

Running the composer –version Command

Run:

composer --version

If it’s working correctly, the composer version will be shown in the terminal. 

Creating Your First composer.json File 

In your project directory: 

cd public_html/myproject

Initialize Composer:

composer init

This will create a composer.json file where the dependencies to be managed will be stored for the PHP project. 
To install packages later, run:

composer install

To add new dependencies:

composer require vendor/package

Troubleshooting Common Issues 

When installing PHP Composer on shared hosting, the following problems may arise: 

“Command Not Found” Errors 

If you receive:

composer: command not found

Your PATH may not be correctly configured. Confirm that: 

~/bin/composer exists

It is executable

Your PATH includes the directory ~/bin 

PHP Version Mismatch Issues

A common problem with shared hosting accounts is that PHP may be set to different versions in your web-based interface and your SSH terminal. Check your terminal version:

php -v

If necessary, declare a different PHP binary when running Composer.

For example, if your terminal defaults to PHP 7.4 but you need 8.2, run composer like this:

/usr/local/bin/ea-php82 ~/bin/composer install

Memory Limit Exhaustion During Install

You may run out of memory during installation of Composer on larger PHP projects. Disable the memory limit temporarily:

COMPOSER_MEMORY_LIMIT=-1 composer install

This is particular helpful on shared hosting accounts which struggle with the PHP version when using Laravel and Symfony.

Conclusion

PHP Composer is one of the best ways to efficiently and securely manage PHP dependencies on shared hosting. Composer simplifies package installation, automates updates, and ensures consistent dependency versions across all settings. 

To keep a PHP project safe and efficient: 

  • Keep PHP Composer updated regularly
  • Before installing, confirm the installer hashes. 
  • Use composer.lock in production
  • Remove unused packages
  • Conduct a composer audit to verify security. 
  • Don’t edit vendor files manually.

Composer on shared hosting can greatly enhance productivity, deployment speed, and project stability for contemporary PHP applications with the right configuration and upkeep.  

Leave a Reply

Your email address will not be published. Required fields are marked *