httpd
httpd
目录- 3.3 web相关的命令
- 3.3.1 curl命令
- 6.虚拟主机配置
- 6.1 相同IP不同端口
- 6.2 不同IP相同端口
- 6.3 相同IP,相同端口,不同域名
- 6.4 ssl
1. httpd简介
httpd是Apache超文本传输协议(HTTP)服务器的主程序。被设计为一个独立运行的后台进程,它会建立一个处理请求的子进程或线程的池。
通常,httpd不应该被直接调用,而应该在类Unix系统中由apachectl调用,在Windows中作为服务运行。
2. httpd版本
本文主要介绍httpd的两大版本,httpd-2.2和httpd-2.4。
- CentOS6系列的版本默认提供的是httpd-2.2版本的rpm包
- CentOS7系列的版本默认提供的是httpd-2.4版本的rpm包
2.1 httpd的特性
httpd有很多特性,下面就分别来说说httpd-2.2版本和httpd-2.4版本各自的特性。
版本 特性 2.2 事先创建进程 按需维持适当的进程 模块化设计,核心比较小,各种功能通过模块添加(包括PHP),支持运行时配置,支持单独编译模块 支持多种方式的虚拟主机配置,如基于ip的虚拟主机,基于端口的虚拟主机,基于域名的虚拟主机等 支持https协议(通过mod_ssl模块实现) 支持用户认证 支持基于IP或域名的ACL访问控制机制 支持每目录的访问控制(用户访问默认主页时不需要提供用户名和密码,但是用户访问某特定目录时需要提供用户名和密码) 支持URL重写 支持MPM(Multi Path Modules,多处理模块)。用于定义httpd的工作模型(单进程、单进程多线程、多进程、多进程单线程、多进程多线程) 2.4 httpd-2.4的新特性: MPM支持运行DSO机制(Dynamic Share Object,模块的动态装/卸载机制),以模块形式按需加载 支持event MPM,eventMPM模块生产环境可用 支持异步读写 支持每个模块及每个目录分别使用各自的日志级别 每个请求相关的专业配置,使用 来配置 增强版的表达式分析器 支持毫秒级的keepalive timeout 基于FQDN的虚拟主机不再需要NameVirtualHost指令 支持用户自定义变量 支持新的指令(AllowOverrideList) 降低对内存的消耗 工作模型 工作方式 prefork 多进程模型,预先生成进程,一个请求用一个进程响应 一个主进程负责生成n个子进程,子进程也称为工作进程 每个子进程处理一个用户请求,即使没有用户请求,也会预先生成多个空闲进程,随时等待请求到达,最大不会超过1024个 worker 基于线程工作,一个请求用一个线程响应(启动多个进程,每个进程生成多个线程) event 基于事件的驱动,一个进程处理多个请求 2.2 httpd-2.4新增的模块
httpd-2.4在之前的版本基础上新增了几大模块,下面就几个常用的来介绍一下。
模块 功能 mod_proxy_fcgi 反向代理时支持apache服务器后端协议的模块 mod_ratelimit 提供速率限制功能的模块 mod_remoteip 基于ip的访问控制机制被改变,不再支持使用Order,Deny,Allow来做基于IP的访问控制 3. httpd基础
3.1 httpd自带的工具程序
工具 功能 htpasswd basic认证基于文件实现时,用到的帐号密码生成工具 apachectl httpd自带的服务控制脚本,支持start,stop,restart apxs 由httpd-devel包提供的,扩展httpd使用第三方模块的工具 rotatelogs 日志滚动工具 suexec 访问某些有特殊权限配置的资源时,临时切换至指定用户运行的工具 ab apache benchmark,httpd的压力测试工具 3.2 rpm包安装的httpd程序环境
文件/目录 对应的功能 /var/log/httpd/access.log 访问日志 /var/log/httpd/error_log 错误日志 /var/www/html/ 站点文档目录 /usr/lib64/httpd/modules/ 模块文件路径 /etc/httpd/conf/httpd.conf 主配置文件 /etc/httpd/conf.modules.d/*.conf 模块配置文件 /etc/httpd/conf.d/*.conf 辅助配置文件 mpm:以DSO机制提供,配置文件为/etc/httpd/conf.modules.d/00-mpm.conf
3.3 web相关的命令
3.3.1 curl命令
curl是基于URL语法在命令行方式下工作的文件传输工具,它支持FTP,FTPS,HTTP,HTTPS,GOPHER,TELNET,DICT,FILE及LDAP等协议。
curl支持以下功能:
- https认证
- http的POST/PUT等方法
- ftp上传
- kerberos认证
- http上传
- 代理服务器
- cookies
- 用户名/密码认证
- 下载文件断点续传
- socks5代理服务器
- 通过http代理服务器上传文件到ftp服务器
//语法:curl [options] [URL ...] //常用的options: -A/--user-agent
//设置用户代理发送给服务器 -basic //使用Http基本认证 --tcp-nodelay //使用TCP_NODELAY选项 -e/--referer //来源网址 --cacert //CA证书(SSL) --compressed //要求返回时压缩的格式 -H/--header //自定义请求首部信息传递给服务器 -I/--head //只显示响应报文首部信息 --limit-rate //设置传输速度 -u/--user //设置服务器的用户和密码 -0/--http1 //使用http 1.0版本,默认使用1.1版本。这个选项是数字0而不是字母o -o/--output //把输出写到文件中 -#/--progress-bar //进度条显示当前的传送状态 //通过curl下载文件 [root@localhost ~]# ls [root@localhost ~]# curl -o myblog.html http://blog.51cto.com/itchentao % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 67025 0 67025 0 0 87248 0 --:--:-- --:--:-- --:--:-- 87385 [root@localhost ~]# ls myblog.html 3.3.2 httpd命令
//语法:httpd [options] //常用的options: -l //查看静态编译的模块,列出核心中编译了哪些模块。 \ //它不会列出使用LoadModule指令动态加载的模块 -M //输出一个已经启用的模块列表,包括静态编译在服务 \ //器中的模块和作为DSO动态加载的模块 -v //显示httpd的版本,然后退出 -V //显示httpd和apr/apr-util的版本和编译参数,然后退出 -X //以调试模式运行httpd。仅启动一个工作进程,并且 \ //服务器不与控制台脱离 -t //检查配置文件是否有语法错误 [root@localhost ~]# httpd -l Compiled in modules: core.c mod_so.c http_core.c [root@localhost ~]# httpd -M AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message Loaded Modules: core_module (static) so_module (static) http_module (static) access_compat_module (shared) actions_module (shared) alias_module (shared) allowmethods_module (shared) auth_basic_module (shared) ...... ...... [root@localhost ~]# httpd -v Server version: Apache/2.4.6 (CentOS) Server built: Jun 27 2018 13:48:59 [root@localhost ~]# httpd -V AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message Server version: Apache/2.4.6 (CentOS) Server built: Jun 27 2018 13:48:59 Server's Module Magic Number: 20120211:24 Server loaded: APR 1.4.8, APR-UTIL 1.5.2 Compiled using: APR 1.4.8, APR-UTIL 1.5.2 Architecture: 64-bit Server MPM: prefork threaded: no forked: yes (variable process count) Server compiled with.... -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 SINGLE_LISTEN_UNSERIALIZED_ACCEPT -D APR_HAS_OTHER_CHILD -D AP_HAVE_RELIABLE_PIPED_LOGS -D DYNAMIC_MODULE_LIMIT=256 -D HTTPD_ROOT="/etc/httpd" -D SUEXEC_BIN="/usr/sbin/suexec" -D DEFAULT_PIDLOG="/run/httpd/httpd.pid" -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. 编译安装httpd-2.4
httpd依赖于apr-1.4+,apr-util-1.4+,[apr-icon]
apr:apache portable runtime
//安装开发环境 [root@localhost ~]# yum groups mark install "Development Tools" [root@localhost ~]# groupadd -r apache [root@localhost ~]# useradd -r -g apache apache [root@localhost ~]# yum -y install openssl-devel pcre-devel expat-devel libtool Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registered with an entitlement server. You can use subscription-manager to register. Resolving Dependencies --> Running transaction check ---> Package openssl-devel.x86_64 1:1.0.2k-12.el7 will be installed --> Processing Dependency: openssl-libs(x86-64) = 1:1.0.2k-12.el7 for package: 1:openssl-devel-1.0.2k-12.el7.x86_64 ...... ...... libselinux-utils.x86_64 0:2.5-12.el7 libsepol.x86_64 0:2.5-8.1.el7 libss.x86_64 0:1.42.9-12.el7_5 openssl.x86_64 1:1.0.2k-12.el7 openssl-libs.x86_64 1:1.0.2k-12.el7 Complete! //下载并安装apr-1.4+和apr-util-1.4+ [root@localhost ~]# cd /usr/src/ [root@localhost src]# wget http://mirror.bit.edu.cn/apache/apr/apr-1.6.5.tar.gz [root@localhost src]# wget http://mirror.bit.edu.cn/apache/apr/apr-util-1.6.1.tar.gz [root@localhost src]# ls apr-1.6.5.tar.gz apr-util-1.6.1.tar.gz debug kernels [root@localhost src]# tar xf apr-1.6.5.tar.gz [root@localhost src]# tar xf apr-util-1.6.1.tar.gz [root@localhost src]# ls apr-1.6.5 apr-1.6.5.tar.gz apr-util-1.6.1 apr-util-1.6.1.tar.gz debug kernels [root@localhost src]# cd apr-1.6.5 [root@localhost apr-1.6.5]# vim configure cfgfile="${ofile}T" trap "$RM \"$cfgfile\"; exit 1" 1 2 15 # $RM "$cfgfile" //将此行加上注释,或者删除此行 [root@localhost apr-1.6.5]# ./configure --prefix=/usr/local/apr 配置过程略... [root@localhost apr-1.6.5]# make && make install 编译安装过程略... [root@localhost apr-1.6.5]# cd /usr/src/apr-util-1.6.1 [root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr 配置过程略... [root@localhost apr-util-1.6.1]# make && make install 编译安装过程略... //编译安装httpd [root@localhost ~]# wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.38.tar.gz [root@localhost ~]# ls httpd-2.4.38.tar.gz [root@localhost ~]# tar xf httpd-2.4.38.tar.gz [root@localhost ~]# cd httpd-2.4.38 [root@localhost httpd-2.4.38]# ./configure --prefix=/usr/local/apache \ --sysconfdir=/etc/httpd24 \ --enable-so \ --enable-ssl \ --enable-cgi \ --enable-rewrite \ --with-zlib \ --with-pcre \ --with-apr=/usr/local/apr \ --with-apr-util=/usr/local/apr-util/ \ --enable-modules=most \ --enable-mpms-shared=all \ --with-mpm=prefork [root@localhost httpd-2.4.38]# make && make install 编译安装过程略...
5. httpd常用配置
切换使用MPM(编辑/etc/httpd/conf.modules.d/00-mpm.conf文件):
//LoadModule mpm_NAME_module modules/mod_mpm_NAME.so //NAME有三种,分别是: prefork event worker //用哪个工作模型就取消哪个的注释 [root@localhost conf.modules.d]# pwd /etc/httpd/conf.modules.d [root@localhost conf.modules.d]# ls 00-base.conf 00-lua.conf 00-proxy.conf 01-cgi.conf 00-dav.conf 00-mpm.conf 00-systemd.conf [root@localhost conf.modules.d]# vim 00-mpm.conf # Select the MPM module which should be used by uncommenting exactly # one of the following LoadModule lines: # prefork MPM: Implements a non-threaded, pre-forking web server # See: http://httpd.apache.org/docs/2.4/mod/prefork.html LoadModule mpm_prefork_module modules/mod_mpm_prefork.so //prefork # worker MPM: Multi-Processing Module implementing a hybrid # multi-threaded multi-process web server # See: http://httpd.apache.org/docs/2.4/mod/worker.html # #LoadModule mpm_worker_module modules/mod_mpm_worker.so //worker # event MPM: A variant of the worker MPM with the goal of consuming # threads only for connections with active processing # See: http://httpd.apache.org/docs/2.4/mod/event.html # #LoadModule mpm_event_module modules/mod_mpm_event.so //event
访问控制法则:
法则 功能 Require all granted 允许所有主机访问 Require all deny 拒绝所有主机访问 Require ip IPADDR 授权指定来源地址的主机访问 Require not ip IPADDR 拒绝指定来源地址的主机访问 Require host HOSTNAME 授权指定来源主机名的主机访问 Require not host HOSTNAME 拒绝指定来源主机名的主机访问 IPADDR的类型 HOSTNAME的类型 IP:192.168.1.1 Network/mask:192.168.1.0/255.255.255.0 Network/Length:192.168.1.0/24 Net:192.168 FQDN:特定主机的全名 DOMAIN:指定域内的所有主机 注意:httpd-2.4版本默认是拒绝所有主机访问的,所以安装以后必须做显示授权访问
示例:
Require not ip 192.168.1.20 Require all granted 6.虚拟主机配置
Apache 的虚拟主机功能是服务器基于用户请求的不同ip地址,主机域名或端口号,实现提供多个网站同时为外部提供访问服务的技术,用户请求资源不同,最终获取到的网页的内容也各不相同。
虚拟主机有三类:
- 相同IP不同端口
- 不同IP相同端口
- 相同IP相同端口不同域名
//设置主机名 [root@localhost ~]# vim /etc/httpd/conf/httpd.conf # ServerName gives the name and port that the server uses to identify itself. # This can often be determined automatically, but we recommend you specify # it explicitly to prevent problems during startup. # # If your host doesn't have a registered DNS name, enter its IP address here. # ServerName www.example.com:80 //取消这一行的注释(95行) //创建网页目录,并修改属主和属组 [root@localhost conf.d]# mkdir -p /data/{www.a.com,www.b.com} [root@localhost conf.d]# ls /data/ www.a.com www.b.com [root@localhost conf.d]# chown -R apache.apache /data/www.a.com/ [root@localhost conf.d]# chown -R apache.apache /data/www.b.com/ [root@localhost conf.d]# ll /data/ total 0 drwxr-xr-x. 2 apache apache 6 May 11 10:19 www.a.com drwxr-xr-x. 2 apache apache 6 May 11 10:19 www.b.com //创建网页 [root@localhost conf.d]# echo 'here is www.a.com' > /data/www.a.com/index.html [root@localhost conf.d]# echo 'here is www.b.com' > /data/www.b.com/index.html //创建日志存放位置 [root@localhost httpd]# pwd /var/log/httpd [root@localhost httpd]# mkdir www.a.com www.b.com [root@localhost httpd]# ls www.a.com www.b.com [root@localhost httpd]# chown -R apache.apache /var/log/httpd/
6.1 相同IP不同端口
服务器IP为192.168.32.125
[root@localhost ~]# cd /etc/httpd/conf.d/ //找到httpd-vhosts.conf这个文件,按它的示例配置 [root@localhost conf.d]# find / -name *hosts.conf /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf [root@localhost conf.d]# cp /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf . [root@localhost conf.d]# autoindex.conf httpd-vhosts.conf README userdir.conf welcome.conf [root@localhost conf.d]# vim httpd-vhosts.conf
ServerAdmin webmaster@dummy-host.example.com DocumentRoot "@@ServerRoot@@/docs/dummy-host.example.com" ServerName dummy-host.example.com ServerAlias www.dummy-host.example.com ErrorLog "/var/log/httpd/dummy-host.example.com-error_log" CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "@@ServerRoot@@/docs/dummy-host2.example.com" ServerName dummy-host2.example.com ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log" CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common //使用同一个IP,一个用80端口一个用81端口 [root@localhost httpd]# vim /etc/httpd/conf.d/httpd-vhosts.confDocumentRoot "/data/www.a.com" ServerName www.a.com ErrorLog "/var/log/httpd/www.a.com/error_log" CustomLog "/var/log/httpd/www.a.com/access_log" common Listen 81Require all granted DocumentRoot "/data/www.b.com" ServerName www.b.com ErrorLog "/var/log/httpd/www.b.com/error_log" CustomLog "/var/log/httpd/www.b.com/access_log" common //检查语法,重启服务 [root@localhost httpd]# apachectl -t Syntax OK [root@localhost httpd]# systemctl restart httpd [root@localhost httpd]# ss -tanl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 [::1]:25 [::]:* LISTEN 0 128 [::]:80 [::]:* LISTEN 0 128 [::]:81 [::]:* LISTEN 0 128 [::]:22 [::]:*Require all granted 另一台主机验证
//直接wget下载下来查看 [root@client ~]# wget 192.168.32.125 [root@client ~]# ls anaconda-ks.cfg index.html [root@client ~]# cat index.html here is www.a.com //另一个端口访问 [root@client ~]# rm -rf index.* [root@client ~]# ls anaconda-ks.cfg [root@client ~]# wget 192.168.32.125:81 [root@client ~]# ls anaconda-ks.cfg index.html [root@client ~]# cat index.html here is www.b.com
6.2 不同IP相同端口
IP:192.168.32.125、192.168.32.126
//添加IP [root@localhost ~]# ip addr add 192.168.32.126/24 dev eth0 [root@localhost ~]# ip a 1: lo:
mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:39:18:a9 brd ff:ff:ff:ff:ff:ff inet 192.168.32.125/24 brd 192.168.32.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet 192.168.32.126/24 scope global secondary eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe39:18a9/64 scope link valid_lft forever preferred_lft forever [root@localhost ~]# vim /etc/httpd/conf.d/httpd-vhosts.conf
DocumentRoot "/data/www.a.com" ServerName www.a.com ErrorLog "/var/log/httpd/www.a.com/error_log" CustomLog "/var/log/httpd/www.a.com/access_log" common Require all granted DocumentRoot "/data/www.b.com" ServerName www.b.com ErrorLog "/var/log/httpd/www.b.com/error_log" CustomLog "/var/log/httpd/www.b.com/access_log" common //检测语法,重启服务 [root@localhost ~]# apachectl -t Syntax OK [root@localhost ~]# systemctl restart httpd [root@localhost ~]# ss -tanl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 [::1]:25 [::]:* LISTEN 0 128 [::]:80 [::]:* LISTEN 0 128 [::]:22 [::]:*Require all granted 在另一台主机上验证
[root@client ~]# wget -O aaa 192.168.32.125 [root@client ~]# wget -O bbb 192.168.32.126 [root@client ~]# cat aaa here is www.a.com [root@client ~]# cat bbb here is www.b.com
6.3 相同IP,相同端口,不同域名
服务器IP为192.168.32.125,端口都为80,
[root@localhost ~]# vim /etc/httpd/conf.d/httpd-vhosts.conf
DocumentRoot "/data/www.a.com" ServerName www.a.com ErrorLog "/var/log/httpd/www.a.com/error_log" CustomLog "/var/log/httpd/www.a.com/access_log" common Require all granted DocumentRoot "/data/www.b.com" ServerName www.b.com ErrorLog "/var/log/httpd/www.b.com/error_log" CustomLog "/var/log/httpd/www.b.com/access_log" common [root@localhost ~]# apachectl -t Syntax OK [root@localhost ~]# systemctl restart httpdRequire all granted 生产环境中,域名解析后就可以通过域名访问了
//实验环境中,想要域名访问,需要做一下映射 //在客户端上验证 1.修改hosts文件 在/etc/hosts中添加如下两行 192.168.32.125 www.a.com 192.168.32.125 www.b.com 2.访问 [root@client ~]# ping www.a.com PING www.a.com (192.168.32.125) 56(84) bytes of data. 64 bytes from www.a.com (192.168.32.125): icmp_seq=1 ttl=64 time=0.329 ms 64 bytes from www.a.com (192.168.32.125): icmp_seq=2 ttl=64 time=0.235 ms 64 bytes from www.a.com (192.168.32.125): icmp_seq=3 ttl=64 time=0.227 ms 64 bytes from www.a.com (192.168.32.125): icmp_seq=4 ttl=64 time=0.269 ms 64 bytes from www.a.com (192.168.32.125): icmp_seq=5 ttl=64 time=0.310 ms ^C --- www.a.com ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4003ms rtt min/avg/max/mdev = 0.227/0.274/0.329/0.040 ms [root@client ~]# wget -O aaa www.a.com [root@client ~]# cat aaa here is www.a.com //在windows中访问 C:\Windows\System32\drivers\etc\hosts中添加 192.168.32.125 www.a.com 192.168.32.125 www.b.com
6.4 ssl
配置https步骤:
生成证书
配置httpd.conf,取消以下内容的注释LoadModule ssl_module modules/mod_ssl.so
Include /etc/httpd24/extra/httpd-vhosts.conf
Include /etc/httpd24/extra/httpd-ssl.conf在httpd-vhosts.conf中配置虚拟主机
在httpd-ssl.conf中配置证书的位置
检查配置文件是否有语法错误
启动或重启服务CA证书脚本
#!/bin/bash if [ $UID -ne 0 ];then echo "请使用管理员执行" exit fi read -p "输入省份名称:" province read -p "输入城市名:" city read -p "输入域名" domain read -p "输入邮箱:" mail yum -y -q install expect cd /etc/pki/CA if [ ! -f /etc/pki/CA/cakey.pem ];then (umask 077;openssl genrsa -out private/cakey.pem 2048) /usr/bin/expect <
serial #c) 客户端(例如httpd服务器)生成密钥 cd /tmp if [ ! -f /tmp/${domain}.key ];then (umask 077;openssl genrsa -out ${domain}.key 2048) fi /usr/bin/expect <