Apache简介
主要特点
开放源代码,跨平台应用
支持多种网页编程程序
模块化设计,运行稳定,良好得安全性
软件版本
1.X
目前最高版本是1.3,运行稳定
向下兼容性较好,但缺乏一些较新得功能
2.X
目前最高版本是2.4
具有更多的功能特性
与1.x相比,配置管理风格存在较大差异
编译安装得优点
具有较大得自由度,功能可定制
可及时获得最新的软件版本
普通适用于大多数Linux版本,便于移植使用
编译安装
1.查询是否具有apache服务,防止包冲突
rpm -qa | grep httpd
2.准备环境
yum -y install gcc gcc-* make
yum -y install apr-util-devel pcre-devel zlib-devel //安装apace需要得依赖包
由于CentOS7系统不维护了,下载会缓慢,可以更改为国内地址
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
3.上传软件包httpd得软件包并效验其完整性
[root@bogon ~]# ls
anaconda-ks.cfg httpd-2.4.25.tar.gz
[root@bogon ~]# md5sum httpd-2.4.25.tar.gz //效验完整性
24fb8b9e36cf131d78caae864fea0f6a httpd-2.4.25.tar.gz
[root@bogon ~]#
3.解压软件包,到指定路径并下载所需得模块功能
[root@bogon ~]# tar zxf httpd-2.4.25.tar.gz -C /usr/src/
[root@bogon ~]# ls /usr/src/
debug httpd-2.4.25 kernels
[root@bogon ~]# cd /usr/src/httpd-2.4.25/
[root@bogon httpd-2.4.25]# ./configure -prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-list --enable-cgi
4.编译安装
make && makeinstall
检测是否成功
echo $? 检测返回值是否是0
ls /usr/local/httpd/ 路径是否有对应得安装包
[root@bogon httpd-2.4.25]# echo $?
0
[root@bogon httpd-2.4.25]# ls /usr/local/httpd/
bin build cgi-bin conf error htdocs icons include logs man manual modules
[root@bogon httpd-2.4.25]#
5.启动服务
[root@bogon httpd-2.4.25]# ls /usr/local/httpd/bin/ //启动服务
ab apachectl apxs checkgid dbmmanage envvars envvars-std fcgistarter htcacheclean htdbm htdigest htpasswd httpd httxt2dbm logresolve rotatelogs
[root@bogon httpd-2.4.25]# ln -s /usr/local/httpd/bin/* /usr/local/bin //由于是源代码包,所以需要把他链接到环境变量中
[root@bogon httpd-2.4.25]# apachectl -t //检测服务配置文件是否正确
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK
6.调用apache http服务得脚本 监听80端口号是否启用
[root@bogon httpd-2.4.25]# apachectl
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
[root@bogon httpd-2.4.25]# netstat -nlpt | grep 80
tcp6 0 0 :::80 :::* LISTEN 18378/httpd
[root@bogon httpd-2.4.25]#
7.尝试通过 HTTP 或 HTTPS 协议从 IP 地址 192.168.27.131
的服务器上获取内容
curl http://192.168.27.131:80
1.关闭 : apachectl stop
8.启动systemctl 管理apache http(因为其缺少services文件,所以配置上)
cat > /lib/systemd/system/httpd.service << 'EOF'
[Unit]
Description=The Apache HTTP Server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/httpd/logs/httpd.pid
ExecStart= /usr/local/bin/apachectl $OPTIONS
ExecrReload= /bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
1.验证是否是开机自启动(启动服务,设置开机自启动)
[root@bogon ~]# systemctl is-enabled httpd
enabled
[root@bogon ~]#
虚拟主机
虚拟web主机
在同一台服务器中运行多个Web站点,其中每一个站点并不独立占用一台真正得计算机
httpd支持得虚拟主机类型
基于域名得虚拟主机
基于IP地址得虚拟主机
基于端口得虚拟主机
1.基于域名的虚拟主机
1.
# 为虚拟主机准备网页文档
mkdir -p /var/www/html/kgccom
mkdir -p /var/www/html/kccecom
echo "<h1>www.kgc.com</h1>"> /var/www/html/kgccom/index.html
echo "<h1>www.kcce.com</h1>"> /var/www/html/kccecom/index.html
2.修改文件
<VirtualHost *:80>DocumentRoot "/var/www/html/kgccom"ServerName www.kgc.comErrorLog "logs/www.kgc.com.error_log"CustomLog "logs/www.kgc.com.access_log" common
<Directory "/var/www/html">Require all granted
</Directory>
</VirtualHost><VirtualHost *:80>DocumentRoot "/var/www/html/kccecom"ServerName www.kcce.comErrorLog "logs/www.kcce.com.error_log"CustomLog "logs/www.kcce.com.access_log" common
<Directory "/var/www/html">Require all granted
</Directory>
</VirtualHost>
3.
4.检测上一步配置是否正确 apachectl -t
5.重启服务 systemctl restart httpd
6.开启一台桌面版验证
1.修改/etc/hosts文件
2.修改为对应自己得配置 3.在桌面版浏览器输入配置得地址查看验证
2.基于端口的虚拟主机
修改配置
<VirtualHost 192.168.27.131:81>DocumentRoot "/var/www/html/kgccom"ServerName www.kgc.comErrorLog "logs/www.kgc.com.error_log"CustomLog "logs/www.kgc.com.access_log" common
<Directory "/var/www/html">Require all granted
</Directory>
</VirtualHost><VirtualHost 192.168.27.131:82>DocumentRoot "/var/www/html/kccecom"ServerName www.kcce.comErrorLog "logs/www.kcce.com.error_log"CustomLog "logs/www.kcce.com.access_log" common
<Directory "/var/www/html">Require all granted
</Directory>
</VirtualHost>
修改主配置在53行左右,添加如图信息
重启httpd服务,验证
[root@bogon ~]# curl 192.168.27.131:81
<h1>www.kgc.com</h1>
[root@bogon ~]# curl 192.168.27.131:82
<h1>www.kcce.com</h1>
[root@bogon ~]#