Knowledgebase:
How to install composer on shared servers.
Posted by Hasan B on 31 October 2019 03:59:29 PM


Composer is a dependency manager for the PHP programming language. It functions as a project manager that helps the programmer manage dependencies that will be used on a project to project basis. Dependency is an engineering term used when a piece of software relies on another one.

After installing composer, you can install various packages from the composer repository and use the appropriate versions of the same in your PHP code.

Installation steps:

1.Make sure allow_url_fopen is enabled under cPanel>Software>MultiPHP editor>Basic Mode:

Please check the below illustration:




2. Make sure that the PHP version is set to 5.5 and above in cPanel>Software>MultiPHP Manager.

3. SSH into your account space on the shared server. You can follow the detailed steps in this guide:

~~~~~~~
https://manage.resellerclub.com/kb/node/1817
~~~~~~~~

4.. Go to cPanel>Files>File Manager and create a php.ini file in the folder you wish to install composer under with the below code:

------------
suhosin.executor.include.whitelist = phar
detect_unicode = Off
allow_url_fopen=On
--------------

5. Navigate to the directory you wish to install composer under. For example, if you wish to install composer under the ‘public_html’ folder, you may navigate to /home/username/public_html with the below command

~~~
$cd /home/username/public_html
~~~

If you wish to install composer on a custom folder, you can create one and change the working directory to the same:

~~~~~
mkdir /home/username/public_html/composer
cd /home/username/public_html/composer
~~~~~~

6. Then run the following command in your SSH terminal to verify the PHP version:

~~~~~~
php -v
~~~~~~~

7. Run the below command to install composer:

~~~~~
curl -sS https://getcomposer.org/installer | php
~~~~~~

Please check the below illustration:



8. Verify the composer version using the below command:

~~~~~
composer -V
~~~~~~

Please check the below illustration:



9. To run composer commands, execute the below command.

~~~~~~
php composer.phar
~~~~~~~



Installing Packages with composer:

Visit packagist.org, composer’s default repository for packages. The package name consists of a vendor name and the project's name. In most cases, the vendor and the project name will be similar.

You can follow the below generic steps to install a package:

1. Change the working directory to the path where composer is installed:

~~~~~~
cd /home/username/path
~~~~~~

a). Run the below command

~~~~~
composer require projectname/vendorname
~~~~~

You can verify that the composer.json file has been updated with the following code by following the command below:
----------------
#cat composer.json

code:

{
"require": {
"project/vendor": "version"
}
}
----------------

b). Composer will generate an “autoload.php” file under the “vendor” directory by default. In your PHP project files, simply add the following line before the rest of your code:

-----------
require '/home/username/composer_install_path/vendor/autoload.php’
------------


Sample Installation: PhpMailer.

1. The vendor/package combination for phpmailer is phpmailer/phpmailer as per https://packagist.org/packages/phpmailer/phpmailer

Steps:

a) #composer require phpmailer/phpmailer



b)#cat composer.json



c) In your email script, add the below code:

~~~~
require '/home/username/composer_install_path/vendor/autoload.php’
~~~~

(0 vote(s))
Helpful
Not helpful

Comments (0)
Post a new comment
 
 
Full Name:
Email:
Comments: