搭建Apache HTTP服务器与认证
一、什么是Apache?
Apache(或httpd)服务,是Internet上使用最多的Web服务器技术之一,通俗来讲就是一个用于搭建网站的服务。
有两个版本:
- http:超文本传输协议,通过线路以明文形式发送,默认使用80端口/TCP
- https:经TLS/SSL安全加密的超文本传输协议,默认使用443端口/TCP
二、Apache的配置文件
1、配置文件的位置
配置文件 存放位置
服务目录 /etc/httpd
主配置文件 /etc/httpd/conf/httpd.conf
虚拟主机的配置文件目录
配置文件 存放位置
服务目录 /etc/httpd
主配置文件 /etc/httpd/conf/httpd.conf
虚拟主机的配置文件目录 /etc/httpd/conf.d
基于用户的配置文件 /etc/httpd/conf.d/userdir.conf
日志文件目录 /etc/httpd/logs
默认的网站数据目录 /var/www/html
2、主配置文件的重要参数
主配置文件:/etc/httpd/conf/httpd.conf
参数 作用 参数 作用
serverRoot 服务目录 Servername 网站服务器的域名
Listen 监听的IP地址端口号 DocumentRoot 默认网站数据目录
User 运行服务的用户 Directory 文件目录权限
Group 运行服务的用户组 DirectoryIndex 默认的索引页面
Serveradmin 管理员邮箱 ErrorLog 错误日志文件
三、如何搭建Apache服务器
基本环境:主机名、网卡网络、yum源
1、更改主机名
[root@localhost ~]# hostnamectl set-hostname Ayaka
[root@localhost ~]# bash
2、配置网络
(1)虚拟机NAT网段配置为192.168.123.0网段(可随意)、网卡适配器选择仅主机模式
(2)配置网卡:
需要修改的参数:
BOOTPROTO=static
IPADDR=192.168.123.101
NETMASK=255.255.255.0
(3)重新启动网络服务
[root@ayaka ~]# systemctl restart network
3、配置yum源
1、搭建简单的httpd服务
1.1、安装Apache服务
[root@ayaka ~]# yum install -y http
1.2、关闭防火墙
[root@ayaka ~]# systemctl stop firewalld
1.3启动Apache服务
[root@ayaka ~]# systemctl restart httpd
访问Apche网站
[root@ayaka ~]# curl 192.168.123.101
2、搭建基于用户的个人网站
·首先确定已经安装了httpd服务、
2.1.新建用户(用于基于该用户)
[root@localhost ~]# useradd ayaka
2.2.创建个人的网页文件
[root@localhost ~]# mkdir /home/ayaka/public_html
[root@localhost ~]# cd /home/ayaka/public_html/
[root@localhost ~]# echo “welcome to ayaka’s website” >>index.html
2.3.修改用户网页文件的访问权限
[root@localhost ~]# chmod -R 705 /home/ayaka
2.4.修改基于用户的配置文件.
[root@localhost ~]# vim /etc/httpd/conf.d/userdir.conf
修改第17行和24行
UserDir enable 改为开启,表示开启个人用户主页功能
UserDir public_html 去注释,表示网站数据在用户家目录中的名称
2.5.关闭防火墙修改selinux权限
[root@localhost public_html]# systemctl stop firewalld
[root@localhost public_html]# setenforce 0
2.6.重启服务
[root@localhost public_html]# systemctl restart httpd
2.7.访问网页
没有图形化:Curl httpd://192.168.123.101/~ayaka/
图形化:firefox //192.168.123.101/~ayaka/
或在主机浏览器搜索192.168.123.101/~ayaka/
3、搭建基于域名访问的虚拟主机
以“www.toto.com”为域名来创建一个虚拟网站
1.网站数据存放在/www/toto/下
2.网站主页内容为:“welcome to toto’s website”
3.网站对所有客户端开放
#老样子 首先还是确认安装了httpd服务
[root@localhost public_html]# rpm -q httpd
httpd-2.4.6-95.el7.centos.x86_64
3.1、创建虚拟主机的网页文件
[root@localhost public_html]# mkdir /www/toto -p
3.2、修改文件的访问权限(使其它用户具有可执行权力)
[root@localhost toto]# chmod o+x /www
[root@localhost toto]# chmod o+x index.html
3.3、配置虚拟主机的网页文件
[root@localhost toto]# cd /etc/httpd/conf.d
[root@localhost conf.d]# vim toto.conf
<Virtualhost 192.168.123.101>
ServerName www.toto.com //定义域名
DocumentRoot /www/toto //网站主页文件的目录
<Directory /www/toto>
require all granted //所有客户端都可以访问
</Directory>
</Virtualhost>
~
3.4、做域名解析文件
[root@localhost conf.d]# vim /etc/hosts
第三行添加 192.168.123.101 www.toto.com
3.5、配置防火墙和selinux
[root@localhost conf.d]# firewall-cmd --reload
[root@localhost conf.d]# firewall-cmd --permanent --add-service=http
3.6、重启服务
[root@localhost conf.d]# systemctl restart httpd
访问:
无图形化界面
Curl www.toto.com
又图形画界面
Firefox www.toto.com
4、搭建基于端口访问的虚拟主机
配置两个新的访问端口,分别为8088和8089
1.网站域名为 www.toto.com
2.网页数据分别存在/www/8088和/www/8089下面
3.每个端口主页内容分别为:“this is new port (8088或8089)for www.toto.com”
配置:
#首确安服
4.1、新建虚拟主机的网页文件
[root@localhost conf.d]# mkdir /www/8088 -p
[root@localhost conf.d]# mkdir /www/8089 -p
[root@localhost conf.d]# cd /www/8088
[root@localhost 8088]# echo "this is a new port 8088 for www.toto.com" >>index.html
[root@localhost 8088]# cd /www/8089
[root@localhost 8089]# echo "this is a new port 8089 for www.toto.com" >>index.html
4.2、修改文件的访问权限
[root@localhost 8089]# chmod o+x /www
[root@localhost 8089]# chmod o+x /www/8088/index.html
[root@localhost 8089]# chmod o+x /www/8089/index.html
4.3、配置虚拟主机的文件
[root@localhost conf.d]# vim 8088.conf
<Directory /www/8088/>
Require all granted
</Directory>
<VirtualHost 192.168.123.101:8088>
DocumentRoot /www/8088
Servername www.toto.com
</VirtualHost>
<VirtualHost 192.168.123.101:8089>
DocumentRoot /www/8089
ServerName www.toto.com
</VirtualHost>
4.4、添加监听端口
[root@localhost conf.d]# vim /etc/httpd/conf/httpd.conf
42 Listen 80
43 Listen 8088
44 Listen 8089
4.5、添加新的端口到防火墙(前面只是添加了服务,并没有添加新的端口)
[root@localhost conf.d]# firewall-cmd --add-port=8088/tcp
success
[root@localhost conf.d]# firewall-cmd --add-port=8089/tcp
success
[root@localhost conf.d]# firewall-cmd --reload
success
4.6、重启服务
systemctl restart httpd
5、搭建网站并完成认证
搭建网站并完成认证
以www.yoyo.com为域名创建一个虚拟网站
1.网页数据放在/www/yoyo 主页为yoyo.html
2.网页主内容为 “welcome to yoyo’s website”
3.创建用户webuser1.webuser2 密码为123,实现网站的认证访问,只有这两个用户才能访问
5.1、新建虚拟机的网页文件
[root@localhost ~]# mkdir /www/toto -p
[root@localhost ~]# cd /www/toto/
[root@localhost toto]# echo “welcome to toto’s website” >>index.html
5.2、修改文件的访问权限
[root@localhost toto]# chmod o+x /www
[root@localhost toto]# chmod o+x /www/toto/index.html
5.3、修改主文件
需要修改的参数
119 DocumentRoot "/www/toto/"
124 <Directory "/www/toto">
131 <Directory "/www/toto">
在服务目录的最后添加认证信息
355 <VirtualHost 192.168.123.101:80>
356 ServerName www.toto.com
357 DocumentRoot /www/toto
358 <Directory /www/toto>
359 AuthType basic
360 Authname passwd
361 AuthUserfile /etc/httpd/webpasswd
362 require user webuser1
363 </Directory>
364
365 </VirtualHost
5.4、配置防火墙和selinux
[root@localhost conf.d]# firewall-cmd --reload
[root@localhost conf.d]# firewall-cmd --permanent --add-service=http
5.5、重启服务
[root@localhost conf.d]# systemctl restart httpd
5.6、测试:
非图形化界面:
Curl 192.168.123.101
图形化界面
Firefox 192.168.123.101