#httpd
文章目录
-
- #httpd
- httpd简介
-
- curl命令
- httpd命令
httpd简介
httpd是Apache超文本传输协议(HTTP)服务器的主程序。设计为独立运行的后台过程,建立子过程或线程池。
通常,httpd不应直接调用,而应为类别Unix系统中由apachectl调用,在Windows作为服务运营。 ##httpd版本 本文主要介绍httpd两个版本,httpd-2.2和httpd-2.4。
CentOS默认提供6系列版本httpd-2.2版本的rpm包 CentOS默认提供7系列版本httpd-2.4版本的rpm包 ###httpd的特性 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可用于模块生产环境 支持异步读写 支持每个模块和目录分别使用自己的日志级别 用于配置各请求相关的专业配置 表达式分析器增强版 支持毫秒级keepalive timeout 基于FQDN不再需要虚拟主机NameVirtualHost指令 支持用户定制变量 支持新指令(AllowOverrideList) 降低对内存的消耗
prefork 多过程模型,提前生成过程,一个请求响应一个过程 负责生成n个子过程的主要过程也称为工作过程 每个子过程处理一个用户请求,即使没有用户请求,也会提前生成多个空闲过程,随时等待请求到达,最大不超过1024个
worker 在线程工作的基础上,一个一个线程响应(启动多个过程,每个过程生成多个线程)
event 基于事件的驱动,一个过程处理多个请求 ###httpd-2.4新增的模块 mod_proxy_fcgi 支持反向代理apache服务器后端协议模块 mod_ratelimit 提供速度限制功能的模块 mod_remoteip 基于ip改变了访问控制机制,不再支持使用Order,Deny,Allow来做基于IP的访问控制 ##httpd基础
###httpd自带工具程序 htpasswd basic基于文件实现认证时使用的帐户密码生成工具 apachectl httpd自带服务控制脚本,支持start,stop,restart apxs 由httpd-devel包提供,扩展httpd使用第三方模块的工具 rotatelogs 日志滚动工具 suexec 临时切换到指定用户权限配置的资源时,临时切换到指定用户运行的工具 ab apache benchmark,httpd压力测试工具 ###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 ##web相关的命令
curl命令
curl支持以下功能:
https认证 http的POST/PUT等方法 ftp上传 kerberos认证 http上传 代理服务器 cookies 用户名/密码认证 断点续传下载文件 socks5代理服务器 通过http代理服务器上传文件ftp服务器
httpd命令
//常用的options: -l //查看静态编译模块,列出核心编译的模块。 //它不会列出使用LoadModule动态加载模块的指令
[root@localhost ~]# httpd -l Compiled in modules: core.c mod_so.c http_core.c
-M //输出已启用的模块列表,包括服务中的静态编译 作为///DSO动态加载模块
[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
-v //显示httpd然后退出版本
[root@localhost ~]# httpd -v Server version: Apache/2.4.37 (centos) Server built: Nov 12 2021 04:57:27
-V //显示httpd和apr/apr-util退出版本和编译参数
[root@localhost ~]# httpd -V Server version: Apache/2.4.37 (centos) Server built: Nov 12 2021 04:57:27 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"
-X ///以调试模式运行httpd。只启动一个工作过程,而且 ///服务器不与控制台分离
[root@localhost ~]# httpd -X AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globall to suppress this message
-t //检查配置文件是否有语法错误
[root@localhost]# wget https://downloads.apache.org/apr/apr-1.6.5.tar.bz2 ~]# httpd -t 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 Syntax OK
##编译安装httpd2.4
下载Apache的包
下载httpd [root@localhost ~]# wget https://downloads.apache.org/httpd/httpd-2.4.54.tar.bz2
下载apr [root@localhost~]# wget https://downloads.apache.org/apr/apr-1.6.5.tar.bz2
下载apr-util [root@localhost] ~]# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.bz2
[root@localhost~]# ls apr-1.6.5.tar.bz2 file httpd-2.4.54.tar.bz2 outfile ss apr-util-1.6.1.tar.bz2 home ifcfg-ens33 passwd yyds [root@localhost ~]#
安装开发环境
[root@localhost ~]# yum groups mark install “Development Tools” [root@localhost ~]# useradd -r -g apache apache [root@localhost ~]# yum -y install openssl-devel pcre-devel expat-devel libtool
解压apr
[root@localhost~]# tar xf apr-1.6.5.tar.bz2 [root@localhost ~]# ls apr-1.6.5 apr-util-1.6.1.tar.bz2 home ifcfg-ens33 passwd yyds apr-1.6.5.tar.bz2 file httpd-2.4.54.tar.bz2 outfile ss [root@localhost ~]# 进入到apr-1.6.5/configure中删掉 R M " RM " RM"cfgfile" 指定apr的安装路径 [root@localhost apr-1.6.5]# ./configure --prefix=/usr/local/apr 同时进行二步曲和三步曲 [root@localhost apr-1.6.5]# make && make install
在解压apr-util
[root@localhost~]# tar xf apr-util-1.6.1.tar.bz2 [root@localhost ~]# ls apr-1.6.5 apr-util-1.6.1 file httpd-2.4.54.tar.bz2 outfile ss apr-1.6.5.tar.bz2 apr-util-1.6.1.tar.bz2 home ifcfg-ens33 passwd yyds [root@localhost ~]# 指定安装位置 依赖关系要带上apr的位置 [root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/ 进行二部三步曲 [root@localhost apr-util-1.6.1]# make && make install
解压httpd
解压httpd [root@localhost ~]# tar xf httpd-2.4.54.tar.bz2 [root@localhost ~]# ls apr-1.6.5 apr-util-1.6.1 file httpd-2.4.54 ifcfg-ens33 passwd yyds apr-1.6.5.tar.bz2 apr-util-1.6.1.tar.bz2 home httpd-2.4.54.tar.bz2 outfile ss [root@localhost ~]# 指定安装位置 [root@localhost httpd-2.4.54]# ./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util/ 进行二部三部曲 [root@localhost httpd-2.4.54]# make && make install
启动Apache
用绝对路径启动 [root@localhost ~]# /usr/local/apache/bin/apachectl start
关闭防火墙
[root@localhost ~]# systemctl stop firewalld [root@localhost ~]# setenforce 0
环境变量
[root@localhost ~]# vim /etc/profile.d/apache.sh
## httpd常用配置
LoadModule mpm_NAME_module modules/mod_mpm_NAME.so //NAME有三种,分别是:
prefork
event
worker
[root@localhost ~]# vim /etc/httpd/conf.modules.d/00-mpm.conf
访问控制法则
|法则|功能|
|-|
|Require all granted|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 conf]# curl http://192.168.17.130/zhang
Moved Permanently
The document has moved
href=“http://192.168.17.130/zhang/”>here.
[root@localhost conf]#
虚拟主机:
虚拟主机有三类:
相同IP不同端口
不同IP相同端口
相同IP相同端口不同域名
ssl
[oot@localhost ~]# yum install -y mod_ssl
Last metadata expiration check: 2:36:33 ago on Thu 21 Jul 2022 07:08:34 PM CST. Dependencies resolved. ============================================================================== Package Arch Version Repo Size ============================================================================== Installing: mod_ssl x86_64 1:2.4.37-30.module_el8.3.0+462+ba287492.0.1 AppStream 133 k Installing dependencies: sscg x86_64 2.3.3-14.el8 AppStream 49 k Transaction Summary ==============================================================================
启用模块:
编辑/etc/httpd/conf.modules.d/00base.conf文件,添加下面这行,如果已经有了但是注释了,则取消注释即可
[root@localhost conf.modules.d]# systemctl restart httpd [root@localhost conf.modules.d]# httpd -M | grep ssl [Thu Jul 21 22:03:06.108490 2022] [so:warn] [pid 4802:tid 140408963324224] AH01574: module ssl_module is already loaded, skipping ssl_module (shared) [root@localhost conf.modules.d]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]😗 LISTEN 0 128 *:443 : LISTEN 0 128 *:80 : [root@localhost conf.modules.d]#
配置https步骤
生成证书(参考博客linux运维系列第6章
生成密钥
[root@localhost pki]# mkdir CA [root@localhost pki]# cd 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]# 提取密钥 [root@localhost CA]# openssl rsa -in private/cakey.pem -pubout writing RSA key -----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw9iQaA47Zt80hM9F44UQ 30Y1JGc1gX+sfamyQHnG8ZjrZA8Z0L9eX9bSlBKWuwW7yH4HwMCs44cmu1Xzz7no Y9lnfgLMgS69L1dpLNJlY9uGjXb+PFwGjJtEtTw30mVbo4zvb+81pX5SOgQaq1+h 0HAEsLrVN/CyLHuVCV8YaN5OhmzGgKke7DY5JXSlXxfBIapjLxNby6C0QBH9AI9N W70bljJUnWHc6cGowxK3j22NINbCodj8x+INv3Ekf0kErJ3D6uOnfkWWzJ1M8HAC nombp3EVMqqnNm7GGXfCB7gtsLbqGrFKyyhhpW0k2tmp9isNghrfMTMRxovv2zfD hwIDAQAB -----END PUBLIC KEY----- [root@localhost CA]#
生成自签证书
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]:L (公司) Organizational Unit Name (eg, section) []:pexun (部门)
Common Name (eg, your name or your server's hostname) []:www.zhuawawa.com (服务名)
Email Address []:7@7.com (邮箱)
[root@localhost CA]# ls
cacert.pem private
[root@localhost CA]#
创建目录
[root@localhost CA]# mkdir certs newcerts crl
[root@localhost CA]# ls cacert.pem certs crl newcerts private [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]#
给服务器生成密钥
[oot@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]:cn
State or Province Name (full name) []:hb
Locality Name (eg, city) [Default City]:wh
Organization Name (eg, company) [Default Company Ltd]:L
Organizational Unit Name (eg, section) []:pexun
Common Name (eg, your name or your server's hostname) []:www.zhuawawa.com
Email Address []:7@7.com
签署证书
[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 21 15:12:57 2022 GMT
Not After : Jul 21 15:12:57 2023 GMT
Subject:
countryName = cn
stateOrProvinceName = hb
organizationName = L
organizationalUnitName = pexun
commonName = www.zhuawawa.com
emailAddress = 1@1.com
在httpdssl.conf中配置证书的位置 进入到/etc/httpd/conf.d/ssl.conf中注释以下内容 注意更改文件位置 DocumentRoot “/var/www/html/zhuawawa” ServerName www.zhuawawa.com:443 更改httpd.crt 和httpd.key的文件位置
SSLCertificateFile /etc/httpd/ssl/httpd.crt
# Server Private Key:
# If the key is not combined with the certificate, use this
# directive to point at the key file. Keep in mind that if
# you've both a RSA and a DSA private key you can configure
# both in parallel (to also allow the use of DSA ciphers, etc.)
# ECC keys, when in use, can also be configured in parallel SSLCertificateKeyFile /etc/httpd/ssl/httpd.key
检查配置文件是否有语法错误 启动或重启服务
[root@localhost conf.d]# systemctl restart httpd
[root@localhost conf.d]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22
[::]:*
LISTEN 0 128 *:443 *:*
LISTEN 0 128 *:80 *:*
[root@localhost conf.d]#