Trang chủ » Hướng dẫn cách tối ưu LAMP Server

Hướng dẫn cách tối ưu LAMP Server

bởi Vinh Leo
2,3K views
5/5 - (4 bình chọn)

Trong bài trước, mình đã hướng dẫn các bạn cách cài LAMP server trên CentOS 7. Để server có thể hoạt động tốt, ổn định, chịu tải cao thì việc đầu tiên phải làm đó là tối ưu LAMP Server

Tương tự như LEMP server, khi cài đặt xong LAMP các bạn nên thực hiện tối ưu server bằng các thủ thuật sau.

Tận dụng browser caching

Mình sẽ hướng dẫn các bạn cách thêm Expires headers vào file .htaccess để tận dụng browser caching. Phương pháp này giúp giảm thiểu tối đa HTTP request đến server bằng cách sử dụng những file tĩnh như hình ảnh, css, javascript đã lưu trong cache của browser lúc bạn truy cập website lần đầu tiên.

Cách thêm Expires header

Trong ví dụ này mình sẽ thêm header cho một số loại file tĩnh như:

  • images: jpg, gif, png
  • favicon/ico
  • javascript
  • css

Thời gian lưu cache thì bạn có thể tùy chọn

  • years
  • months
  • weeks
  • days
  • hours
  • minutes
  • seconds

Đây là đoạn code mình sử dụng, các bạn thêm vào cuối file .htaccess

<IfModule mod_expires.c>

# Enable expirations
ExpiresActive On

# Default directive
ExpiresDefault "access plus 1 month"

# My favicon
ExpiresByType image/x-icon "access plus 1 year"

# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"

# CSS
ExpiresByType text/css "access 1 month"

# Javascript
ExpiresByType application/javascript "access plus 1 year"

</IfModule>

Kích hoạt GZIP Compression cho Apache Server

GZIP giúp tối ưu và tăng tốc website bằng cách nén nội dung trả về từ server mỗi khi có request gửi đến.

Để thực hiện, các bạn chỉ cần thêm đoạn sau vào cuối file .htaccess. Apache 1.3 sử dụng mod_gzip trong khi Apache 2.x sử dụng mod_deflate.

Cấu hình mod_gzip cho Apache 1.3.x

<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>

Cấu hình mod_deflate cho Apache 2.x

<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
# Don't compress
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
#Dealing with proxy servers
<IfModule mod_headers.c>
Header append Vary User-Agent
</IfModule>
</IfModule>

Để test lại xem đã thành công hay chưa các bạn sử dụng tool REDbot, điền link site bạn vào và tìm xem có dòng Content negotiation for gzip compression is supporte trong kết quả trả về là được.

REDbot Học Mạng Máy Tính
Nguồn: Học VPS

Chúc các bạn thành công! Mọi thắc mắc, cần hỗ trợ hãy bình luận bên dưới hoặc qua Fanpage Facebook, form liên hệ.

Xem tiếp các bài trong Series LAMP cho VPS/Server

CÓ THỂ BẠN QUAN TÂM

Subscribe
Thông báo cho
guest
0 Góp ý
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x