[实践OK]Linux下 php环境安装、配置composer,使用composer安装laravel/lumen、zan等框架,写一个读取数据库的。

jackxiang 2017-1-10 23:17 | |
/usr/bin/env: PHP: No such file or directory报错
su www

[root@VM-0-15-centos ~]# curl -sS https://getcomposer.org/installer | php

#把composer.phar转移之bin目录以便全局使用
[root@VM-0-15-centos ~]# mv composer.phar /usr/local/bin/composer
#展示版本信息说明成功
[root@VM-0-15-centos ~]# composer -v
如果出现这个提示,是因为composer不建议root账号运行,可以输入y,或者切换账号

Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]?

sudo -u www -H/usr/local/composer/bin/composer config repo.packagist composer https://mirrors.aliyun.com/composer/
[ErrorException]
  touch(): Unable to create file /home/www/.config/composer/config.json because No such file or directory  

#composer clear-cache
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? yes
Cache directory does not exist (cache-vcs-dir):
Cache directory does not exist (cache-repo-dir):
Cache directory does not exist (cache-files-dir):
Clearing cache (cache-dir): /root/.cache/composer
如果是其它用户就是在它的家目录下面建立配置文件。

[root@nexus-repo-10-10-0-109:/data/www/l.levoo.com]
#sudo -u www -H /usr/local/composer/composer update
Loading composer repositories with package information

[root@nexus-repo-10-10-0-109:/data/www/l.levoo.com]
#sudo -u www -H /usr/local/composer/composer update
......
Package operations: 41 installs, 14 updates, 38 removals
    Failed to download dragonmantank/cron-expression from dist: The zip extension is missing and unzip/7z commands cannot be called as proc_open is disabled, skipping.
Your command-line PHP is using multiple ini files. Run `php --ini` to show them.
    Now trying to download from source

PHP的一些安全函数禁用了,打开就成功了:
  - Upgrading easyswoole/task (1.0.5 => 1.1.2): Extracting archive
  - Upgrading easyswoole/log (1.0.3 => 1.1.1): Extracting archive
Generating autoload files
28 packages you are using are looking for funding.
Use the `composer fund` command to find out more!



然后执行compser up
或者备份composer.lock
==========================================================================

composer config -g repo.packagist composer https://packagist.phpcomposer.com #换国内镜像URL地址。
composer是一个可执行命令,你在一个空目录下写一个composer.json
然后执行composer update,就能生成。
====================两步:软链接composer/PATH/下载autoloader===========================
软链接加PATH:


easy swoole 3.0.9 官方并不是最新的,换换阿里云镜像:
全局替换:
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
composer clear-cache # 此步奏选泽性操作,清除所有 package 缓存。
composer clear-cache
Cache directory does not exist (cache-vcs-dir):
Clearing cache (cache-repo-dir): /data/www/.cache/composer/repo
Clearing cache (cache-files-dir): /data/www/.cache/composer/files
Clearing cache (cache-dir): /data/www/.cache/composer

当前项目替换: (注意:不让root用户运行,用www用户)
cd 项目目录
composer config repo.packagist composer https://mirrors.aliyun.com/composer/


不重新开终端不会生效,则可直接导入变量:
source /etc/profile.d/composer.sh


cd /usr/local/kafka-php
cat composer.json
{
        "require": {
                "nmred/kafka-php": "0.2.*"
        }
}

composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 3 installs, 0 updates, 0 removals
  - Installing amphp/amp (v1.2.2): Downloading (100%)        
  - Installing psr/log (1.0.2): Downloading (100%)        
  - Installing nmred/kafka-php (v0.2.0.8): Downloading (100%)        
Writing lock file
Generating autoload files

cd /usr/local/kafka-php
composer.json  composer.lock  vendor
目的是看下同事所说的Kafka死光了,kafka-php会退出的问题:
https://github.com/weiboad/kafka-php

===========================================================================

cd /usr/local/composer  ,sudo -u www -H composer.phar install ,会去读取:/usr/local/composer/composer.json文件。
chown -R www.www /data/htdocs/sd.levoo.com #否则会报:/data/htdocs/sd.levoo.com/vendor does not exist and could not be created.
sudo -u www -H composer.phar install

实践如下:


vi /etc/sudoers
Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/composer:/usr/local/php/bin


#sudo -u www -H composer.phar config -g repo.packagist composer https://packagist.phpcomposer.com                                                                                                    
  [ErrorException]                                                                                  
  touch(): Unable to create file /home/www/.composer/config.json because No such file or directory  

#mkdir  /home/www
#chown www.www /home/www
#chmod 755 /home/www
#sudo -u www -H composer.phar config -g repo.packagist composer https://packagist.phpcomposer.com


#sudo -u www -H composer.phar install
Composer could not find a composer.json file in /usr/local/composer
To initialize a project, please create a composer.json file as described in the https://getcomposer.org/ "Getting Started" section

建立composer.json如下,可以根据需要添加你所依赖的库,但下面一定得包含在内,autoload必须要指定app和test的目录不得省略。

{
  "require": {
    "tmtbe/swooledistributed":">2.0.0"
  },
"autoload": {
    "psr-4": {
      "app\\": "src/app",
      "test\\": "src/test"
    }
  }
}
/usr/local/composer/composer.json
运行:
#sudo -u www -H composer.phar install
Loading composer repositories with package information
Updating dependencies (including require-dev)
  [RuntimeException]                                                  
  /usr/local/composer/vendor does not exist and could not be created.

  [RuntimeException]                                                                
  /usr/local/composer/vendor/symfony/intl does not exist and could not be created

  - Installing symfony/intl (v3.3.8):
                                                                                          
  [Symfony\Component\Process\Exception\RuntimeException]                                  
  The Process class relies on proc_open, which is not available on your PHP installation.  
      
PHP需有proc_open,得从PHP的php.ini里给去掉:
; disable_functions = chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_
restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocket

#sudo -u www -H composer.phar install
Composer could not find a composer.json file in /usr/local/php/etc
To initialize a project, please create a composer.json file as described in the https://getcomposer.org/ "Getting Started" section
得cd到:/usr/local/composer 里,再执行:sudo -u www -H composer.phar install
#sudo -u www -H composer.phar install
  [ErrorException]                                          
  proc_get_status() has been disabled for security reasons   和上面一样:得从PHP的php.ini里给去掉。
  [RuntimeException]                                          
  Could not delete /usr/local/composer/vendor/symfony/intl:
  chown -R www.www  /usr/local/composer/vendor
  [ErrorException]                                                              
  file_put_contents(./composer.lock): failed to open stream: Permission denied   #写入/usr/local/composer/composer.lock权限不够。
[root@gitlab-jenkins_php-redis_123.57.252.183:/usr/local]
#chown www.www composer
#sudo -u www -H composer.phar install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。


From:https://getcomposer.org/download/
http://blog.csdn.net/yoywow/article/details/52074512

二、使用composer安装laravel/lumen:
遇到问题:
修改 composer 的全局配置文件(推荐方式)

打开命令行窗口(windows用户)或控制台(Linux、Mac 用户)并执行如下命令:
[root@a composer]# chtdocs
[root@a htdocs]# cd lumen.levoo.com/
[root@a lumen.levoo.com]# /usr/local/composer/composer.phar global require "laravel/lumen-installer"
Changed current directory to /root/.config/composer
Do not run Composer as root/super user! See https://getcomposer.org/root for details
compser 执行命令提示do not run composer as root/super !
这个是因为composer为了防止非法脚本在root下执行,解决办法随便切换到非root用户即可
su www
mkdir -p /home/www/.cache
chown -R www:www /home/www/.cache

composer require easyswoole/hot-reload
Cannot create cache directory /home/www/.cache/composer/repo/https---mirrors.aliyun.com-composer/, or directory is not writable. Proceeding without cache
Cannot create cache directory /home/www/.cache/composer/files/, or directory is not writable. Proceeding without cache
Using version ^0.1.2 for easyswoole/hot-reload
./composer.json has been updated


PATH变量设置Ok的方法如下:
打开~/.bashrc
sudo vim ~/.bashrc
添加行:
export PATH=$PATH:/home/lumen/.config/composer/vendor/bin


用Linux系统。比如要把/etc/apache/bin目录添加到PATH中,方法有三:
1.#PATH=c/etc/apache/bin
使用这种方法,只对当前会话有效,也就是说每当登出或注销系统以后,PATH 设置就会失效

2.#vi /etc/profile
在适当位置添加 PATH=$PATH:/etc/apache/bin (注意:= 即等号两边不能有任何空格)
这种方法最好,除非你手动强制修改PATH的值,否则将不会被改变

3.#vi ~/.bash_profile
修改PATH行,把/etc/apache/bin添加进去
这种方法是针对用户起作用的
From:http://opsmysql.blog.51cto.com/2238445/665990


lumen没有成功:



useradd lumen
su lumen
cd /data/htdocs/laravel.levoo.com
/usr/local/composer/composer.phar  config -g repo.packagist composer https://packagist.phpcomposer.com


进行上述设置后,laravel有门,能下载了:
[lumen@a laravel.levoo.com]$ pwd
/data/htdocs/laravel.levoo.com
[lumen@a laravel.levoo.com]$ /usr/local/composer/composer.phar global require "laravel/lumen-installer"  
Changed current directory to /home/lumen/.config/composer
Using version ^1.0 for laravel/lumen-installer
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 10 installs, 0 updates, 0 removals
  - Installing symfony/process (v3.2.1) Downloading: 100%        
  - Installing psr/log (1.0.2) Downloading: 100%        
  - Installing symfony/debug (v3.2.1) Downloading: 100%        
  - Installing symfony/polyfill-mbstring (v1.3.0) Downloading: 100%        
  - Installing symfony/console (v3.2.1) Downloading: 100%        
  - Installing guzzlehttp/promises (v1.3.1) Downloading: 100%        
  - Installing psr/http-message (1.0.1) Downloading: 100%        
  - Installing guzzlehttp/psr7 (1.3.1) Downloading: 100%        
  - Installing guzzlehttp/guzzle (6.2.2) Downloading: 100%        
  - Installing laravel/lumen-installer (v1.0.2) Downloading: 100%        
symfony/console suggests installing symfony/event-dispatcher ()
symfony/console suggests installing symfony/filesystem ()
Writing lock file
Generating autoload files


生成文件如下:
[lumen@a composer]$ pwd
/home/lumen/.config/composer

[lumen@a composer]$ ls  /home/lumen/.config/composer
auth.json  composer.json  composer.lock  config.json  vendor


开始学习数据库:
http://blog.csdn.net/wowkk/article/details/52104689

/data/htdocs/lumen.levoo.com/blog/.env
APP_ENV=local
APP_DEBUG=true
APP_KEY=
APP_TIMEZONE=UTC

DB_CONNECTION=mysql
DB_HOST=10.44.202.177
DB_PORT=3306
DB_DATABASE=levoo_egg
DB_USERNAME=levoo
DB_PASSWORD=new@levoo.com

CACHE_DRIVER=memcached
QUEUE_DRIVER=sync

/data/htdocs/lumen.levoo.com/blog/app/Http/Controllers/AccountController.PHP


/data/htdocs/lumen.levoo.com/blog/routes/web.php
<?php

$app->group(["namespace"=>"App\Http\Controllers"], function()use($app){
    //账户控制器
    $app->get("/AccountController",["uses" => "AccountController@accountController"]);
});


model:/data/htdocs/lumen.levoo.com/blog/app/Models/User.php
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
class User extends Model
{
}



在app/Http/Controllers目录下AccountController.php,修改为

<?php

namespace App\Http\Controllers;

use App\user;   //新增部分
use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Http\Request;

class AccountController extends BaseController
{
    //直接传人sql方式操作数据库
    function accountController(Request $request){
        return User::all();
    }
}

http://101.200.189.210/AccountController/accountController

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/9170/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2022-4-19 23:33
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]