Developing software in the Real World

Using Composer with shared hosting

I can't use Composer because I'm using shared hosting and don't have SSH

I've seen this sentiment a few times now, so this seems like a good time to point out that you do not need SSH access to your server in order to use Composer. In fact, I don't run Composer on a live server (regardless of whether it's using shared hosting) and it's not on my list of things to do in the near future.

What you do need is a process where you handle your Composer dependencies on your own computer where you have PHP running.

In my view, you have two choices: commit your Composer dependencies directly to your project or write a build script that runs Composer for you and uploads the resulting files.

In either case, you need to install Composer globally, so follow the instructions relevant to your operating system.

Let's look at the process for both options, starting with committing your Composer dependencies.

Commit Composer dependencies

The easiest way to handle Composer dependencies is to run Composer locally and commit the vendor directory into your repository.

Write your website, using Composer, as usual and commit composer.json, composer.lock and all the files in vendor.

Note the following:

  1. Ensure that your .gitignore file does not exclude vendor. This is very common when starting from a skeleton project or using a tool like artisan to create your project.
  2. Ensure that you only use packages that have a release number. That is never use dev-master in your composer.json as if you do, Composer will install it via git and you won't be able to add it to your own repository. There are good reasons for avoiding dev-master dependencies anyway.

Your git repository now has all the files needed to run the website directly within it and so you can now simply upload your website to your shared host as you usually do.

Use a build script

If you don't want to commit the dependencies to your git repository, another solution is to write a script that you run locally that downloads the dependencies and thenuploads the files to your host.

The process looks like this:

  1. Check's out your source code to a clean directory
  2. Runs composer install
  3. Removes all .git directories and any another files and directories that shouldn't be on your live site
  4. Uploads all the remaining files to your shared host. (if your host uses FTP, then use ncftpput for this as it supports recursion)
  5. Deletes the directory

Note that in this situation, you need to ensure that the vendor directory is excluded in your .gitignore file and that composer.lock is committed to git.

Run the script every time you need to put new code onto your live site.

Summary

As you can see, using Composer to manage the dependencies of your PHP project has nothing to do with your final choice of hosting for your live site. You should always have the ability to run your PHP website on your local computer and so you can deal with Composer there and it's simply a case of transferring the right files to live.

In general, I'm a big fan of scripting things that are done by hand, so recommend using a build script, even if you choose to commit your dependencies to your own repository.

8 thoughts on “Using Composer with shared hosting

  1. I have been creating projects with these method you have described and have had no problems. I use symfony, symfony cmf, not too much twig,sqlite and no mysql. Projects I deployed on share hosting are small in nature. After I have finished the projects on my local development, it's just drag and drop, clear my cache and hit the home page. I am planing to blog about it this coming year.

  2. You mentioned "I don't run Composer on a live server". Is it really bad? What are your reasons? I generally run 'composer install' (never update) on productions server and think it is supposed to be like that.

  3. Hi Rob, nice post. I've done similar research and one of the things I find very important is the use of the config.platform option inside the composer.json file.

    When installing or updating the dependencies, composer will choose a version of the package compatible with the PHP version that we are using to run composer. The config.platform option must be used to instruct composer to consider a specific version of PHP (which can be different from the one running the composer command). This is needed to ensure that the packages downloaded by composer are compatible with the version of PHP available in the production environment.

  4. This Method seems logical, but, I'm having issues because I use a windows dev machine and my hosting server is linux. Composer is generating file paths and using functions that seem to only work in windows. Is there a way to tell composer to generate linux format file paths?

    1. Nick,

      I don't know. However, I would suggest looking into using vagrant running Linux for your local dev environment so that you more closely match live.

Leave a Reply

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