15158846557 在线咨询 在线咨询
15158846557 在线咨询
所在位置: 首页 > 营销资讯 > 网站运营 > httpd

httpd

时间:2023-07-16 01:54:02 | 来源:网站运营

时间:2023-07-16 01:54:02 来源:网站运营

httpd:











1. httpd简介

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

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

2. httpd版本

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

2.1 httpd的特性

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

版本特性



工作模型工作方式

2.2 httpd-2.4新增的模块

httpd-2.4在之前的版本基础上新增了几大模块,下面就几个常用的来介绍一下。

模块功能

3. httpd基础

3.1 httpd自带的工具程序

工具功能

3.2 rpm包安装的httpd程序环境

文件/目录对应的功能
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支持以下功能:

//语法: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

[root@localhost ~]# curl -o myblog.html http://http://blog.51cto.com/itchentao

% Total % Received % Xferd Average Speed Time Time Time Current

Dload Upload Total Spent Left Speed100 67025 0 67025 0 0 87248 0 --:--:-- --:--:-- --:--:-- 87385

[root@localhost ~]# ls

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 -l

Compiled in modules:

core.c

mod_so.c

http_core.c

[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)

......

......

[root@localhost ~]# httpd -vServer version: Apache/2.4.6 (CentOS)Server built: Jun 27 2018 13:48:59




[root@localhost ~]# httpd -V

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

Server version: Apache/2.4.6 (CentOS)

Server built: Jun 27 2018 13:48:59

Server's Module Magic Number: 20120211:24Server loaded: APR 1.4.8, APR-UTIL 1.5.2

Compiled using: APR 1.4.8, APR-UTIL 1.5.2

Architecture: 64-bitServer 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"

4. 编译安装httpd-2.4

httpd依赖于apr-1.4+,apr-util-1.4+,[apr-icon]

apr:apache portable runtime

//安装开发环境

[root@localhost ~]# yum groups mark install "Development Tools"




[root@localhost ~]# groupadd -r apache

[root@localhost ~]# useradd -r -g apache apache

[root@localhost ~]# yum -y install openssl-devel pcre-devel expat-devel libtool

Loaded plugins: product-id, search-disabled-repos, subscription-manager

This system is not registered with an entitlement server. You can use subscription-manager to register.

Resolving Dependencies

--> Running transaction check

---> Package openssl-devel.x86_64 1:1.0.2k-12.el7 will be installed

--> Processing Dependency: openssl-libs(x86-64) = 1:1.0.2k-12.el7 for package: 1:openssl-devel-1.0.2k-12.el7.x86_64

......

......

libselinux-utils.x86_64 0:2.5-12.el7 libsepol.x86_64 0:2.5-8.1.el7

libss.x86_64 0:1.42.9-12.el7_5 openssl.x86_64 1:1.0.2k-12.el7

openssl-libs.x86_64 1:1.0.2k-12.el7




Complete!




//下载并安装apr-1.4+和apr-util-1.4+

[root@localhost ~]# cd /usr/src/

[root@localhost src]# wget http://mirror.bit.edu.cn/apache/apr/apr-1.6.5.tar.gz




[root@localhost src]# wget http://mirror.bit.edu.cn/apache/apr/apr-util-1.6.1.tar.gz




[root@localhost src]# ls

apr-1.6.5.tar.gz apr-util-1.6.1.tar.gz debug kernels

[root@localhost src]# tar xf apr-1.6.5.tar.gz

[root@localhost src]# tar xf apr-util-1.6.1.tar.gz

[root@localhost src]# ls

apr-1.6.5 apr-1.6.5.tar.gz apr-util-1.6.1 apr-util-1.6.1.tar.gz debug kernels

[root@localhost src]# cd apr-1.6.5

[root@localhost apr-1.6.5]# vim configure

cfgfile="${ofile}T"

trap "$RM /"$cfgfile/"; exit 1" 1 2 15

# $RM "$cfgfile" //将此行加上注释,或者删除此行




[root@localhost apr-1.6.5]# ./configure --prefix=/usr/local/apr

配置过程略...

[root@localhost apr-1.6.5]# make && make install

编译安装过程略...




[root@localhost apr-1.6.5]# cd /usr/src/apr-util-1.6.1

[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

[root@localhost ~]# wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.38.tar.gz







[root@localhost ~]# ls

httpd-2.4.38.tar.gz

[root@localhost ~]# tar xf httpd-2.4.38.tar.gz

[root@localhost ~]# cd httpd-2.4.38

[root@localhost httpd-2.4.38]# ./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.38]# make && make install

编译安装过程略...

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 conf.modules.d]# pwd

/etc/httpd/conf.modules.d

[root@localhost conf.modules.d]# ls00-base.conf 00-lua.conf 00-proxy.conf 01-cgi.conf00-dav.conf 00-mpm.conf 00-systemd.conf

[root@localhost conf.modules.d]# vim 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

访问控制法则:

法则功能



IPADDR的类型HOSTNAME的类型
注意:httpd-2.4版本默认是拒绝所有主机访问的,所以安装以后必须做显示授权访问

示例:

<Directory /var/www/html/www>

<RequireAll>

Require not ip 192.168.1.20

Require all granted

</RequireAll></Directory>

虚拟主机:
虚拟主机有三类:

//设置主机名

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf

......

ServerAdmin root@localhost

## ServerName gives the name and port that the server uses to identify itself.# This can often be determined automatically, but we recommend you specify# it explicitly to prevent problems during startup.## If your host doesn't have a registered DNS name, enter its IP address here.#

ServerName http://www.example.com:80 //取消此行前面的#号

......




[root@localhost ~]# vim /etc/httpd/conf/httpd.conf//在配置文件的最后加上如下内容

[root@localhost ~]# tail -25 /etc/httpd/conf/httpd.conf#virtual host 1   # 虚拟主机1的配置

<VirtualHost 172.16.12.128:80>

ServerName http://www.wangqing.com

DocumentRoot "/var/www/html/www"

ErrorLog "/var/log/httpd/www/error_log"

CustomLog "/var/log/httpd/www/access_log" combined

<Directory /var/www/html/www>

<RequireAll>

Require all granted

Require not ip 192.168.1

</RequireAll>

</Directory>

</VirtualHost># virtual host 2 # 虚拟主机2的配置

<VirtualHost 172.16.12.128:80>

ServerName http://blog.wangqing.com

DocumentRoot "/var/www/html/blog"

ErrorLog "/var/log/httpd/blog/error_log"

CustomLog "/var/log/httpd/blog/access_log" combined

<Directory /var/www/html/blog>

<RequireAll>

  Require all granted

</RequireAll>

</Directory>

</VirtualHost>

//创建网页目录并修改属主属组

[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

//创建网页

[root@localhost html]# pwd

/var/www/html

[root@localhost html]# ls

blog www

[root@localhost html]# echo 'hello welcome to visit www' > www/index.html

[root@localhost html]# echo 'hello welcome to visit blog' > blog/index.html

//创建相应网页的日志目录

[root@localhost ~]# mkdir /var/log/httpd/{www,blog}

[root@localhost ~]# ll /var/log/httpd/

total 0

drwxr-xr-x 2 root root 6 Aug 5 09:11 blog

drwxr-xr-x 2 root root 6 Aug 5 09:11 www

[root@localhost ~]# chown -R apache.apache /var/log/httpd/

//启动服务并查看是否有80端口

[root@localhost ~]# systemctl start httpd

[root@localhost ~]# ss -antl

State Recv-Q Send-Q Local Address:Port Peer Address:Port

LISTEN 0 128 *:22 *:*

LISTEN 0 100 127.0.0.1:25 *:*

LISTEN 0 128 :::80 :::*

LISTEN 0 128 :::22 :::*

LISTEN 0 100 ::1:25 :::*

//在客户机上验证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 localhost255.255.255.255 broadcasthost

::1 localhost118.31.33.0 zabbix.forevercq.com0.0.0.0 http://account.jetbrains.com//添加以下2行172.16.12.128 www.wangqing.com172.16.12.128 http://blog.wangqing.com

2.在浏览器上打开网页测试

ssl:
启用模块:编辑/etc/httpd/conf.modules.d/00-base.conf文件,添加下面这行,如果已经有了但是注释了,则取消注释即可

LoadModule ssl_module modules/mod_ssl.so

配置https步骤:

LoadModule ssl_module modules/mod_ssl.so

Include /etc/httpd24/extra/httpd-vhosts.conf

Include /etc/httpd24/extra/httpd-ssl.conf

作业

1.编译安装httpd-2.4
2.配置三种不同风格的虚拟主机
3.写博客,要写明环境背景、配置步骤,要有验证截图

更多网络知识学习,微信runtime66

关键词:

74
73
25
news

版权所有© 亿企邦 1997-2025 保留一切法律许可权利。

为了最佳展示效果,本站不支持IE9及以下版本的浏览器,建议您使用谷歌Chrome浏览器。 点击下载Chrome浏览器
关闭