PHP調用Elasticsearch之composer安裝

安裝 - *nix


下載 Composer 的可執(zhí)行文件

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

注意: 如果上述方法由于某些原因失敗了,你還可以通過 php >下載安裝器:

php -r "readfile('https://getcomposer.org/installer');" | php

這將檢查一些 PHP 的設置,然后下載 composer.phar 到你的工作目錄中。這是 Composer 的二進制文件。這是一個 PHAR 包(PHP 的歸檔),這是 PHP 的歸檔格式可以幫助用戶在命令行中執(zhí)行一些操作。

局部安裝

你可以通過 --install-dir 選項指定 Composer 的安裝目錄(它可以是一個絕對或相對路徑):

php composer-setup.php --install-dir=bin --filename=composer

全局安裝

你可以將此文件放在任何地方。如果你把它放在系統(tǒng)的 PATH 目錄中,你就能在全局訪問它。 在類Unix系統(tǒng)中,你甚至可以在使用時不加 php 前綴。

你可以執(zhí)行這些命令讓 composer 在你的系統(tǒng)中進行全局調用:

mv composer.phar /usr/local/bin/composer

現(xiàn)在只需要運行 composer 命令就可以使用 Composer 而不需要輸入 php composer.phar。

[elastic@iZm5ebjvditkx9x8pm3dw4Z bin]$ composer
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.5.1 2017-08-09 16:07:22

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

Available commands:
  about           Shows the short information about Composer.
  archive         Creates an archive of this composer package.
  browse          Opens the package's repository URL or homepage in your browser.
  clear-cache     Clears composer's internal package cache.
  clearcache      Clears composer's internal package cache.
  config          Sets config options.
  create-project  Creates new project from a package into given directory.
  depends         Shows which packages cause the given package to be installed.
  diagnose        Diagnoses the system to identify common errors.
  dump-autoload   Dumps the autoloader.
  dumpautoload    Dumps the autoloader.
  exec            Executes a vendored binary/script.
  global          Allows running commands in the global composer dir ($COMPOSER_HOME).
  help            Displays help for a command
  home            Opens the package's repository URL or homepage in your browser.
  info            Shows information about packages.
  init            Creates a basic composer.json file in current directory.
  install         Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
  licenses        Shows information about licenses of dependencies.
  list            Lists commands
  outdated        Shows a list of installed packages that have updates available, including their latest version.
  prohibits       Shows which packages prevent the given package from being installed.
  remove          Removes a package from the require or require-dev.
  require         Adds required packages to your composer.json and installs them.
  run-script      Runs the scripts defined in composer.json.
  search          Searches for packages.
  self-update     Updates composer.phar to the latest version.
  selfupdate      Updates composer.phar to the latest version.
  show            Shows information about packages.
  status          Shows a list of locally modified packages.
  suggests        Shows package suggestions.
  update          Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  validate        Validates a composer.json and composer.lock.
  why             Shows which packages cause the given package to be installed.
  why-not         Shows which packages prevent the given package from being installed.
[elastic@iZm5ebjvditkx9x8pm3dw4Z bin]$ 

修改鏡像源地址:


為了提升下載安裝包的速度,建議將composer的鏡像源修改為國內的源:

官方推薦有兩種使用方法,使用任意一種均可。

方法一: 修改 composer 的全局配置文件(推薦方式)

打開命令行窗口(windows用戶)或控制臺(Linux、Mac 用戶)并執(zhí)行如下命令:
composer config -g repo.packagist composer https://packagist.phpcomposer.com

方法二: 修改當前項目的 composer.json 配置文件:

打開命令行窗口(windows用戶)或控制臺(Linux、Mac 用戶),進入你的項目的根目錄(也就是 composer.json 文件所在目錄),執(zhí)行如下命令:
composer config repo.packagist composer https://packagist.phpcomposer.com

上述命令將會在當前項目中的 composer.json 文件的末尾自動添加鏡像的配置信息(你也可以自己手工添加):

"repositories": {
    "packagist": {
        "type": "composer",
        "url": "https://packagist.phpcomposer.com"
    }
}

以 laravel 項目的 composer.json 配置文件為例,執(zhí)行上述命令后如下所示(注意最后幾行):

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.5.9",
        "laravel/framework": "5.2.*"
    },
    "config": {
        "preferred-install": "dist"
    },
    "repositories": {
        "packagist": {
            "type": "composer",
            "url": "https://packagist.phpcomposer.com"
        }
    }
}

OK,一切搞定!試一下 composer install 來體驗飛一般的速度吧!

鏡像原理:


一般情況下,安裝包的數(shù)據(jù)(主要是 zip 文件)一般是從 github.com 上下載的,安裝包的元數(shù)據(jù)是從 packagist.org 上下載的。

然而,由于眾所周知的原因,國外的網站連接速度很慢,并且隨時可能被“墻”甚至“不存在”。

“Packagist 中國全量鏡像”所做的就是緩存所有安裝包和元數(shù)據(jù)到國內的機房并通過國內的 CDN 進行加速,這樣就不必再去向國外的網站發(fā)起請求,從而達到加速 composer install 以及 composer update 的過程,并且更加快速、穩(wěn)定。因此,即使 packagist.org、github.com 發(fā)生故障(主要是連接速度太慢和被墻),你仍然可以下載、更新安裝包。

修改composer地址參考文檔: Packagist / Composer中國全量鏡像

附:全局安裝英文原文:


Globally#

You can place the Composer PHAR anywhere you wish. If you put it in a directory that is part of your PATH, you can access it globally. On unixy systems you can even make it executable and invoke it without directly using the php interpreter.

After running the installer following the Download page instructions you can run this to move composer.phar to a directory that is in your path:

mv composer.phar /usr/local/bin/composer

Note:

If the above fails due to permissions, you may need to run it again with sudo.

Note:
On some versions of OSX the /usr directory does not exist by default. If you receive the error "/usr/local/bin/composer: No such file or directory" then you must create the directory manually before proceeding: mkdir -p /usr/local/bin.

Note:
For information on changing your PATH, please read the Wikipedia article and/or use Google.

Now just run composer in order to run Composer instead of php composer.phar.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容