debian 10 安装

(Introduction)

Composer is a popular dependency management tool for PHP, created mainly to facilitate installation and updates for project dependencies. It will check which other packages a specific project depends on and install them for you, using the appropriate versions according to the project requirements. Composer is also commonly used to bootstrap new projects based on popular PHP frameworks, such as Symfony and Laravel.

Composer是一种流行PHP依赖性管理工具,主要用于促进项目依赖性的安装和更新。 它将检查特定项目依赖的其他软件包,并根据项目要求使用适当的版本为您安装它们。 Composer通常还用于引导基于流行PHP框架(例如Symfony和Laravel)的新项目。

In this guide, we’ll see how to install and use Composer on a Debian 10 server.

在本指南中,我们将了解如何在Debian 10服务器上安装和使用Composer。

(Prerequisites)

To complete this tutorial, you will need one Debian 10 server set up by following the Debian 10 initial server setup guide, including a regular user with sudo privileges.

要完成本教程,您需要按照Debian 10初始服务器设置指南设置一个Debian 10服务器,包括具有sudo特权的普通用户。

(Step 1 — Installing the Dependencies)

Before you can download and install Composer, we’ll ensure your server has all dependencies installed.

在您下载和安装Composer之前,我们将确保您的服务器已安装所有依赖项。

First, update the package manager cache by running:

首先,通过运行以下命令更新程序包管理器缓存:



  • sudo apt update



Now, let’s install the dependencies. We’ll need curl in order to download Composer and php-cli for installing and running it. The php-mbstring package is necessary to provide functions for a library we’ll be using. git is used by Composer for downloading project dependencies, and unzip for extracting zipped packages. Everything can be installed with the following command:

现在,让我们安装依赖项。 我们需要curl来下载Composer和php-cli来安装和运行它。 php-mbstring包对于为我们将要使用的库提供功能是必需的。 git由Composer使用,用于下载项目依赖项,并unzip以提取压缩包。 可以使用以下命令安装所有内容:



  • sudo apt install curl php-cli php-mbstring git unzip



With the prerequisites installed, we can install Composer itself.

安装了先决条件后,我们可以安装Composer本身。

(Step 2 — Downloading and Installing Composer)

Composer provides an installer, written in PHP. We’ll download it, verify that it’s not corrupted, and then use it to install Composer.

Composer提供了一个用PHP编写的安装程序 。 我们将下载它,确认它没有损坏,然后使用它来安装Composer。

Make sure you’re in your home directory, then retrieve the installer using curl:

确保您位于主目录中,然后使用curl检索安装程序:



  • cd ~
  • curl -sS https://getcomposer.org/installer -o composer-setup.php



Next, verify that the installer matches the SHA-384 hash for the latest installer found on the [Composer Public Keys / Signatures][composer-sigs] page. Copy the hash from that page and store it as a shell variable:

接下来,验证安装程序是否与[Composer公钥/签名] [composer-sigs]页上找到的最新安装程序的SHA-384哈希匹配。 从该页面复制哈希并将其存储为shell变量:



  • HASH=48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5
    哈希 = 48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5



Make sure that you substitute the latest hash for the highlighted value.

确保将最新的哈希值替换为突出显示的值。

Now execute the following PHP script to verify that the installation script is safe to run:

现在执行以下PHP脚本,以验证安装脚本可以安全运行:



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



You’ll see the following output.

您将看到以下输出。



Output



输出量



Installer verified

If you see Installer corrupt, then you’ll need to download the installation script again and double check that you’re using the correct hash. Then run the command to verify the installer again. Once you have a verified installer, you can continue.

如果看到Installer corrupt ,则需要再次下载安装脚本,并仔细检查您使用的哈希值是否正确。 然后运行命令以再次验证安装程序。 安装程序通过验证后,即可继续。

To install composer globally, use the following command which will download and install Composer as a system-wide command named composer, under /usr/local/bin:

要全局安装composer ,请使用以下命令,该命令将在/usr/local/bin下以名为composer的系统级命令下载并安装Composer:



  • sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
    sudo php composer-setup.php --install-dir = / usr / local / bin --filename = composer



You’ll see the following output:

您将看到以下输出:

Output
   
   All settings correct for using Composer
Downloading...

Composer (version 1.8.6) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

To test your installation, run:

要测试您的安装,请运行:



  • composer



And you’ll see output displaying Composer’s version and arguments, similar to this:

您将看到显示Composer版本和参数的输出,类似于以下内容:

Output
   
      ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.8.6 2019-06-11 15:03:05

Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug


. . .

This verifies that Composer installed successfully on your system and is available system-wide.

这可以验证Composer在您的系统上是否已成功安装并且在系统范围内可用。

Note: If you prefer to have separate Composer executables for each project you host on this server, you can install it locally, on a per-project basis. Users of NPM will be familiar with this approach. This method is also useful when your system user doesn’t have permission to install software system-wide.

注意:如果您希望在此服务器上托管的每个项目都具有单独的Composer可执行文件,则可以在每个项目的基础上在本地安装它。 NPM的用户将熟悉这种方法。 当您的系统用户无权在系统范围内安装软件时,此方法也很有用。

To do this, use the command php composer-setup.php. This will generate a composer.phar file in your current directory, which can be executed with ./composer.phar command.

为此,请使用命令php composer-setup.php 。 这将在当前目录中生成一个composer.phar文件,可以使用./composer.phar command执行该文件。

Now let’s look at using Composer to manage PHP dependencies.

现在让我们看一下使用Composer来管理PHP依赖项。

(Step 3 — Using Composer in a PHP Project)

PHP projects often depend on external libraries, and managing those dependencies and their versions can be tricky. Composer solves that by tracking your dependencies and making it easy for others to install them.

PHP项目通常依赖于外部库,并且管理那些依赖关系及其版本可能很棘手。 Composer通过跟踪您的依赖关系并使其他人易于安装来解决此问题。

In order to use Composer in your project, you’ll need a composer.json file. The composer.json file tells Composer which dependencies it needs to download for your project, and which versions of each package are allowed to be installed. This is extremely important to keep your project consistent and avoid installing unstable versions that could potentially cause backwards compatibility issues.

为了在您的项目中使用Composer,您需要一个composer.json文件。 composer.json文件告诉Composer您需要为项目下载哪些依赖项,以及允许安装每个软件包的哪个版本。 这对于保持项目的一致性并避免安装可能导致向后兼容性问题的不稳定版本非常重要。

You don’t need to create this file manually - it’s easy to run into syntax errors when you do so. Composer auto-generates the composer.json file when you add a dependency to your project using the require command. You can add additional dependencies in the same way, without the need to manually edit this file.

您无需手动创建此文件-这样做很容易遇到语法错误。 使用require命令向项目中添加依赖项时,Composer会自动生成composer.json文件。 您可以用相同的方式添加其他依赖项,而无需手动编辑此文件。

The process of using Composer to install a package as dependency in a project involves the following steps:

使用Composer在项目中将软件包安装为依赖项的过程涉及以下步骤:

  • Identify what kind of library the application needs.
  • Research a suitable open source library on Packagist.org, the official package repository for Composer.
    Packagist.org (Composer的官方软件包存储库)上研究合适的开源库。
  • Choose the package you want to depend on.
  • Run composer require to include the dependency in the composer.json file and install the package.
    运行composer require将该依赖项包含在composer.json文件中并安装该软件包。

Let’s try this out with a demo application.

让我们通过演示应用程序进行尝试。

The goal of this application is to transform a given sentence into a URL-friendly string - a slug. This is commonly used to convert page titles to URL paths (like the final portion of the URL for this tutorial).

该应用程序的目标是将给定的句子转换为URL友好的字符串-slug 。 这通常用于将页面标题转换为URL路径(例如本教程URL的最后部分)。

Let’s start by creating a directory for our project. We’ll call it slugify:

让我们从为项目创建目录开始。 我们称它为Slugify



  • cd ~
  • mkdir slugify
  • cd slugify



Now it’s time to search Packagist.org for a package that can help us generate slugs. If you search for the term “slug” on Packagist, you’ll get a result similar to this:

现在是时候来搜索Packagist.org一个包,可以帮助我们产生蛞蝓 。 如果在Packagist上搜索“ slug”一词,则会得到类似以下结果:

You’ll see two numbers on the right side of each package in the list. The number on the top represents how many times the package was installed, and the number on the bottom shows how many times a package was starred on GitHub. You can reorder the search results based on these numbers (look for the two icons on the right side of the search bar). Generally speaking, packages with more installations and more stars tend to be more stable, since so many people are using them. It’s also important to check the package description for relevance to make sure it’s what you need.

您会在列表中每个包装的右侧看到两个数字。 顶部的数字表示软件包已被安装的次数,底部的数字表示软件包在GitHub加注星标的次数。 您可以根据这些数字对搜索结果重新排序(在搜索栏的右侧查找两个图标)。 一般而言,具有更多安装和更多星形的软件包往往更稳定,因为有很多人在使用它们。 检查软件包说明的相关性以确保它是您所需要的也很重要。

We need a simple string-to-slug converter. From the search results, the package cocur/slugify seems to be a good match, with a reasonable amount of installations and stars.

我们需要一个简单的字符串到子转换器。 从搜索结果来看, cocur/slugify软件包似乎是一个不错的选择,具有合理数量的安装和星级。

Packages on Packagist have a vendor name and a package name. Each package has a unique identifier (a namespace) in the same format GitHub uses for its repositories, in the form vendor/package. The library we want to install uses the namespace cocur/slugif. You need the namespace in order to require the package in your project.

Packagist上的软件包具有供应商名称和软件包名称。 每个软件包都有一个唯一标识符(名称空间),其标识符与GitHub用于其存储库的格式相同,格式为vendor / package 。 我们要安装的库使用名称空间cocur/slugif 。 您需要命名空间,以便在项目中需要包。

Now that you know exactly which package you want to install, run composer require to include it as a dependency and also generate the composer.json file for the project:

既然您已经知道要安装哪个软件包,请运行composer require将其作为依赖项包含在内,并为项目生成composer.json文件:



  • composer require cocur/slugify



You’ll see the following output as Composer downloads the dependency:

当Composer下载依赖项时,您将看到以下输出:

Output
   
   Using version ^3.2 for cocur/slugify
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing cocur/slugify (v3.2): Downloading (100%)         
Writing lock file
Generating autoload files

As you can see from the output, Composer automatically decided which version of the package to use. If you check your project’s directory now, it will contain two new files: composer.json and composer.lock, and a vendor directory:

从输出中可以看到,Composer会自动决定要使用哪个版本的软件包。 如果现在检查项目的目录,它将包含两个新文件: composer.jsoncomposer.lock以及vendor目录:



  • ls -l



Output
   
   total 12
-rw-r--r-- 1 sammy sammy   59 jul 15 13:53 composer.json
-rw-r--r-- 1 sammy sammy 2952 jul 15 13:53 composer.lock
drwxr-xr-x 4 sammy sammy 4096 jul 15 13:53 vendor

The composer.lock file is used to store information about which versions of each package are installed, and ensure the same versions are used if someone else clones your project and installs its dependencies. The vendor directory is where the project dependencies are located. The vendor folder doesn’t need to be committed into version control - you only need to include the composer.json and composer.lock files.

composer.lock文件用于存储有关安装每个软件包的版本的信息,并确保在其他人克隆您的项目并安装其依赖项时确保使用相同的版本。 vendor目录是项目依赖项所在的位置。 vendor文件夹不需要提交到版本控制中-您只需要包含composer.jsoncomposer.lock文件。

When installing a project that already contains a composer.json file, run composer install in order to download the project’s dependencies.

当安装已经包含composer.json文件的项目时,请运行composer install来下载项目的依赖项。

Let’s take a quick look at version constraints. If you check the contents of your composer.json file, you’ll see something like this:

让我们快速看一下版本约束。 如果检查composer.json文件的内容,则会看到类似以下内容:



  • cat composer.json



Output
   
   {
    "require": {
        "cocur/slugify": "^3.2"
    }
}

You might notice the special character ^ before the version number in composer.json. Composer supports several different constraints and formats for defining the required package version, in order to provide flexibility while also keeping your project stable. The caret (^) operator used by the auto-generated composer.json file is the recommended operator for maximum interoperability, following semantic versioning. In this case, it defines 3.2 as the minimum compatible version, and allows updates to any future version below 4.0.

您可能会在composer.json的版本号之前注意到特殊字符^ 。 Composer支持几种不同的约束和格式来定义所需的软件包版本,以便在保持项目稳定的同时提供灵活性。 自动生成的composer.json文件使用的插入符( ^ )是建议的运算符,它遵循语义版本控制以实现最大的互操作性。 在这种情况下,它将3.2定义为最低兼容版本,并允许更新到4.0以下的任何将来版本。

Generally speaking, you won’t need to tamper with version constraints in your composer.json file. However, some situations might require that you manually edit the constraints–for instance, when a major new version of your required library is released and you want to upgrade, or when the library you want to use doesn’t follow semantic versioning.

一般来说,您无需篡改composer.json文件中的版本限制。 但是,在某些情况下,可能需要手动编辑约束-例如,当发布所需库的主要新版本并且要升级时,或者要使用的库不遵循语义版本控制时。

Here are some examples to give you a better understanding of how Composer version constraints work:

以下是一些示例,可以使您更好地了解Composer版本约束的工作方式:

Constraint

Meaning

Example Versions Allowed

^1.0

>= 1.0 < 2.0

1.0, 1.2.3, 1.9.9

^1.1.0

>= 1.1.0 < 2.0

1.1.0, 1.5.6, 1.9.9

~1.0

>= 1.0 < 2.0.0

1.0, 1.4.1, 1.9.9

~1.0.0

>= 1.0.0 < 1.1

1.0.0, 1.0.4, 1.0.9

1.2.1

1.2.1

1.2.1

1.*

>= 1.0 < 2.0

1.0.0, 1.4.5, 1.9.9

