httpd/apache编译安装配置详解

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpd/apache编译安装配置详解相关的知识,希望对你有一定的参考价值。

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


httpd 特性

  1. 高度模块化:Core+Modules
  2. DSO:Dynamic Shared Ojbect动态加/卸载
  3. MPM:Multi-processing Module多路处理模块
  4. 虚拟主机:IP,Port,FQDN
  5. CGI:通用网关接口
  6. 反向代理(和正向代理的区别:正向代理距离客户端近,加速客户端的访问速度;反向代理距离服务器近,可以做调度,把client的请求转发到websrv集群中的某一个client<--->proxy(cache)<--->reverse proxy<--->websrv1,websrv2,sebsrnv)
  7. 负载均衡
  8. 路径别名
  9. 双向认证
  10. 支持第三方模块

httpd常用的配置
在httpd中实现虚拟主机

所谓的虚拟主机是指在通过在配置文件中提供不同的配置,从而可以实现在同一台物理服务器上提供多个站点的访问路径,实现方式有三种,分别是:

IP地址相同,监听的端口不同,通过不同的端口号来访问
IP地址不同,端口可以相同,通过不同的IP来访问
主机名不同,端口号和IP地址可以相同,通过不同的主机名称来访问。
使用虚拟主机的前提是关闭中心主机功能,即将主配置文件中的DocumentRoot这一指令注释。


Apache服务器默认在80端口监听
一台机器可以有1到65535号端口,一个端口代表2个字节

Netstat -an 该命令用来查询本机器有哪些端口正在被监听

Netstat -anb 该命令用来查询本机器有哪些端口正在被监听及其对应的应用程序
端口中的1-1024号叫做有名端口,这些端口一般不要用,他们已经分配好了


Apache如何配置端口:
Apache软件的端口是在httpd.conf文件中配置的,该文件在Apache目录下的conf文件下。在该文件中可以修改端口,修改后重新启动Apache,就生效。

控制访问法则


法则 功能
Require all granted 允许所有主机访问
Require all deny 拒绝所有主机访问
Require ip IPADDR 授权指定来源地址的主机访问
Require not ip IPADDR 拒绝指定来源地址的主机访问
Require host HOSTNAME 授权指定来源主机名的主机访问
Require not host HOSTNAME 拒绝指定来源主机名的主机访问
Require not ip 拒绝指定ip主机访问


httpd编译安装
实验环境说明:

主机名 IP
[[email protected] ~] 192.168.47.12.24

准备环境,将防火墙和selinux

[[email protected] ~]# setenforce 0
[[email protected] ~]#  systemctl stop firewalld

安装开发环境

[[email protected] ~]# yum groupinstall "Development Tools" 

创建apache组和用户apache

[[email protected] ~]# groupadd -r apache
[[email protected] ~]# useradd -M -s /sbin/nologin -g apache apache
[[email protected] ~]# id apache
uid=1000(apache) gid=996(apache) 组=996(apache)

安装相关的软件包

[[email protected] ~]# yum -y install openssl-devel pcre-devel expat-devel libtool

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

[[email protected] ~]# yum -y install wget
[[email protected] ~]# wget http://mirrors.shu.edu.cn/apache//apr/apr-1.6.3.tar.bz2
[[email protected] ~]# wget http://mirrors.shu.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2

解压下载安装apr-1.4和apr-util-1.4+的压缩包

[[email protected] ~]# tar xf apr-1.6.3.tar.bz2 
[[email protected] ~]# tar xf apr-util-1.6.1.tar.bz2 
[[email protected] ~]# ls
anaconda-ks.cfg  apr-1.6.3  apr-1.6.3.tar.bz2  apr-util-1.6.1  apr-util-1.6.1.tar.bz2

进入apr-1.6.3将修改configure配置文件

[[email protected] ~]# cd apr-1.6.3/
[[email protected] apr-1.6.3]# vim configure
cfgfile=${ofile}T
    trap "$RM "$cfgfile"; exit 1" 1 2 15
    #$RM "$cfgfile"      //将此行加入注释,或者删除此行

编译安装

[[email protected] apr-1.6.3]#  ./configure --prefix=/usr/local/apr
[[email protected] apr-1.6.3]# make && make install
[[email protected] apr-1.6.3]# cd /usr/src/apr-util-1.6.1
[[email protected] apr-util-1.6.1]#  ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[[email protected] apr-util-1.6.1]# make && make install

编译安装httpd

[[email protected] ~]# wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.34.tar.bz2
[[email protected] ~]# ls
anaconda-ks.cfg  httpd-2.4.34.tar.bz2
[[email protected] ~]# tar xf httpd-2.4.34.tar.bz2
[[email protected] ~]# cd httpd-2.4.34/
[[email protected] 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

当相同IP不同端口时

[[email protected] ~]# vim /etc/httpd24/httpd.conf
//找到ServerName www.example.com:80 取消#号注释

//找到Listen 80  在下面添加不同端口 Listen 81
//在最后面添加下面如下内容
<VirtualHost 192.168.47.12:80>
   DocumentRoot "/usr/local/apache/htdocs/yan"
   ErrorLog "logs/yan/error_log"
   CustomLog "logs/yan/access_log" combined
   <Directory "/usr/local/apache/htdocs/yan">
        <RequireAll>
        Require all granted
        </RequireAll>
    </Directory>
</VirtualHost>

<VirtualHost 192.168.47.12:81>
   DocumentRoot "/usr/local/apache/htdocs/yyl"
   ErrorLog "logs/yyl/error_log"
   CustomLog "logs/yyl/access_log" combined
   <Directory "/usr/local/apache/htdocs/yyl">
        <RequireAll>
        Require all granted
        </RequireAll>
    </Directory>
</VirtualHost>
[[email protected] ~]# tail -25 /etc/httpd24/httpd.conf
[[email protected] ~]# cd /usr/local/apache/logs/        //建立与httpd主配置文件相同路径的目录

[[email protected] logs]# mkdir yan
[[email protected] logs]# mkdir yyl
[[email protected] logs]# cd /usr/local/apache/htdocs/      //在网站存放目录下 也创建与之相同的目录

[[email protected] htdocs]# mkdir yan
[[email protected] htdocs]# mkdir yyl
[[email protected] htdocs]#  chown -R apache.apache /usr/local/apache/htdocs/
//给网站存放的目录更改属主属组为apache
[[email protected] htdocs]# echo ‘hello yan‘ > yan/index.html
[[email protected] htdocs]# echo ‘hello yyl‘ > yyl/index.html
[[email protected]nyinglai3 htdocs]# cd /usr/local/apache/bin/
[[email protected] bin]# ./apachectl start
[[email protected] bin]# ./apachectl -t

客户端验证
技术分享图片技术分享图片

不同IP相同端口

[[email protected] bin]# vim /etc/httpd24/httpd.conf
<VirtualHost 192.168.47.12:80>
   DocumentRoot "/usr/local/apache/htdocs/yan" 
   ErrorLog "logs/yan/error_log"
   CustomLog "logs/yan/access_log" combined
   <Directory "/usr/local/apache/htdocs/yan">
        <RequireAll>
        Require all granted
        </RequireAll>
    </Directory>
</VirtualHost>

<VirtualHost 192.168.47.13:80>  
   DocumentRoot "/usr/local/apache/htdocs/yyl"
   ErrorLog "logs/yyl/error_log"
   CustomLog "logs/yyl/access_log" combined
   <Directory "/usr/local/apache/htdocs/yyl">
        <RequireAll>
        Require all granted
        </RequireAll>
    </Directory>
</VirtualHost>
[[email protected] bin]# ip addr add 192.168.47.13/24 dev ens32
建立与编辑文件对应的临时ip
[[email protected] ~]# pkill  httpd
[[email protected] ~]#  /usr/local/apache/bin/httpd
[[email protected] ~]# ss -antl
[[email protected] ~]# cd /usr/local/apache/bin/
[[email protected] bin]# ./apachectl start
httpd (pid 62137) already running
[[email protected] bin]# ./apachectl -t
Syntax OK

客户端检测
技术分享图片技术分享图片

.相同IP相同端口不同域名
[[email protected] ~]# vim /etc/httpd24/httpd.conf
<VirtualHost 192.168.47.12:80>
ServerName www.yanyinglai.com:80
DocumentRoot "/usr/local/apache/htdocs/yan"
ErrorLog "logs/yan/error_log"
CustomLog "logs/yan/access_log" combined
<Directory "/usr/local/apache/htdocs/yan">
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>

<VirtualHost 192.168.47.12:80>
ServerName www.yyl.com:80
DocumentRoot "/usr/local/apache/htdocs/yyl"
ErrorLog "logs/yyl/error_log"
CustomLog "logs/yyl/access_log" combined
<Directory "/usr/local/apache/htdocs/yyl">
<RequireAll>
Require all granted
</RequireAll>
</Directory>
</VirtualHost>

[email protected] ~]# pkill httpd
[[email protected] ~]# /usr/local/apache/bin/httpd
[[email protected] ~]# ss -antl
[[email protected] ~]# cd /usr/local/apache/bin/
[[email protected] bin]# ./apachectl start
httpd (pid 62137) already running
[[email protected] bin]# ./apachectl -t
Syntax OK

客户端检测
在windows电脑上修改, C:WindowsSystem32driversetc 文件
技术分享图片

技术分享图片技术分享图片

以上是关于httpd/apache编译安装配置详解的主要内容,如果未能解决你的问题,请参考以下文章

Apache httpd(apache2)服务配置详解,Mac下设置虚拟主机部署多个web项目,及反向代理部署Java项目

基于Linux搭建Apache网站服务配置详解

编译安装LAMP环境:httpd-2.4+mysql5.6+php5.5

httpd源码编译安装

CentOS Apache 安装 配置 启动

编译安装httpd apache服务器