分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

httpd/apache编译安装配置详解

发布时间:2023-09-06 02:09责任编辑:彭小芳关键词:apache配置http编译
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
[root@yanyinglai3 ~]192.168.47.12.24

准备环境,将防火墙和selinux

[root@yanyinglai3 ~]# setenforce 0[root@yanyinglai3 ~]# ?systemctl stop firewalld

安装开发环境

[root@yanyinglai3 ~]# yum groupinstall "Development Tools" 

创建apache组和用户apache

[root@yanyinglai3 ~]# groupadd -r apache[root@yanyinglai3 ~]# useradd -M -s /sbin/nologin -g apache apache[root@yanyinglai3 ~]# id apacheuid=1000(apache) gid=996(apache) 组=996(apache)

安装相关的软件包

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

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

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

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

[root@yanyinglai3 ~]# tar xf apr-1.6.3.tar.bz2 [root@yanyinglai3 ~]# tar xf apr-util-1.6.1.tar.bz2 [root@yanyinglai3 ~]# lsanaconda-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配置文件

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

编译安装

[root@yanyinglai3 apr-1.6.3]# ?./configure --prefix=/usr/local/apr[root@yanyinglai3 apr-1.6.3]# make && make install[root@yanyinglai3 apr-1.6.3]# cd /usr/src/apr-util-1.6.1[root@yanyinglai3 apr-util-1.6.1]# ?./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr[root@yanyinglai3 apr-util-1.6.1]# make && make install

编译安装httpd

[root@yanyinglai3 ~]# wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.34.tar.bz2[root@yanyinglai3 ~]# lsanaconda-ks.cfg ?httpd-2.4.34.tar.bz2[root@yanyinglai3 ~]# tar xf httpd-2.4.34.tar.bz2[root@yanyinglai3 ~]# cd httpd-2.4.34/[root@yanyinglai3 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不同端口时

[root@yanyinglai3 ~]# 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>[root@yanyinglai3 ~]# tail -25 /etc/httpd24/httpd.conf[root@yanyinglai3 ~]# cd /usr/local/apache/logs/ ???????//建立与httpd主配置文件相同路径的目录[root@yanyinglai3 logs]# mkdir yan[root@yanyinglai3 logs]# mkdir yyl[root@yanyinglai3 logs]# cd /usr/local/apache/htdocs/ ?????//在网站存放目录下 也创建与之相同的目录[root@yanyinglai3 htdocs]# mkdir yan[root@yanyinglai3 htdocs]# mkdir yyl[root@yanyinglai3 htdocs]# ?chown -R apache.apache /usr/local/apache/htdocs///给网站存放的目录更改属主属组为apache[root@yanyinglai3 htdocs]# echo ‘hello yan‘ > yan/index.html[root@yanyinglai3 htdocs]# echo ‘hello yyl‘ > yyl/index.html[root@yanyinglai3 htdocs]# cd /usr/local/apache/bin/[root@yanyinglai3 bin]# ./apachectl start[root@yanyinglai3 bin]# ./apachectl -t

客户端验证

不同IP相同端口

[root@yanyinglai3 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>[root@yanyinglai3 bin]# ip addr add 192.168.47.13/24 dev ens32建立与编辑文件对应的临时ip[root@yanyinglai3 ~]# pkill ?httpd[root@yanyinglai3 ~]# ?/usr/local/apache/bin/httpd[root@yanyinglai3 ~]# ss -antl[root@yanyinglai3 ~]# cd /usr/local/apache/bin/[root@yanyinglai3 bin]# ./apachectl starthttpd (pid 62137) already running[root@yanyinglai3 bin]# ./apachectl -tSyntax OK

客户端检测

.相同IP相同端口不同域名
[root@yanyinglai3 ~]# 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>

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

客户端检测
在windows电脑上修改, C:\Windows\System32\drivers\etc 文件

httpd/apache编译安装配置详解

原文地址:http://blog.51cto.com/13910274/2158187

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved