vagrant的学习 之 Laravel
本文根据慕课网的视频教程练习,感谢慕课网!
慕课视频学习地址:https://www.imooc.com/video/14218。
慕课的参考文档地址:https://github.com/apanly/mooc/tree/master/va
Laravel的官网地址:https://www.golaravel.com/
第一步,下载Laravel框架:
laravel对php的版本有要求,
本环境的php版本是5.5.9,所以只能选择laravel版本为5.2和之前的版本。
查看laravel的安装教程:https://docs.golaravel.com/docs/5.0/installation/
环境要求:
- PHP 版本 >= 5.4
- Mcrypt PHP 扩展
- OpenSSL PHP 扩展
- Mbstring PHP 扩展
- Tokenizer PHP 扩展
- php5-json 扩展
检查各项是否已安装,如果php5-json没有安装:sudo apt-get install php5-json。
然后按照文档安装:
方式一,通过 Laravel 安装工具:
首先,使用 Composer 下载 Laravel 安装包:
composer global require "laravel/installer=~1.1"
然后创建项目:
laravel new 项目名字
注意如果laravel不是全局的命令,则可以执行使用全路径:
~/.composer/vendor/bin/laravel new 项目名字
结果虽然框架创建下来了,但是报了很多错误问题,显示环境不符合要求,
这样默认下载的是最新的laravel框架,需要php版本为7以上,框架中还缺少vender目录,所以尝试第二种方式下载框架。
方式二,通过 Composer Create-Project:
composer create-project laravel/laravel 路径/项目名字 5.0 --prefer-dist
结果显示:
Problem
- laravel/framework v5.0.9 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system. To enable extensions, verify that they are enabled in your .ini files:
- /etc/php5/cli/conf.d/-opcache.ini
- /etc/php5/cli/conf.d/-pdo.ini
- /etc/php5/cli/conf.d/-gd.ini
- /etc/php5/cli/conf.d/-json.ini
- /etc/php5/cli/conf.d/-mysql.ini
- /etc/php5/cli/conf.d/-mysqli.ini
- /etc/php5/cli/conf.d/-pdo_mysql.ini
- /etc/php5/cli/conf.d/-readline.ini
提示没有这个mcrypt扩展,框架中依然没有vender目录。
至于提示中的那些.ini文件在/etc/php5/cli/conf.d/下都已经存在。
搜索mcrypt官方文档,http://php.net/manual/zh/mcrypt.installation.php。
提示:
Note, for Ubuntu, simply installing php5-mcrypt did not get mcrypt to work. You need to execute the following commands as root to enable it:
//安装php5-mcrypt ,这一步已经安装过,跳过。
sudo apt-get install php5-mcrypt
//移动到指定目录下,发现文件已经在指定的目录里,也跳过。
mv -i /etc/php5/conf.d/mcrypt.ini /etc/php5/mods-available/
//开启扩展,执行一下
sudo php5enmod mcrypt
//重启apache
sudo service apache2 restart
然后重新创建项目,结果又有错误,显示如下:
proc_open(): fork failed - Cannot allocate memory
The archive may contain identical file names with different capitalization (which fails on case insensitive filesystems)
Unzip with unzip command failed, falling back to ZipArchive class
The following exception is caused by a lack of memory or swap, or not having swap configured
Check https://getcomposer.org/doc/articles/troubleshooting.md#proc-open-fork-failed-errors for details PHP Warning: proc_open(): fork failed - Cannot allocate memory in phar:///usr/local/bin/composer/vendor/symfony/console/Application.php on line 959 Warning: proc_open(): fork failed - Cannot allocate memory in phar:///usr/local/bin/composer/vendor/symfony/console/Application.php on line 959 [ErrorException]
proc_open(): fork failed - Cannot allocate memory
搜索到的结论是可能交换空间不足,需要建立交换空间:
sudo /bin/dd if=/dev/zero of=/var/swap. bs=1M count=
sudo /sbin/mkswap /var/swap.
sudo /sbin/swapon /var/swap.
再次创建项目,框架下载成功。
第二步,修改配置文件:
nginx配置:
创建配置文件:
cd /etc/nginx/conf.d/
touch laravel.conf
编辑内容:
sudo vim laravel.conf
server{
server_name study.laravel.com;
root /home/www/laravel/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
} location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:;
try_files $uri = ;
}
}
然后在host文件中增加
IP地址 study.laravel.com
重启nginx,然后在本地电脑上的host里也增加配置
IP地址 study.laravel.com
配置apache:
进入apache的配置目录:
cd /etc/apache2/sites-enabled/
创建配置文件:
touch laravel.conf
编辑配置文件:
sudo vim laravel.conf
<VirtualHost *:>
ServerName study.laravel.com
DocumentRoot /home/www/laravel/public/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
然后重启apache,完成。
第三步,访问项目,study.laravel.com:
有错误:
[error] #: * FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught exception 'UnexpectedValueException' with message 'The stream or file "/home/www/laravel/storage/logs/laravel---.log" could not be opened: failed to open stream: Permission denied' in /home/www/laravel/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:107
Stack trace:
# /home/www/laravel/vendor/monolog/monolog/src/Monolog/Handler/RotatingFileHandler.php(): Monolog\Handler\StreamHandler->write(Array)
# /home/www/laravel/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(): Monolog\Handler\RotatingFileHandler->write(Array)
# /home/www/laravel/vendor/monolog/monolog/src/Monolog/Logger.php(): Monolog\Handler\AbstractProcessingHandler->handle(Array)
# /home/www/laravel/vendor/monolog/monolog/src/Monolog/Logger.php(): Monolog\Logger->addRecord(, 'exception 'Unex...', Array)
# /home/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(): Monolog\Logger->error('exception 'Unex...'...
PHP message: PHP Fatal error: Uncaught exception 'UnexpectedValueException' with message 'The stream or file "/home/www/laravel/storage/logs/laravel-2018-08-31.log" could not be opened: failed to open stream: Permission denied' in /home/www/laravel/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:
Stack trace:
# /home/www/laravel/vendor/monolog/monolog/src/Monolog/Handler/RotatingFileHandler.php(): Monolog\Handler\StreamHandler->write(Array)
# /home/www/laravel/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(): Monolog\Handler\RotatingFileHandler->write(Array)
# /home/www/laravel/vendor/monolog/monolog/src/Monolog/Logger.php(): Monolog\Handler\AbstractProcessingHandler->handle(Array)
# /home/www/laravel/vendor/monolog/monolog/src/Monolog/Logger.php(): Monolog\Logger->addRecord(, 'exception 'Symf...', Array)
# /home/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions
还有错误,然后给文件增加权限:
chmod -R storage/
chmod -R bootstrap/cache/
再次访问,错误消失,访问项目成功。
总结:
(1)安装laravel框架时总是出问题,需要一点点分析解决,有时很耗时间,需要耐心。
(2)发现错误日志的时间不对,设置时区为上海的命令为:
sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime