优化 Laravel 网站打开速度9条
优化 Laravel 网站打开速度
Laravel是一个功能强大的框架,组件很多,代码也很庞大,它的易用方便是牺牲了性能的,即便如此它仍然是一个优秀的框架,但在正式环境下要做好优化提升网站的打开速度。
1.关闭debug
打开.env文件,把debug设置为false.
1 2 3 | APP_ENV = local APP_DEBUG = false APP_KEY = base64 : sT / aTFeaE13eyao1Raee6jC9Ff + Yle1SE + wtyk0H6B4 = |
2.缓存路由和配置
php artisan route:cache
php artisan config:cache
3.Laravel优化命令
php artisan optimize
4.composer优化
sudo composer dump-autoload optimize
5.使用Laravel缓存
使用Laravel的Cache方法缓存内容,有文件缓存,数据库缓存,redis缓存,使用redis也可以用predis组件,也可以多种缓存方式结合。在Laravel中使用缓存就是这么优雅方便:
1 2 3 | $lists = Cache:: remember ( ‘travel.destination.lists’ , 20 , function ( ) { return $this -> destination -> getList ( ) ; } ) ; |
6.使用CDN
本站用的是七牛CDN,每月送你20G流量和20G存储空间,具体多少不记得了,总之对于小站来说完全足够了。
7.使用PHP 7并开启OPcache
class=”wp-embedded-content” sandbox=”allow-scripts” security=”restricted” src=”https://blog.tanteng.me/2016/06/php7-opcache/embed/\#?secret=JktcTXNp4m“ data-secret=”JktcTXNp4m” width=”525” height=”200” title=”《PHP 7安装和开启opcache》—小谈博客” frameborder=”0” marginwidth=”0” marginheight=”0” scrolling=”no” style=”box-sizing: inherit; margin-bottom: 0px; max-width: 100%; letter-spacing: 0px !important;”>
8.nginx开启gzip压缩
class=”wp-embedded-content” sandbox=”allow-scripts” security=”restricted” src=”https://blog.tanteng.me/2016/06/nginx-gzip/embed/\#?secret=7TklorVMFQ“ data-secret=”7TklorVMFQ” width=”525” height=”200” title=”《Nginx开启gzip压缩》—小谈博客” frameborder=”0” marginwidth=”0” marginheight=”0” scrolling=”no” style=”box-sizing: inherit; margin-bottom: 0px; max-width: 100%; letter-spacing: 0px !important;”>
这不仅仅是针对Laravel网站的性能优化方法,其中很多是通用的网站性能优化的方法,当然还有很多可以优化的地方。
可以把以上提到的优化命令写成一个脚本:
1 2 3 4 5 6 7 8 9 | #!/usr/bin/env bash php artisan clear - compiled php artisan cache : clear php artisan route : cache php artisan config : cache php artisan optimize — force composer dump - autoload — optimize chmod - R 777 storage chmod - R 777 bootstrap / cache |
命名为 optimize.sh 放网站根目录,这样可以方便执行。
以上都是本站(tanteng.me)和博客用到的提升网站速度的tips.转载请注明出处(https://blog.tanteng.me/2016/06/laravel-optimize/)
还没有评论,来说两句吧...