`

PHPMotion 性能调优

 
阅读更多

http://www.delishost.com/2012/01/tuning-your-phpmotion-theme-for-performance/

 


Tuning your PHPmotion theme for performance

Nothing is worse then having to wait for a website to load and speed is even more important when your website has huge competitors such as Youtube and Dailymotion. Here we’ll take you through the steps to making your PHPmotion website load faster.

Note: These changes are based on the default theme, but should be applicable to custom themes with a few minor adjustments. Tests are measured on the home page, but will improve the entire site.

Scores are measured with the excellent YSlow! plugin for Firefox and Chrome .

The Test Case: phpmotionperformance.delishost.com

  • Default PHPmotion V3.5 installation
  • Some uploaded content to fill the site.
  • Total page size: 465KB
  • YSlow! Rating: C
  • Page loaded in 6.40s

Warning: Always keep copies and backups of any files you modify!

Step 1: Host it close-by.

The closer your website is to your visitors the better, latency is reduced meaning files are transferred faster as the delay between connecting to the server and actually receiving the file is reduced.

If your main visitors are in the USA, then choosing a server there is an obvious choice. The same goes for hosting in Europe, having your site hosted with the correct host can make a noticeable difference to your end users. We offer fast, reliable hosting in European datacentres .

Step 2: Optimize your javascript and CSS files

To speed a site up we need to minimize the amount of bytes that are actually transferred from the server to the visitor’s browser. To do so we can compress all the javascript and CSS files that aren’t currently optimized.

Useful online tools for compressing files include:

This is a fairly simple step, simply search through your site for javascript and css files. The main places to look include:

  • javascripts/
  • css/
  • themes/yourthemes/css/

Now upload these files one by one to the appropriate site from the above links. Make sure you’re not trying to compress an already compressed file. You can check this by opening the file, if everything looks like a solid block or a single line of text then it is already compressed.

Download their optimized versions and replace the file on the website.

Remember to keep a backup of each file and test the site functions after each change. As compressing can occasionally break Javascript functions so parts of your site will appear to stop working.

Step 3: Using DEFLATE to compress content

DEFLATE is an excellent way to reduce the amount of content that is sent to the browser. It works by compressing non binary files and sending the compressed versions to the visitor. Their browser then uncompresses and loads the content.

Enabling DEFLATE is pretty simple and definitely worth it. Open your .htaccess file and paste the following:

  1. # DEFLATE
  2. <IfModule mod_deflate.c>
  3. AddOutputFilterByType DEFLATE application/x-javascript application/javascript text/javascript text/html text/plain text/css text/xml
  4. </IfModule>
  5. # End DEFLATE

Now upload your new .htaccess file and clear your browser cache (otherwise YSlow! may incorrectly report that it isn’t working). The total page size (no other changes) is now 218KB (53% savings! )

Step 4: Building a CDN

We’ll now concentrate on setting up a basic CDN. Many browsers limit parallel HTTP requests to two by domain. The default home page for our test site has 40 components which can only be downloaded 2 by 2. Setting up CDNs will allow us to multiple the maximum number of parallel requests and thus noticeably speed up the entire website.

In the following examples replace example.com with your website domain name and /phpmotionpath/ with the path to your PHPmotion installation.

Here I will walk you through how to create the following:

  • theme.example.com for theme stylesheets (themes/yourtheme/css/)
  • css.example.com for non theme related stylesheets (css/)
  • js.example.com for javascript files (js/)
  • thumbs.example.com for video thumbnails (uploads/thumbs/)

You will be able to take this further if you wish, but these should already help load times on your website quite a bit.

To continue you will need a code editor that is capable of doing a search/replace across files otherwise you will spend a lot of time going through the files.

To make updates to CDN names easier in the future I have created a PHP file which will hold all CDN names in an array for easy re-use in the templates. Copy and save the following code into addons/dh-cdn.php .

  1. <?php
  2. /**
  3. * @author Ben Newman http://www.delishost.com
  4. *
  5. * This file simply holds variables for the different cdn names you can use in
  6. * your themes. It allows you to add/modify CDNs without having to go through all
  7. * your templates.
  8. *
  9. * You can access these through in the templates like so:
  10. * [var.asset_cdn.theme]
  11. */
  12. $asset_cdn = array(
  13. 'theme' => 'http://theme.example.com ', // To load theme assets
  14. 'cdn' => 'http://cdn.example.com ', // Mirrors the normal PHPmotion path
  15. 'js' => 'http://js.example.com ', // Loads the javascript files
  16. 'videothumbs' => 'http://thumbs.example.com ', // Loads thumbs from uploads/thumbs/
  17. );

Upload this file to your server after making sure to enter the correct addresses for each one. Then open classes/config.inc.php and add the following on a new line before ?> .

  1. include installation_paths().'/addons/dh-cdn.php';

Theme CSS CDN

Create a subdomain called theme.example.com that points to /phpmotionpath/themes/yourtheme . Once this is done you can test it is working by trying to load http://theme.example.com/css/main.css in your browser. If it shows everything is correct then you can continue.

Note: You will need to update this if you switch themes in the future.

Editing themes/yourtheme/templates/main_1.htm – search for the following strings:

  1. [var.base_url]/themes/[var.user_theme]

And replace with:

  1. [var.asset_cdn.theme]

Default CDN

The following CDN will point directly to the same directory as your PHPmotion installation. This allows us to load any PHPmotion files over the CDN. Create a subdomain cdn.example.com that points to /phpmotionpath/ .

You will now be able to load any assets you like by simply replacing the base url with this CDN one. Most strings that need editing are in themes/yourtheme/templates/main_1.htm .

  1. <link href="css/rating_style.css" rel="stylesheet" type="text/css" media="all">
  2. <link href="css/thickbox.css" rel="stylesheet" type="text/css" media="screen">
  3. <link href="greybox/gb_styles.css" rel="stylesheet" type="text/css" media="all">
  4. <link href="javascripts/jquery.jcarousel.css" rel="stylesheet" type="text/css" media="all">

Becomes:

  1. <link href="[var.asset_cdn.cdn]/css/rating_style.css" rel="stylesheet" type="text/css" media="all">
  2. <link href="[var.asset_cdn.cdn]/css/thickbox.css" rel="stylesheet" type="text/css" media="screen">
  3. <link href="[var.asset_cdn.cdn]/greybox/gb_styles.css" rel="stylesheet" type="text/css" media="all">
  4. <link href="javascripts/jquery.jcarousel.css" rel="stylesheet" type="text/css" media="all">

Leave the javascripts/ one as we’ll use the js CDN for this one.

Javascript CDN

Create a subdomain js.example.com that points to /phpmotionpath/javascripts/ . This will allow you to load most of the javascript files. Using your search/replace function use these two to setup the js cdn.

  • Search: “javascripts/
  • Replace with: “[var.asset_cdn.js]/

And:

  • Search: “[var.base_url]/javascripts/
  • Replace with: “[var.asset_cdn.js]/

Video thumbs CDN

Create a subdomain thumbs.example.com that points to /phpmotionpath/uploads/thumbs/ . We’ll now be able to load all the video thumbs over this CDN.

Search for this string in your template files: "uploads/thumbs/" and also "[var.base_url]/uploads/thumbs/
Replace with: "[var.asset_cdn.videothumbs]/

Step 5: Expires

The mod_expires Apache module allows you to set how long certain elements should stay cached in a visitors browser. This is a great way to speed up your website as browsers will know not to bother requesting certain content until it has expired.

You have to be careful when using this as changes you make to any cached content will be ignored. You can get around this by appending ?v=1 to the end of your elements and updating the number each time you make a change to that element. The browser will then think that it is a new item and will re-download it.

We’ll set up some basic Expires rules in the /phpmotionpath/.htaccess file.

  1. # Expires
  2. <IfModule mod_expires.c>
  3. <FilesMatch "\.(gif|png|jpe?g|js|css|ico|swf)$">
  4. ExpiresActive On
  5. ExpiresDefault "access plus 1 week"
  6. </FilesMatch>
  7. </IfModule>
  8. # End Expires

As you can probably tell from the above example your browser will know to cache the files one week after their first access. It is a good idea to only set this up once you’ve finished any theme changes to your website otherwise you will have to append ?v=X to all your assets to force them to be flushed from your browser cache.

Step 6: Advanced Optimisations

There are many other ways to improve load performance, but they require more work and knowledge of coding and server management. Here are a few things that you can look into to further optimize performance.

Image sprites

Image sprites are images (usually small theme images) that are grouped together into a single image file then used within the layout with css backgrounds, changing the background-position to display the right image in the right place. If your theme uses a lot of background images this is a fairly simple way to reduce http requests – providing you have a solid knowledge of CSS that is.

Proxy Cache

If you have a VPS or dedicated server you can setup a proxy cache server which will create and cache pages as they are accessed to majorly improve performance as your PHP files won’t even be accessed. This would require some configuration to avoid pages becoming stale and out of date.

Conclusion

Here is the final result after applying the above steps to the same page quite an improvement don’t you agree?

  • Total page size: 217KB
  • YSlow! Rating: A
  • Page loaded in 4.43s
  • Handles more parallel HTTP requests for assets
  • Decreases bandwidth usage
  • Caches assets in the browser with the mod_expires

Page load time has been shortened by a few seconds (the gain will vary depending on the page). Bandwidth usage will be reduced and thanks to caching less http requests mean less server traffic. Caching is also improved which means that repeat visits and any other pages loaded will be much faster.

I hope you found this post useful. If you did please be kind enough to share it. Feel free to leave any questions or suggestions you might have in the comments below.

 

分享到:
评论

相关推荐

    tables-3.6.1-cp39-cp39-win_amd64.whl

    tables-3.6.1-cp39-cp39-win_amd64.whl

    基于springboot大学生心理咨询平台源码数据库文档.zip

    基于springboot大学生心理咨询平台源码数据库文档.zip

    Javaweb仓库管理系统项目源码.zip

    基于Java web 实现的仓库管理系统源码,适用于初学者了解Java web的开发过程以及仓库管理系统的实现。

    基于springboot智能推荐旅游平台源码数据库文档.zip

    基于springboot智能推荐旅游平台源码数据库文档.zip

    Ruby语言教程:从基础知识到高级特性的全面指南

    内容概要:本文是一份详尽的Ruby语言教程,首先介绍了Ruby语言的基本信息和发展背景。接着详细讲解了Ruby的基础语法,如变量、数据类型、运算符、控制流等,并深入探讨了面向对象编程的关键概念,包括类、对象、继承、封装和多态。随后介绍了Ruby的一些高级特性,如模块、异常处理、迭代器和文件I/O操作。最后,讨论了Ruby在Web开发中的应用,尤其是与Rails框架的结合。每个部分都配有相应的代码示例,帮助读者更好地理解和实践。 适合人群:适用于初学者和有一定基础的程序员,特别是对Ruby语言感兴趣的人。 使用场景及目标:学习和掌握Ruby语言的各项基础知识和高级特性,为进一步进行Web开发或其他相关编程打下坚实的基础。 其他说明:教程中的每一部分内容都有详细的解释和代码示例,非常适合自学和教学使用。

    L7_NDVI_sd.txt

    GEE训练教程——Landsat5、8和Sentinel-2、DEM和各2哦想指数下载

    基于springboot在线问诊系统源码数据库文档.zip

    基于springboot在线问诊系统源码数据库文档.zip

    基于springboot的流浪猫狗救助系统源码数据库文档.zip

    基于springboot的流浪猫狗救助系统源码数据库文档.zip

    value_at_a_point.ipynb

    GEE训练教程

    毕业设计&课设_利用 WiFi 实现室内定位,含定位 APP 与数据采集 APP.zip

    该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过严格测试运行成功才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

    SQLite参考手册中文CHM版最新版本

    本教程帮助您了解什么是 SQLite,它与 SQL 之间的不同,为什么需要它,以及它的应用程序数据库处理方式。需要的朋友们可以参考看看! SQLite是一个软件库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。SQLite是一个增长最快的数据库引擎,这是在普及方面的增长,与它的尺寸大小无关。SQLite 源代码不受版权限制。 什么是 SQLite? SQLite是一个进程内的库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。它是一个零配置的数据库,这意味着与其他数据库一样,您不需要在系统中配置。 就像其他数据库,SQLite 引擎不是一个独立的进程,可以按应用程序需求进行静态或动态连接。SQLite 直接访问其存储文件。 为什么要用 SQLite? 不需要一个单独的服务器进程或操作的系统(无服务器的)。 SQLite 不需要配置,这意味着不需要安装或管理。 一个完整的 SQLite 数据库是存储在一个单一的跨平台的磁盘文件。 SQLite 是非常小的,是轻量级的,完全配置时小于 400KiB,省略可选功能配置时小于250K

    基于springboot学生选课系统源码数据库文档.zip

    基于springboot学生选课系统源码数据库文档.zip

    【Android UI】SurfaceView中使用 Canvas 绘制可缩放大图

    【Android UI】SurfaceView中使用 Canvas 绘制可缩放大图 ( 拖动和缩放相关的变量 | Canvas 绘图函数 | 手势识别 多点触控流程 | 拖动图片 | 缩放图片 ) 博客链接:https://blog.csdn.net/shulianghan/article/details/143950948 一、需求分析 和 核心要点 说明 1、需求说明 2、核心要点 - 拖动和缩放相关的变量 3、核心要点 - Canvas 绘图函数 4、核心要点 - 手势识别 多点触控流程 5、核心要点 - 拖动图片 6、核心要点 - 缩放图片 二、完整代码示例 1、环境说明 2、SurfaceView 完整代码 3、运行结果

    基于springboot+web的二手闲置交易系统源码数据库文档.zip

    基于springboot+web的二手闲置交易系统源码数据库文档.zip

    band_computations.ipynb

    GEE训练教程

    毕业设计&课设_众筹平台系统,基于基础语言开发,含功能流程与运行步骤,适合新手学习借鉴.zip

    该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过严格测试运行成功才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

    基于SpringBoot的宠物寄领养网站源码数据库文档.zip

    基于SpringBoot的宠物寄领养网站源码数据库文档.zip

    基于springboot的电影院售票管理系统源码数据库文档.zip

    基于springboot的电影院售票管理系统源码数据库文档.zip

    毕业设计&课设_含构建设置及相关操作,基于特定技术,具体功能未详细说明.zip

    该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过严格测试运行成功才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

    工具变量上市公司企业银行贷款数据(2019-2024年).txt

    数据存放网盘,txt文件内包含下载链接及提取码,永久有效。失效会第一时间进行补充。样例数据及详细介绍参见文章:https://blog.csdn.net/T0620514/article/details/143956923

Global site tag (gtag.js) - Google Analytics