Apache简介
主要特点
开放源代码,跨平台应用
支持多种网页编程语言
模块化设计,允行稳定,良好的安全性
编译安装httpd服务器
编译安装的优点
具有较大的自由度,功能可定制
可及时获得最新软件版本
普遍适用于大多数Linux版本,便于移植使用
[root@bogon ~]# rpm -qa | grep httpd #查询已安装得apache软件包
[root@bogon ~]# yum -y install gcc gcc-* make 安装gcc和make
[root@bogon ~]# yum -y install apr-util-devel pcre-devel zlib-devel #自动安装apr-util-devel,pcre-devel和zlib-devel
上传httpd-2.4.25软件包
[root@bogon ~]# md5sum httpd-2.4.25.tar.gz #校验文件哈希值
[root@bogon ~]# tar zxf httpd-2.4.25.tar.gz -C /usr/src/ #解压软件包
[root@bogon ~]# ls /usr/src/ #查看解压位置
[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-lite --enable-cgi #安装所需要的包
[root@bogon httpd-2.4.25]# make && make install
[root@bogon httpd-2.4.25]# ls /usr/local/httpd/
[root@bogon httpd-2.4.25]# echo $PATH
[root@bogon httpd-2.4.25]# ln -s /usr/local/httpd/bin/* /usr/local/bin/ #设置软链接
[root@bogon httpd-2.4.25]# apachectl -t #检测软连接是否设置成功
[root@bogon ~]# netstat -nlpt |grep 80 #过滤端口号
[root@bogon ~]# curl 192.168.93.143:80 #测试访问
[root@bogon ~]# 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
[root@bogon ~]# apachectl stop #关闭apache
[root@bogon ~]# systemctl start httpd
[root@bogon ~]# systemctl status httpd
[root@bogon ~]# systemctl enable httpd
[root@bogon ~]# systemctl is-enabled httpd
[root@bogon ~]# cd /usr/local/httpd/htdocs/
[root@bogon htdocs]# ls
[root@bogon htdocs]# vi index.html
构建虚拟机
虚拟web主机
在同一台服务器中运行多个web站点其中每个站点不独立占用一台真正的计算机
httpd支持的虚拟机类型
基于域名的虚拟主机
基于ip地址的虚拟主机
基于端口的虚拟主机
# 为虚拟主机准备网页文档
[root@bogon ~]# mkdir -p /var/www/html/kgccom
[root@bogon ~]# mkdir -p /var/www/html/kccecom
[root@bogon ~]# echo "<h1>www.kgc.com</h1>"> /var/www/html/kgccom/index.html
[root@bogon ~]# echo "<h1>www.kcce.com</h1>"> /var/www/html/kccecom/index.html
[root@bogon ~]# vi /usr/local/httpd/conf/extra/httpd-vhosts.conf 在里面添加内容
<VirtualHost *:80>
DocumentRoot "/var/www/html/kgccom"
ServerName www.kgc.com
ErrorLog "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.com
ErrorLog "logs/www.kcce.com.error_log"
CustomLog "logs/www.kcce.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
[root@bogon ~]# vi /usr/local/httpd/conf/httpd.conf #更改配置
[root@bogon ~]# systemctl restart httpd #重启服务
#打开一台桌面版centos
[root@bogon ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.93.143 www.kgc.com
192.168.93.143 www.kcce.com
然后打开浏览器访问
基于IP地址的虚拟主机
[root@bogon ~]# vi /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost 192.168.93.143:81>
DocumentRoot "/var/www/html/kgccom"
ServerName www.kgc.com
ErrorLog "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.193.143:82>
DocumentRoot "/var/www/html/kccecom"
ServerName www.kcce.com
ErrorLog "logs/www.kcce.com.error_log"
CustomLog "logs/www.kcce.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
[root@bogon ~]# vi /usr/local/httpd/conf/httpd.conf
在52行下添加
Listen 81
Listen 82
[root@bogon ~]# curl 192.168.93.143:82 #验证