httpd运用

本文详细介绍了Apache httpd服务器的基础知识,包括httpd的版本特性、安装过程、常用命令以及配置方法,如访问控制和虚拟主机的设置。此外,还提供了使用curl命令进行访问和下载的实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. httpd简介

httpd是Apache超文本传输协议(HTTP)服务器的主程序。被设计为一个独立运行的后台进程,它会建立一个处理请求的子进程或线程的池。

通常,httpd不应该被直接调用,而应该在类Unix系统中由apachectl调用,在Windows中作为服务运行。

2. httpd版本

本文主要介绍httpd的两大版本,httpd-2.2和httpd-2.4。

  1. CentOS6系列的版本默认提供的是httpd-2.2版本的rpm包
  2. CentOS7系列的版本默认提供的是httpd-2.4版本的rpm包

2.1 httpd的特性

httpd有很多特性,下面就分别来说说httpd-2.2版本和httpd-2.4版本各自的特性。

版本特性
2.21. 事先创建进程
2. 按需维持适当的进程
3. 模块化设计,核心比较小,各种功能通过模块添加(包括PHP),支持运行时配置,支持单独编译模块
4. 支持多种方式的虚拟主机配置,如基于ip的虚拟主机,基于端口的虚拟主机,基于域名的虚拟主机等
5. 支持https协议(通过mod_ssl模块实现)
6. 支持用户认证
7. 支持基于IP或域名的ACL访问控制机制
8. 支持每目录的访问控制(用户访问默认主页时不需要提供用户名和密码,但是用户访问某特定目录时需要提供用户名和密码)
9. 支持URL重写
10. 支持MPM(Multi Path Modules,多处理模块)。用于定义httpd的工作模型(单进程、单进程多线程、多进程、多进程单线程、多进程多线程)
2.4httpd-2.4的新特性:
1. MPM支持运行DSO机制(Dynamic Share Object,模块的动态装/卸载机制),以模块形式按需加载
2. 支持event MPM,eventMPM模块生产环境可用
3. 支持异步读写
4. 支持每个模块及每个目录分别使用各自的日志级别
5. 每个请求相关的专业配置,使用来配置
6. 增强版的表达式分析器
7. 支持毫秒级的keepalive timeout
8. 基于FQDN的虚拟主机不再需要NameVirtualHost指令
9. 支持用户自定义变量
10. 支持新的指令(AllowOverrideList)
11. 降低对内存的消耗
工作模型(不能同时使用)工作方式
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自带的工具程序

工具功能
htpasswdbasic认证基于文件实现时,用到的帐号密码生成工具
apachectlhttpd自带的服务控制脚本,支持start,stop,restart
apxs由httpd-devel包提供的,扩展httpd使用第三方模块的工具
rotatelogs日志滚动工具
suexec访问某些有特殊权限配置的资源时,临时切换至指定用户运行的工具
abapache benchmark,httpd的压力测试工具

apachectl stop=systemctl stop httpd
yum用systemctl
rpm用apachectl

3.2 安装的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

//先关掉防火墙和selinux

[root@yeqixian1 ~]# getenforce
Disabled
[root@yeqixian1 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)

//yum安装

[root@yeqixian1 ~]# yum -y install httpd
[root@yeqixian1 ~]# ls /etc/httpd/
conf conf.d conf.modules.d logs modules run

网站所放的位置
[root@yeqixian1 ~]# ls /var/www/html/(默认为无)

[root@yeqixian1 ~]# systemctl start httpd(启动httpd服务)
[root@yeqixian1 ~]# ll /var/log/httpd
total 4
-rw-r–r-- 1 root root 0 May 11 18:41 access_log
-rw-r–r-- 1 root root 793 May 11 18:41 error_log

//工作模型的使用

//切换使用MPM(编辑/etc/httpd/conf.modules.d/00-mpm.conf文件):

[root@yeqixian1 ~]# vim /etc/httpd/conf.modules.d/00-mpm.conf(想用什么工作模型;就取消其前面的注释)
# Select the MPM module which should be used by uncommenting exactly
# one of the following LoadModule lines:

# prefork MPM: Implements a non-threaded, pre-forking web server
# See: http://httpd.apache.org/docs/2.4/mod/prefork.html
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so(prefork)

# 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(worker)

# 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(event)

[root@yeqixian ~]# httpd -M|grep prefork
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::b11b:ad9d:46c7:b3ec. Set the 'ServerName' directive globally to suppress this message
 mpm_prefork_module (shared)
[root@yeqixian ~]# httpd -M|grep worker
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::b11b:ad9d:46c7:b3ec. Set the 'ServerName' directive globally to suppress this message
[root@yeqixian ~]# httpd -M|grep event
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::b11b:ad9d:46c7:b3ec. Set the 'ServerName' directive globally to suppress this message

3.3 web相关的命令

3.3.1 curl命令(用来访问或下载)

curl是基于URL语法在命令行方式下工作的文件传输工具,它支持FTP,FTPS,HTTP,HTTPS,GOPHER,TELNET,DICT,FILE及LDAP等协议。

curl支持以下功能:

  1. https认证
  2. http的POST/PUT等方法
  3. ftp上传
  4. kerberos认证
  5. http上传
  6. 代理服务器
  7. cookies
  8. 用户名/密码认证
  9. 下载文件断点续传
  10. socks5代理服务器
  11. 通过http代理服务器上传文件到ftp服务器
//语法:curl [options] [URL …]

//常用的options:
-A/–user-agent //设置用户代理发送给服务器
-basic //使用Http基本认证
–tcp-nodelay //使用TCP_NODELAY选项
-e/–referer //来源网址
–cacert //CA证书(SSL)
–compressed //要求返回时压缩的格式
-H/–header //自定义请求首部信息传递给服务器
-I/–head //只显示响应报文首部信息
–limit-rate //设置传输速度
-u/–user <user[:password]> //设置服务器的用户和密码
-0/–http1 //使用http 1.0版本,默认使用1.1版本。这个选项是数字0而不是字母o
-o/–output //把输出写到文件中
-#/–progress-bar //进度条显示当前的传送状态

//访问

[root@yeqixian ~]# curl http://192.168.116.145

//下载

root@yeqixian ~]# curl -o baidu http://www.baidu.com
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2381 100 2381 0 0 12833 0 --:–:-- --:–:-- --:–:-- 12940
[root@yeqixian ~]# ls
20200507_叶启贤_shell_nfs脚本_作业提交.sh info
aaa mima
anaconda-ks.cfg nfs-sh
backup.tar.gz rsa
baidu rsa.pub
gg sg
ggg ssh.sh
httpd-2.4.43 text
httpd-2.4.43.tar.gz yeqixian.tar.gz
[root@yeqixian ~]# cp baidu /opt/fff/

3.3.2 httpd命令

//语法:httpd [options]

//常用的options:
-l //查看静态编译的模块,列出核心中编译了哪些模块。
//它不会列出使用LoadModule指令动态加载的模块
-M //输出一个已经启用的模块列表,包括静态编译在服务
//器中的模块和作为DSO动态加载的模块
-v //显示httpd的版本,然后退出
-V //显示httpd和apr/apr-util的版本和编译参数,然后退出
-X //以调试模式运行httpd。仅启动一个工作进程,并且
//服务器不与控制台脱离
-t //检查配置文件是否有语法错误(=apachectl -t)

[root@yeqixian ~]# httpd -l
Compiled in modules:
  core.c
  mod_so.c
  http_core.c
[root@yeqixian ~]# httpd -M
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::b11b:ad9d:46c7:b3ec. 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)
 authn_file_module (shared)
 authn_socache_module (shared)
 authz_core_module (shared)
 authz_dbd_module (shared)
 authz_dbm_module (shared)
 authz_groupfile_module (shared)
 authz_host_module (shared)
 authz_owner_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 cache_module (shared)
 cache_disk_module (shared)
 data_module (shared)
 dbd_module (shared)
 deflate_module (shared)
 dir_module (shared)
 dumpio_module (shared)
 echo_module (shared)
 env_module (shared)
 expires_module (shared)
 ext_filter_module (shared)
 filter_module (shared)
 headers_module (shared)
 include_module (shared)
 info_module (shared)
 log_config_module (shared)
 logio_module (shared)
 mime_magic_module (shared)
 mime_module (shared)
 negotiation_module (shared)
 remoteip_module (shared)
 reqtimeout_module (shared)
 rewrite_module (shared)
 setenvif_module (shared)
 slotmem_plain_module (shared)
 slotmem_shm_module (shared)
 socache_dbm_module (shared)
 socache_memcache_module (shared)
 socache_shmcb_module (shared)
 status_module (shared)
 substitute_module (shared)
 suexec_module (shared)
 unique_id_module (shared)
 unixd_module (shared)
 userdir_module (shared)
 version_module (shared)
 vhost_alias_module (shared)
 dav_module (shared)
 dav_fs_module (shared)
 dav_lock_module (shared)
 lua_module (shared)
 mpm_prefork_module (shared)
 proxy_module (shared)
 lbmethod_bybusyness_module (shared)
 lbmethod_byrequests_module (shared)
 lbmethod_bytraffic_module (shared)
 lbmethod_heartbeat_module (shared)
 proxy_ajp_module (shared)
 proxy_balancer_module (shared)
 proxy_connect_module (shared)
 proxy_express_module (shared)
 proxy_fcgi_module (shared)
 proxy_fdpass_module (shared)
 proxy_ftp_module (shared)
 proxy_http_module (shared)
 proxy_scgi_module (shared)
 proxy_wstunnel_module (shared)
 systemd_module (shared)
 cgi_module (shared)
[root@yeqixian ~]# httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built:   Aug  8 2019 11:41:18
[root@yeqixian ~]# httpd -V
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::b11b:ad9d:46c7:b3ec. Set the 'ServerName' directive globally to suppress this message
Server version: Apache/2.4.6 (CentOS)
Server built:   Aug  8 2019 11:41:18
Server's Module Magic Number: 20120211:24
Server loaded:  APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture:   64-bit
Server MPM:     prefork
  threaded:     no
    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/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@yeqixian ~]# httpd -X
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::b11b:ad9d:46c7:b3ec. Set the 'ServerName' directive globally to suppress this message
^C
[root@yeqixian ~]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::b11b:ad9d:46c7:b3ec. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@yeqixian ~]#

4. httpd常用配置

4.1访问控制法则:

法则功能
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/mask192.168.1.0/255.255.255.0
Network/Length:192.168.1.0/24
Net:192.168(192.168.0.0)
FQDN:特定主机的全名
DOMAIN:指定域内的所有主机

注意:httpd-2.4版本默认是拒绝所有主机访问的,所以安装以后必须做显示授权访问
[root@yeqixian1 html]# echo ‘hello world’ > index.html(页面文件为index.html;不能改)
http://192.168.116.145/runtime/
示例:

<Directory /var/www/html/www>

Require not ip 192.168.1.20
Require all granted

4.2虚拟主机:

虚拟主机有三类:

  1. 相同IP不同端口
  2. 不同IP相同端口
  3. 相同IP相同端口不同域名
//查找虚拟主机.conf的位置并把它复制到本地

[root@yeqixian1 ~]# find / -name *vhosts.conf
/usr/share/doc/httpd-2.4.6/httpd-vhosts.conf
[root@yeqixian1 ~]# cd /etc/httpd/conf.d/
root@yeqixian1 conf.d]# cp /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf .

//标准配置

<VirtualHost *(ip)😡@Port(端口)@@>
ServerAdmin webmaster@dummy-host.example.com(管理员的邮箱地址,不需要)
DocumentRoot “@@ServerRoot@@/docs/dummy-host.example.com”(网站放的位置)
ServerName dummy-host.example.com(域名)
ServerAlias www.dummy-host.example.com(别名,不需要)
ErrorLog “/var/log/httpd/dummy-host.example.com-error_log”(错误日志)
CustomLog “/var/log/httpd/dummy-host.example.com-access_log” common(正确日志)

#### 1. 相同IP不同端口

[root@yeqixian1 ~]# mkdir -p /data/{www.a.com,www.b.com}
[root@yeqixian1 ~]# ll /data/
total 0
drwxr-xr-x 2 root root 6 May 12 15:09 www.a.com
drwxr-xr-x 2 root root 6 May 12 15:09 www.b.com
[root@yeqixian1 ~]# echo ‘www.a.com’ > www.a.com/index.html
-bash: www.a.com/index.html: No such file or directory
[root@yeqixian1 ~]# cd /data/
[root@yeqixian1 data]# echo ‘www.a.com’ > www.a.com/index.html
[root@yeqixian1 data]# echo ‘www.b.com’ > www.b.com/index.html
[root@yeqixian1 data]# httpd -t
AH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using 192.168.116.145. Set the ‘ServerName’ directive globally to suppress this message
Syntax OK
[root@yeqixian1 data]# systemctl restart httpd

访问网页
http://192.168.116.145:81/(不加端口,默认是80)

2. 不同IP相同端口

[root@yeqixian1 conf.d]# vim httpd-vhosts.conf
<VirtualHost 192.168.116.145:80>
DocumentRoot “/data/www.a.com”
ServerName www.a.com
ErrorLog “/var/log/httpd/www.a.com-error_log”
CustomLog “/var/log/httpd/www.a.com-access_log” common
<Directory “/data/www.a.com”>

Require all granted


<VirtualHost 192.168.116.250:80>
DocumentRoot “/data/www.b.com”
ServerName www.b.com
ErrorLog “/var/log/httpd/www.b.com-error_log”
CustomLog “/var/log/httpd/www.b.com-access_log” common
<Directory “/data/www.b.com”>

Require all granted



[root@yeqixian1 conf.d]# ip addr add 192.168.116.250/24 dev eth0
[root@yeqixian1 conf.d]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:2a:89:d3 brd ff:ff:ff:ff:ff:ff
inet 192.168.116.145/24 brd 192.168.116.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 192.168.116.250/24 scope global secondary eth0
valid_lft forever preferred_lft forever
[root@yeqixian1 conf.d]# systemctl restart httpd
[root@yeqixian1 conf.d]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:45917 :
LISTEN 0 64 *:2049 :
LISTEN 0 25 *:514 :
LISTEN 0 50 *:3306 :
LISTEN 0 128 *:111 :
LISTEN 0 128 *:80 :
当访问192.168.116.145时
在这里插入图片描述
当访问192.168.116.250时
在这里插入图片描述

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

[root@yeqixian1 conf.d]# vim httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot “/data/www.a.com”
ServerName www.a.com
ErrorLog “/var/log/httpd/www.a.com-error_log”
CustomLog “/var/log/httpd/www.a.com-access_log” common
<Directory “/data/www.a.com”>

Require all granted


<VirtualHost *:80>
DocumentRoot “/data/www.b.com”
ServerName www.b.com
ErrorLog “/var/log/httpd/www.b.com-error_log”
CustomLog “/var/log/httpd/www.b.com-access_log” common
<Directory “/data/www.b.com”>

Require all granted


//在客户机上验证
1.修改hosts文件
 ~ cat /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost
118.31.33.0 zabbix.forevercq.com
0.0.0.0 account.jetbrains.com
//添加以下x行
172.16.12.128 www.wangqing.com(上文写了哪些ip和对应的域名写入到这个文件中)

要做映射(公司不需要)

windows:

C:\windows\system32\drivers\etc\hosts
在这里插入图片描述

linux:

\etc\hosts
//创建网页目录并修改属主属组(如果要别人可以写入)
[root@yeqixian1 conf.d]# ps -ef|grep httpd
root 22964 1 0 16:15 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 22981 22964 0 16:15 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache(需要改变的属主) 22982 22964 0 16:15 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 22983 22964 0 16:15 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 22984 22964 0 16:15 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 22985 22964 0 16:15 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 22989 22964 0 16:15 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
root 23280 18379 0 16:21 pts/0 00:00:00 grep --color=auto httpd
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# mkdir www blog
[root@localhost html]# ll
total 0
drwxr-xr-x 2 root root 6 Aug 5 16:56 blog
drwxr-xr-x 2 root root 6 Aug 5 16:56 www
[root@localhost html]# chown -R apache.apache blog
[root@localhost html]# chown -R apache.apache www
[root@localhost html]# ll
total 0
drwxr-xr-x 2 apache apache 6 Aug 5 16:56 blog
drwxr-xr-x 2 apache apache 6 Aug 5 16:56 www

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值