您的位置:首页 > 理论基础 > 计算机网络

基于Linux的HTTP服务搭建(详细)

2019-01-22 00:02 741 查看

HTTP
(HyperText Transfer Protocal)超文本传输协议,默认端口号80,是互联网上最广泛的一种网络协议。
HTTP协议是用于从WWW服务器传输超文本到本地浏览器的传输协议。它可以使浏览器更加高效,使网络传输减少。它不仅保证计算机正确快速地传输超文本文档,还确定传输文档中的哪一部分,以及哪部分内容首先显示(如文本先于图形)等。

什么是静态网页和动态网页?
静态网页:每个网页中都有一个固定的URL,后缀常为htm、HTML等,网页内容一旦发布,都是直接保存在网站中的,不会有变动。
动态网页:与后台数据库有交互,可以实现用户的注册登录, 用 php,java,python等语言编写,有较强的实时性。

1.应用层使用HTTP协议。
2.HTML(标准通用标记语言下的一个应用)文档格式。
3.浏览器统一资源定位器(URL)。(URI)

格式: http://192.168.11.128:80
域名 端口默认为80

响应格式:通过报文返回一个状态码
–200 正常,请求成功
–401 静止访问,未授权(没有登录或者登陆失败)
–403 禁止访问,通常代表已认证通过,但没有访问权限
–404 未找到资源
–500 服务器内部错误(服务器内部出现问题,服务器内部故障,或者访问过多没有显示就可能会出现500错误)

Apache的主配置文件:
Vim /etc/httpd/conf/httpd.conf
httpd.conf配置文件主要由3个部分组成
全局环境
主服务器配置
虚拟主机

配置路径: /etc/httpd/*
主配置目录: /etc/httpd/conf
子配置目录: /etc/httpd/conf.d(apache是一个模块化的一个配置服务,所以我们可以根据每个模块进行一些配置,不仅对程序本身进行模块化配置对配置文件也进行了模块化配置)

先开始一个简单的实验:

实验思路: 装包—配置—起服务—测试

挂载光盘

mount /dev/sr0 /mnt

配置yum仓库:

[root@localhost ~]# vim /etc/yum.repos.d/base.repo
[base]
name=base
baseurl=file:///mnt
gpgcheck=0
Enable=1  开机后启用
1.yum install httpd -y  安装
2.Systemctl restart httpd   重启
3.systemctl stop firewalld 关闭防火墙
4.setenforce 0
5.Vim /var/www/html/index.html  编写文件  写个hello
6.用IE浏览器搜索服务器地址,网页中出现hello

实验完成

可以用Systemctl status httpd -l 进行检查

三种搭建静态网页:

1:指定IP地址访问网页

echo hahahaha >/haha/index.html
[root@localhost conf.d]# vim /etc/httpd/conf.d/vhosts.conf
<Directory /haha>
AllowOverride none
Require All granted
</Directory>
<VirtualHost 192.168.11.128:80>
ServerAdmin root@localhosts.localdomain
ServerName 192.168.11.128
DocumentRoot /haha
</VirtualHost>

更改.conf的文件需要重启服务:

Systemctl restart httpd
Systemctl status httpd -l    进行查看

可以在命令行中用命令curl http://192.168.11.128进行测试

若在配置中为 DocumentRoot /haha/177
则在测试时要用: https://blog.csdn.net/weixin_43617426/article/details/HTTP:/192.168.11.128/177

2.指定端口号访问网页:

echo hahahaha >/haha/index.html
[root@localhost conf.d]# vim /etc/httpd/conf.d/vhosts.conf
<Directory /haha>
AllowOverride none
Require All granted
</Directory>
listen 10000     指定端口一般最好设为不常见的端口
<VirtualHost 192.168.11.128:10000>
ServerAdmin root@localhosts.localdomain
ServerName 192.168.11.128
DocumentRoot /haha
</VirtualHost>

3.指定域名访问页面:

Echo hahahaha >/haha/index.html
[root@localhost conf.d]# vim /etc/httpd/conf.d/vhosts.conf
<Directory /haha>
AllowOverride none
Require All granted
</Directory>
listen 10000
<VirtualHost 192.168.11.128:10000>
ServerAdmin root@localhosts.localdomain
ServerName  www.dada.com    更改域名
DocumentRoot /haha
</VirtualHost>

在linux系统中添加地址相应的域名,在虚拟机中的火狐浏览器可以进行测试,或者用命令行curl进行测试

Vim /etc/hosts
192.168.11.128   www.dada.com

在windows操作系统中需要在C:\Windows\System32\drivers\etc\hosts中改才能显示

HTTPS 安全套接字层超文本传输协议

为了数据传输的安全,HTTPS(端口号443)在HTTP(端口号80)的基础上加入了SSL协议,SSL依靠证书来验证服务器的身份,并为浏览器和服务器之间的通信加密。

证书格式包含三个文件:key,csr,crt。
key是服务器上的私钥文件,用于加密和解密
csr是证书签名请求文件,用于提交给证书颁发机构(CA)对证书签名
crt是由证书颁发机构(CA)签名后的证书

配置过程(4步):
1.装包:

yum install mod_ssl -y
rpm -ql mod_ssl
systemctl stop firewalld
setenforce 0

2.编辑证书:

[root@localhost ~]# cd /etc/pki/tls/certs
[root@localhost certs]# make dada.crt

Enter pass phrase:                          输入密码
Verifying - Enter pass phrase:                  确认密码
Enter pass phrase for dada.key:                确认密码
Country Name (2 letter code) [XX]: dd            国家代码
State or Province Name (full name) []:shanxi      省份
Locality Name (eg, city) [Default City]:xian         城市
... ....

3.对配置文件进行配置:

[root@localhost certs]# mkdir /dada
[root@localhost certs]# echo this is dada >/dada/index.html
Vim /etc/httpd/conf.d/dada.conf
<Directory /dada>
AllowOverride none
Require all granted
</Directory>
<VirtualHost 192.168.11.128:443>     默认端口为443
DocumentRoot /dada
ServerName 192.168.11.128
ErrorLog /var/log/httpd/dummy-host2.example.com-error_log
CustomLog /var/log/httpd/dummy-host2.example.com-access_log common
SSLEngine on                                          引擎打开
SSLProtocol all -SSLv2                               支持所有协议除了
SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5:!SEED:!IDEA  密码套件客户端和服务端 协商
SSLCertificateFile /etc/pki/tls/certs/dada.crt       指定证书路径
SSLCertificateKeyFile /etc/pki/tls/certs/dada.key    指定秘钥路径
</VirtualHost>

4.重启服务: systemctl restart httpd
需要输入证书密码(123456),访问网站时会提示证书有问题。

[root@localhost certs]# systemctl restart httpd
Enter SSL pass phrase for 192.168.11.128:443 (RSA) : ******

虚拟目录和用户控制
在一台计算机上创建多个WEB站点,并为每个WEB站点设置不同的主目录和虚拟子目录,每个WEB站点作为各自独立的网站分配给不同的公司或部门。
多个公司或部门的网站就可以共用同一台计算机提供不同的服务

创建目录

[root@localhost certs]# mkdir /www/xixi -pv
Echo this is www>/www/index.html
Echo this is user>/www/xixi/index.html

添加zhangsan,lisi 两个认证用户

[root@localhost certs]# htpasswd -c /etc/httpd/userfile zhangsan
第一次添加需要 -c 覆盖之前信息
[root@localhost certs]# htpasswd /etc/httpd/userfile lisi

编写配置文件

Vim /etc/httpd/conf.d/dada.conf
<Directory /www/xixi>                 对/www/xixi开启认证
AuthType Basic                  基本认证类型
AuthName " please login:"        提示信息
AuthUserFile /etc/httpd/userfile       用户认证文件用户名和密码
Require user zhangsan lisi            指定zhangsan,lisi 可以进行认证
</Directory>
<Directory /www>
AllowOverride none
Require all granted
</Directory>
<VirtualHost 192.168.11.128:80>
DocumentRoot /www                   默认访问路径和域名
ServerName 192.168.11.128              默认主机域名
Alias    /xixi    /www/xixi           访问/xixi要到/www下    虚拟主机访问
ServerAlias www.xixi.com            虚拟主机域名
</VirtualHost>

进行测试:
输入网址访问http://192.168.11.128

再访问http://192.168.11.128/xixi

输入密码后:

动态网站搭建:

装包

yum install mod_wsgi -y

编辑

vim /etc/httpd/conf.d/dada.conf
<Directory /www/alt>
AllowOverride none
Require all granted
</Directory>
<VirtualHost 192.168.11.128:80>
WSGIScriptAlias / /www/alt/webinfo.wsgi    后缀不能改变
</VirtualHost>

创建目录

mkdir /www/alt -pv

编写python脚本内容:

vim /var/www/alt/webinfo.wsgi
def application(environ, start_response):
status = '200 OK'
output = 'Hello World'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]

systemctl restart httpd 重启服务

结果:页面出现Hello World 字样

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: