search
HomeOperation and MaintenanceLinux Operation and MaintenanceCentOS builds dynamic caching and static resource optimization of web servers

CentOS builds dynamic caching and static resource optimization of web servers

Aug 06, 2023 am 09:16 AM
centosweb serverCache optimization

CentOS builds dynamic caching and static resource optimization of web servers

With the development of the Internet, the number of visits to websites is increasing, and the performance optimization of web servers has become more and more important. This article will introduce how to build a web server on a CentOS server and optimize dynamic caching and static resources.

1. Install the Apache server

First, we need to install the Apache server. On CentOS, you can install it with the following command:

sudo yum install httpd

After the installation is completed, start the Apache server:

sudo systemctl start httpd

2. Dynamic cache optimization

Dynamic cache is a kind of A technology for caching frequently changing web page content on the server. By caching dynamic pages, the load on the server can be reduced and the page loading speed can be improved.

  1. Install memcached

On CentOS, you can install memcached through the following command:

sudo yum install memcached

After the installation is complete, start memcached:

sudo systemctl start memcached
  1. Configuring Apache and PHP

Open the Apache configuration file:

sudo vi /etc/httpd/conf/httpd.conf

Find the following line:

LoadModule expires_module modules/mod_expires.so

Replace the comment symbol "#" in front of it "Remove it to enable this module.

Then find the following line:

LoadModule headers_module modules/mod_headers.so

Similarly, remove the comment symbol "#" in front of it.

Save and exit the configuration file.

  1. Configure memcached

Open the memcached configuration file:

sudo vi /etc/sysconfig/memcached

Find the following line:

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""

You can modify it according to actual needs parameters, then save and exit the configuration file.

  1. Configure PHP extension

Install memcached PHP extension:

sudo yum install php-pecl-memcached

After the installation is complete, restart the Apache server:

sudo systemctl restart httpd
  1. Using dynamic cache in PHP code

Using dynamic cache in PHP code can be achieved through the following code:

$memcached = new Memcached();
$memcached->addServer('localhost', 11211);
$data = $memcached->get('cached_data');

if (!$data) {
    // 从数据库或其他途径获取数据
    $data = getDataFromDatabase();

    // 将数据存入缓存
    $memcached->set('cached_data', $data, 3600);
}

// 使用数据进行页面渲染
renderPage($data);
  1. Monitor the usage of dynamic cache

Install the memcached monitoring tool:

sudo yum install php-pecl-memcache

After the installation is completed, visit the following address in the browser to view the usage of dynamic cache:

http://your_server_ip/memcache.php

3. Static Resource Optimization

For the optimization of static resources (such as images, CSS and JavaScript files), there are the following common methods.

  1. Enable Gzip compression

Open the Apache configuration file:

sudo vi /etc/httpd/conf/httpd.conf

Find the following line:

LoadModule deflate_module modules/mod_deflate.so

Replace the comment in front of it The symbol "#" is removed to indicate that the module is enabled.

Then find the following line:

#Insert filter
SetOutputFilter DEFLATE

Similarly, remove the comment symbol "#" in front of it. Save and exit the configuration file.

Restart the Apache server:

sudo systemctl restart httpd
  1. Enable browser cache

Add the following code snippet to the Apache configuration file:

<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresByType text/css "access plus 1 year"
    ExpiresByType application/javascript "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
</IfModule>

Save and exit the configuration file.

  1. Use CDN to accelerate

CDN (Content Delivery Network) is a distributed server system that can distribute static resources to the server closest to the user, improving resources loading speed. You can use the following code snippet to convert the website's static resource references to CDN addresses:

<link rel="stylesheet" href="http://cdn.example.com/style.css">
<script src="http://cdn.example.com/script.js"></script>
<img  src="/static/imghwm/default1.png"  data-src="http://cdn.example.com/image.jpg"  class="lazy" alt="CentOS builds dynamic caching and static resource optimization of web servers" >

IV. Summary

This article introduces the establishment of a web server on a CentOS server, as well as the dynamic cache and static How to optimize resources. By properly configuring the server, caching dynamic pages, and compressing and caching static resources, the access speed of the website can be improved and the user experience can be improved. At the same time, using CDN acceleration can further improve the loading speed of resources. Hope this article is helpful to you.

The above is the detailed content of CentOS builds dynamic caching and static resource optimization of web servers. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is Maintenance Mode in Linux? ExplainedWhat is Maintenance Mode in Linux? ExplainedApr 22, 2025 am 12:06 AM

MaintenanceModeinLinuxisaspecialbootenvironmentforcriticalsystemmaintenancetasks.Itallowsadministratorstoperformtaskslikeresettingpasswords,repairingfilesystems,andrecoveringfrombootfailuresinaminimalenvironment.ToenterMaintenanceMode,interrupttheboo

Linux: A Deep Dive into Its Fundamental PartsLinux: A Deep Dive into Its Fundamental PartsApr 21, 2025 am 12:03 AM

The core components of Linux include kernel, file system, shell, user and kernel space, device drivers, and performance optimization and best practices. 1) The kernel is the core of the system, managing hardware, memory and processes. 2) The file system organizes data and supports multiple types such as ext4, Btrfs and XFS. 3) Shell is the command center for users to interact with the system and supports scripting. 4) Separate user space from kernel space to ensure system stability. 5) The device driver connects the hardware to the operating system. 6) Performance optimization includes tuning system configuration and following best practices.

Linux Architecture: Unveiling the 5 Basic ComponentsLinux Architecture: Unveiling the 5 Basic ComponentsApr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Linux Operations: Utilizing the Maintenance ModeLinux Operations: Utilizing the Maintenance ModeApr 19, 2025 am 12:08 AM

Linux maintenance mode can be entered through the GRUB menu. The specific steps are: 1) Select the kernel in the GRUB menu and press 'e' to edit, 2) Add 'single' or '1' at the end of the 'linux' line, 3) Press Ctrl X to start. Maintenance mode provides a secure environment for tasks such as system repair, password reset and system upgrade.

Linux: How to Enter Recovery Mode (and Maintenance)Linux: How to Enter Recovery Mode (and Maintenance)Apr 18, 2025 am 12:05 AM

The steps to enter Linux recovery mode are: 1. Restart the system and press the specific key to enter the GRUB menu; 2. Select the option with (recoverymode); 3. Select the operation in the recovery mode menu, such as fsck or root. Recovery mode allows you to start the system in single-user mode, perform file system checks and repairs, edit configuration files, and other operations to help solve system problems.

Linux's Essential Components: Explained for BeginnersLinux's Essential Components: Explained for BeginnersApr 17, 2025 am 12:08 AM

The core components of Linux include the kernel, file system, shell and common tools. 1. The kernel manages hardware resources and provides basic services. 2. The file system organizes and stores data. 3. Shell is the interface for users to interact with the system. 4. Common tools help complete daily tasks.

Linux: A Look at Its Fundamental StructureLinux: A Look at Its Fundamental StructureApr 16, 2025 am 12:01 AM

The basic structure of Linux includes the kernel, file system, and shell. 1) Kernel management hardware resources and use uname-r to view the version. 2) The EXT4 file system supports large files and logs and is created using mkfs.ext4. 3) Shell provides command line interaction such as Bash, and lists files using ls-l.

Linux Operations: System Administration and MaintenanceLinux Operations: System Administration and MaintenanceApr 15, 2025 am 12:10 AM

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment