httpd是Apache超文本传输协议(HTTP)服务器的主程序。被设计为一个独立运行的后台进程,它会建立一个处理请求的子进程或线程的池。
通常,httpd不应该被直接调用,而应该在类Unix系统中由apachectl调用,在Windows中作为服务运行。
主要介绍httpd的两大版本,httpd-2.2和httpd-2.4。
httpd有很多特性,下面分别介绍httpd2.2和httpd2.4各自的特性
httpd2.2
httpd2.4
工作模型:
httpd2.4新增模块
工具 | 功能 |
---|---|
htpasswd | basic认证基于文件实现时,用到的帐号密码生成工具 |
apachectl | httpd自带的服务控制脚本,支持start,stop,restart |
apxs | 由httpd-devel包提供的,扩展httpd使用第三方模块的工具 |
rotatelogs | 日志滚动工具 |
suexec | 访问某些有特殊权限配置的资源时,临时切换至指定用户运行的工具 |
ab | apache benchmark,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
除了编译安装外也可以使用yum安装,这里只是为了演示一下如何编译安装httpd
httpd有三个依赖:apr-1.4+,apr-util-1.4+,[apr-icon]
安装顺序:apr -> apr-util -> httpd
#安装开发环境[root@lynkser ~]# yum groupinstall "Development Tools"#添加一个服务用的用户[root@lynkser ~]# groupadd -r apache[root@lynkser ~]# useradd -r -g apache -M -s /bin/nologin apache#安装前置工具[root@lynkser ~]# yum -y install openssl-devel pcre-devel expat-devel libtool#下载所需的源码包,各位可以各自从常用的站点下载,我这里已经下好了[root@lynkser ~]# cd /usr/src[root@lynkser src]# lsapr-1.6.3.tar.bz2 apr-util-1.6.1.tar.bz2 debug httpd-2.4.34.tar.bz2 kernels#解压源码包[root@lynkser src]# tar -xf apr-1.6.3.tar.bz2 [root@lynkser src]# tar -xf apr-util-1.6.1.tar.bz2 [root@lynkser src]# tar -xf httpd-2.4.34.tar.bz2 #修改一下apr的配置文件,避免出问题。[root@lynkser src]# cd apr-1.6.3/[root@lynkser apr-1.6.3]# vim configure cfgfile=${ofile}T trap "$RM \"$cfgfile\"; exit 1" 1 2 15# $RM "$cfgfile" #注释掉这一行#编译安装apr[root@lynkser apr-1.6.3]# ./configure --prefix=/usr/local/apr[root@lynkser apr-1.6.3]# make && make install#编译安装apr-util[root@lynkser apr-1.6.3]# cd /usr/src/apr-util-1.6.1[root@lynkser apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr[root@lynkser apr-util-1.6.1]# make && make install#编译安装httpd[root@lynkser src]# cd /usr/src/httpd-2.4.34/[root@lynkser httpd-2.4.34]# ./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@lynkser httpd-2.4.34]# make && make install
切换使用MPM
#yum安装的情况下[root@lynkser ~]# cd /etc/httpd/conf.modules.d[root@lynkser conf.modules.d]# ls00-base.conf 00-lua.conf 00-proxy.conf 01-cgi.conf00-dav.conf 00-mpm.conf 00-systemd.conf[root@lynkser 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.htmlLoadModule mpm_prefork_module modules/mod_mpm_prefork.so#编译安装的情况下[root@lynkser ~]# vim /etc/httpd24/httpd.conf# To be able to use the functionality of a module which was built as a DSO you# have to place corresponding `LoadModule' lines at this location so the# directives contained in it are actually available _before_ they are used.# Statically compiled modules (those listed by `httpd -l') do not need# to be loaded here.## Example:# LoadModule foo_module modules/mod_foo.so##LoadModule mpm_event_module modules/mod_mpm_event.soLoadModule mpm_prefork_module modules/mod_mpm_prefork.so
法则 | 功能 |
---|---|
Require all granted | 允许所有主机访问 |
Require all deny | 拒绝所有主机访问 |
Require ip IPADDR | 授权指定来源地址的主机访问 |
Require not ip IPADDR | 拒绝指定来源地址的主机访问 |
Require host HOSTNAME | 授权指定来源主机名的主机访问 |
Require not host HOSTNAME | 拒绝指定来源主机名的主机访问 |
IPADDR的类型:
HOSTNAME的类型
http-2.4版本默认拒绝所有主机访问,安装后必须做显示授权访问
示例:
<Directory /var/www/html/www> <RequireAll> Require not ip 192.168.26.128 Require all granted </RequireAll></Directory>
虚拟主机有三类:
#设置主机名#编译安装的情况下[root@lynkser ~]# vim /etc/httpd24/httpd.conf #要编辑的内容和其他操作和yum安装是相同的#yum安装的情况下[root@lynkser ~]# vim /etc/httpd/conf/httpd.conf# ServerAdmin: Your address, where problems with the server should be# e-mailed. This address appears on some server-generated pages, such# as error documents. e.g. admin@your-domain.com#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 www.example.com:80#在文件最后加上如下内容:#virtual host 1 #虚拟主机1的配置<VirtualHost 192.168.26.129:80> #写自己的IP ServerName www.lynk.com #写自己的IP 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 192.168.26.129:80> #写自己的IP ServerName blog.lynk.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@lynkser var]# mkdir -p /var/www/html[root@lynkser var]# cd /var/www/html[root@lynkser html]# mkdir www blog[root@lynkser html]# lltotal 0drwxr-xr-x. 2 root root 6 Jan 16 01:34 blogdrwxr-xr-x. 2 root root 6 Jan 16 01:34 www[root@lynkser html]# chown -R apache.apache blog[root@lynkser html]# chown -R apache.apache www[root@lynkser html]# lltotal 0drwxr-xr-x. 2 apache apache 6 Jan 16 01:34 blogdrwxr-xr-x. 2 apache apache 6 Jan 16 01:34 www#创建网页[root@lynkser html]# echo "hello world www" > www/index.html [root@lynkser html]# echo "hello world blog" > blog/index.html #创建对应网页的日志目录[root@lynkser html]# mkdir -p /var/log/httpd/{www,blog}[root@lynkser html]# chown -R apache.apache /var/log/httpd/[root@lynkser html]# ll /var/log/httpd/total 0drwxr-xr-x. 2 apache apache 6 Jan 16 01:39 blogdrwxr-xr-x. 2 apache apache 6 Jan 16 01:39 www#启动服务#使用yum安装的情况下[root@lynkser ~]# systemctl start httpd#使用编译安装的情况下[root@lynkser ~]# /usr/local/apache/bin/apachectl start#查看有没有80端口[root@lynkser ~]# ss -antlState Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:111 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::111 :::* LISTEN 0 128 :::80 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::*
在客户机上打开浏览器验证
如果没有DNS,需要修改hosts,请自行修改,不做演示