1.2.*

>= 1.2 < 1.3

1.2.0, 1.2.3, 1.2.9

约束

含义

允许的示例版本

^ 1.0

> = 1.0 <2.0

1.0、1.2.3、1.9.9

^ 1.1.0

> = 1.1.0 <2.0

1.1.0、1.5.6、1.9.9

〜1.0

> = 1.0 <2.0.0

1.0、1.4.1、1.9.9

〜1.0.0

> = 1.0.0 <1.1

1.0.0、1.0.4、1.0.9

1.2.1

1.2.1

1.2.1

1. *

> = 1.0 <2.0

1.0.0、1.4.5、1.9.9

1.2。*

> = 1.2 <1.3

1.2.0、1.2.3、1.2.9

For a more in-depth view of Composer version constraints, see the official documentation.

有关Composer版本限制的更深入的信息,请参见官方文档

Next, let’s look at how to load dependencies automatically with Composer.

接下来,让我们看看如何使用Composer自动加载依赖项。

(Step 4 — Including the Autoload Script)

Since PHP itself doesn’t automatically load classes, Composer provides an autoload script that you can include in your project to get autoloading for free. This makes it much easier to work with your dependencies.

由于PHP本身不会自动加载类,因此Composer提供了一个自动加载脚本,您可以将其包含在项目中以免费获得自动加载。 这样可以更轻松地处理依赖关系。

The only thing you need to do is include the vendor/autoload.php file in your PHP scripts before any class instantiation. This file is automatically generated by Composer when you add your first dependency.

您唯一需要做的是在任何类实例化之前,在PHP脚本中包含vendor/autoload.php文件。 添加第一个依赖项时,此文件由Composer自动生成。

Let’s try it out in our application. Create the file test.php and open it in your text editor:

让我们在我们的应用程序中尝试一下。 创建文件test.php并在文本编辑器中将其打开:



  • nano test.php



Add the following code which brings in the vendor/autoload.php file, loads the cocur/slugify dependency, and uses it to create a slug:

添加以下代码,该代码将cocur/slugify vendor/autoload.php文件,加载cocur/slugify依赖项,并使用它来创建一个slug:



test.php



test.php



<?php
require __DIR__ . '/vendor/autoload.php'; 
use Cocur\Slugify\Slugify;

$slugify = new Slugify();

echo $slugify->slugify('Hello World, this is a long sentence and I need to make a slug from it!');

Save the file and exit your editor.

保存文件并退出编辑器。

Now run the script:

现在运行脚本:



  • php test.php



This produces the output hello-world-this-is-a-long-sentence-and-i-need-to-make-a-slug-from-it.

这将产生输出hello-world-this-is-a-long-sentence-and-i-need-to-make-a-slug-from-it

Dependencies need updates when new versions come out, so let’s look at how to handle that.

新版本发布时,依赖项需要更新,因此让我们看一下如何处理。

(Step 5 — Updating Project Dependencies)

Whenever you want to update your project dependencies to more recent versions, run the update command:

每当您要将项目依赖项更新到最新版本时,请运行update命令:



  • composer update



This will check for newer versions of the libraries you required in your project. If a newer version is found and it’s compatible with the version constraint defined in the composer.json file, Composer will replace the previous version installed. The composer.lock file will be updated to reflect these changes.

这将检查项目中所需的较新版本的库。 如果找到较新的版本,并且与composer.json文件中定义的版本限制兼容,则Composer将替换以前安装的版本。 composer.lock文件将被更新以反映这些更改。

You can also update one or more specific libraries by specifying them like this:

您还可以通过这样指定一个或多个特定库来更新它们:



  • composer update vendor/package vendor2/package2
    作曲家更新供应商/程序包vendor2 / package2



Be sure to commit the changes to your composer.json and composer.lock files after you update your dependencies, so whoever is working in the project has access to the same package versions.

更新依赖项后,请确保将更改提交到composer.jsoncomposer.lock文件,以便在项目中工作的任何人都可以访问相同的软件包版本。

(Conclusion)

Composer is a powerful tool every PHP developer should have in their utility belt. In this tutorial you installed Composer on Debian 10 and used it in a simple project. You now know how to install and update dependencies.

Composer是每个PHP开发人员都应该在实用程序中拥有的强大工具。 在本教程中,您在Debian 10上安装了Composer,并在一个简单的项目中使用了它。 您现在知道了如何安装和更新依赖关系。

Beyond providing an easy and reliable way for managing project dependencies, it also establishes a new de facto standard for sharing and discovering PHP packages created by the community.

除了提供一种简单可靠的方法来管理项目依赖关系外,它还建立了一个新的事实上的标准,用于共享和发现社区创建PHP软件包。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-composer-on-debian-10

debian 10 安装