httpd简介
httpd是Apache超文本传输协议(HTTP)服务器的主程序。被设计为一个独立运行的后台进程,它会建立一个处理请求的子进程或线程的池。
通常,httpd不应该被直接调用,而应该在类Unix系统中由apachectl调用,在Windows中作为服务运行。
httpd特性
CentOS6系列的版本默认提供的是httpd-2.2版本的rpm包
CentOS7系列的版本默认提供的是httpd-2.4版本
2.2特性(2.2已停止维护)
事先创建进程
按需维持适当的进程
模块化设计,核心比较小,各种功能通过模块添加(包括PHP),支持运行时配置,支持单独编译模块
支持多种方式的虚拟主机配置,如基于ip的虚拟主机,基于端口的虚拟主机,基于域名的虚拟主机等
支持https协议(通过mod_ssl模块实现)
支持用户认证
支持基于IP或域名的ACL访问控制机制
支持每目录的访问控制(用户访问默认主页时不需要提供用户名和密码,但是用户访问某特定目录时需要提供用户名和密码)
支持URL重写
支持MPM(Multi Path Modules,多处理模块)。用于定义httpd的工作模型(单进程、单进程多线程、多进程、多进程单线程、多进程多线程)
2.4在2.2的基础上增加了很多新特性
MPM支持运行DSO机制(Dynamic Share Object,模块的动态装/卸载机制),以模块形式按需加载
支持event MPM,eventMPM模块生产环境可用
支持异步读写
支持每个模块及每个目录分别使用各自的日志级别
每个请求相关的专业配置,使用来配置
增强版的表达式分析器
支持毫秒级的keepalive timeout
基于FQDN的虚拟主机不再需要NameVirtualHost指令
支持用户自定义变量
支持新的指令(AllowOverrideList)
降低对内存的消耗
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 | 辅助配置文件 |
httpd命令
//语法:httpd [options]
//常用的options:
-l | 查看静态编译的模块,列出核心中编译了哪些模块。它不会列出使用LoadModule指令动态加载的模块 |
---|---|
-M | 输出一个已经启用的模块列表,包括静态编译在服务器中的模块和作为DSO动态加载的模块 |
-v | 显示httpd的版本,然后退出 |
-V | 显示httpd和apr/apr-util的版本和编译参数,然后退出 |
-X | 以调试模式运行httpd。仅启动一个工作进程,并且服务器不与控制台脱离 |
-t | 检查配置文件是否有语法错误 |
web相关命令
curl命令
curl是基于URL语法在命令行方式下工作的文件传输工具
语法:curl 【options】< url >
常用的选项:
-A/--user-agent <string> //设置用户代理发送给服务器
-basic //使用Http基本认证
--tcp-nodelay //使用TCP_NODELAY选项
-e/--referer <URL> //来源网址
--cacert <file> //CA证书(SSL)
--compressed //要求返回时压缩的格式
-H/--header <line> //自定义请求首部信息传递给服务器
-I/--head //只显示响应报文首部信息
--limit-rate <rate> //设置传输速度
-u/--user <user[:password]> //设置服务器的用户和密码
-0/--http1 //使用http 1.0版本,默认使用1.1版本。这个选项是数字0而不是字母o
-o/--output //把输出写到文件中
-#/--progress-bar //进度条显示当前的传送状态
http编译安装
[root@souls local]# yum groups mark install "Development Tools" //安装开发环境
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register
[root@souls local]# useradd -r -M -s /sbin/nologin -g apache apache //创建apache的系统用户
[root@souls local]# yum -y install openssl-devel pcre-devel expat-devel libtool //安装相关工具包
......
Dependency Installed:
autoconf.noarch 0:2.69-11.el7 automake.noarch 0:1.13.4-3.el7
m4.x86_64 0:1.4.16-10.el7 perl-Data-Dumper.x86_64 0:2.145-3.el7
perl-Test-Harness.noarch 0:3.28-3.el7 perl-Thread-Queue.noarch 0:3.02-2.el7
Complete!
//下载3个安装包
[root@souls local]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.bz2 https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.bz2 https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.38.tar.bz2
[root@souls local]# tar xf apr-util-1.6.1.tar.bz2
[root@souls local]# tar xf apr-1.7.0.tar.bz2
[root@souls local]# tar xf httpd-2.4.38.tar.bz2
//安装apr
[root@souls local]# cd apr-1.7.0/
[root@souls apr-1.7.0]# vim configure
......
cfgfile=${ofile}T
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
# $RM "$cfgfile //将这行注释或删除
......
[root@souls apr-1.7.0]# ./configure --prefix=/usr/local/apr
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
...
config.status: creating include/arch/unix/apr_private.h
config.status: executing libtool commands
config.status: executing default commands
[root@souls apr-1.7.0]# make && make install
.....
/usr/bin/install -c -m 644 /usr/local/apr-1.7.0/build/${f} /usr/local/apr/build-1; \
done
/usr/bin/install -c -m 644 build/apr_rules.out /usr/local/apr/build-1/apr_rules.mk
/usr/bin/install -c -m 755 apr-config.out /usr/local/apr/bin/apr-1-config
//安装apr-util
[root@souls apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
......
config.status: creating include/private/apu_config.h
config.status: executing default commands
[root@souls apr-util-1.6.1]# make && make install
//安装httpd
[root@souls 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
......
config.status: executing default commands
configure: summary of build options:
Server Version: 2.4.38
Install prefix: /usr/local/apache
C compiler: gcc -std=gnu99
CFLAGS: -g -O2 -pthread
CPPFLAGS: -DLINUX -D_REENTRANT -D_GNU_SOURCE
LDFLAGS:
LIBS:
C preprocessor: gcc -E
[root@souls httpd-2.4.38]# make && make install
......mkdir /usr/local/apache/man/man8
mkdir /usr/local/apache/manual
make[1]: Leaving directory `/usr/local/httpd-2.4.38'
//创建http的控制脚本
[root@souls httpd-2.4.38]# echo 'export PATH=/usr/local/apache/bin:$PATH' >/etc/profile.d/httpd.sh
[root@souls httpd-2.4.38]# which httpd
/usr/local/apache/bin/httpd //指向正确
[root@souls httpd-2.4.38]# /usr/local/apache/bin/apachectl start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::20c:29ff:fe45:4e9d%ens33. Set the 'ServerName' directive globally to suppress this message
//修改配置文件解决报错
[root@souls httpd-2.4.38]# vim /etc/httpd24/httpd.conf
......
ServerName www.example.com:80 //根据报错内容,将这句的注释取消掉
......
[root@souls httpd-2.4.38]# ss -antl //80端口开启,http运行
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
//关闭防护墙和selinux后访问主页
httpd常用设置
切换使用MPM
工作模型 | 工作方式 |
---|---|
prefork | 多进程模型,预先生成进程,一个请求用一个进程响应。 一个主进程负责生成n个子进程,子进程也称为工作进程 每个子进程处理一个用户请求,即使没有用户请求,也会预先生成多个空闲进程,随时等待请求到达,最大不会超过1024个 |
worker | 基于线程工作,一个请求用一个线程响应(启动多个进程,每个进程生成多个线程) |
event | 基于事件的驱动,一个进程处理多个请求 |
查看配置文件/etc/httpd24/httpd.conf
注意前三个只能选一个不注释
访问控制法则:
法则 | 功能 |
---|---|
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版本默认是拒绝所有主机访问的,所以安装以后必须做显示授权访问
禁止特殊IP访问
[root@souls httpd-2.4.38]# vim /etc/httpd24/httpd.conf //加入下面这段文字
<VirtualHost 192.168.161.200:80>
DocumentRoot "/usr/local/apache/htdocs"
<Directory /usr/local/apache/htdocs>
<RequireAll>
Require all granted
Require not ip 192.168.161.1
</RequireAll>
</Directory>
</VirtualHost>
[root@souls httpd-2.4.38]# /usr/local/apache/bin/apachectl restart
//本地主机被禁止访问
虚拟主机
虚拟主机有三类:相同IP不同端口
不同IP相同端口
相同IP相同端口不同域名(一般都是这种)
<VirtualHost 使用的IP:使用的端口>
ServerAdmin webmaster@dummy-host.example.com //邮箱地址,如果发生故障邮件发给管理员(可以不要)
DocumentRoot "/usr/local/apache/docs/dummy-host.example.com" //网站文件存放路径
ServerName dummy-host.example.com //域名
ServerAlias www.dummy-host.example.com //域名的别名(可以不要)
ErrorLog "logs/dummy-host.example.com-error_log" //错误日志路径
CustomLog "logs/dummy-host.example.com-access_log" common //访问日志路径
</VirtualHost>