This week we’ve already provided the instruction on how to install PHP Composer via SSH, and today let’s see how to set up and run this dependencies management tool using intuitive Jelastic UI. In addition, you’ll find out how to enable a special authentication to prevent unauthorized access to this tool.
Install PHP Composer
To start with, let’s upload Composer into the project working directory and create essential files:
1. Log in to the Jelastic dashboard with your credentials and navigate to your Apache PHP or NGINX PHP server file manager (Apache 2.4 in our example) by clicking the Config button next to the compute node.
2. In the opened file manager, go to the var > www > webroot > ROOT directory and select the Upload option from the expandable actions list at the top.
3. Within the appeared dialog box, specify the following URL and click the Upload button in order to add the Composer archive to the directory:
https://getcomposer.org/composer.phar
After uploading is finished, your Composer is ready to work!
Configure Composer Workflow
1. Create composer.php file in the var > www > webroot > ROOT directory and paste the lines below to it:
<?php
$argument = $_GET['arg'];
$command = "php composer.phar";
$pwd = getcwd();
putenv("COMPOSER_HOME=$pwd");
echo '<pre>';
// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
system("{$command} {$argument} 2>&1", $retval);
// Printing additional info
echo "
</pre>
<hr />Return value from \"$command $argument\": $retval<br>
Working dir: $pwd";
?>
Save the file with the same-named button above the editor.
2. Then create an .htaccess file in the same directory to ensure the proper workflow of the Composer tool.
Press Enter to save this file after typing its name.
Add User Authentication
In order to enhance Composer security, you can supplement it with user authentication. This will restrict access manager configurations so no one else could control dependencies within your projects.
1. Create one more .htpasswd file in the var > www > webroot > ROOT app server directory. Here, you can specify login and password to access Composer.
As an example, we set admin/admin credentials by inserting the admin: YjeJLJ0SYlBHU line into the file.
Don’t forget to Save the changes you’ve made.
2. Now, navigate to the httpd.conf Apache configuration file (Favorites > conf folder), scroll down to its very end and add the following content:
<Location /composer.php>
AuthName "Restricted area"
AuthType Basic
AuthBasicProvider file
AuthUserFile /var/www/webroot/ROOT/.htpasswd
Require valid-user
</Location>
location = /composer.php { root /var/www/webroot/ROOT; auth_basic "closed site"; auth_basic_user_file /var/www/webroot/ROOT/.htpasswd; include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; fastcgi_param DOCUMENT_ROOT $realpath_root; fastcgi_param PATH_INFO $fastcgi_script_name; }
Save the changes.
3. Restart your Apache server in order to apply new password policy.
Now, composer.php page is protected from unauthorized access.
Run Composer Dependencies Management Tool
When all necessary configurations are made, you can run Composer right via browser using the created composer.php page. For that, specify the required command within the ?arg={command} suffix, added to the link end.
http://{env_name}/composer.php?arg={command}
where
- {env_name} - the domain name of your environment
- {command} - command parameters stated using URL encoding to avoid whitespaces and other specific characters
Example:
1. Let’s add monolog/monolog:1.0.* dependencies to our project using the Composer require command:
http://composer.jelastic.com/composer.php?arg=require%20monolog%2Fmonolog%3A1.0.*
2. If using authentication, you’ll be asked to Log In in order to confirm your identity, i.e. to enter the previously configured credentials (admin/admin, in our case).
3. Upon successful login, Composer will create the appropriate .json file and start loading the declared packages from the appropriate repositories to install dependencies.
That’s all! Now, with the help of Composer you can effortlessly manage dependencies within your PHP projects on Apache or NGINX application server right from the Jelastic dashboard.
If you face any issues while installing PHP Composer to Jelastic, feel free to appeal for our technical experts’ assistance at Stackoverflow.