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 timeout
8.基于FQDN的虚拟主机不再需要NameVirtualHost指令
9.支持用户自定义变量
10.支持新的指令(AllowOverrideList)
11.降低对内存的消耗

日志级别(log level):从低到高级别越低越详细

  1. debug:程序和系统的调试信息。非常详细连命令都会记录
  2. info: 一般消息
  3. notice: 不影响正常功能,需要注意的消息
  4. warning/warn:可能影响系统功能,需要提醒用户的重要事件
  5. err/error: 错误信息
  6. crit: 紧急比较严重的事件
  7. alert: 必须马上处理的
  8. emerg/panic:会导致系统不可用的
  9. *: 表示所有日志级别
  10. 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 <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 //进度条显示当前的传送状态 //通过curl下载文件
[root@localhost ~]# ls
anaconda-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 Speed
100 42062 100 42062 0 0 128k 0 --:--:-- --:--:-- --:--:-- 129k
[root@localhost ~]# ls
anaconda-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 -l
Compiled in modules:
core.c
mod_so.c
http_core.c
event.c [root@cwh ~]# httpd -M
AH00558: 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 message
Loaded 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 -v
Server version: Apache/2.4.38 (Unix)
Server built: Apr 18 2019 11:28:19 [root@cwh ~]# httpd -V
Server version: Apache/2.4.38 (Unix)
Server built: Apr 18 2019 11:28:19
Server's Module Magic Number: 20120211:83
Server loaded: APR 1.6.5, APR-UTIL 1.6.1
Compiled using: APR 1.6.5, APR-UTIL 1.6.1
Architecture: 64-bit
Server 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 -t
AH00558: 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 message
Syntax OK
//此处会提示一个关于'ServerName'的信息,此时进入到主配置文件中进行修改
[root@cwh ~]# vim /usr/local/apache/conf/httpd.conf
讲ServerName www.example.com:801前注释取消掉
[root@cwh ~]# httpd -t
Syntax 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]# ls
apr-1.6.5 apr-util-1.6.1 auto CHANGES.ru configure html httpd-2.4.38.tar.gz Makefile objs src
apr-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 directory
config.status: executing default commands
// 编辑 configure
[root@localhost apr-1.6.5]# vi configure
cfgfile=${ofile}T
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
$RM "$cfgfile"
删除$RM "$cfgfile" //再次编译
[root@localhost apr-1.6.5]# ./configure --prefix=/usr/local/apr
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
Configuring APR library
Platform: x86_64-pc-linux-gnu
checking for working mkdir -p... yes
APR Version: 1.6.5
.
.
.
config.status: creating apr.pc
config.status: creating test/Makefile
config.status: creating test/internal/Makefile
config.status: creating include/arch/unix/apr_private.h
config.status: executing libtool commands
config.status: executing default commands
config.status: include/apr.h is unchanged
config.status: include/arch/unix/apr_private.h is unchanged //使用make命令安装 [root@localhost apr-1.6.5]# make && make install
make[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/apr
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking for working mkdir -p... yes
APR-util Version: 1.6.1
checking for chosen layout... apr-util
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
.
.
.
config.status: creating apu-1-config
config.status: creating include/private/apu_select_dbm.h
config.status: creating include/apr_ldap.h
config.status: creating include/apu.h
config.status: creating include/apu_want.h
config.status: creating test/Makefile
config.status: creating include/private/apu_config.h
config.status: executing default commands // 使用make && make install安装apr-uti [root@localhost apr-util-1.6.1]# make && make install
make[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-util cServer 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 ~]# ls
httpd-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 install
make[2]: Leaving directory `/root/httpd-2.4.38/support' Installing configuration files
mkdir /opt/httpd/conf
mkdir /opt/httpd/conf/extra
mkdir /opt/httpd/conf/original
mkdir /opt/httpd/conf/original/extra
Installing HTML documents
mkdir /opt/httpd/htdocs
Installing error documents
mkdir /opt/httpd/error
.
.
.
mkdir /opt/httpd/build
Installing man pages and online manual
mkdir /opt/httpd/man
mkdir /opt/httpd/man/man1
mkdir /opt/httpd/man/man8
mkdir /opt/httpd/manual

4.1 安装完成后运行httpd

[root@localhost ~]# cat /usr/local/apache/htdocs/index.html
<html><body><h1>It is cwh's Html</h1></body></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.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版本默认是拒绝所有主机访问的,所以安装以后必须做显示授权访问

示例:

<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.conf
<Directory "/usr/local/apache/htdocs/test">
Require all denied
</Directory> //加入你要设置的目录
//配置完成后检查配置文件是否有语法错误并重启httpd服务
[root@server htdocs]# apachectl -t
[root@server htdocs]# apachectl restart
//在在客户端山进行验证
[root@client ~]# curl http://192.168.112.146/test/index.html
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /test/index.html
on this server.<br />
</p>
</body></html> //可以看出访问被拒绝

2.对不同IP地址进行配置

//首先配置httpd.conf配置文件
<Directory "/usr/local/apache/htdocs">
<RequireAll>
Require not ip 192.168.112.149
Require all granted
</RequireAll>
</Directory>
//配置完成后检查配置文件是否有语法错误并重启httpd服务
[root@server htdocs]# apachectl -t
Syntax 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
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /
on this server.<br />
</p>
</body></html> //可以看出访问被拒绝

3.相同IP不同端口

//2.2需要在<VirtualHost*:80>前加NamevirtualHost
//第一步首先找到/etc/httpd24/extra/httpd-vhosts.conf这个虚拟主机的配置文件在哪里使用find命令
[root@server htdocs]# find / -name *vhosts.conf
/etc/httpd24/extra/httpd-vhosts.conf
//第二步在虚拟机主机的配置文件中写虚拟主机的配置
<VirtualHost *:80>
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
</VirtualHost> <VirtualHost *:81>
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
</VirtualHost>
//第三步在著配置文件/etc/httpd24/httpd.conf下开启虚拟主机的配置文件
# Virtual hosts
Include /etc/httpd24/extra/httpd-vhosts.conf //讲这一行的注释井号去掉就可以开启
//第四步使用apache -t检测配置文件是否出错
[root@server htdocs]# apachectl -t
AH00112: Warning: DocumentRoot [/usr/local/apache/htdocs/runtime] does not exist
AH00112: Warning: DocumentRoot [/usr/local/apache/htdocs/cwh] does not exist
Syntax 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:80
runtime //80端口对应的是runtime
[root@client ~]# curl http://192.168.112.146:81
hello cwh //81端口对应的是cwh

4.不同IP相同端口

//第一步在虚拟机主机的配置文件中写虚拟主机的配置
//注意这里配置的不同ip是服务端本身的,是客户端登陆服务端不同的IP从而进入不同的网页
[root@server htdocs]# vim /etc/httpd24/extra/httpd-vhosts.conf
<VirtualHost 192.168.112.3:80>
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
</VirtualHost> <VirtualHost 192.168.112.146:80>
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
</VirtualHost>
//重启httpd服务
[root@server htdocs]# apachectl restart
//在服务端查看效果
[root@client ~]# curl http://192.168.112.3
runtime
[root@client ~]# curl http://192.168.112.146
hello cwh

5.相同IP相同端口不同域名

//第一步在服务端/etc/httpd24/extra/httpd-vhosts.conf修改配置文件
<VirtualHost *:80>
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
</VirtualHost> <VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/cwh"
ServerName cwh.example.com
//第二步修改之后重启apache服务
[root@localhost ~]# apachectl start
//第三步此时客户端无法解析dns所以要爱客户端修改/etc/hosts文件添加dns解析域名
[root@localhost ~]# vim /etc/hosts
192.168.112.146 runtime.example.com
192.168.112.146 cwh.example.com //添加这两条
//第四步是用客户端连接验证
[root@localhost ~]# curl http://runtime.example.com
runtime
[root@localhost ~]# curl http://cwh.example.com
hello 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 -pubout
writing RSA key
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsC5t1jD9bDut4vXlB29x
9/Z3njheEYfdMeWb6NSgJBPxEpB1qZuCX9EchoJc6ZAfkNeQMZIdJBpmMCOUsWCc
6TrzPps49IXqZhn2BH69z+doUvan6gNwBXBib0/rI9O+t6kFoTAfgwckqxYT6XRK
ebIAOjBF4Bb+FGOxohg9JsAhTqV/Nd0sblW3llw17i7VuXyrHa/DaciruwKxwSUS
my3j2+M9xY5Gd3j6aHSsu+Rd5lD5sHxyW89oCmUkXD7m2j4K5OuU0w1K2moDAfiV
CIBBwZtgovuc1oTm13lfcO82c+zC6esH/3k5ZzJDkRTJ0mXqUON5FyW/vhIX7OyV
IwIDAQAB
-----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 365
You are about to be asked to enter information that will be incorporated
into 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 blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HuBei
Locality Name (eg, city) [Default City]:WuHan
Organization Name (eg, company) [Default Company Ltd]:runtime.example.com
Organizational Unit Name (eg, section) []:runtime.example.com
Common Name (eg, your name or your server's hostname) []:runtime.example.com
Email Address []:1@2.com openssl 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.csr
You are about to be asked to enter information that will be incorporated
into 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 blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HuBei
Locality Name (eg, city) [Default City]:WuHan
Organization Name (eg, company) [Default Company Ltd]:runtime.example.com
Organizational Unit Name (eg, section) []:runtime.example.com
Common Name (eg, your name or your server's hostname) []:runtime.example.com
Email Address []:1@2.com Please enter the following 'extra' attributes
to be sent with your certificate request
A 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 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate 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:2C Certificate is to be certified until Apr 20 04:49:16 2020 GMT (365 days)
Sign the certificate? [y/n]:y 1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data 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]# ls
httpd.crt httpd.csr httpd.key //第三步编辑/etc/httpd24/http.conf配置文件
LoadModule ssl_module modules/mod_ssl.so
# Virtual hosts
Include /etc/httpd24/extra/httpd-vhosts.conf
# Secure (SSL/TLS) connections
Include /etc/httpd24/extra/httpd-ssl.conf
将这三行前的注释取消掉
//注意:如果包下面这个错误
[root@localhost ~]# apachectl -t
AH00526: 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 host
DocumentRoot "/usr/local/apache/htdocs/runtime" //网站所在目录
ServerName runtime.example.com:443 //改成服务器域名
ServerAdmin you@example.com
ErrorLog "/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" //第五步重启服务,用浏览器验证

验证效果:



Linux服务-http的更多相关文章

  1. 第11章 Linux服务管理

    1. 服务分类 (1)Linux的服务 ①Linux中绝大多数的服务都是独立的,直接运行于内存中.当用户访问时,该服务直接响应用户,其好处是服务访问响应速度快.但不利之处是系统中服务越多,消耗的资源越 ...

  2. 在 CentOS7 上将自定义的 jar 包注册为 linux 服务 service

    在 CentOS7 上将自定义的 jar 包注册为 linux 服务 service 1.在 /etc/rc.d/init.d/ 目录下创建一个名字和服务名完全相同的 shell 脚本文件 joyup ...

  3. linux笔记:linux服务管理

    linux服务的分类: 启动和自启动: 查询已经安装的服务: RPM包的默认安装路径: 独立服务的启动: 独立服务的自启动: 基于xinetd的服务的管理: xinetd服务的自启动: 源码包安装服务 ...

  4. Linux学习笔记(19) Linux服务管理

    1. 服务的分类 Linux服务可分为RPM包默认安装的服务和源码包安装的服务.前者可细分为独立的服务(直接作用于内存中)和基于xinetd服务.xinetd本身是独立的服务,其唯一的功能是管理其他服 ...

  5. [Linux]服务管理:rpm包, 源码包

    --------------------------------------------------------------------------------------------------- ...

  6. linux服务器之LVS、Nginx和HAProxy负载均衡器对比

    linux服务器之LVS.Nginx和HAProxy负载均衡器对比. LVS特点:  1.抗负载能力强,使用IP负载均衡技术,只做分发,所以LVS本身并没有多少流量产生:  2.稳定性.可靠性好,自身 ...

  7. 《如何将windows上的软件包或文件上传到linux服务上》

    昨天晚上朋友让我帮他简单的搭建个环境,他公司让他做款软件测试温度的,他自己搞的是嵌入式,在公司担任的是软件工程师,应届毕业生.也可能他们搞嵌入式的对这个linux系统不太熟,不会把windows上的软 ...

  8. linux服务端的网络编程

    常见的Linux服务端的开发模型有多进程.多线程和IO复用,即select.poll和epoll三种方式,其中现在广泛使用的IO模型主要epoll,关于该模型的性能相较于select和poll要好不少 ...

  9. Linux服务的管理

    1.Linux服务的介绍 系统服务 --某些服务的服务的对象是Linux系统本身,或者Linux系统系统用户,这类服务我们称为系统服务(System Service) 网络服务 --提供给网络中的其他 ...

  10. linux程序自启动和新建linux服务的方法

    1 linux创建自启动程序    自启动的两种方法,都经过自己测试.1.1 自启动程序方法1:    在etc/rc.local在里面加入/home/robin/code/autoruntest & ...

随机推荐

  1. 破解 jeb 2.3.7 demo

    前言 使用的技术和上文的一样. mips 版本的修改版 修改版: https://gitee.com/hac425/jeb-mips 正文 安卓版 jeb-2.3.7.201710262129-JEB ...

  2. Pwn with File结构体(三)

    前言 本文由 本人 首发于 先知安全技术社区: https://xianzhi.aliyun.com/forum/user/5274 前面介绍了几种 File 结构体的攻击方式,其中包括修改 vtab ...

  3. Teamviewer 手机端怎么拖动窗口,选中文字

    Teamviewer 手机端怎么拖动窗口,选中文字 Teamviewer 手机端拖动窗口,选中文字和触摸板的使用方式是一样的 点两下不松开就可以拖动 点两下不松开也可以选中文字 Teamviewer ...

  4. 使用 Azure CLI 管理 Azure 虚拟网络和 Linux 虚拟机

    Azure 虚拟机使用 Azure 网络进行内部和外部网络通信. 本教程将指导读者部署两个虚拟机,并为这些 VM 配置 Azure 网络. 本教程中的示例假设 VM 将要托管包含数据库后端的 Web ...

  5. Linux wget安装详解

    Wget是一个十分常用命令行下载工具,多数Linux发行版本都默认包含这个工具.如果没有安装可在 http://www.gnu.org/software/wget/wget.html下载最新版本,并使 ...

  6. C#实体类对象修改日志记录

    C#实体类对象修改日志记录 类型验证帮助类 public static class TypeExtensions { public static bool InheritsFrom(this Type ...

  7. Sql Server数据库备份脚本以及如何在阿里云云数据库RDS还原数据库(代码源自阿里云)

    今天研究阿里云服务数据库的迁移,备份和还原的时候,在阿里云web后台发现了一个很好用的sql脚本,就默默地偷了过来,它可以支持全量备份,差异备份和日志备份,代码解释也都很清楚,我也尝试着跑了一下,性能 ...

  8. .net通用类型转换方法

    由于数据类型多,要按照逐个类型写一个类型转换的方法的话一是代码量多,显得累赘. using System; using System.ComponentModel; using System.Glob ...

  9. 使用keychain永久存储数据

    使用keychain永久存储数据 https://github.com/soffes/sskeychain keychain当然还是使用开源的好:),keychain是干啥用的?这个,baidu一下你 ...

  10. 使用FastCoder写缓存单例

    使用FastCoder写缓存单例 FastCoder可以存储字典,数组,鄙人将FastCoder封装,CoreData可以缓存的东西,用这个都可以缓存,但是只适合缓存少量的数据(不适合存储几万条数据) ...