1. httpd简介2. httpd版本2.1 httpd的特性2.2 httpd-2.4新增的模块
3. httpd基础3.1 httpd自带的工具程序3.2 rpm包安装的httpd程序环境3.3 web相关的命令3.3.1 curl命令3.3.2 httpd命令4. 编译安装httpd-2.44.1准备环境4.2下载apr apr-util httpd4.3解压apr apr-util httpd4.4安装apr apr-util httpd4.5配置环境变量4.6关闭防火墙和seLinux服务4.7打开apache服务,并测试能否访问httpd常用配置
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模块生产环境可用 支持异步读写 支持每个模块及每个目录分别使用各自的日志级别 每个请求相关的专业配置,使用<If>来配置 增强版的表达式分析器 支持毫秒级的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 <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 //进度条显示当前的传送状态 [root@localhost ~]# ls anaconda-ks.cfg [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 312 100 312 0 0 1399 0 --:--:-- --:--:-- --:--:-- 1399 [root@localhost ~]# ls anaconda-ks.cfg 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 -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) auth_digest_module (shared) authn_anon_module (shared) authn_core_module (shared) authn_dbd_module (shared) authn_dbm_module (shared) ...... ...... [root@localhost ~]# httpd -v Server version: Apache/2.4.37 (centos) Server built: Apr 6 2022 14:54:37 [root@localhost ~]# httpd -V Server version: Apache/2.4.37 (centos) Server built: Apr 6 2022 14:54:37 Server's Module Magic Number: 20120211:83 Server loaded: APR 1.6.3, APR-UTIL 1.6.1 Compiled using: APR 1.6.3, 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="/etc/httpd" -D SUEXEC_BIN="/usr/sbin/suexec" -D DEFAULT_PIDLOG="run/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
4.1准备环境
[root@localhost ~]# dnf groups mark install "Development Tools" Failed to set locale, defaulting to C.UTF-8 Last metadata expiration check: 20:00:54 ago on Tue Jul 12 22:42:38 2022. Dependencies resolved. ================================================================================================================ Package Architecture Version Repository Size ================================================================================================================ Installing Groups: Development Tools Transaction Summary ================================================================================================================ Is this ok [y/N]: y Complete! [root@localhost ~]# useradd -rMs /sbin/nologin apache [root@localhost ~]# id apache uid=994(apache) gid=991(apache) groups=991(apache) [root@localhost ~]# dnf -y install wget [root@localhost ~]# dnf -y install openssl-devel pcre-devel expat-devel libtool libxml2-devel [root@localhost ~]# dnf -y install gcc gcc-c++ [root@localhost ~]# dnf -y install make
4.2下载apr apr-util httpd
[root@localhost ~]# wget http://mirrors.aliyun.com/apache/apr/apr-1.7.0.tar.gz --2022-07-13 21:25:33-- http://mirrors.aliyun.com/apache/apr/apr-1.7.0.tar.gz Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 223.76.170.217, 223.76.171.227, 111.48.71.116, ... Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|223.76.170.217|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 1093896 (1.0M) [application/octet-stream] Saving to: ‘apr-1.7.0.tar.gz’ apr-1.7.0.tar.gz 100%[===========================================================>] 1.04M 132KB/s in 7.9s 2022-07-13 21:25:42 (135 KB/s) - ‘apr-1.7.0.tar.gz’ saved [1093896/1093896] [root@localhost ~]# wget https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz --2022-07-13 21:28:27-- https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 111.48.71.115, 111.48.71.112, 111.48.71.113, ... Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|111.48.71.115|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 554301 (541K) [application/octet-stream] Saving to: ‘apr-util-1.6.1.tar.gz’ apr-util-1.6.1.tar.gz 100%[===========================================================>] 541.31K 128KB/s in 4.2s 2022-07-13 21:28:32 (129 KB/s) - ‘apr-util-1.6.1.tar.gz’ saved [554301/554301] [root@localhost ~]# wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.gz --2022-07-13 21:29:02-- https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.gz Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 111.48.71.115, 223.76.171.226, 223.76.170.217, ... Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|111.48.71.115|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 9743277 (9.3M) [application/octet-stream] Saving to: ‘httpd-2.4.54.tar.gz’ httpd-2.4.54.tar.gz 100%[===========================================================>] 9.29M 130KB/s in 80s 2022-07-13 21:30:22 (119 KB/s) - ‘httpd-2.4.54.tar.gz’ saved [9743277/9743277]
4.3解压apr apr-util httpd
[root@localhost ~]# tar -xf apr-util-1.6.1.tar.gz -C /usr/local/src/ [root@localhost ~]# tar -xf httpd-2.4.54.tar.gz -C /usr/local/src/ [root@localhost ~]# tar -xf apr-1.7.0.tar.gz -C /usr/local/src/ [root@localhost ~]# ls /usr/local/src/ apr-1.7.0 apr-util-1.6.1 httpd-2.4.54
4.4安装apr apr-util httpd
安装apr
[root@localhost ~]# cd /usr/local/src/apr-1.7.0/ [root@localhost apr-1.7.0]# vi configure [root@localhost apr-1.7.0]# cfgfile=${ofile}T trap "$RM \"$cfgfile\"; exit 1" 1 2 15 #$RM "$cfgfile" //注释这行,在$RM前加上'#' [root@localhost 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 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.7.0 ......省略 [root@localhost apr-1.7.0]# make && make install make[1]: Entering directory '/usr/local/src/apr-1.7.0' /bin/sh /usr/local/src/apr-1.7.0/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I./include -I/usr/local/src/apr-1.7.0/include/arch/unix -I./include/arch/unix -I/usr/local/src/apr-1.7.0/include/arch/unix -I/usr/local/src/apr-1.7.0/include -I/usr/local/src/apr-1.7.0/include/private -I/usr/local/src/apr-1.7.0/include/private -o encoding/apr_encode.lo -c encoding/apr_encode.c && touch encoding/apr_encode.lo /usr/local/src/apr-1.7.0/build/mkdir.sh tools /bin/sh /usr/local/src/apr-1.7.0/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I./include -I/usr/local/src/apr-1.7.0/include/arch/unix -I./include/arch/unix -I/usr/local/src/apr-1.7.0/include/arch/unix -I/usr/local/src/apr-1.7.0/include -I/usr/local/src/apr-1.7.0/include/private -I/usr/local/src/apr-1.7.0/include/private -o tools/gen_test_char.lo -c tools/gen_test_char.c && touch tools/gen_test_char.lo ......省略 [root@localhost apr-1.7.0]# ls /usr/local/apr/ bin build-1 include lib [root@localhost apr-1.7.0]#
安装apr-util
[root@localhost ~]# cd /usr/local/src/apr-util-1.6.1/ [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 ......省略 [root@localhost apr-util-1.6.1]# make && make install make[1]: Entering directory '/usr/local/src/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/usr/local/src/apr-util-1.6.1/include -I/usr/local/src/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/usr/local/src/apr-util-1.6.1/include -I/usr/local/src/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 ......省略 [root@localhost apr-util-1.6.1]# ls /usr/local/apr-util/ bin include lib [root@localhost apr-util-1.6.1]#
安装httpd
[root@localhost apr-util-1.6.1]# cd /usr/local/src/httpd-2.4.54/ [root@localhost httpd-2.4.54]# ./configure --prefix=/usr/local/apache --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-rpm=prefork checking for chosen layout... Apache checking for working mkdir -p... yes checking for grep that handles long lines and -e... /usr/bin/grep checking for egrep... /usr/bin/grep -E 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 configure: configure: Configuring Apache Portable Runtime library... configure: checking for APR... yes ......省略 [root@localhost httpd-2.4.54]# make && make install ......省略 mkdir /usr/local/apache/man mkdir /usr/local/apache/man/man1 mkdir /usr/local/apache/man/man8 mkdir /usr/local/apache/manual make[1]: Leaving directory '/usr/local/src/httpd-2.4.54' [root@localhost httpd-2.4.54]# ls /usr/local/apache/ bin build cgi-bin conf error htdocs icons include logs man manual modules [root@localhost httpd-2.4.54]#
4.5配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/apache.sh [root@localhost ~]# source /etc/profile.d/apache.sh [root@localhost ~]# which apachectl /usr/local/apache/bin/apachectl [root@localhost ~]# ll /usr/include/ | grep apache lrwxrwxrwx. 1 root root 26 Jul 13 21:50 apache -> /usr/local/apache/include/ [root@localhost ~]# vi /etc/man_db.conf # every automatically generated MANPATH includes these fields # #MANDATORY_MANPATH /usr/src/pvm3/man # MANDATORY_MANPATH /usr/man MANDATORY_MANPATH /usr/share/man MANDATORY_MANPATH /usr/local/share/man MANDATORY_MANPATH /usr/local/apache/man //添加此行
4.6关闭防火墙和seLinux服务
[root@localhost ~]# systemctl stop firewalld [root@localhost ~]# systemctl disable firewalld Removed /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. [root@localhost ~]# setenforce 0 [root@localhost ~]# vi /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled //更改此行
4.7打开apache服务,并测试能否访问
[root@localhost ~]# apachectl start
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
[root@localhost ~]# ss -anltup | grep httpd
tcp LISTEN 0 128 *:80 *:* users:(("httpd",pid=70759,fd=4),("httpd",pid=70757,fd=4),("httpd",pid=70755,fd=4),("httpd",pid=70753,fd=4))
[root@localhost ~]#
编辑
http常用配置
MPM(编辑/etc/httpd/conf.modules.d/00-mpm.conf文件):
//LoadModule mpm_NAME_module modules/mod_mpm_NAME.so //NAME有三种,分别是: prefork event worker
[root@localhost ~]# cd /etc/httpd/conf.modules.d/ [root@localhost conf.modules.d]# ls 00-base.conf 00-lua.conf 00-optional.conf 00-systemd.conf 10-h2.conf README 00-dav.conf 00-mpm.conf 00-proxy.conf 01-cgi.conf 10-proxy_h2.conf [root@localhost conf.modules.d]# vim 00-mpm.com -bash: vim: command not found [root@localhost conf.modules.d]# vi 00-mpm.com [root@localhost conf.modules.d]# cat 00-mpm.conf # Select the MPM module which should be used by uncommenting exactly # one of the following LoadModule lines. See the httpd.conf(5) man # page for more information on changing the MPM. # prefork MPM: Implements a non-threaded, pre-forking web server # See: http://httpd.apache.org/docs/2.4/mod/prefork.html # # NOTE: If enabling prefork, the httpd_graceful_shutdown SELinux # boolean should be enabled, to allow graceful stop/shutdown. # #LoadModule mpm_prefork_module modules/mod_mpm_prefork.so # 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 # 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 //CentOS8默认使用的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版本默认是拒绝所有主机访问的,所以安装以后必须做显示授权访问
[root@localhost ~]# vi /etc/httpd/conf/httpd.conf <Directory "/var/www/html/Sia"> <RequireALL> require not ip 192.168.48.1 Require all granted </requireALL> </Directory> [root@localhost ~]# httpd -t Syntax OK [root@129 ~]# systemctl restart httpd [root@localhost html]# curl http://192.168.48.150/Sia/a.html hello Sia
虚拟主机
虚拟主机有三类:
相同IP不同端口
不同IP相同端口
相同IP相同端口不同域名
1.相同IP不同端口
[root@localhost ~]# find / -name *vhosts.conf /usr/share/doc/httpd/httpd-vhosts.conf [root@localhost ~]# cd /etc/httpd/ [root@localhost httpd]# ls conf conf.d conf.modules.d logs modules run state #conf主配置文件 conf.d 辅助配置文件,一般编写的都在写在辅助 conf.modules.d模块 [root@localhost httpd]# cd conf.d [root@localhost conf.d]# ls README autoindex.conf userdir.conf welcome.conf [root@localhost conf.d]# cp /usr/share/doc/httpd/httpd-vhosts.conf . [root@localhost conf.d]# ls README autoindex.conf httpd-vhosts.conf userdir.conf welcome.conf [root@localhost conf.d]# vim httpd-vhosts.conf #里面有两个虚拟机配置 # match a ServerName or ServerAlias in any <VirtualHost> block. # <VirtualHost *:80> DocumentRoot "/var/www/html/feijidazhan" ServerName www.feijidazhan.com ErrorLog "/var/log/httpd/www.feijidazhan.com-error_log" CustomLog "/var/log/httpd/www.feijidazhan.com-access_log" common </VirtualHost> Lisren 81 <VirtualHost *:81> DocumentRoot "/var/www/html/zhuawawa" ServerName www.zhuawawa.com ErrorLog "/var/log/httpd/www.zhuawawa.com-error_log" CustomLog "/var/log/httpd/www.zhuawawa.com-access_log" common </VirtualHost> #更改完虚拟机配置之后记得创建自己所配置的文件“feijidazhan ,zhuawawa” [root@localhost conf.d]# cd /var/www/html/ [root@localhost html]# ls Sia index.html [root@localhost html]# mkdir feijidazhan zhuawawa [root@localhost html]# ls Sia feijidazhan index.html zhuawawa [root@localhost html]# cd feijidazhan [root@localhost feijidazhan]# ls feijiedazhan.zip #从主机中把所选的小游戏移动到虚拟机目录中 [root@localhost feijidazhan]# dnf -y install zip* ...... Installing : unzip-6.0-46.el8.x86_64 1/2 Installing : zip-3.0-23.el8.x86_64 2/2 Running scriptlet: zip-3.0-23.el8.x86_64 2/2 Verifying : unzip-6.0-46.el8.x86_64 1/2 Verifying : zip-3.0-23.el8.x86_64 2/2 Installed: unzip-6.0-46.el8.x86_64 zip-3.0-23.el8.x86_64 Complete! #飞机大战 [root@localhost feijidazhan]# unzip feijiedazhan.zip [root@localhost feijidazhan]# ls HTML5#U5168#U6c11#U98de#U673a#U5927#U6218#U5c0f#U6e38#U620f feijiedazhan.zip [root@localhost feijidazhan]# mv HTML5#U5168#U6c11#U98de#U673a#U5927#U6218#U5c0f#U6e38#U620f/ feijidazhan [root@localhost feijidazhan]# ls feijidazhan feijiedazhan.zip [root@localhost feijidazhan]# rm -rf feijidazhan.zip [root@localhost feijidazhan]# ls feijidazhan [root@localhost feijidazhan]# mv 'feijidazhan'/* . [root@localhost feijidazhan]# ls css feijidazhan img index.html js [root@localhost feijidazhan]# [root@localhost feijidazhan]# rm -rf feijidazhan/ [root@localhost feijidazhan]# ls css img index.html js [root@localhost feijidazhan]# #抓娃娃 [root@localhost html]# cd zhuawawa/ [root@localhost zhuawawa]# unzip zhuawawa.zip unzip: cannot find or open zhuawawa.zip, zhuawawa.zip.zip or zhuawawa.zip.ZIP. [root@localhost zhuawawa]# ls zhuawawaji.zip [root@localhost zhuawawa]# unzip zhuawawaji.zip [root@localhost zhuawawa]# rm -rf zhuawawaji.zip [root@localhost zhuawawa]# ls jQuery#U6293#U5a03#U5a03#U673a#U6e38#U620f#U4ee3#U7801 [root@localhost zhuawawa]# mv jQuery#U6293#U5a03#U5a03#U673a#U6e38#U620f#U4ee3#U7801/ zhuawawa [root@localhost zhuawawa]# ls zhuawawa [root@localhost zhuawawa]# mv 'zhuawawa'/* . [root@localhost zhuawawa]# ls images img index.html js zhuawawa [root@localhost zhuawawa]# rm -rf zhuawawa/ [root@localhost zhuawawa]# ls images img index.html js [root@localhost zhuawawa]# cd .. [root@localhost html]# ls Sia feijidazhan index.html zhuawawa [root@localhost conf.d]# httpd -t Syntax OK
[root@localhost ~]# ss -anlt State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 128 *:80 *:* LISTEN 0 128 *:81 *:*
2.不同IP相同端口
[root@localhost conf.d]# vim httpd-vhosts.conf ...... # match a ServerName or ServerAlias in any <VirtualHost> block. # <VirtualHost 192.168.48.150:80> DocumentRoot "/var/www/html/feijidazhan" ServerName www.feijidazhan.com DocumentRoot "/var/www/html/feijidazhan" ServerName www.feijidazhan.com ErrorLog "/var/log/httpd/www.feijidazhan.com-error_log" CustomLog "/var/log/httpd/www.feijidazhan.com-access_log" common </VirtualHost> <VirtualHost 192.168.48.151:80> DocumentRoot "/var/www/html/zhuawawa" ServerName www.zhuawawa.com ...... [root@localhost conf.d]# ip a ...... ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 00:0c:29:39:ef:7e brd ff:ff:ff:ff:ff:ff inet 192.168.48.150/24 brd 192.168.48.255 scope global dynamic noprefixroute ens33 ...... #在'ens33'中添加一个临时ip "192.168.48.151" [root@localhost conf.d]# ip addr add 192.168.48.151/24 dev ens33 [root@localhost conf.d]# ip a ...... ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 00:0c:29:39:ef:7e brd ff:ff:ff:ff:ff:ff inet 192.168.48.150/24 brd 192.168.48.255 scope global dynamic noprefixroute ens33 valid_lft 1669sec preferred_lft 1669sec inet 192.168.48.151/24 scope global secondary ens33 ...... [root@localhost conf.d]# ping 192.168.48.151 64 bytes from 192.168.48.151: icmp_seq=25 ttl=64 time=0.041 ms 64 bytes from 192.168.48.151: icmp_seq=26 ttl=64 time=0.031 ms --- 192.168.48.151 ping statistics --- 26 packets transmitted, 26 received, 0% packet loss, time 25622ms rtt min/avg/max/mdev = 0.026/0.044/0.093/0.018 ms [root@localhost conf.d]# httpd -t Syntax OK [root@localhost conf.d]# systemctl restart httpd
3.相同IP相同端口不同域名
[root@localhost conf.d]# dnf -y install bind-utils ...... bind-license-32:9.11.36-3.el8.noarch bind-utils-32:9.11.36-3.el8.x86_64 fstrm-0.6.1-2.el8.x86_64 geolite2-city-20180605-1.el8.noarch geolite2-country-20180605-1.el8.noarch libmaxminddb-1.2.0-10.el8.x86_64 protobuf-c-1.3.0-6.el8.x86_64 python3-bind-32:9.11.36-3.el8.noarch python3-ply-3.9-9.el8.noarch Complete! <VirtualHost 192.168.48.150:80> DocumentRoot "/var/www/html/feijidazhan" ServerName www.feijidazhan.com ErrorLog "/var/log/httpd/www.feijidazhan.com-error_log" CustomLog "/var/log/httpd/www.feijidazhan.com-access_log" common </VirtualHost> <VirtualHost 192.168.48.150:80> DocumentRoot "/var/www/html/zhuawawa" ServerName www.zhuawawa.com ErrorLog "/var/log/httpd/www.zhuawawa.com-error_log" CustomLog "/var/log/httpd/www.zhuawawa.com-access_log" common </VirtualHost>
在C盘中找到hosts文件添加虚拟机网址及其域名
ssl
启用模块:编辑/etc/httpd/conf.modules.d/00-base.conf文件,添加下面这行,如果已经有了但是注释了,则取消注释即可0
LoadModule ssl_module modules/mod_ssl.so
配置https步骤:
生成证书(参考博客linux运维系列第6章)
配置httpd.conf,取消以下内容的注释
LoadModule ssl_module modules/mod_ssl.so Include /etc/httpd24/extra/httpd-vhosts.conf Include /etc/httpd24/extra/httpd-ssl.conf
先检查一下有没有ssl模块,没有列出任何文件就没有安装ssl模块
[root@localhost ~]# httpd -M | grep ssl
安装ssl模块后重启服务,查到有ssl模块
[root@localhost ~]# httpd -M | grep ssl [root@localhost ~]# systemctl restart httpd [root@localhost ~]# httpd -t Syntax OK [root@localhost ~]# httpd -M |grep ssl ssl_module (shared)
CA生成一对密钥
[root@localhost ~]# cd /etc/pki/ [root@localhost pki]# ls ca-trust java rpm-gpg rsyslog tls [root@localhost pki]# mkdir CA [root@localhost pki]# cd CA/ [root@localhost CA]# pwd /etc/pki/CA [root@localhost CA]# mkdir private [root@localhost CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048) Generating RSA private key, 2048 bit long modulus (2 primes) .............+++++ ..................................................................................+++++ e is 65537 (0x010001) [root@localhost CA]# openssl rsa -in private/cakey.pem -pubout writing RSA key -----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA14G8RhnuUxIA7zsI9Nz4 oRkM6Hu/W92LDMyLf2D9DPktKnS9DewwerA/9hfrbkJqLxIjD6xByVLr9R3qFvEv RsXWYznWw42YmusyrorJeIxEAV4nnjK6QB4BCHq7CtvNG6pmi9qL+CTEw1EpXN/i wHAPYFtOPzAqtoxpLWem8sLWRRF40mLcFQM7tWKcE//kpuS8vqgw2o07wRIBdtw7 GpPTTieRyN3p6bUJhXvqQFAQkFP0VcEoJTE/lob3xJqNcMLqPqnrA5pVEmh01uwU 0DWlPx6glCXc9F8PVYiMsmseKIR27OgjAKjvWuUUfRIrZ8X6bJpJwtSbwTNQj/FR rQIDAQAB -----END PUBLIC KEY-----
CA生成自签署证书
[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) []:hb Locality Name (eg, city) [Default City]:wh Organization Name (eg, company) [Default Company Ltd]:runtime Organizational Unit Name (eg, section) []:peixun Common Name (eg, your name or your server's hostname) []:www.Sia.com Email Address []:1@2.com [root@localhost CA]# openssl x509 -text -in cacert.pem [root@localhost CA]# mkdir certs newcerts crl [root@localhost CA]# touch index.txt && echo 01 > serial [root@localhost CA]# ls cacert.pem certs crl index.txt newcerts private serial [root@localhost CA]# cat serial 01
客户端生成密钥
[root@localhost CA]# cd /etc/httpd && mkdir ssl && cd ssl [root@localhost ssl]# pwd /etc/httpd/ssl [root@localhost ssl]# (umask 077;openssl genrsa -out httpd.key 2048) Generating RSA private key, 2048 bit long modulus (2 primes) ............................................................................................................+++++ ....+++++ e is 65537 (0x010001) [root@localhost ssl]# ls httpd.key
客户端生成证书签署请求
[root@localhost ssl]# openssl req -new -key httpd.key -days 365 -out httpd.csr Ignoring -days; not generating a certificate 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]: string is too long, it needs to be no more than 2 bytes long Country Name (2 letter code) [XX]:cn State or Province Name (full name) []:hb Locality Name (eg, city) [Default City]:wu Organization Name (eg, company) [Default Company Ltd]:runtime Organizational Unit Name (eg, section) []:peixun Common Name (eg, your name or your server's hostname) []:www.Sia.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 []: [root@localhost ssl]# ls httpd.csr httpd.key
CA签署客户端提交上来的证书
[root@localhost ssl]# openssl ca -in ./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: Jul 24 10:16:31 2022 GMT Not After : Jul 24 10:16:31 2023 GMT Subject: countryName = cn stateOrProvinceName = hb organizationName = runtime organizationalUnitName = peixun commonName = www.Sia.com emailAddress = 1@2.com X509v3 extensions: X509v3 Basic Constraints: CA:FALSE Netscape Comment: OpenSSL Generated Certificate X509v3 Subject Key Identifier: 69:0C:EE:F8:38:DA:2B:17:26:4E:7D:57:0A:02:B4:33:AC:DB:0D:C9 X509v3 Authority Key Identifier: keyid:88:59:A9:2A:B6:67:D7:A4:7B:FB:74:4B:46:72:9C:1F:47:32:39:4A Certificate is to be certified until Jul 24 10:16:31 2023 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
修改ssl.conf配置文件
[root@localhost ssl]# pwd /etc/httpd/ssl [root@localhost ssl]# cd /etc/httpd/conf.d/ [root@localhost conf.d]# ls autoindex.conf httpd-vhosts.conf README ssl.conf userdir.conf welcome.conf [root@localhost conf.d]# vim ssl.conf <VirtualHost _default_:443> # General setup for the virtual host, inherited from global configuration DocumentRoot "/var/www/html/zhuawawaji" ServerName www.zhuawawaji.com:443 SSLCertificateFile /etc/httpd/ssl/httpd.crt //修改位置 SSLCertificateKeyFile /etc/httpd/ssl/httpd.key //修改位置 [root@localhost conf.d]# httpd -t Syntax OK [root@localhost conf.d]# systemctl restart httpd [root@localhost conf.d]# ss -anlt | grep 443 LISTEN 0 128 *:443 *:*
查看效果