一、Web 服务
1. Apache 软件包
- httpd-2.4.6-45.el7.x86_64.rpm
主程序包,服务器端必须安装该软件包 - httpd-devel-2.4.6-45.el7.x86_64.rpm
Apache 开发程序包 - httpd-manual-2.4.6-45.el7.x86_64.noarch.rpm
Apache 的手册文档和说明指南 - Apache 版本的更新一般要快于 Linux 内核的更新,要下载新的 Apache 版本,可到网站下载:
http://updates.redhat.com
http://www.apache.org
2. Web 服务器的安装与运行管理
安装 Apache 软件包:
yum -y install httpd
Apache 启动、重启、重新装载、关闭
systemctl start|stop|restart|reload|status httpd.service
设置自动启动
systemctl enable|disable httpd
检查是否运行了 httpd 进程
ps -ef | grep httpd
查看 httpd 运行的端口
ss -nutap | grep httpd
3. Apache 服务的测试
Apache服务启动后,可以在浏览器里输入以下地址,若可看到默认首 页,则工作正常。
http://ip 或者 http://127.0.0.1,该页面实际在 /var/www/error/noindex.html
二、搭建 Web 主服务器
1. 修改主配置文件
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
ServerRoot "/etc/httpd" // 31行,设置 Apache 的根目录为 /etc/httpd
Listen 80 // 42行,设置 httpd 监听端口 80
ServerAdmin root@localhost // 86行,设置管理员 E-mail 地址为 root@dyzx.com
ServerName 192.168.1.1:80 // 95行,设置Web服务器的主机名和监听端口为
DocumentRoot "/var/www/html" // 119行,设置网页文档的主目录为 /var/www/html
DirectoryIndex index.html // 164行,设置主页文件为 index.html
2. 自己建立网页
将制作好的网页文档存放在目录 /var/www/html 中,测试用首页建立如下:echo "Welcome to www.linux.com!!" >> /var/www/html/index.html
默认情况下,Web 服务要通过 TCP 协议的 80 端口对外通信,如果安装了防火墙,需要用以下命令打开 Web 服务的 80 端口或者停止防火墙后才可以从其他主机进行访问。
[root@localhost ~]# firewall-cmd --zone=public --add-service=http --permanent
success
[root@localhost ~]# firewall-cmd --reload
success
3. 测试配置文件
测试配置文件→重新启动httpd服务。
[root@localhost ~]# apachectl configtest
Syntax OK
[root@localhost ~]# service httpd restart
Redirecting to /bin/systemctl restart httpd.service
4. 测试
测试。在浏览器地址栏中输入“http://192.168.1.1”,便 可访问首页。
三、使用虚拟目录建子网站
虚拟目录有以下优点。
便于访问。
便于移动站点中的目录。
能灵活加大磁盘空间。
安全性好。
使用Alias选项可以创建虚拟目录。
1. 创建目录及首页文件
创建虚拟目录存放位置及虚拟目录默认首页文件
[root@localhost ~]# mkdir -p /xyz/rjgc
[root@localhost ~]# echo "Welcome to rjgc index" > /xyz/rjgc/index.html
2. 创建编辑配置文件
创建 、编辑虚拟目录子配置文件。默认情况下,位于 /etc/httpd/conf.d/ 目录下的所有以 .conf 结尾的文件都会被加载作为 Apache 的配置信息,为此,在 /etc/httpd/conf.d/ 下新建一个子配置文件(如 vdir.conf)来配 置虚拟目录
[root@localhost ~]# vim /etc/httpd/conf.d/vdir.conf
Alias /rjgcx "/xyz/rjgc" // 定义虚拟目录的别名为 /rjgcx,物理路径为 /xyz/rjgc
<Directory "/xyz/rjgc">
Options Indexes FollowSymLinks // 当所设目录下没有 index.html 文件时就显示目录结构
AllowOverride None // 禁止 .htaccess 文件覆盖配置
Require all granted // 授权允许所有访问
</Directory>
3. 修改安全上下文
若开启了 SELinux,且网页文件的存放目录不在标准位置(/var/www/)时,则必须将 /xyz/ 目录的安全上下文修改为 httpd_sys_content_t,才能有权访问其中的网页。
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t '/xyz(/.*)?'
[root@localhost ~]# restorecon -Rv /xyz/ // 将设置的默认上下文应用于指定的目录文件
restorecon reset /xyz context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
restorecon reset /xyz/rjgc context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
restorecon reset /xyz/rjgc/index.html context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
4. 重新启动服务
执行指令:systemctl restart httpd.service
5. 测试
测试。在浏览器地址栏中输入“http://192.168.1.1/rjgcx” 便可访问
四、使用虚拟主机建立多个网站
- 基于名称虚主机: 一个网卡 一个 IP 多个域名 多个网站
- 基于 IP 虚主机: 一个网卡 多个 IP 多个网站
- 基于端口号: 一个网卡 一个 IP 多个端口 多个网站
- 根据用户限制网站访问权限:
- 建立用户主页:
1. 基于域名的虚拟主机
名称 IP 地址 端口 域名 站点主目录 日志文件位置
Web_A 192.168.1.1 80 www1.xyz.edu /var/www/web1/ /etc/httpd/logs/
Web_B 192.168.1.1 80 www2.xyz.edu /var/www/web2/ /etc/httpd/logs/
1.1 创建目录与文件
创建所需的目录和默认首页文件
[root@localhost ~]# mkdir -p /var/www/web1 /var/www/web2
[root@localhost ~]# echo "this is www1.xyz.edu's web1" > /var/www/web1/index.html
[root@localhost ~]# echo "this is www2.xyz.edu's web2" > /var/www/web2/index.html
1.2 编辑配置文件
复制虚拟主机配置文件的样本文件→编辑虚拟主机配 置文件→重启httpd服务
[root@localhost ~]# cp /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf /etc/httpd/conf.d
[root@localhost ~]# vim /etc/httpd/conf.d/httpd-vhosts.conf
<VirtualHost 192.168.1.1>
DocumentRoot /var/www/web1
ServerName www1.xyz.edu
</VirtualHost>
<VirtualHost 192.168.1.1>
DocumentRoot /var/www/web2
ServerName www2.xyz.edu
</VirtualHost>
[root@localhost ~]# systemctl restart httpd.service
1.3 添加域名解析记录
为了实现用域名访问虚拟主机,在客户机上通过编辑 hosts 文件添加域名解析记录。若是 Linux 客户机,则修改 “/etc/hosts”
[root@localhost ~]# vim /etc/hosts
192.168.1.1 www.web1.com www.web2.com
1.4 访问
在客户机启动浏览器,在地址栏键入分别输入两个虚拟主机的域名,则会显示各自的网站首页,如图所示
2. 基于端口号的虚拟主机
名称 IP 地址 端口 域名 站点主目录
Web_A 192.168.1.1 80 www1.xyz.edu /var/www/web1/
Web_C 192.168.1.1 8088 www2.xyz.edu /var/www/web2/
2.1 创建目录与文件
创建 Web_C 站点的主目录(Web_A 站点的主目录不变)和两个首页文件
[root@localhost ~]# mkdir -p /var/www/web3
[root@localhost ~]# echo "this is port80's web" > /var/www/web1/index.html
[root@localhost ~]# echo "this is port8088's web" > /var/www/web3/index.html
2.2 添加监听端口
编辑 httpd.conf 配置文件,添加 httpd 监听端口 8088
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
Listen 80
Listen 8088
2.3 编辑配置文件
编辑虚拟主机配置文件 /etc/httpd/conf.d/httpd- vhost.conf →重新启动 httpd 服务。
[root@localhost ~]# vim /etc/httpd/conf.d/httpd-vhosts.conf
// 以下4行在前面已添加,可保持不变
<VirtualHost 192.168.1.1:80>
DocumentRoot /var/www/web1/
</VirtualHost>
// 在文件末尾添加以下4行:
<VirtualHost 192.168.1.1:8088>
DocumentRoot /var/www/web3/
</VirtualHost>
[root@localhost ~]# systemctl restart httpd.service // 此时重启httpd可能会失败
Job for httpd.service failed because the control process exited with error code.
See "systemctl status httpd.service" and "journalctl -xe" for details.
2.4 允许在端口允许服务
在 http_port_t 端口标签中添加 8088 端口,以允许 http 服务运行在 8088 端口上。
[root@localhost ~]# firewall-cmd --zone=public --add-port=8088/tcp --permanent
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# semanage port -a -t http_port_t -p tcp 8088
2.5 在客户端访问站点
在客户端访问站点。必须在域名或 IP 地址后面加上端口号(使用 80 端口的 Web 站点可省),如,http://192.168.1.1:8088
2.6 访问测试
访问测试。在 Linux/Windows 客户端访问站点时,必须在 IP 地址或域名后面加上端口号(使用 80 端口的Web 站点可省)
[root@localhost ~]# curl -k http://192.168.1.1:80
this is port80's web
[root@localhost ~]# curl -k http://192.168.1.1:8088
this is port8088's web
3. 基于 IP 地址的虚拟主机
名称 IP 地址 端口 域名
Web_A 192.168.1.1 80 /var/www/web1/
Web_D 192.168.1.2 80 /var/www/web4/
3.1 绑定 IP 地址
为服务器上的一块网卡绑定第 2 个 IP 地址→重启网络服务
{root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
...... // 其它行省略
IPADDR=192.168.1.1
PREFIX=24
IPADDR1=192.168.1.2
PREFIX1=24
[root@localhost ~]# systemctl restart network
3.2 创建目录和文件
创建 Web 站点网页文档的主目录和默认首页文件
[root@localhost ~]# mkdir -p /var/www/web4
[root@localhost ~]# echo "this is 192.168.1's web" > /var/www/web1/index.html
[root@localhost ~]# echo "this is 192.168.2's web" > /var/www/web4/index.html
3.3 编辑配置文件
编辑虚拟主机配置文件 /etc/httpd/conf.d/httpd-vhost.conf,设置基于 IP 地址的虚拟主机→重新启动 httpd 服务
[root@localhost ~]# vim /etc/httpd/conf.d/httpd-vhosts.conf
<VirtualHost 192.168.1.1>
ServerName 192.168.1.1
DocumentRoot /var/www/web1/
</VirtualHost>
<VirtualHost 192.168.1.2>
ServerName 192.168.1.2
DocumentRoot /var/www/web4/
</VirtualHost>
[root@localhost ~]# systemctl restart httpd.service
3.4 测试
测试。在同网段的客户端启动浏览器→在地址栏中先后键入 http://192.168.1.1 和 http://192.168.1.2 访问地址→观察各自的页面能否显示
五、Web 服务的访问控制
1. 基于用户的访问控制
实例:在别名为 “/lt” 的虚拟目录上,配置其只允许 user1 和 user2 等认证用户访问
1.1 创建用户
使用 htpasswd 工具创建 user1 用户,并保存在 .ltuser文件中
[root@localhost ~]# htpasswd -c /etc/httpd/.ltuser user1
New password:
Re-type new password:
Adding password for user user1
[root@localhost ~]# cat /etc/httpd/.ltuser
user1:$apr1$l9adoPSc$amIH/S5cGibijjH9GL09Z/
- htpasswd 命令中的 “-c” 选项表示无论认证文件是否存在,都重新写入文件并删除文件中原有的内容,因此,向认证文件中添加第二个用户时,就不要再使用 “-c” 选项了。若要修改 user1 用户的密码,可使用 “htpasswd -m .ltuser user1” 命令实现
1.2 创建目录和文件
创建物理目录路径及用于测试的虚拟目录默认首页文件
[root@localhost ~]# mkdir -p /xyz/lt
[root@localhost ~]# echo "Welcome to lt" > /xyz/lt/index.html
1.3 添加用户认证授权访问
设置虚拟目录并对其添加用户认证授权访问→重 新启动 httpd 服务
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf ……//在文件末尾添加以下各行:
Alias /lt "/xyz/lt” // 定义虚拟目录的别名为 /lt,物理路径为 /xyz/lt
<Directory "/xyz/lt">
AuthType Basic
AuthName "This is ltuser directory,Please Login:"
AuthUserFile /etc/httpd/.ltuser
Require valid-user
</Directory>
[root@localhost ~]# systemctl restart httpd.service