Installation and configuration of Composer under Windows and Linux
Introduction: Composer is a tool used by PHP to manage dependency relationships. You can declare the external libraries you depend on in your project, and Composer will install these dependent library files for you.
Composer official website download Composer.exe application (https ://getcomposer.org/)
The downloaded version is the latest Composer version and the PATH environment variable will be automatically set, so that you You can call Composer from any directory
# win+R 输入cmd 进入C:\Users\Administrator>composer ______ / ____/___ ____ ___ ____ ____ ________ _____ / / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/ / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ / \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/ /_/ Composer version 2.1.9 2021-10-05 09:47:38
**# 进入根目录下载** [root@VM-12-9-centos /] curl -sS https://getcomposer.org/installer | php Composer (version 2.1.9) successfully installed to: //composer.phar Use it: php composer.phar #下载成功#友情提示:下载因为是外网可能会出现请求失败的情况,耐心多尝试下就成功了
[root@VM-12-9-centos /] curl -sS mirrors.aliyun.com/composer/compos...
# 复制composer.phar到/usr/local/bin/目录下生成composer的目录,方便全局使用[root@VM-12-9-centos /] mv composer.phar /usr/local/bin/composer
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
composer config -g repo.packagist composer https://packagist.phpcomposer.com
composer config -g repo.packagist composer https://mirrors.cloud.tencent.com/composer/
composer config -g repo.packagist composer https://repo.huaweicloud.com/repository/php/
composer config -g repo.packagist composer https://php.cnpkg.org/
[root@VM-12-9-centos etc]# composer ______ / ____/___ ____ ___ ____ ____ ________ _____ / / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/ / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ / \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/ /_/ Composer version 2.1.9 2021-10-05 09:47:38
composer --version# 查看版本会看到当前版本以及更新时间[root@VM-12-9-centos /]# composer --versionComposer version 2.1.9 2021-10-05 09:47:38
# 升级将默认升到最新版本composer self-update# **友情提示:** 版本升级命令将会从官方版本去下载composer,phar镜像,速度将受限!强烈建议全局配置加速镜像
composer diagnose
composer clear
composer update --lock
# **Composer调试只需要在命令前加上-vvv就可以打印出错信息** # 例如下载laravel命名为blog的项目composer -vvv create-project laravel/laravel blog
# 该命令仅限在项目根目录操作 composer install
# 该命令仅限在项目根目录操作 ### 更新所有依赖包 composer update ### 更新指定依赖包 composer update monolog/monolog ### 更新多个指定依赖包 composer update monolog/monolog symfony/dependency-injection ### 根据通配符匹配更新依赖包 composer update monolog/monolog symfony/*
# 移除指定依赖包 composer remove monolog/monolog # 根据通配符匹配移除依赖包 composer remove monolog/monolog symfony/*
# 搜索依赖包:输出包及其描述信息 composer search monolog # 只搜索包:--only-name composer search --only-name monolog # 根据通配符搜索依赖包 composer search mono*
# **该命令仅限项目根目录使用**# 查看**当前项目**已安装的依赖包 composer show # 根据通配符**当前项目**进行筛选 composer show laravel*# 查看**当前项目**指定依赖包 composer show laravel/tinker
composer 的安装需要php开启 putenv 函数 ,否则会安装失败!由于 putenv 函数可以设置特定的环境变量而被认为是一个潜在的安全漏洞,因此php在安装之初是默认禁止它的!
宝塔面板出于对于安全的考虑,自动禁用了一些函数;因为安装Composer也会用到相应的函数,我们应该先删除在安装 Composer 。 安装 Composer 需要删除 的 PHP 禁用函数:
putenv()
友情提示: 在使用 Composer 的时候需要根据项目的php版本去做操作(安装多少个PHP版本就需要相对应删除),下图示范是根据 php-8.0 来操作的。
# 进入php.ini配置文件编辑--->搜索(输入 / 进入搜索模式,搜索disable_functions)# ---->依次删除putenv**### 第一步:查询php.ini路径** [root@VM-12-9-centos 74]# find / -name "php.ini"/www/server/php/56/etc/php.ini /www/server/php/80/etc/php.ini /www/server/php/74/etc/php.ini **### 第二步:依次编辑php.ini文件** : 删除函数putenvvim /www/server/php/56/etc/php.ini **### 第三步:搜索“disable_functions”** # ESC + :+ / + disable_functions 将会自动定位到指定位置disable_functions = passthru,exec,system,**putenv** ,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv **### 第四步:保存文件并退出** ESC + :wq!
宝塔是支持php多版本的,如果在不同版本的php中使用 composer ,只要开启php中的 putenv 函数即可!
使用场景: 由于下载代码或者git代码或者部署项目时会存在
composer.lock
, 首先应该删除composer.lock
# 项目中composer.lock已经存在,需要进行删除,再重新安装,安装成功后会重新生成composer.lock文件# 案例示范:**### 第一步:进入项目blog根目录** cd www\wwwroot\blog **### 第二步:删除composer.lock文件** # 删除请核对文件名后谨慎操作rm -rf composer.lock **### 第三步:重新composer安装项目所需包** composer install
提示: 不要忘了经常执行
composer selfupdate
以保持 Composer 一直是最新版本哦!原文地址:https://learnku.com/articles/62076
The above is the detailed content of The most comprehensive and detailed Composer installation and configuration tutorial (win/linux). For more information, please follow other related articles on the PHP Chinese website!