本文根據慕課網的視頻教程練習,感謝慕課網!php
慕課視頻學習地址:https://www.imooc.com/video/14218。html
慕課的參考文檔地址:https://github.com/apanly/mooc/tree/master/va mysql
Laravel的官網地址:https://www.golaravel.com/nginx
laravel對php的版本有要求,laravel
本環境的php版本是5.5.9,因此只能選擇laravel版本爲5.2和以前的版本。git
查看laravel的安裝教程:https://docs.golaravel.com/docs/5.0/installation/github
環境要求:sql
檢查各項是否已安裝,若是php5-json沒有安裝:sudo apt-get install php5-json。apache
而後按照文檔安裝: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 1 - 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/05-opcache.ini - /etc/php5/cli/conf.d/10-pdo.ini - /etc/php5/cli/conf.d/20-gd.ini - /etc/php5/cli/conf.d/20-json.ini - /etc/php5/cli/conf.d/20-mysql.ini - /etc/php5/cli/conf.d/20-mysqli.ini - /etc/php5/cli/conf.d/20-pdo_mysql.ini - /etc/php5/cli/conf.d/20-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.1 bs=1M count=1024 sudo /sbin/mkswap /var/swap.1 sudo /sbin/swapon /var/swap.1
再次建立項目,框架下載成功。
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:9000; try_files $uri = 404; } }
而後在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 *:8888> ServerName study.laravel.com DocumentRoot /home/www/laravel/public/ ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
而後重啓apache,完成。
有錯誤:
[error] 905#0: *1 FastCGI sent in stderr: "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:107 Stack trace: #0 /home/www/laravel/vendor/monolog/monolog/src/Monolog/Handler/RotatingFileHandler.php(107): Monolog\Handler\StreamHandler->write(Array) #1 /home/www/laravel/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(37): Monolog\Handler\RotatingFileHandler->write(Array) #2 /home/www/laravel/vendor/monolog/monolog/src/Monolog/Logger.php(337): Monolog\Handler\AbstractProcessingHandler->handle(Array) #3 /home/www/laravel/vendor/monolog/monolog/src/Monolog/Logger.php(616): Monolog\Logger->addRecord(400, 'exception 'Unex...', Array) #4 /home/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(47): 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:107 Stack trace: #0 /home/www/laravel/vendor/monolog/monolog/src/Monolog/Handler/RotatingFileHandler.php(107): Monolog\Handler\StreamHandler->write(Array) #1 /home/www/laravel/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(37): Monolog\Handler\RotatingFileHandler->write(Array) #2 /home/www/laravel/vendor/monolog/monolog/src/Monolog/Logger.php(337): Monolog\Handler\AbstractProcessingHandler->handle(Array) #3 /home/www/laravel/vendor/monolog/monolog/src/Monolog/Logger.php(616): Monolog\Logger->addRecord(400, 'exception 'Symf...', Array) #4 /home/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions
還有錯誤,而後給文件增長權限:
chmod -R 777 storage/ chmod -R 777 bootstrap/cache/
再次訪問,錯誤消失,訪問項目成功。
總結:
(1)安裝laravel框架時老是出問題,須要一點點分析解決,有時很耗時間,須要耐心。
(2)發現錯誤日誌的時間不對,設置時區爲上海的命令爲:
sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime