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):從低到高級別越低越詳細
- 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 <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"
//第五步重啟服務,用瀏覽器驗證
驗證效果:


