目录
Linux服务-http
httpd是Apache超文本传输协议(HTTP)服务器的主程序。被设计为一个独立运行的后台进程,它会建立一个处理请求的子进程或线程的池。
通常,httpd不应该被直接调用,而应该在类Unix系统中由apachectl调用,在Windows中作为服务运行。
本文主要介绍httpd的两大版本,httpd-2.2和httpd-2.4。
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):从低到高级别越低越详细
工作模型 | 工作方式 |
---|---|
prefork | 多进程模型,预先生成进程,一个请求用一个进程响应 一个主进程负责生成n个子进程,子进程也称为工作进程 每个子进程处理一个用户请求,即使没有用户请求,也会预先生成多个空闲进程,随时等待请求到达,最大不会超过1024个 |
worker | 基于线程工作,一个请求用一个线程响应(启动多个进程,每个进程生成多个线程) |
event | 基于事件的驱动,一个进程处理多个请求 |
httpd-2.4在之前的版本基础上新增了几大模块,下面就几个常用的来介绍一下。
模块 | 功能 |
---|---|
mod_proxy_fcgi | 反向代理时支持apache服务器后端协议的模块 |
mod_ratelimit | 提供速率限制功能的模块 |
mod_remoteip | 基于ip的访问控制机制被改变,不再支持使用Order,Deny,Allow来做基于IP的访问控制 |
工具 | 功能 |
---|---|
htpasswd | basic认证基于文件实现时,用到的帐号密码生成工具 |
apachectl | httpd自带的服务控制脚本,支持start,stop,restart |
apxs | 由httpd-devel包提供的,扩展httpd使用第三方模块的工具 |
rotatelogs | 日志滚动工具 |
suexec | 访问某些有特殊权限配置的资源时,临时切换至指定用户运行的工具 |
ab | apache benchmark,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
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
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
//语法: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
//然后就不会出现错误信息了
第一步:基础环境准备
用YUM安装pcre-devel,openssl-devel,expat-devel
同时下载apache得三个安装包apr,apr-util,httpd
第二步:解压三个安装包
[root@localhost nginx-1.14.2]# tar xf httpd-2.4.38.tar.gz
[root@localhost nginx-1.14.2]# tar xf apr-1.6.5.tar.gz
[root@localhost nginx-1.14.2]# tar xf apr-util-1.6.1.tar.gz
[root@localhost nginx-1.14.2]# 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
[root@localhost ~]# cat /usr/local/apache/htdocs/index.html
<html><body><h1>It is cwh's Html</h1></body></html>
[root@localhost ~]# apachectl restart
登陆效果:
切换使用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>
虚拟主机:
虚拟主机有三类:
原文:https://www.cnblogs.com/chengweihong/p/10735130.html