Linux -->
-->
nginx
.....
python
django
flask
webpy
java
jetty
windows -->
IIS
www.baidu.com
--> dns
--> C --> www.baidu.com IP ..
--> S --> www.baidu.com IP ..
--> s --> C
-->
HTTP:超文本传输协议
http/0.9:仅仅纯文本(超链接),ASCII
HTML:标记语言
<h2>title</h2>
browser:客户端
返回解析之后的结果
192.168.1.1 web a.html
192.168.1.2 web a.html
URI:为了识别在全球(不仅仅包含互联网)上不同文件的一种方式,统一资源标识符
统一:路径格式上的统一
子对象:
URL:统一资源定位符, (专门标识互联网资源)
protocol://HOST:port/path/to/file
http://www.uplooking.com/download/my.tar.gz
web资源:
通过URL访问到的文件
http://www.uplooking.com/1.jpg
http://www.uplooking.com/2.txt
http://www.uplooking.com/3.pdf
多个资源整合为一个html文档
html文档:就是可以实现将分散在多台服务器上的资源集合起来统一展现为一个页面
资源访问的方法(HTTP方法):
http/0.9:GET
http/1.0:GET GET/POST PUT/DELETE HEAD
MIME:multipurpose internet Mail extension 多用途互联网邮件扩展
传输前将非文本格式编码为文本格式,接收方用相反的方式还原原来格式,还能调用相应程序查看此文件
SMTP:简单邮件传输协议,早期只能传输文本
协议首部:
告诉客户端文件格式
image/jpeg
客户端浏览器:
通过插件去解析
动态效果
Java, Applet, JRE
后面出现了动态页面技术
在服务器端存储的文档非html格式,通常是编程语言开发的脚本
将客户端属性以参数方式传递给脚本,执行一次,完成后生成html格式文档,并发送给客户端
web服务器:不会去执行这些脚本,需要调用外部工具来执行脚本
web index.php
web------>protocol----->php(运行index.php)------>html------>web
动态网页:包含静态内容和动态内容
动态内容:才需要运行
http/1.0:引入MIME和动态技术,才有了更好的多媒体展示
缓存
www.uplooking.com<---->到达web<----->用户进程通过套接字(监听)进行接收(阻塞,非阻塞)
|
DNS(递归,迭代)
http首部:
GET /2.html
HOST:www.uplooking.com(为虚拟主机做准备)
http报文:
request:<method> <request-URL> <version>
<headers>
//空白行必须
<entity-body>
response:<version> <status-code> <reason-phrase>
<headers>
//空白行必须
<entity-body>
status-code:
1XX:纯信息
2XX:成功类信息 200:正常相应
3XX:重定向信息 301:永久重定向 302:临时重定向 304:没有放生任何改变
4XX:客户端错误 404:找不到
5XX:服务端错误
web服务器的主要操作
1.建立连接:接受或拒绝客户端连接请求
2.接收请求:通过网络读取HTTP请求报文
3.处理请求:解析请求报文并做出相应动作
4.访问资源-访问请求报文中相关资源
5.构建响应:使用正确的首部生成HTTP相应报文
6.发送响应:想客户端发送生成的响应报文
7.记录日志:当已经完成的HTTP事务记录进日志文件
缓存:
5s 6 images,4 css,9 html
19个资源
每个资源需要单独请求
为了快速访问资源:客户端(浏览器)基本都是多进程的和支持本地缓存
http: tcp 三次握手 四次断开
http/1.1:
加强缓存管理功能
引入长连接概念
请求之后不在请求:空闲超时
一直请求:请求多少次数之后重新进行连接
如何响应多个用户的请求
单进程(线程)服务模型:用户请求排队
多进程(线程)服务模型:父进程----->多个子进程--->交给子进程处理
基于事件驱动服务模型:
水平:一个进程处理多个请求(轮询处理请求状态,并有请求状态通知一次)
边缘:一个进程处理多个请求(轮询处理请求状态,并有请求状态通知多次)
一个进程处理多个请求(轮询处理请求状态,并有请求状态定点通知)
第二种和第三种整合起来:进程----->多个子进程--->交给子进程处理----->一个进程处理多个请求(轮询处理请求状态,并有请求状态通知)
http://www.netcraft.com/
httpd:
事先创建进程
按需维持适当进程
模块设计:核心比较小,各种功能添加模块
支持运行配置,支持单独编译模块
支持多种方式虚拟主机配置
socket ip:port
虚拟主机:
基于ip虚拟主机
基于端口虚拟主机
基于域名虚拟主机
支持https:
支持用户认证:
支持基于ip或主机的acl
支持目录acl
支持URL重写
1.安装
rpm:yum install httpd(MPM:prefork)
源码:实际生产环境中 http://httpd.apache.org/
2.启动
/etc/init.d/httpd start|stop|restart
[root@salt-master ~]# ps aux|grep httpd
root 9876 0.0 1.7 234964 8296 ? Ss 15:52 0:00 /usr/sbin/httpd----------master process
apache 9878 0.0 1.0 234964 4880 ? S 15:52 0:00 /usr/sbin/httpd----------worker process
apache 9879 0.0 1.0 234964 4864 ? S 15:52 0:00 /usr/sbin/httpd----------worker process
apache 9880 0.0 1.0 234964 4864 ? S 15:52 0:00 /usr/sbin/httpd----------worker process
apache 9881 0.0 1.0 234964 4864 ? S 15:52 0:00 /usr/sbin/httpd----------worker process
apache 9882 0.0 1.0 234964 4864 ? S 15:52 0:00 /usr/sbin/httpd----------worker process
apache 9883 0.0 1.0 234964 4864 ? S 15:52 0:00 /usr/sbin/httpd----------worker process
apache 9884 0.0 1.0 234964 4864 ? S 15:52 0:00 /usr/sbin/httpd----------worker process
apache 9885 0.0 1.0 234964 4864 ? S 15:52 0:00 /usr/sbin/httpd----------worker process
prot:80
port:443
配置文件:
/etc/httpd/ 工作目录
/etc/httpd/conf/httpd.conf 主配置文件通过include包含进来
/conf.d/子配置文件
/etc/httpd/modules 模块目录
/etc/httpd/logs 日志目录(access.log,error.log)
网页根目录:
/var/www/html/
web服务器能够与额外工具通信的目录
/var/www/cgi-bin/ (common gateway Interface)
client---->httpd(index.cgi)---->spawn process----->httpd---->client
perl,python,java(servlet,JSP),php
访问:浏览器中输入:192.168.1.200就可以看到欢迎页面
这个配置文件在/etc/httpd/conf.d/welcome.conf中
移除它只要不以conf结尾即可
[root@salt-master conf.d]# mv welcome.conf welcome.conf.bak
[root@salt-master conf.d]# /etc/init.d/httpd reload
Reloading httpd:
再访问发现页面不是欢迎页面了
编辑html页面
[root@salt-master html]# pwd
/var/www/html
[root@salt-master html]# vim a.html
编辑html文件不用重启httpd服务器
在访问看到是我们刚才的文件点击他就可以看到我们刚才编辑的网页展示了
[root@salt-master conf]# grep "Section" httpd.conf
### Section 1: Global Environment
### Section 2: 'Main' server configuration
### Section 3: Virtual Host
注意2和3不能同时存在
错误页面中最下面显示信息
Not Found
The requested URL /4.html was not found on this server.
Apache/2.2.15 (CentOS) Server at 192.168.1.200 Port 80
ServerTokens OS
#ServerTokens OS|Full|Major|Minor|Prod
具体某个指令和参数是什么可以通过http://httpd.apache.org/进行文档查看
也可以安装httpd-manual进行查看
http://192.168.1.200/manual
Timeout 60
TCP三次握手链接超时时间
KeepAlive Off
是否使用长连接
MaxKeepAliveRequests 100
长连接请求次数,0为无限制
KeepAliveTimeout 15
长连接空闲超时时间
MPM
多道处理模块
# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# ServerLimit: maximum value for MaxClients for the lifetime of the server
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves
预先生成进程,一个请求用一个进程响应
<IfModule prefork.c>
StartServers 8 启动空闲进程
MinSpareServers 5 最少空闲进程
MaxSpareServers 20 最大空闲进程
ServerLimit 256 为MaxClients指定上限值
MaxClients 256 最大请求数
MaxRequestsPerChild 4000 一个进程最多响应多少次请求
</IfModule>
# worker MPM
# StartServers: initial number of server processes to start
# MaxClients: maximum number of simultaneous client connections
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestsPerChild: maximum number of requests a server process serves
一个请求用一个线程响应
<IfModule worker.c>
StartServers 4
MaxClients 300
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>
列出默认支持的核心模块
[root@salt-master html]# httpd -l
Compiled in modules:
core.c
prefork.c
http_core.c
mod_so.c
[root@salt-master html]# httpd.worker -l
Compiled in modules:
core.c
worker.c
http_core.c
mod_so.c
要使用worker模型
[root@salt-master html]# vim /etc/sysconfig/httpd
# Configuration file for the httpd service.
#
# The default processing model (MPM) is the process-based
# 'prefork' model. A thread-based model, 'worker', is also
# available, but does not work with some modules (such as PHP).
# The service must be stopped before changing this variable.
#
#HTTPD=/usr/sbin/httpd.worker
#
# To pass additional options (for instance, -D definitions) to the
# httpd binary at startup, set OPTIONS here.
#
#OPTIONS=
#
# By default, the httpd process is started in the C locale; to
# change the locale in which the server runs, the HTTPD_LANG
# variable can be set.
#
#HTTPD_LANG=C
#
# By default, the httpd process will create the file
# /var/run/httpd/httpd.pid in which it records its process
# identification number when it starts. If an alternate location is
# specified in httpd.conf (via the PidFile directive), the new
# location needs to be reported in the PIDFILE.
#
#PIDFILE=/var/run/httpd/httpd.pid
打开即可
还有另外一种event,一个进程处理多个请求2.2默认使用prefork
Listen 80
Listen 8080
Listen 192.168.1.200:8081
监听端口
LoadModule auth_basic_module modules/mod_auth_basic.so
.....
...
加载模块
Include conf.d/*.conf
包含配置文件
User apache
Group apache
用户和组
以上是section 1 全局环境
ServerAdmin root@localhost
站点管理员,可以每站点设置
ServerName localhost
服务器名字
DocumentRoot "/var/www/html"
网页文档根目录
<Directory "/var/www/html">
Options Indexes FollowSymLinks
DirectoryIndex index.html index.html.var index.php
AllowOverride None
Order allow,deny
Allow from all
</Directory>
对/var/www/html这个目录进行属性设置
Options:
Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
默认 None
Indexes:没有主页作为索引显示出来
FollowSymLinks:跟随符号连接
Includes:允许执行服务端包含(SSI)
SymLinksifOwnerMatch:允许符号连接已httpd进程用户访问
ExecCGI:是否允许执行cgi脚本
MultiViews:内容协商
ALL:启用所有选项
AllowOverride:
FileInfo AuthConfig Limit
允许覆盖
Order allow,deny 定义acl顺序
Allow from all
AllowOverride AuthConfig
AuthType basic
AuthName ""restricted files"
AuthUserFile /etc/httpd/passwords
Require user yys
#Require valid-user 只要是passwords中的用户就可以访问
[root@salt-master html]# htpasswd -c -m /etc/httpd/passwords yys
New password:
Re-type new password:
Adding password for user yys
[root@salt-master html]# htpasswd -m /etc/httpd/passwords linux
New password:
Re-type new password:
Adding password for user linux
指定默认页面
[root@salt-master conf]# httpd -t
Syntax OK
AllowOverride AuthConfig
AuthType basic
AuthName ""restricted files"
AuthUserFile /etc/httpd/passwords
AuthGroupFile /etc/httpd/htgroup
Require group myusers
htgroup
myusers:yys1 yys2
这里面的用户必须在passwords中存在
DirectoryIndex index.html index.html.var
指定默认页面
TypesConfig /etc/mime.types
mime类型文件
DefaultType text/plain
默认mime类型文件
HostnameLookups Off
关闭域名反解
ErrorLog logs/error_log
LogLevel warn
错误日志
PV:page view每天页面访问量
UV:user view每天独立ip访问量
Alias /icons/ "/var/www/icons/"
Alias /icons "/var/www/icons"
别名
section 3
#NameVirtualHost *:80
#<VirtualHost *:80>
# ServerAdmin webmaster@dummy-host.example.com
# DocumentRoot /www/docs/dummy-host.example.com
# ServerName dummy-host.example.com
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>
需要注意使用虚拟主机必须把中心主机注视掉
#DocumentRoot
基于ip:
[root@salt-master conf]# vim /etc/httpd/conf.d/v1.conf
<VirtualHost 192.168.1.235:80>
DocumentRoot /www/yys1/
ServerName www.yys1.com
#ErrorLog logs/dummy-host.example.com-error_log
#CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
<VirtualHost 192.168.1.200:80>
DocumentRoot /www/yys2/
ServerName www.yys2.com
#ErrorLog logs/dummy-host.example.com-error_log
#CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
[root@salt-master conf]# curl 192.168.1.200
hello yys2
[root@salt-master conf]# curl 192.168.1.235
hello yys1
可以看到不通ip访问到不同站点了,即使是同一台服务器
基于端口:
<VirtualHost 192.168.1.235:80>
DocumentRoot /www/yys1/
ServerName www.yys1.com
</VirtualHost>
<VirtualHost 192.168.1.200:80>
DocumentRoot /www/yys2/
ServerName www.yys2.com
</VirtualHost>
<VirtualHost 192.168.1.200:8080>
DocumentRoot /www/yys3/
ServerName www.yys3.com
</VirtualHost>
注意主配置文件当中需要监听8080这个端口
[root@salt-master conf]# curl 192.168.1.200:80
hello yys2
[root@salt-master conf]# curl 192.168.1.200:8080
hello yys3
基于域名:
NameVirtualHost 192.168.1.200:80
<VirtualHost 192.168.1.200:80>
DocumentRoot /www/yys1/
ServerName www.linux1.com
</VirtualHost>
<VirtualHost 192.168.1.200:80>
DocumentRoot /www/yys2/
ServerName www.linux2.com
</VirtualHost>
[root@salt-master conf]# curl www.linux2.com
hello yys2
[root@salt-master conf]# curl www.linux1.com
hello yys1
添加日志
<VirtualHost 192.168.1.200:80>
DocumentRoot /www/yys1/
ServerName www.linux1.com
CustomLog /var/log/httpd/linux1.com/access_log combined
</VirtualHost>
<VirtualHost 192.168.1.200:80>
DocumentRoot /www/yys2/
ServerName www.linux2.com
CustomLog /var/log/httpd/linux2.com/access_log combined
</VirtualHost>
也可以添加目录属性
<VirtualHost 192.168.1.200:80>
DocumentRoot /www/yys2/
<Directory "/www/yys2">
Options none
AllowOverride none
Order deny,allow
Deny from 192.168.1.100
</Directory>
ServerName www.linux2.com
CustomLog /var/log/httpd/linux2.com/access_log combined
</VirtualHost>
查看服务器状态信息:
<Location /server-status>
SetHandler server-status
Order allow,deny
Allow from all
</Location>
打开即可查看
web服务器(Linux, hw, web-server)
第一、apache
1、web服务器的概念以及http的概念
架构: C/S : Client (浏览器) <-------> Server (apache)
网站的技术:静态和动态
静态页面:一般都是.html .htm .shtml
动态页面:一般是 .php .asp .jsp
特点:需要语言的解析模块去完成页面代码编译,最后把结果返回给web服务器,还有一个常见的特点就是涉及到数据库查询的
2、apache软件包
# yum install httpd httpd-devel httpd-manual -y
服务名字:httpd
进程名字:httpd
默认端口:80 (https:443)
:8080
服务的工作目录: /etc/httpd
主配置:/etc/httpd/conf/httpd.conf
3、分析配置文件
分三部分:全局配置、局部配置、虚拟空间配置
全局:
ServerRoot "/etc/httpd" <---后面的路径不能添加 /,rpm安装不要去修改
Listen 80 <---监听的端口
Listen 8080
ServerName www.example.com:80 <---设定服务器默认网站绑定的域名(ip)和端口
实例1:安装后直接重启,使用默认网站
service httpd restart
实例2:取消默认报错并且对主配置文件进行简单的修改
功能:取消test page的显示,显示我们自己的首页
Listen 80
Listen 192.168.0.7:80
Listen 192.168.2.7:8080
1、vim /etc/httpd/conf/httpd.conf
ServerName 10.1.1.20:80
或者 vim /etc/hosts <---把你的主机名字和你的IP对应上
2、vim /var/www/html/index.html 《---默认情况下,默认网站的根目录 /var/www/html
概念:网站的根目录--->网站页面程序存放的起始路径
默认情况下,apache读取一个网站的的默认首页就是index.html
DirectoryIndex index.html index.html.var 《---这里就是定义网站的,默认首页的名字
3、修改参数
DirectoryIndex index.htm index.html index.html.var
新建一个index.htm <---内容随便你,用来测试区别
4、service httpd restart
5、打开firefox,在浏览器中输入域名或者IP就可以。
实例3:修改默认网站的根目录
1、修改配置文件的根目录定义参数
DocumentRoot "/www"
2、创建定义的网站根目录
mkdir /www
vim /www/index.html
3、重启服务,让配置生效
service httpd restart
实例4:学习目录控制参数
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None # 是否启动.htaccess
Order allow,deny
Allow from all
</Directory>
xml
分析参数:
Options
Indexes <---是否对于没有默认首先目录下面的文件以及子目录进行索引,去掉之后就取消这个功能
FollowSymLinks <---跟踪软连接,存在的意义:隐藏目录的真实路径,可以方便网站扩容
<Directory "/www"> <---取消indexes的配置
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
实例5:启用访问控制
<Directory "/www">
Options FollowSymLinks
AllowOverride None
Order allow,deny <-----定义acl控制的顺序,这里代表先允许,不再允许范围内的都拒绝
Allow from all <----定义允许所有
</Directory>
<Directory "/www">
Options FollowSymLinks
AllowOverride None
Order allow,deny <-----定义acl控制的顺序,这里代表先允许,不再允许范围内的都拒绝
Allow from 10.1.1.0/24 <----定义只允许10.1.1.0/24
</Directory>
----------------------------
<Directory "/www">
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from 10.1.2.0/24 《---只拒绝10.1.2.0/24这个网段访问
</Directory>
实例6:口令验证控制
1、修改参数 ,把AllowOverride none 改成 AllowOverride ALL
<Directory "/www">
Options FollowSymLinks
AllowOverride ALL 《---ALL启用口令验证,none关闭口令验证
Order deny,allow
Deny from 10.1.2.0/24 《---只拒绝10.1.2.0/24这个网段访问
</Directory>
2、建立.htaccess文件,定义验证配置文件
我要对网站的根目录进行验证,那么在根目录下创建 .htaccess
vim /www/.htaccess
authname "Just for test"
authtype basic
authuserfile /etc/httpd/userpw
require valid-user
3、建立密码帐号文件
htpasswd -c /etc/httpd/userpw test
htpasswd /etc/httpd/userpw bbs
4、service httpd restart
实例7:建立用户个人空间
1、修改配置文件
搜索 UserDir
<IfModule mod_userdir.c>
#
# UserDir is disabled by default since it can confirm the presence
# of a username on the system (depending on home directory
# permissions).
#
#UserDir disable
#
# To enable requests to /~user/ to serve the user's public_html
# directory, remove the "UserDir disable" line above, and uncomment
# the following line instead:
#
UserDir public_html 《---启用用户个人空间的功能,访问http://ip/~mary --》读取/home/mary/public_html
</IfModule>
<Directory /home/*/public_html>
AllowOverride FileInfo AuthConfig Limit
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
<Limit GET POST OPTIONS>
Order allow,deny
Allow from all
</Limit>
<LimitExcept GET POST OPTIONS>
Order deny,allow
Deny from all
</LimitExcept>
</Directory>
2、建立用户以及个人空间的目录
useradd web
mkdir /home/web/public_html 《---建立个人空间根目录
vim /home/web/public_html/index.html
This is Web's Home!
chmod o+x /home/web <---默认用户家目录权限是700,httpd进程无法访问,所以给予它访问的权限
3、重启服务,使配置生效
service httpd restart
4. 测试: http://localhost/~web/
===========================================================
实例8:建立目录别名
Alias /tobbs "/bbs" <---访问 IP/tobbs 的时候实际读取的内容是/bss(系统的绝对路径)
加入我们的apache定义哪个别名指向那个文件夹里面,存放的是一些cgi,perl...alias定义的别名就无法达到执行程序的目的。
ScriptAlias /tobbs "/bbs" <---这样就解决的执行脚本的问题
作用:方便扩容,隐藏目录增加安全性
实例9:虚拟空间 <---一个服务器跑多个网站
@@基于域名的虚拟空间: 《---在同一个服务器,在只有一个IP的情况下,通过不同域名来区分不同的网站
1、启用参数:
NameVirtualHost *:80 《--让apache在一个服务器上,单个IP 运行多个网站,每个网站独立内容
2、添加虚拟主机配置
<VirtualHost *:80>
DocumentRoot /www/uplooking
ServerName www.uplooking.com
ErrorLog logs/www.uplooking.com-error_log
CustomLog logs/www.uplooking.com-access_log common
</VirtualHost>
3、建立网站目录和文件
mkdir /www/uplooking
vim /www/uplooking/index.html
4、配置DNS
做A记录或CNAME记录都行, 如果都做A记录不行, 就把其中一个域名做A记录,
其他的做CNAME记录
真正在互联网该如何操作?
背景:公司搭建一台web服务器,绑定的网址 www.uplooking.com,IP是公网:61.191.53.56
公司想建立一个论坛,绑定的网址bbs.uplooking.com,网站是建立同一个服务器上
a、添加虚拟主机配置
<VirtualHost *:80>
DocumentRoot /bbs
ServerName bbs.uplooking.com
ErrorLog logs/bbs.uplooking.com-error_log
CustomLog logs/bbs.uplooking.com-access_log common
</VirtualHost>
b、建立网站目录和文件
mkdir /bbs
vim /bbs/index.html
测试: http://www.uplooking.com --->访问的是/www/uplooking
http://bbs.uplooking.com --->访问的是/bbs下的内容
@@@基于IP的虚拟空间:
1、修改配置文件
#NameVirtualHost *:80 <---注释这个配置
<VirtualHost 10.1.1.21:80>
DocumentRoot /bbs
ServerName bbs.uplooking.com
ErrorLog logs/bbs.uplooking.com-error_log
CustomLog logs/bbs.uplooking.com-access_log common
</VirtualHost>
2、修改DNS,添加A记录
bbs IN A 10.1.1.21
测试: http://bbs.uplooking.com 过程: Client ---> 数据包[dst:10.1.1.21 url:bbs.uplooking.com]-->Server
@@@基于同一个IP不同端口:
1、修改参数文件
[root@kadefor ule-sa3]# grep ^Listen /etc/httpd/conf/httpd.conf
Listen 80
Listen 8080 <---增加apache监听的端口
[root@kadefor ule-sa3]# tail -12 !$
tail -12 /etc/httpd/conf/httpd.conf
<VirtualHost 192.168.56.1:80>
DocumentRoot /www2
ServerName web.kadefor.com
ErrorLog logs/www2.kadefor.com-error_log
CustomLog logs/www2.kadefor.com-access_log common
</VirtualHost>
<VirtualHost 192.168.56.1:8080>
DocumentRoot /www
ServerName web.kadefor.com
ErrorLog logs/www.kadefor.com-error_log
CustomLog logs/www.kadefor.com-access_log common
</VirtualHost>
[root@kadefor ule-sa3]#
2、增加DNS记录
测试 过程: Client ---> 数据包[dst:192.168.56.1 url:web.kadefor.com port:8080]-->Server
http://web.kadefor.com:8080/
http://web.kadefor.com
作业:
建立两个虚拟主机(vhost),分别帮定两个网址是: www.uplooking.com 和 bbs.uplooking.com
www.uplooking.com对应的网站根目录是/www/uplooking,随便编写一个默认首页测试,方便区别不同网站
bbs.uplooking.com对应的网站根目录是/bbs
要求利用其中一种别名的方法,让访问www.uplooking.com/bbs的时候,实际访问的是/bbs下的内容
网站所用的网址要求使用自己的DNS服务器去解析,而不是修改hosts文件
网站要求只允许10.1.1.0网段去访问,但不能让10.1.1.20访问
搭按:
1 vim /etc/httpd/conf/httpd.conf
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/uplooking
ServerName www.uplooking.com
ErrorLog logs/www.uplooking.com-error_log
CustomLog logs/www.uplooking.com-access_log common
Alias /bbs "/bbs"
<Directory "/www/uplooking">
Order allow,deny
deny from 10.1.1.20
allow from 10.1.1.0/24
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /bbs
ServerName bbs.uplooking.com
ErrorLog logs/bbs.uplooking.com-error_log
CustomLog logs/bbs.uplooking.com-access_log common
<Directory "/www/uplooking">
Order allow,deny
deny from 10.1.1.20
allow from 10.1.1.0/24
</Directory>
</VirtualHost>
2、mkdir /www/uplooking;mkdir /bbs
vim /www/uplooking/index.html
This www.uplooking.com
vim /bbs/index.html
This bbs.uplooking.com
3、配置DNS添加别名记录
web IN A 10.1.1.20
www IN CNAME web
bbs IN CNAME web
service named restart
service httpd restart
怎样设置只能通过域名访问, 而不能通过IP访问?
方法1.
添加一个虚拟主机可以实现:
<VirtualHost *:80>
ServerName localhost
DocumentRoot /nosite # 这个目录不存在!
</VirtualHost>
方法2
<VirtualHost *:80>
ServerName localhost
#DocumentRoot /nosite
<Location /> # 禁止所有人访问
order deny,allow
deny from all
</Location>
</VirtualHost>
1. yum install httpd -y
2. yum install httpd-manual -y
/etc/init.d/httpd restart
firefox http://<ip>/manual
3. change DocumentRoot
vim /etc/httpd/conf/httpd.conf
DocumentRoot "/var/www/html"
-->
DocumentRoot "/opt/www"
<Directory "/var/www/html">
-->
<Directory "/opt/www">
4. control directory
<Directory "/opt/www/abc">
order deny,allow
deny from 192.168.248.1
</Directory>
5. UserDir
UserDir disabled
-->
UserDir public_html
<Directory "/home/*/public_html">
Options Indexes FollowSymlinks
AllowOverride None
</Directory>
test:
su - oracle
mkdir public_html
chown o+x ~oracle
6. .htaccess
相应目录控制语句里, 修改 AllowOverride all
eg:
/opt/www/abc
vim /etc/httpd/conf/httpd.conf
<Directory />
allowoverride all
</Diretory>
or: 如果有:
<Directory "/opt/www/abc">
allowoverride all
</Diretory>
vim /opt/www/abc/.htaccess
authname "Must auth"
authtype basic
authuserfile /etc/httpd/conf/user.db
require valid-user
7. cgi
a) 确认 cgi 模块是否加载
b) 确认存放 cgi 程序的目录:
ScriptAlias "/cgi-bin/" "/var/www/cgi"
c) 控制目录允许执行cgi程序:
<Directory /var/www/cgi>
Options +ExecCGI
</Directory>
b) 取消注释
AddHandler cgi-script .cgi
8. selinux --> cgi
chcon -t httpd_sys_script_exec_t /var/www/cgi-bin -R
9. v-host
a) port
vim /etc/httpd/conf/httpd.conf
Listen 8080
<VirtualHost *:80>
ServerAdmin root@localhost
ServerName www.up.com
DocumentRoot /opt/www80
ErrorLog logs/www80.error.log
CustomLog logs/www80.access.log common
</VirtualHost>
<VirtualHost *:8080>
ServerAdmin root@localhost
ServerName www.up.com
DocumentRoot /opt/www8080
ErrorLog logs/www8080.error.log
CustomLog logs/www8080.access.log common
</VirtualHost>
b) name-based
vim /etc/httpd/conf/httpd.conf
NameVitrualHost *:80
<VirtualHost *:80>
ServerAdmin root@localhost
ServerName www.up.com
DocumentRoot /opt/www
ErrorLog logs/www80.error.log
CustomLog logs/www80.access.log common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin root@localhost
ServerName blog.up.com
DocumentRoot /opt/blog
ErrorLog logs/blog.error.log
CustomLog logs/blog.access.log common
</VirtualHost>
c) ip
vim /etc/httpd/conf/httpd.conf
Listen *:80
<VirtualHost 192.168.248.11:80>
ServerAdmin root@localhost
ServerName www.up.com
DocumentRoot /opt/www80
ErrorLog logs/www80.error.log
CustomLog logs/www80.access.log common
</VirtualHost>
<VirtualHost 172.16.248.11:80>
ServerAdmin root@localhost
ServerName blog.up.com
DocumentRoot /opt/www8080
ErrorLog logs/www8080.error.log
CustomLog logs/www8080.access.log common
</VirtualHost>
10. 禁止使用ip,或非法解析的域名来访问:
建立第一个虚拟主机, 使之不可访问
<VirtualHost 192.168.248.11:80>
DocumentRoot /www/docs
<Directory /www/docs>
order deny,allow
deny from all
</Directory>
</VirtualHost>
https
[root@dns ~]# yum install openssl mod_ssl -y
[root@dns ~]# ls /etc/httpd/conf.d/ssl.conf
/etc/httpd/conf.d/ssl.conf
[root@dns ~]#
[root@dns ~]# cd /etc/pki/tls/certs
[root@dns certs]# ls Makefile
Makefile
[root@dns certs]# make testcert
[root@dns certs]#
[root@dns certs]# ls ../private/localhost.key
../private/localhost.key
[root@dns certs]# ls localhost.crt
localhost.crt
[root@dns certs]# mkdir /etc/httpd/ssl
[root@dns certs]# cp ../private/localhost.key /etc/httpd/ssl/server.key
[root@dns certs]# cp localhost.crt /etc/httpd/ssl/server.crt
[root@dns certs]#
[root@dns certs]# vim /etc/httpd/conf.d/ssl.conf
[root@dns certs]# grep server.key /etc/httpd/conf.d/ssl.conf
SSLCertificateKeyFile /etc/httpd/ssl/server.key
[root@dns certs]# grep server.crt /etc/httpd/conf.d/ssl.conf
SSLCertificateFile /etc/httpd/ssl/server.crt
#SSLCACertificateFile /etc/httpd/ssl/server.crt
[root@dns certs]# /etc/init.d/httpd restart
[root@dns certs]# --> password
[root@dns certs]#
[root@dns certs]# openssl rsa -in /etc/httpd/ssl/server.key -out /etc/httpd/ssl/nopw.key
Enter pass phrase for /etc/httpd/ssl/server.key:
writing RSA key
[root@dns certs]# vim /etc/httpd/conf/httpd.conf
[root@dns certs]# vim /etc/httpd/conf.d/ssl.conf
[root@dns certs]# grep nopw.key /etc/httpd/conf.d/ssl.conf
SSLCertificateKeyFile /etc/httpd/ssl/nopw.key
[root@dns certs]#
[root@dns certs]# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@dns certs]#