目录
Linux服务-http
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 | 1.事先创建进程按需维持适当的进程2.模块化设计,核心比较小,各种功能通过模块添加(包括PHP),支持运行时配置,支持单独编译模块3.支持多种方式的虚拟主机配置,如基于ip的虚拟主机,基于端口的虚拟主机,基于域名的虚拟主机等4.支持https协议(通过mod_ssl模块实现)5.支持用户认证6.支持基于IP或域名的ACL访问控制机制7.支持每目录的访问控制(用户访问默认主页时不需要提供用户名和密码但是用户访问某特定目录时需要提供用户名和密码)8.支持URL重写9.支持MPM(Multi Path Modules,多处理模块)。用于定义httpd的工作模型(单进程、单进程多线程、多进程、多进程单线程、多进程多线程) |
2.4 | 1.MPM支持运行DSO机制(Dynamic Share Object,模块的动态装/卸载机制),以模块形式按需加载2.支持eventMPM,eventMPM模块生产环境可用3.支持异步读写4.支持每个模块及每个目录分别使用各自的==日志级别==5.每个请求相关的专业配置,使用来配置6.增强版的表达式分析器7.支持毫秒级的keepalive timeout8.基于FQDN的虚拟主机不再需要NameVirtualHost指令9.支持用户自定义变量10.支持新的指令(AllowOverrideList)11.降低对内存的消耗 |
日志级别(log level):从低到高级别越低越详细
- debug:程序和系统的调试信息。非常详细连命令都会记录
- info: 一般消息
- notice: 不影响正常功能,需要注意的消息
- warning/warn:可能影响系统功能,需要提醒用户的重要事件
- err/error: 错误信息
- crit: 紧急比较严重的事件
- alert: 必须马上处理的
- emerg/panic:会导致系统不可用的
- *: 表示所有日志级别
- none:表示啥也没有
工作模型 | 工作方式 |
---|---|
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程序环境
YUM安装的情况下
文件/目录 | 对应的功能 |
---|---|
/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加密)认证
- 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 ~]# lsanaconda-ks.cfg cwhhttp.sh[root@localhost ~]# curl -o http.html http://www.itwangqing.net.cn/15333747858558.html % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed100 42062 100 42062 0 0 128k 0 --:--:-- --:--:-- --:--:-- 129k[root@localhost ~]# lsanaconda-ks.cfg cwhhttp.sh http.html
3.3.2 httpd命令
//语法:httpd [options]//常用的options: -l //查看静态编译的模块,列出核心中编译了哪些模块。 \ //它不会列出使用LoadModule指令动态加载的模块 -M //输出一个已经启用的模块列表,包括静态编译在服务 \ //器中的模块和作为DSO动态加载的模块 -v //显示httpd的版本,然后退出 -V //显示httpd和apr/apr-util的版本和编译参数,然后退出 -X //以调试模式运行httpd。仅启动一个工作进程,并且 \ //服务器不与控制台脱离 -t //检查配置文件是否有语法错误 [root@cwh ~]# httpd -lCompiled in modules: core.c mod_so.c http_core.c event.c[root@cwh ~]# httpd -MAH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::fa28:6d0:a965:7db7. Set the 'ServerName' directive globally to suppress this messageLoaded Modules: core_module (static) so_module (static) http_module (static) mpm_event_module (static) authn_file_module (shared) authn_core_module (shared) authz_host_module (shared) authz_groupfile_module (shared) authz_user_module (shared) authz_core_module (shared) access_compat_module (shared) auth_basic_module (shared) reqtimeout_module (shared) filter_module (shared) mime_module (shared) log_config_module (shared) env_module (shared) headers_module (shared) setenvif_module (shared) version_module (shared) unixd_module (shared) status_module (shared) autoindex_module (shared) dir_module (shared) alias_module (shared)[root@cwh ~]# httpd -vServer version: Apache/2.4.38 (Unix)Server built: Apr 18 2019 11:28:19[root@cwh ~]# httpd -VServer version: Apache/2.4.38 (Unix)Server built: Apr 18 2019 11:28:19Server's Module Magic Number: 20120211:83Server loaded: APR 1.6.5, APR-UTIL 1.6.1Compiled using: APR 1.6.5, APR-UTIL 1.6.1Architecture: 64-bitServer MPM: event threaded: yes (fixed thread count) 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="/usr/local/apache" -D SUEXEC_BIN="/usr/local/apache/bin/suexec" -D DEFAULT_PIDLOG="logs/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"[root@cwh ~]# httpd -tAH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::fa28:6d0:a965:7db7. Set the 'ServerName' directive globally to suppress this messageSyntax OK//此处会提示一个关于'ServerName'的信息,此时进入到主配置文件中进行修改[root@cwh ~]# vim /usr/local/apache/conf/httpd.conf 讲ServerName www.example.com:801前注释取消掉[root@cwh ~]# httpd -tSyntax OK//然后就不会出现错误信息了
4. 编译安装httpd-2.4
第一步:基础环境准备用YUM安装pcre-devel,openssl-devel,expat-devel同时下载apache得三个安装包apr,apr-util,httpd第二步:解压三个安装包[root@localhost local]# tar xf httpd-2.4.38.tar.gz [root@localhost local]# tar xf apr-1.6.5.tar.gz [root@localhost local]# tar xf apr-util-1.6.1.tar.gz [root@localhost local]# lsapr-1.6.5 apr-util-1.6.1 auto CHANGES.ru configure html httpd-2.4.38.tar.gz Makefile objs srcapr-1.6.5.tar.gz apr-util-1.6.1.tar.gz CHANGES conf contrib httpd-2.4.38 第三步:编译安装apr-1.6.5 [root@localhost apr-1.6.5]# ./configure --prefix=/usr/local/apr//此时报错原因是没有编辑apr下得configure文件rm: cannot remove 'libtoolT': No such file or directoryconfig.status: executing default commands// 编辑 configure[root@localhost apr-1.6.5]# vi configurecfgfile=${ofile}T trap "$RM \"$cfgfile\"; exit 1" 1 2 15 $RM "$cfgfile"删除$RM "$cfgfile"//再次编译[root@localhost apr-1.6.5]# ./configure --prefix=/usr/local/aprchecking build system type... x86_64-pc-linux-gnuchecking host system type... x86_64-pc-linux-gnuchecking target system type... x86_64-pc-linux-gnuConfiguring APR libraryPlatform: x86_64-pc-linux-gnuchecking for working mkdir -p... yesAPR Version: 1.6.5...config.status: creating apr.pcconfig.status: creating test/Makefileconfig.status: creating test/internal/Makefileconfig.status: creating include/arch/unix/apr_private.hconfig.status: executing libtool commandsconfig.status: executing default commandsconfig.status: include/apr.h is unchangedconfig.status: include/arch/unix/apr_private.h is unchanged//使用make命令安装[root@localhost apr-1.6.5]# make && make installmake[1]: 进入目录“/root/apr-1.6.5”/root/apr-1.6.5/build/mkdir.sh tools/bin/sh /root/apr-1.6.5/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I./include -I/root/apr-1.6.5/include/arch/unix -I./include/arch/unix -I/root/apr-1.6.5/include/arch/unix -I/root/apr-1.6.5/include -I/root/apr-1.6.5/include/private -I/root/apr-1.6.5/include/private -o tools/gen_test_char.lo -c tools/gen_test_char.c && touch tools/gen_test_char.lo/bin/sh /root/apr-1.6.5/libtool --silent --mode=link gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I./include -I/root/apr-1.6.5/include/arch/unix -I./include/arch/unix -I/root/apr-1.6.5/include/arch/unix -I/root/apr-1.6.5/include -I/root/apr-1.6.5/include/private -I/root/apr-1.6.5/include/private -no-install -o tools/gen_test_char tools/gen_test_char.lo -lrt -lcrypt -lpthread -ldl/root/apr-1.6.5/build/mkdir.sh include/private...done/usr/bin/install -c -m 644 build/apr_rules.out /usr/local/apr/build-1/apr_rules.mk/usr/bin/install -c -m 644 /root/apr-1.6.5/build/apr_common.m4 /usr/local/apr/build-1/usr/bin/install -c -m 644 /root/apr-1.6.5/build/find_apr.m4 /usr/local/apr/build-1/usr/bin/install -c -m 755 apr-config.out /usr/local/apr/bin/apr-1-config[root@localhost apr-1.6.5]# ls /usr/local/第三部:编译安装apr-util//注意在编译安装apr-util时需要指定apr得路径 --with-apr=/usr/local/apr[root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/aprchecking build system type... x86_64-pc-linux-gnuchecking host system type... x86_64-pc-linux-gnuchecking target system type... x86_64-pc-linux-gnuchecking for a BSD-compatible install... /usr/bin/install -cchecking for working mkdir -p... yesAPR-util Version: 1.6.1checking for chosen layout... apr-utilchecking for gcc... gccchecking whether the C compiler works... yeschecking for C compiler default output file name... a.out...config.status: creating apu-1-configconfig.status: creating include/private/apu_select_dbm.hconfig.status: creating include/apr_ldap.hconfig.status: creating include/apu.hconfig.status: creating include/apu_want.hconfig.status: creating test/Makefileconfig.status: creating include/private/apu_config.hconfig.status: executing default commands// 使用make && make install安装apr-uti[root@localhost apr-util-1.6.1]# make && make installmake[1]: 进入目录“/root/apr-util-1.6.1”/bin/sh /usr/local/apr/build-1/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I/root/apr-util-1.6.1/include -I/root/apr-util-1.6.1/include/private -I/usr/local/apr/include/apr-1 -o buckets/apr_brigade.lo -c buckets/apr_brigade.c && touch buckets/apr_brigade.lo/bin/sh /usr/local/apr/build-1/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I/root/apr-util-1.6.1/include -I/root/apr-util-1.6.1/include/private -I/usr/local/apr/include/apr-1 -o buckets/apr_buckets.lo -c buckets/apr_buckets.c && touch buckets/apr_buckets.lo/bin/sh /usr/local/apr/build-1/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I/root/apr-util-1.6.1/include -I/root/apr-util-1.6.1/include/private -I/usr/local/apr/include/apr-1 -o buckets/apr_buckets_alloc.lo -c buckets/apr_buckets_alloc.c && touch buckets/apr_buckets_alloc.lo...----------------------------------------------------------------------/usr/bin/install -c -m 644 aprutil.exp /usr/local/apr-util/lib/usr/bin/install -c -m 755 apu-config.out /usr/local/apr-util/bin/apu-1-config// 检验apr-util安装成功没[root@localhost apr-util-1.6.1]# ls /usr/local/apr apr-util bin etc games include lib lib64 libexec nginx sbin share src第四部:编译安装http(apache)//首先编译./configure[root@localhost httpd]# ./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-utilcServer Version: 2.4.34 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@localhost ~]# lshttpd-2.4.37.tar.bz2[root@localhost ~]# tar xf httpd-2.4.37.tar.bz2[root@localhost ~]# cd httpd-2.4.37[root@localhost httpd-2.4.37]# ./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.37]# make && make install编译安装过程略...//编译完成后用make && make install命令编译[root@localhost httpd-2.4.38]# make && make installmake[2]: Leaving directory `/root/httpd-2.4.38/support'Installing configuration filesmkdir /opt/httpd/confmkdir /opt/httpd/conf/extramkdir /opt/httpd/conf/originalmkdir /opt/httpd/conf/original/extraInstalling HTML documentsmkdir /opt/httpd/htdocsInstalling error documentsmkdir /opt/httpd/error...mkdir /opt/httpd/buildInstalling man pages and online manualmkdir /opt/httpd/manmkdir /opt/httpd/man/man1mkdir /opt/httpd/man/man8mkdir /opt/httpd/manual
4.1 安装完成后运行httpd
[root@localhost ~]# cat /usr/local/apache/htdocs/index.htmlIt is cwh's Html
[root@localhost ~]# apachectl restart
登陆效果:
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 ~]# vim /etc/httpd24/httpd.conf LoadModule mpm_event_module modules/mod_mpm_event.so#LoadModule mpm_prefork_module modules/mod_mpm_prefork.so#LoadModule mpm_worker_module modules/mod_mpm_worker.so
访问控制法则:
|法则|功能| |---|---| |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.1Network/mask:192.168.1.0/255.255.255.0Network/Length:192.168.1.0/24Net:192.168 | FQDN:特定主机的全名DOMAIN:指定域内的所有主机 |
注意:httpd-2.4版本默认是拒绝所有主机访问的,所以安装以后必须做显示授权访问
示例:<Directory /var/www/html/www> <RequireAll> Require not ip 192.168.112.146 Require all granted </RequireAll> </Directory>
虚拟主机: 虚拟主机有三类: - 相同IP不同端口
- 不同IP相同端口
- 相同IP相同端口不同域名 实验测试 1.对不同目录(网页)进行设置
//首先在服务端对配置文件进行配置[root@server htdocs]# vim /etc/httpd24/httpd.confRequire all denied //加入你要设置的目录//配置完成后检查配置文件是否有语法错误并重启httpd服务[root@server htdocs]# apachectl -t[root@server htdocs]# apachectl restart//在在客户端山进行验证[root@client ~]# curl http://192.168.112.146/test/index.html403 Forbidden Forbidden
You don't have permission to access /test/index.htmlon this server.
//可以看出访问被拒绝
2.对不同IP地址进行配置
//首先配置httpd.conf配置文件//配置完成后检查配置文件是否有语法错误并重启httpd服务[root@server htdocs]# apachectl -tSyntax OK[root@server htdocs]# apachectl restart//在客户端验证效果[root@client ~]# ip a |grep inet |awk 'NR==3 {print $2}'192.168.112.149/24[root@client ~]# curl http://192.168.112.146 Require not ip 192.168.112.149Require all granted 403 Forbidden Forbidden
You don't have permission to access /on this server.
//可以看出访问被拒绝
3.相同IP不同端口
//2.2需要在前加NamevirtualHost//第一步首先找到/etc/httpd24/extra/httpd-vhosts.conf这个虚拟主机的配置文件在哪里使用find命令[root@server htdocs]# find / -name *vhosts.conf/etc/httpd24/extra/httpd-vhosts.conf//第二步在虚拟机主机的配置文件中写虚拟主机的配置 DocumentRoot "/usr/local/apache/htdocs/runtime" ServerName runtime.example.com ErrorLog "/usr/local/apache/logs/runtime.example.com-error_log" CustomLog "/usr/local/apache/logs/runtime.example.com-access_log" common DocumentRoot "/usr/local/apache/htdocs/cwh" ServerName cwh.example.com ErrorLog "/usr/local/apache/logs/cwh.example.com-error_log" CustomLog "/usr/local/apache/logs/cwh.example.com-access_log" common //第三步在著配置文件/etc/httpd24/httpd.conf下开启虚拟主机的配置文件# Virtual hostsInclude /etc/httpd24/extra/httpd-vhosts.conf //讲这一行的注释井号去掉就可以开启//第四步使用apache -t检测配置文件是否出错[root@server htdocs]# apachectl -tAH00112: Warning: DocumentRoot [/usr/local/apache/htdocs/runtime] does not existAH00112: Warning: DocumentRoot [/usr/local/apache/htdocs/cwh] does not existSyntax OK //报错原因是因为在配置文件中写入的目下没有runtime和cwh两个,目录的存在,解决办法是在配置文件设置的路径下创建这两个目录//第五步解决报错[root@server htdocs]# cd /usr/local/apache/htdocs/[[root@server htdocs]# echo 'runtime' > runtime/index.html[root@server htdocs]# echo 'hello cwh' > cwh/index.html//第六步重启httpd服务[root@server htdocs]# apachectl restart//第七步在客户端分别连接两个端口看效果[root@client ~]# curl http://192.168.112.146:80runtime //80端口对应的是runtime[root@client ~]# curl http://192.168.112.146:81hello cwh //81端口对应的是cwh
4.不同IP相同端口
//第一步在虚拟机主机的配置文件中写虚拟主机的配置//注意这里配置的不同ip是服务端本身的,是客户端登陆服务端不同的IP从而进入不同的网页[root@server htdocs]# vim /etc/httpd24/extra/httpd-vhosts.confDocumentRoot "/usr/local/apache/htdocs/runtime" ServerName runtime.example.com ErrorLog "/usr/local/apache/logs/runtime.example.com-error_log" CustomLog "/usr/local/apache/logs/runtime.example.com-access_log" common DocumentRoot "/usr/local/apache/htdocs/cwh" ServerName cwh.example.com ErrorLog "/usr/local/apache/logs/cwh.example.com-error_log" CustomLog "/usr/local/apache/logs/cwh.example.com-access_log" common //重启httpd服务[root@server htdocs]# apachectl restart//在服务端查看效果[root@client ~]# curl http://192.168.112.3runtime[root@client ~]# curl http://192.168.112.146hello cwh
5.相同IP相同端口不同域名
//第一步在服务端/etc/httpd24/extra/httpd-vhosts.conf修改配置文件DocumentRoot "/usr/local/apache/htdocs/runtime" ServerName runtime.example.com ErrorLog "/usr/local/apache/logs/runtime.example.com-error_log" CustomLog "/usr/local/apache/logs/runtime.example.com-access_log" common DocumentRoot "/usr/local/apache/htdocs/cwh" ServerName cwh.example.com//第二步修改之后重启apache服务[root@localhost ~]# apachectl start//第三步此时客户端无法解析dns所以要爱客户端修改/etc/hosts文件添加dns解析域名[root@localhost ~]# vim /etc/hosts192.168.112.146 runtime.example.com192.168.112.146 cwh.example.com //添加这两条//第四步是用客户端连接验证[root@localhost ~]# curl http://runtime.example.comruntime[root@localhost ~]# curl http://cwh.example.comhello cwh
6.ssl:
启用模块:编辑/etc/httpd/conf.modules.d/00-base.conf文件,添加下面这行,如果已经有了但是注释了,则取消注释即可//第一步配置私有证书a) CA生成一对密钥//在客户端上配(不要再网页所在服务端配置)cd /etc/pki/CA (umask 077;openssl genrsa -out private/cakey.pem 2048) #生成密钥,括号必须要[root@localhost ~]# cd /etc/pki/CA/[root@localhost CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)Generating RSA private key, 2048 bit long modulus..............+++..............................................................................+++e is 65537 (0x10001)openssl rsa -in private/cakey.pem -pubout #提取公钥[root@localhost CA]# openssl rsa -in private/cakey.pem -puboutwriting RSA key-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsC5t1jD9bDut4vXlB29x9/Z3njheEYfdMeWb6NSgJBPxEpB1qZuCX9EchoJc6ZAfkNeQMZIdJBpmMCOUsWCc6TrzPps49IXqZhn2BH69z+doUvan6gNwBXBib0/rI9O+t6kFoTAfgwckqxYT6XRKebIAOjBF4Bb+FGOxohg9JsAhTqV/Nd0sblW3llw17i7VuXyrHa/DaciruwKxwSUSmy3j2+M9xY5Gd3j6aHSsu+Rd5lD5sHxyW89oCmUkXD7m2j4K5OuU0w1K2moDAfiVCIBBwZtgovuc1oTm13lfcO82c+zC6esH/3k5ZzJDkRTJ0mXqUON5FyW/vhIX7OyVIwIDAQAB-----END PUBLIC KEY----- b) CA生成自签署证书openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365 #生成自签署证书[root@localhost CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365You are about to be asked to enter information that will be incorporatedinto your certificate request.What you are about to enter is what is called a Distinguished Name or a DN.There are quite a few fields but you can leave some blankFor some fields there will be a default value,If you enter '.', the field will be left blank.-----Country Name (2 letter code) [XX]:CNState or Province Name (full name) []:HuBeiLocality Name (eg, city) [Default City]:WuHanOrganization Name (eg, company) [Default Company Ltd]:runtime.example.comOrganizational Unit Name (eg, section) []:runtime.example.comCommon Name (eg, your name or your server's hostname) []:runtime.example.comEmail Address []:1@2.comopenssl x509 -text -in cacert.pem #读出cacert.pem证书的内容c) httpd服务器生成密钥cd /etc/httpd24 && mkdir ssl && cd ssl(umask 077;openssl genrsa -out httpd.key 2048)[root@localhost ssl]# (umask 077;openssl genrsa -out httpd.key 2048)Generating RSA private key, 2048 bit long modulus...+++........................................................+++e is 65537 (0x10001)d) 客户端生成证书签署请求openssl req -new -key httpd.key -days 365 -out httpd.csr[root@localhost ssl]# openssl req -new -key httpd.key -days 365 -out httpd.csrYou are about to be asked to enter information that will be incorporatedinto your certificate request.What you are about to enter is what is called a Distinguished Name or a DN.There are quite a few fields but you can leave some blankFor some fields there will be a default value,If you enter '.', the field will be left blank.-----Country Name (2 letter code) [XX]:CNState or Province Name (full name) []:HuBeiLocality Name (eg, city) [Default City]:WuHanOrganization Name (eg, company) [Default Company Ltd]:runtime.example.com Organizational Unit Name (eg, section) []:runtime.example.comCommon Name (eg, your name or your server's hostname) []:runtime.example.comEmail Address []:1@2.comPlease enter the following 'extra' attributesto be sent with your certificate requestA challenge password []: //不填写An optional company name []: //不填写 e) 客户端把证书签署请求文件发送给CA [root@localhost ssl]# scp httpd.csr root@192.168.112.149:/root/ f) CA签署客户端提交上来的证书(在非网页服务器端) openssl ca -in /root/httpd.csr -out httpd.crt -days 365 [root@localhost ~]# openssl ca -in /root/httpd.csr -out httpd.crt -days 365Using configuration from /etc/pki/tls/openssl.cnfCheck that the request matches the signatureSignature okCertificate Details: Serial Number: 1 (0x1) Validity Not Before: Apr 21 04:49:16 2019 GMT Not After : Apr 20 04:49:16 2020 GMT Subject: countryName = CN stateOrProvinceName = HuBei organizationName = runtime.example.com organizationalUnitName = runtime.example.com commonName = runtime.example.com emailAddress = 1@2.com X509v3 extensions: X509v3 Basic Constraints: CA:FALSE Netscape Comment: OpenSSL Generated Certificate X509v3 Subject Key Identifier: 11:2A:E1:E6:8F:83:1F:B5:E8:5D:F2:D7:F5:B9:71:9E:9D:08:1D:4B X509v3 Authority Key Identifier: keyid:93:8F:C9:74:E5:23:7A:82:48:9B:23:FC:8B:90:EE:BA:8A:59:43:2CCertificate is to be certified until Apr 20 04:49:16 2020 GMT (365 days)Sign the certificate? [y/n]:y1 out of 1 certificate requests certified, commit? [y/n]yWrite out database with 1 new entriesData Base Updated g) CA把签署好的证书httpd.crt发给客户端 [root@localhost ~]# scp httpd.crt root@192.168.112.146:/root/ //第二步在网页服务端讲/root/下的httpd.crt移动到/etc/httpd/ssl去 [root@localhost ~]# mv httpd.crt /etc/httpd24/ssl/[root@localhost ~]# cd /etc/httpd24/ssl/[root@localhost ssl]# lshttpd.crt httpd.csr httpd.key//第三步编辑/etc/httpd24/http.conf配置文件LoadModule ssl_module modules/mod_ssl.so# Virtual hostsInclude /etc/httpd24/extra/httpd-vhosts.conf# Secure (SSL/TLS) connectionsInclude /etc/httpd24/extra/httpd-ssl.conf将这三行前的注释取消掉//注意:如果包下面这个错误[root@localhost ~]# apachectl -tAH00526: Syntax error on line 92 of /etc/httpd24/extra/httpd-ssl.conf:SSLSessionCache: 'shmcb' session cache not supported (known names: ). Maybe you need to load the appropriate socache module (mod_socache_shmcb?).则需要将著配置文件中的LoadModule socache_shmcb_module modules/mod_socache_shmcb.so这一条前得注释取消掉//第四步修改ssl配置文件 /etc/httpd24/extra/httpd-ssl.conf # General setup for the virtual hostDocumentRoot "/usr/local/apache/htdocs/runtime" //网站所在目录ServerName runtime.example.com:443 //改成服务器域名ServerAdmin you@example.comErrorLog "/usr/local/apache/logs/runtime.example.com-error_log"TransferLog "/usr/local/apache/logs/runtime.example.com-access_log" //修改下发的证书目录SSLCertificateFile "/etc/httpd24/ssl/httpd.crt"SSLCertificateKeyFile "/etc/httpd24/ssl/httpd.key"//第五步重启服务,用浏览器验证
验证效果: