Search code examples
terminalcomposer-phptypo3webserverweb-hosting

Why is my composer command not working in subdirectories on a 1and1 webhosting server?


I'm in the process of building a TYPO3 website on a webserver hosted by 1and1 ionos. I need to install TYPO3 via composer, so I installed composer to this webserver first. It seemed to work fine and I was able to do a composer reuqire to install TYPO3 to a new directory (named: typo3-composer) within the root of the server.

But for some reason I have two problems now:

  1. Within the root directory of my server I cannot run the "composer xx" command. It only works using the prefix "php composer xx"

  2. In my subdirectory typo3-composer (the one I installed TYPO3 into) I cannot run ANY composer command. Terminal simply says command composer not found.

What I did so far:

1. Install composer

curl -sS https://getcomposer.org/installer | /usr/bin/php8.0-cli

2.Start composer

/usr/bin/php8.0-cli composer.phar

3.Rename file

mv composer.phar composer

4.Tried to move the file to any directory (usr/local/bin) of the $PATHvariable to make composer globally accessible. This failed because all of those directories are read-only. So i made the file executable

chmod +x composer

5.And added the current path to the system's $PATH variable

export PATH=$PATH:$PWD

I'm pretty sure this is a very basic logial mistake. Where do I have to execute the composer install command and where do I have to put the composer file so that I can use the command from every directory?

(PS: I did set the PHP Version of the Shell to php 8.0 already so I don't have to use /usr/bin/php8.0-cli all the time)


Solution

  • Thanks for your well formulated question which I'd like to answer the following:

    If you would like to (really) execute Composer on the remote host1, you already have the answer albeit you might not be aware of it. To quote from your very own question:

    It only works using the prefix "php composer xx"

    This is fine and how PHP works on the command-line. However as you have noticed it depends on the directory you're in (the working direcory). Not for the command (php) but for the file you command php(1) to execute: "composer".

    Some more context if it helps: The first operand of the php command you entered, "composer", is the path to the file you want php(1) to execute.

    So when:

    user@host:~$ php composer info
    ...
    

    works and you then change the directory and it doesn't any longer:

    user@host:~$ cd typo3-composer
    user@host:~/typo3-composer$ php composer info
    Could not open input file: composer
    user@host:~/typo3-composer$ echo $?
    1
    

    then replacing composer with the actual path of it should work:

    user@host:~/typo3-composer$ php ../composer info
    ...
    

    before actually running php(1), you can also make use of the file(1) command to check first if a file exists and of what type it is:

    Not existing file:

    user@host:~/typo3-composer$ file composer
    composer: cannot open `composer' (No such file or directory)
    

    Existing "composer" file (with executable bits set):

    user@host:~/typo3-composer$ file ../composer
    ../composer: a /usr/bin/env php script executable (binary data)
    

    Composer phar (even with no executable bits):

    user@host:~/typo3-composer$ file ../composer
    ../composer: a /usr/bin/env php script executable (binary data)
    

    Composer is just a PHP file (sort-of, it is a PHP archive "phar") and the idiom to execute it under the name composer is not really necessary. The path (absolute or relative to your currend working directory "PWD"2) suffices. If you take a look in the Composer documentation for example, you find the following idiom:

    php composer.phar <options>|<operands>...
    

    TLDR: Composer works regardless how you invoke it, be it composer, php composer.phar or similar. The php(1) command only needs a pathname of the PHP file to execute, be it composer.phar or one of the typo3 command-line scripts (to give a different example).


    1. As user Nico Haase already has pointed out, there is no inherent requirement to execute composer(1) on the remote host.

      Composer interacts with the wide-area-network (WAN, also: WWW, internet) to download files and applies the result to the working directory (project directory), more specifically the vendor-dir (vendor directory, "vendor" relative to the composer.json configuration file by default).

      You normally then afterwards deploy that (local) project to the remote host. There is no requirement at all to do the dependency resolution on the remote host, quite the opposite: bring the work near to yourself so that you can much faster deal with any problems that may arise and make the deployment step dead simple (e.g. a single, simple transaction). Remote machines go offline, network connection may get lost in the middle and you may also need to (or want to) fully replace the remote host occasionally. All you should need to do is to re-deploy the project, not configuring remote systems and run commands there interactively. Life is too short for that.

      Now the Typo3 project might have thought otherwise (which would not be very user-friendly if you allow me the comment, but I know the typo3 folks are user-friendly for real) then you may go through additional hurdles, however those are less related to Composer (and how you invoke it) but more to Typo3 (then, if at all). But, as you don't present Typo3 as an issue in your question, I'm pretty sure you can just do the "Composer work" locally in your own shell which has the benefit that you can configure Composer and how you invoke it to your liking without the requirement to do this only short and half at best on every remote host you may come in contact with.

    2. PWD

      This variable shall represent an absolute pathname of the current working directory. It shall not contain any components that are dot or dot-dot. The value is set by the cd utility, and by the sh utility during initialization. [from]