一步一步教你apache深入优化

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一步一步教你apache深入优化相关的知识,希望对你有一定的参考价值。

Aache深入优化

一、在Centos01上安装apache服务,在Centos02配置DNS服务器使用域名www.stz.com

1、在Centos01上安装apache服务配置网站

1)切换光盘

一步一步教你apache深入优化_apache

2)挂载光盘解压源代码程序

[root@centos01 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# tar zxf /mnt/httpd-2.2.17.tar.gz -C /usr/src/
[root@centos01 ~]# ls /usr/src/
debug httpd-2.2.17 kernels

3)配置安装apache

[root@centos01 src]# cd /usr/src/httpd-2.2.17/
[root@centos01 httpd-2.2.17]# ./configure \\
> --prefix=/usr/local/httpd \\
> --enable-so \\
> --enable-rewrite \\
> --enable-charset-lite \\
> --enable-cgi

4)编译安装apache

[root@centos01 httpd-2.2.17]# make && make install

2、配置apache服务

1)生成服务控制文件添加执行权限

[root@centos01 httpd-2.2.17]# cd
[root@centos01 ~]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[root@centos01 ~]# chmod +x /etc/init.d/httpd

2)修改服务控制文件

[root@centos01 ~]# vim /etc/init.d/httpd
#!/bin/sh
#chkconfig:35 21 80
#description:Apache Server

3)添加系统服务设置开机自动启动

[root@centos01 ~]# chkconfig --add httpd
[root@centos01 ~]# chkconfig --level 35 httpd on

4)优化apache命令

[root@centos01 ~]# ln -s /usr/local/httpd/bin/* /usr/local/bin/

3、启动apache服务查看服务运行状态

1)启动服务

[root@centos01 ~]# systemctl start httpd

2)查看服务运行状态

[root@centos01 ~]# netstat -anptu | grep http
tcp6
0 0 :::80 :::* LISTEN 53628/httpd

4、修改apache网站根目录设置网站主页

1)创建网站主页根目录设置主页

[root@centos01 ~]# mkdir /www
[root@centos01 ~]# echo "www.stz.com" > /www/index.html

2)修改apahce主配置文件

[root@centos01 ~]# vim
/usr/local/httpd/conf/httpd.conf
97 ServerName www.benet.com:80
131 <Directory "/www">
132 Options Indexes FollowSymLinks
133 AllowOverride None
134 Order allow,deny
135 Allow from all
136 </Directory>
142 <IfModule dir_module>
143 DirectoryIndex index.html
144 </IfModule>

3)检查主配置文件

[root@centos01 ~]# httpd -t
Syntax OK

4)重新启动apache配置文件加载配置

[root@centos01 ~]# systemctl restart httpd

二、在Centos02上安装DNS使用域名访问日志

1、安装DNS服务设置开机自动启动

1)挂载光盘安装DNS服务

[root@centos02 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos02 ~]# ls /mnt/
CentOS_BuildTag GPL
LiveOS RPM-GPG-KEY-CentOS-7
EFI images Packages
RPM-GPG-KEY-CentOS-Testing-7
EULA isolinux repodata
TRANS.TBL

2)安装DNS服务

[root@centos02 ~]# rpm -ivh /mnt/Packages/bind-9.9.4-50.el7.x86_64.rpm
[root@centos02 ~]# rpm -ivh /mnt/Packages/bind-chroot-9.9.4-50.el7.x86_64.rpm

3)设置服务开机自动启动

[root@centos02 ~]# systemctl start named
[root@centos02 ~]# systemctl enable named

2、修改主配置文件和区域配置文件

1)修改DNS主配置文件

options      
listen-on port 53 any; ;

directory
"/var/named";
;
zone "stz.com" IN

type master;

file "/var/named/stz.com.zone";
;

2)检查主配置文件是否错误

[root@centos02 ~]# named-checkconf /etc/named.conf

3)修改区域配置文件

[root@centos02 ~]# vim
/var/named/stz.com.zone
$TTL 86400
@ SOA stz.com. root.stz.com. (
202321210
1H
15M
1W
1D
)
@ NS centos02.stz.com.
centos02 A 192.168.100.20
www A 192.168.100.10

4)检查区域配置文件是否错误

[root@centos02 ~]# named-checkzone stz.com /var/named/stz.com.zone 
zone stz.com/IN: loaded serial 202321210
OK

3、启动DNS服务查看服务运行状态

1)启动DNS服务

[root@centos02 ~]# systemctl start named

2)查看服务运行状态

[root@centos02 ~]# netstat -anptu | grep named
tcp
0 0 127.0.0.1:53 0.0.0.0:* LISTEN 39645/named
tcp
0 0 127.0.0.1:953 0.0.0.0:* LISTEN 39645/named
tcp6
0 0 ::1:53 :::* LISTEN 39645/named
tcp6
0 0 ::1:953 :::* LISTEN 39645/named
udp
0 0 127.0.0.1:53 0.0.0.0:* 39645/named
udp6
0 0 ::1:53 :::* 39645/named

4、配置验证DNS访问网站

1)给客户端配置ip

一步一步教你apache深入优化_配置文件_02

2)配置域名解析

一步一步教你apache深入优化_配置文件_03

3)访问网站

一步一步教你apache深入优化_配置文件_04

三、在Centos01上配置apache工作模式优化

1、管理Apache的prefork工作模式

1)查看Apache默认的prefork工作模式

[root@centos01 ~]# httpd -V
Server version: Apache/2.2.17 (Unix)
Server built: Feb 12 2023 18:02:11
Servers Module Magic Number: 20051115:25
Server loaded: APR 1.4.2, APR-Util 1.3.10
Compiled using: APR 1.4.2, APR-Util 1.3.10
Architecture: 64-bit
Server MPM: Prefork

threaded: no
forked: yes (variable process count)
Server compiled with....
-D
APACHE_MPM_DIR="server/mpm/prefork"
-D
APR_HAS_SENDFILE
-D
APR_HAS_MMAP
-D
APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D
APR_USE_SYSVSEM_SERIALIZE
-D
APR_USE_PTHREAD_SERIALIZE
-D
APR_HAS_OTHER_CHILD
-D
AP_HAVE_RELIABLE_PIPED_LOGS
-D
DYNAMIC_MODULE_LIMIT=128
-D
HTTPD_ROOT="/usr/local/httpd"
-D
SUEXEC_BIN="/usr/local/httpd/bin/suexec"
-D
DEFAULT_PIDLOG="logs/httpd.pid"
-D
DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D
DEFAULT_LOCKFILE="logs/accept.lock"
-D
DEFAULT_ERRORLOG="logs/error_log"
-D
AP_TYPES_CONFIG_FILE="conf/mime.types"
-D
SERVER_CONFIG_FILE="conf/httpd.conf"

2)优化prefork工作模式

[root@centos01 ~]# vim /usr/local/httpd/conf/extra/httpd-mpm.conf
<IfModule mpm_prefork_module>

StartServers 5 //启动进程数
MinSpareServers 5 //最小空闲进程,空闲小于当前值自动创建

MaxSpareServers 10 //空闲进程大于当前值自动kill掉

Serverlimit 200 //最大进程

MaxClients 150 //最大并发请求数必须前150处理完处理151

MaxRequestsPerChild 0 //每个子进程最大处理请求数0不限制,可以防止内存外溢和负载下降可以减少子进程数
</IfModule>

3)查看prefork的进程信息

[root@centos01 ~]# ps -ef | grep http
root
53908 1 0 18:26 ? 00:00:00 /usr/local/httpd/bin/httpd -k
start
daemon
53909 53908 0 18:26 ? 00:00:00 /usr/local/httpd/bin/httpd -k
start
daemon
53910 53908 0 18:26 ? 00:00:00 /usr/local/httpd/bin/httpd -k
start
daemon
53911 53908 0 18:26 ? 00:00:00 /usr/local/httpd/bin/httpd -k
start
daemon
53912 53908 0 18:26 ? 00:00:00 /usr/local/httpd/bin/httpd -k
start
daemon
53913 53908 0 18:26 ? 00:00:00 /usr/local/httpd/bin/httpd -k
start
daemon
92661 53908 0 18:51 ? 00:00:00 /usr/local/httpd/bin/httpd -k
start
root
92840 92692 0 19:00 pts/1 00:00:00 grep --color=auto http

2、管理apache的woker工作模式

1)修改apache的工作模式为worker

[root@centos01 httpd-2.2.17]# ./configure
--prefix=/usr/local/httpd/ --enable-so \\
> --enable-rewrite \\
> --enable-charset-lite \\
> --enable-cgi \\
> --with-mpm=worker

2)安装apache

[root@centos01 httpd-2.2.17]# make && make install

3)查看修改后的工作模式

[root@centos01 ~]# httpd -V
Server version: Apache/2.2.17 (Unix)
Server built: Feb 12 2023 19:06:06
Servers Module Magic Number: 20051115:25
Server loaded: APR 1.4.2, APR-Util 1.3.10
Compiled using: APR 1.4.2, APR-Util 1.3.10
Architecture: 64-bit
Server MPM: Worker

threaded: yes (fixed thread
count)

forked: yes (variable process
count)
Server compiled with....
-D
APACHE_MPM_DIR="server/mpm/worker"
-D
APR_HAS_SENDFILE
-D
APR_HAS_MMAP
-D
APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D
APR_USE_SYSVSEM_SERIALIZE
-D
APR_USE_PTHREAD_SERIALIZE
-D
APR_HAS_OTHER_CHILD
-D
AP_HAVE_RELIABLE_PIPED_LOGS
-D
DYNAMIC_MODULE_LIMIT=128
-D
HTTPD_ROOT="/usr/local/httpd/"
-D
SUEXEC_BIN="/usr/local/httpd//bin/suexec"
-D
DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D
DEFAULT_ERRORLOG="logs/error_log"
-D
AP_TYPES_CONFIG_FILE="conf/mime.types"
-D
SERVER_CONFIG_FILE="conf/httpd.conf"

4)修改apache主配置文件加载worker配置

[root@centos01 ~]# vim /usr/local/httpd/conf/httpd.conf
347 Include conf/extra/httpd-mpm.con

5)修改apache工作模式配置文件

[root@centos01 ~]# vim /usr/local/httpd/conf/extra/httpd-mpm.conf
<IfModule mpm_worker_module>

StartServers 2 //启动进程

ServerLimit 512 //最大进程数

ThreadLimit 64 //每个进程处理最大线程

MaxClients 12800 //最大连接并发请求

MinSpareThreads 25 //最小空闲线程

MaxSpareThreads 75 //最大空闲线程

ThreadsPerChild 25 //每个进程默认处理线程数

MaxRequestsPerChild 0 ///每个子进程最大处理请求数0不限制
</IfModule>

6)检查配置是否错误

[root@centos01 ~]# httpd -t
Syntax OK

7)重启服务

[root@centos01 ~]# systemctl restart httpd

8)使用pstree查看线程数-a详细信息

[root@centos01 ~]# pstree
systemd─┬─ModemManager───2*[ModemManager]

├─NetworkManager───2*[NetworkManager]

├─VGAuthService

├─2*[abrt-watch-log]

├─abrtd

├─agetty

├─alsactl

├─atd

├─auditd─┬─audispd─┬─sedispatch

│ │ └─audispd

│ └─auditd

├─avahi-daemon───avahi-daemon

├─chronyd

├─crond

├─cupsd

├─dbus-daemon

├─dnsmasq───dnsmasq

├─gssproxy───5*[gssproxy]

├─httpd─┬─httpd

│ └─2*[httpd───26*[httpd]]

├─ksmtuned───sleep

├─libvirtd───15*[libvirtd]

├─lsmd

├─lvmetad

├─master─┬─pickup

│ └─qmgr
├─polkitd───5*[polkitd]

├─rngd

├─rsyslogd───2*[rsyslogd]

├─smartd

├─sshd─┬─sshd───bash

│ └─sshd───bash───pstree

├─systemd-journal

├─systemd-logind

├─systemd-udevd

├─tuned───4*[tuned]

└─vmtoolsd───vmtoolsd

3、配置使用ab压力测试

1)压力测试

[root@centos01 ~]# ab -n1000 -c500
www.stz.com/index.html
This is ApacheBench, Version 2.3
<$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology
Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation,
http://www.apache.org/

Benchmarking www.stz.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software: Apache/2.2.17
Server Hostname: www.stz.com
Server Port: 80

Document Path: /index.html
Document Length: 12 bytes

Concurrency Level: 500
Time taken for tests: 0.823 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 272205 bytes
HTML transferred: 12420 bytes
Requests per second: 1214.71 [#/sec] (mean)
Time per request: 411.623 [ms] (mean)
Time per request: 0.823 [ms] (mean, across all concurrent
requests)
Transfer rate: 322.90 [Kbytes/sec] received

Connection Times (ms)
min
mean[+/-sd] median max
Connect: 0
2 1.9 1
6
Processing: 4
75 137.9 7 816
Waiting: 4
75 138.0 7 816
Total: 4
77 139.3 8 820

Percentage of the requests served within a
certain time (ms)

50% 8

66% 10

75% 11

80% 215

90% 417

95% 420

98% 420

99% 421
100%
820 (longest request)

2)查看线程和进程数

[root@centos01 ~]# pstree -a
systemd --switched-root --system
--deserialize21

├─ModemManager

│ └─2*[ModemManager]

├─NetworkManager --no-daemon

│ └─2*[NetworkManager]

├─VGAuthService -s

├─abrt-watch-log -F Backtrace /var/log/Xorg.0.log --/usr/b

├─abrt-watch-log -F BUG: WARNING: at WARNING: CPU:INFO: po

├─abrtd -d -s

├─agetty --noclear tty1 linux

├─alsactl -s -n 19 -c -EALSA_CONFIG_PATH=/etc/alsa/alsact

├─atd -f

├─auditd

│ ├─audispd

│ │ ├─sedispatch

│ │ └─audispd

│ └─auditd

├─avahi-daemon

│ └─avahi-daemon

├─chronyd

├─crond -n

├─cupsd -f

├─dbus-daemon --system --address=systemd: --nofork ...

├─dnsmasq--conf-file=/var/lib/libvirt/dnsmasq/default.con


└─dnsmasq--conf-file=/var/lib/libvirt/dnsmasq/default.con
├─gssproxy
-D

│ └─5*[gssproxy]

├─httpd -k start

│ ├─httpd -k start

│ ├─httpd -k start

│ │ └─26*[httpd]

│ └─httpd -k start

│ └─26*[httpd]

├─ksmtuned /usr/sbin/ksmtuned

│ └─sleep 60

├─libvirtd

│ └─15*[libvirtd]

├─lsmd -d

├─lvmetad -f

├─master -w

│ ├─pickup -l -t unix -u

│ └─qmgr -l -t unix -u

├─polkitd --no-debug

│ └─5*[polkitd]

├─rngd -f

├─rsyslogd -n

│ └─2*[rsyslogd]

├─smartd -n -q never

├─sshd -D

│ ├─sshd

│ │ └─bash

│ └─sshd

│ └─bash

│ └─pstree -a

├─systemd-journal

├─systemd-logind

├─systemd-udevd

├─tuned -Es /usr/sbin/tuned -l -P

│ └─4*[tuned]

└─vmtoolsd

└─vmtoolsd

以上是关于一步一步教你apache深入优化的主要内容,如果未能解决你的问题,请参考以下文章

一步一步教你Nginx优化和防盗链

一步一步教你Nginx优化和防盗链

一步一步教你 https 抓包

一步一步教你使用 LSMW 批量处理数据

一步一步教你认识闭包

一步一步教你写perl