前言:
最近整理一些以前的学习笔记。
过去都是存储在本地,此次传到网络留待备用。\
网页类型:
- 静态页面:html、mp3、flv、jpg、gif。(httpd默认)
- 动态页面:需要 服务端解释器 解释的页面,java、php、python、shell,(httpd不能解释)。
网页的目录结构:
层级目录
- 默认访问DocumentRoot下的index.html(/var/www/网站目录/index.html)
http://域名 == http://域名/index.html- 访问DocumentRoot下的其他目录(将列出该目录下的所有可读文档)
http://域名/目录名/- 访问DocumentRoot下的其他目录文件
http://域名/目录名/目录名/文件名.html
web服务器:把某个目录共享 放到网页文件.html
- 网站基于B/S(Brower/server)设计: 浏览器/服务器
- 默认网页文件格式: html
http网站
- 是Apache软件基金会的一个开放源代码的网页服务器。
- 具有跨平台性和安全性。
- 软件包:httpd
- 进程服务:httpd
- 默认端口:80
- 配置文件路径:
- 主配置文件: /etc/httpd/conf/httpd.conf
- 其他配置文件: /etc/httpd/conf.d/
- 默认根目录: /var/www/html(启动服务后自动生成)
- 网页文件路径: /var/www/目录名
- 默认索引: index.html
一、httpd配置文件
- 主配置文件: /etc/httpd/conf/httpd.conf
- 其他配置文件: /etc/httpd/conf.d/*
]# vim /etc/httpd/conf/httpd.conf #编辑主配置文件 ... Listen 80 #服务器监听端口 ... DocumentRoot "/var/www/html" #网站(文档)根目录,此处是默认网站路径 ... DirectoryIndex index.html #默认的首页名称。(目录索引) ... #ServerName www.example.com:80 #服务器的域名 ... IncludeOptional conf.d/*.conf #加载conf.d/下的所有配置文件。(包含可选)
一、静态网站
1 安装软件包:
]# yum -y install httpd #Apache基金会的httpd软件
2 启动服务:
]# systemctl start httpd #启动后,默认共享/var/www/html/目录
3 编辑网页文件:
]# cd /var/www/html
]# vim index.html #默认索引网页文件index.html
<html>
<head>
<title>头部标题</title>
</head>
<body>
<h1>标题1</h1>
<h2>标题2</h2>
<font color=red>字体颜色=红</font>
<marquee>横屏滚动</marquee>
<font size=100>字大小100</font>
</body>
</html>
4 配置防火墙:
]# firewall-cmd --permanent --add-service=http
5 客户端访问web服务
]# firefox http://127.0.0.1 #访问服务器ip或域名
二、配置动态网站:
- 支持PHP程序:软件包httpd、php
- 支持Python:软件包httpd、mod_wsgi(使httpd与python交互,wsgi=web服务网关接口)
配置python动态网站不使用默认端口:
1.安装软件包:
]# yum -y install httpd mod_wsgi #网站软件,python解释器
2.创建网站根目录:
]# mkdir /var/www/web00 #创建网站根目录
3.编辑py脚本网页:
]# vim /var/www/web00/py.wsgi
#!/usr/bin/env python
from time import time
def application (environ, start_response):
response_body = 'UNIX EPOCH time is now: {}\n'.format(time())
status = '200 OK'
response_headers = [('Content-Type', 'text/plain'),
('Content-Length', '1'),
('Content-Length', str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
4.配置文件:
]# vim /etc/httpd/conf.d/web00.conf #网站配置文件
Listen 8909 #监听的端口
<VirtualHost *:8909> #<虚拟主机 允许访问的主机:端口>
ServerName web00.exampl.com #网站域名
DocumentRoot /var/www/web00 #网站根目录
WSGIScriptAlias / /var/www/web00/py.wsgi #将/指向网站脚本
</VirtualHost>
5.配置端口或放宽SELinux:
]# yum provides semanage #查询提供semanage命令的软件
]# yum -y install policycoreutils-python #安装软件
]# semanage port -l | grep ^http_port #查看当前的web端口
http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
]# semanage port -a -t http_port_t -p tcp 8909 #添加端口
//使用selinux管理器命令修改端口配置
// -a 添加一条规则;-t 指定类型;-p 允许类型使用tcp的某端口
//selinux可以控制软件所用的端口,默认80(不加密默认端口)、443(加密默认端口)
]# semanage port -l | grep ^http_port #确认端口
http_port_t tcp 8909, 80, 81, 443, 488, 8008, 8009, 8443, 9000
6.重启服务:
]# systemctl restart httpd
7.客户端访问:
]# firefox http://域名:端口
]# curl http://web00.example.com:8909/
UNIX EPOCH time is now: 1562842060.94
]# curl http://web00.example.com:8909/
UNIX EPOCH time is now: 1562842075.41
三、网站目录ACL访问内容控制:
-
语法:(编辑网站的配置文件)
]# vim /etc/httpd/conf.d/网站配置文件.conf ... <Directory 被设置权限目录的绝对路径> Require all denied(或 granted ) #要求 全部 拒绝(或同意) 访问 Require ip IP地址或网段地址 ... #只同意 某ip或网段 访问 </Directory>
-
实例:
服务端: ]# mkdir -p /var/www/server/{private,deny} #创建网站目录 ]# echo "index" > /var/www/server/index.html #编辑测试页面 ]# echo "private" > /var/www/server/private/private.html ]# echo "denied" > /var/www/server/deny/deny.html ]# vim /etc/httpd/conf.d/server.conf #编辑网站配置文件 <VirtualHost *:80> #设置网站配置 DocumentRoot /var/www/server </VirtualHost> <Directory "/var/www/server"> #设置acl访问限制 Require all granted #根目录全部允许访问 </Directory> <Directory "/var/www/server/private"> Require ip 127.0.0.1 ::1 192.168.4.11 #子目录private只允许本机访问 </Directory> <Directory "/var/www/server/deny"> Require all denied #子目录deny拒绝全部访问 </Directory> ]# systemctl restart httpd #编辑配置文件后必须重启刷新服务 客户端测试: 11]# curl localhost index 12]# curl 192.168.4.11/ index 11]# curl localhost/private/private.html private 12]# curl 192.168.4.11/private/private.html You don't have permission to access /private/private.html 11]# elinks --dump 127.0.0.1/deny/deny.html You don't have permission to access /deny/deny.html on this server. 12]# curl 192.168.4.11/deny/deny.html You don't have permission to access /deny/deny.html
四、虚拟主机多网站
-
虚拟主机网站类型:
-
基于IP地址:要求服务器有多个ip
-
基于域名: 要求有多个域名
-
基于端口: 开放多端口
-
-
基于域名 配置多网站:
(1).编辑第一个网站
]# mkdir /var/www/网站1目录 #创建网站1的目录 ]# vim /etc/httpd/conf.d/网站1配置文件名.conf #编辑网站1的配置文件 <VirtualHost *:80> #<虚拟主机 ip:端口> ServerName 域名1 #服务器名称 DocumentRoot /var/www/网站1目录 #网站根目录 </VirtualHost> ]# vim /var/www/网站1目录/index.html #编辑网页文件 编写网页数据1
(2).编辑第二个网站
]# mkdir /var/www/网站2目录 ]# vim /etc/httpd/conf.d/网站2配置文件名.conf <VirtualHost *:80> ServerName 域名2 DocumentRoot /var/www/网站2目录 </VirtualHost> ]# vim /var/www/网站2目录/index.html 编写网页数据2
(3).重启httpd
]# systemctl restart httpd #编辑文件后需要重启刷新服务
(4).客户端测试访问
]# curl http://域名1 #网站1的页面 ]# curl http://域名2 #网站2的页面
五、使用自定义web目录
- 需要设置 自定义网站目录SELinux上下文件的情况
- 需要设置 网站内容访问控制
1.搭建基本网站
]# mkdir /myweb #创建自定义网站目录 ]# echo "myweb" > /myweb/index.html #编辑测试网页 ]# vim /etc/httpd/conf.d/myweb.conf #编辑基础网站配置文件 <VirtualHost *:80> DocumentRoot /myweb ServerName myweb.example.com </VirtualHost> ]# systemctl restart httpd #启服务 ]# firewall-cmd --permanent --add-service=http #设置防火墙 ]# firewall-cmd --reload
- 测试网站
]# elinks myweb.example.com 不能正常访问,显示测试页面
2.调整selinux安全上下文
]# ls -Zd /myweb drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /myweb 方法1:参照httpd标准目录,重设新目录的属性 ]# chcon -R --reference=/var/www /myweb ]# ls -Zd /myweb drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /myweb 方法2:将新目录增加到预设的标准Web目录范围 ]# semanage fcontext -a -t httpd_sys_content_t "/myweb(/.*)?" ]# ls -Zd /myweb #不能看到目录的安全上下文 drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /myweb ]# semanage fcontext -l | grep myweb #可以看到安全上下文的改变 /myweb all files system_u:object_r:httpd_sys_content_t:s0
- 测试网站:
]# elinks myweb.example.com 仍然不能查看
3.调整网站内容访问控制
]# vim /etc/httpd/conf.d/myweb.conf ... <Directory "/myweb"> Require all granted </Directory> ]# systemctl restart httpd #重启httpd服务
- 测试网站:
]# elinks myweb.example.com myweb #可以正常查看
- 测试网站