Home  >  Article  >  Operation and Maintenance  >  Log management and monitoring skills for building a web server on CentOS

Log management and monitoring skills for building a web server on CentOS

WBOY
WBOYOriginal
2023-08-05 08:33:141361browse

Log management and monitoring skills for building web servers on CentOS

Web servers are an important part of modern Internet applications, and server log management and monitoring are the key to ensuring stable server operation and troubleshooting. This article will introduce how to build a web server on the CentOS operating system, and provide some log management and monitoring techniques.

1. Build a Web server

  1. Install Apache

Apache is a popular open source Web server software. Installing Apache on CentOS is very simple, just enter the following command in the terminal:

sudo yum install httpd
  1. Configuring Apache

After installing Apache, you need to do some basic things on it configuration. The configuration file is located at /etc/httpd/conf/httpd.conf. You can open the file using an editor such as vi or nano.

For example, you can change the root directory of the default website:

DocumentRoot "/var/www/html"
  1. Start Apache

After completing the configuration, you can start Apache. Enter the following command:

sudo systemctl start httpd

2. Log management

  1. Log file location

Apache generates various types of files in the /var/log/httpd/ directory Log files. The following are some common log files:

  • access_log: records information about all HTTP requests;
  • error_log: records information about server errors;
  • ssl_access_log: records Information on all HTTPS requests;
  • ssl_error_log: records error information on HTTPS requests.
  1. Log file cutting

Since the log files generated by Apache can easily grow to very large sizes, you may need to periodically cut the log files for easier management. Linux provides a tool called logrotate that can easily cut and compress log files.

First, install logrotate:

sudo yum install logrotate

Then, create a log cutting configuration file named httpd:

sudo nano /etc/logrotate.d/httpd

In this file, you can specify the log file cutting rule. Here is an example configuration:

/var/log/httpd/access_log {
    rotate 7
    daily
    missingok
    compress
    delaycompress
    notifempty
    create 640 root root
    sharedscripts
    postrotate
        /sbin/service httpd reload > /dev/null 2>/dev/null || true
    endscript
}

In this configuration, the log files will be rotated daily and backups of the last 7 days will be retained. The cut files will be compressed and archived.

  1. Analysis log

The log file contains rich information and is very helpful for server analysis and monitoring. You can use some tools to analyze logs, such as Awstats, Webalizer, and ELK Stack.

Awstats and Webalizer are two tools for generating detailed website statistics reports. You can install them using the yum command:

sudo yum install awstats webalizer

After the installation is complete, you need to configure them to analyze Apache's log files. Please refer to the respective official documentation for specific configuration methods.

ELK Stack is a powerful log management and analysis platform, which consists of three open source tools: Elasticsearch, Logstash and Kibana. You can use ELK Stack to centrally store, analyze, and visualize Apache log data.

3. Monitoring skills

  1. Use monitoring tools

In order to discover and solve server faults and problems in a timely manner, you can use some monitoring tools, such as Nagios, Zabbix and Prometheus, etc.

Nagios is a widely used infrastructure monitoring tool that can monitor various services and applications and provide alerting and reporting functions. You can install Nagios on CentOS and configure it to monitor the running status of Apache.

Zabbix is ​​a powerful network monitoring tool that supports real-time monitoring of server performance and resources. You can use Zabbix to monitor key metrics of Apache, such as CPU usage, memory usage, and network traffic.

Prometheus is a recently popular monitoring system that provides rich built-in indicators and flexible query language. You can use Prometheus to monitor Apache performance metrics and visualize them using tools such as Grafana.

  1. Custom monitoring script

In addition to using existing monitoring tools, you can also write your own monitoring scripts to monitor Apache.

For example, you can write a simple Bash script to regularly check the running status of Apache and send alert emails to the administrator:

#!/bin/bash

# 检查Apache是否运行
if ! pgrep -x "httpd" > /dev/null
then
    # 发送警报邮件
    echo "Apache is not running" | mail -s "Apache Alert" admin@example.com
fi

Save the above script as check_apache.sh and use The cron scheduled task executes it:

*/5 * * * * /path/to/check_apache.sh

This script will run every 5 minutes and check whether Apache is running. If Apache is not running, the script will send an alert email to the administrator.

Summary:

This article introduces the steps to build a web server on the CentOS operating system, and provides some log management and monitoring techniques. By properly configuring log files and using monitoring tools, you can better manage and maintain your web server, ensure its stable operation and find and solve problems in a timely manner. By customizing monitoring scripts, you can flexibly monitor the running status of Apache. Hope these tips are helpful to you.

The above is the detailed content of Log management and monitoring skills for building a web server on CentOS. 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