世界使用广泛的Web服务器软件——apache

一、apache的简介

Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其多平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩展,将Perl/Python等解释器编译到服务器中。同时Apache音译为阿帕奇
Apache HTTP服务器是一个模块化的服务器,源于NCSAhttpd服务器,经过多次修改,成为世界使用排名第一的Web服务器软件。
它可以运行在几乎所有广泛使用的计算机平台上。是企业中常用的web服务,用来提供http://(超文本传输协议)

二、apache的环境部署
yum install httpd -y			##apache软件
yum install httpd-manual		##apache的手册
systemctl start httpd           ##启动服务
systemctl enable httpd          ##允许服务开机自启动
firewall-cmd --list-all							##列出火墙信息
firewall-cmd --permanent --add-service=http		##永久允许http
firewall-cmd --reload							##火墙从新加载策略

在这里插入图片描述

/var/www/html						##apache的默认发布目录
/var/www/html/index.html			##apache的默认发布文件
vim /var/www/html/index.html
<h1> hello world </h1>              ##标题字体设置,放大字体

在这里插入图片描述
##测试:

输入以下地址:
http://172.25.254.100               ##可以看到刚才编辑的默认发布文件
http://172.25.254.100/manual        ##可以看到刚才下载的apache手册

在这里插入图片描述
在这里插入图片描述

三、apache的服务管理

1.apache的基础信息

#主配置目录:	/etc/httpd/conf
#主配置文件:	/etc/httpd/conf/httpd.conf
#子配置目录:	/etc/httpd/conf.d/
#子配置文件:	/etc/httpd/conf.d/*.conf 
#默认发布目录:	/var/www/html
#默认发布文件:	index.html
#默认端口:	80                                     (即访问网址时的真实网址是http://172.25.254.121:80)
#默认安全上下文:httpd_sys_content_t
#程序开启默认用户: apache
#apache日志:	/etc/httpd/logs/*

2.默认端口的修改

netstat -antlupe | grep httpd                      ##查看http正在使用的端口
semanage port -l | grep http                       ##查看http自带的供选择使用的一些端口

在这里插入图片描述
(1)当修改默认端口为http自带其他端口时

vim /etc/httpd/conf/httpd.conf                      ##编辑主配置文件
43 Listen 8080										##修改默认端口为8080
#浏览器的默认端口是80,所以此处不用输入:80
firewall-cmd --permanent --add-port=8080/tcp        ##给防火墙添加端口,允许通过
firewall-cmd --reload                               ##使火墙重新读取设定
systemctl restart httpd                             ##重启服务

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
(2)当修改默认端口为一个新端口时

semanage port -a -t http_port_t -p tcp 666          ##给http添加一个端口,使这个端口可以被使用
vim /etc/httpd/conf/httpd.conf                      ##编辑主配置文件
43 Listen 666										##修改默认端口为666
#浏览器的默认端口是80,所以此处不用输入:80
firewall-cmd --permanent --add-port=666/tcp         ##给防火墙添加端口,允许通过
firewall-cmd --reload                               ##使火墙重新读取设定
systemctl restart httpd                             ##重启服务

在这里插入图片描述
3.默认发布文件的修改
##默认发布文件就是访问apache时没有指定文件名称时默认访问的文件
##这个文件可以指定多个,但是有访问顺序

vim /etc/httpd/conf/httpd.conf
164     DirectoryIndex index.html test.html			##当index.html不存在时访问test.html

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
4.修改默认发布目录

mkdir -p /westos/html                ##新建一个目录
vim /etc/httpd/conf/httpd.conf       

120 DocumentRoot "/westos/html"
121 <Directory "/westos/html">
122         Require all granted      ##服务的安全设定,允许别人访问
123 </Directory>
systemctl restart httpd              ##重启服务

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

semanage fcontext -a -t httpd_sys_content_t '/westos/html(/.*)?'  		 ##修改发布目录的安全上下文  
rstorecon -RvvF /westos/html/                                   	 	 ##使目录及内部的所有内容更改

在这里插入图片描述
在这里插入图片描述
5.apache的虚拟主机(让不同的站点访问不同的目录)

vim /etc/httpd/conf.d/vhost.conf                        ##新建一个配置文件,名字任意,但是必须以.conf结尾 
  1 <VirtualHost _default_:80>                          ##默认访问的发布目录,不指定就为www
  2         DocumentRoot /var/www/html                 
  3         CustomLog logs/default.log combined         ##站点日志(combined)表示四种日志的集合
  4 </VirtualHost>
  5 
  6 <VirtualHost *:80>
  7         ServerName news.westos.com					##指定站点名称
  8         DocumentRoot /var/www/html/vhost/news       ###站点默认发布目录
  9         CustomLog logs/news.log combined
 10 </VirtualHost>
 11 <Directory "/var/www/vhost/news">
 12         Require all granted                         ##安全设定为允许
 13 </Directory>
 14 
 15 <VirtualHost *:80>
 16         ServerName music.westos.com
 17         DocumentRoot /var/www/html/vhost/music
 18         CustomLog logs/music.log combined
 19 </VirtualHost>
 20 <Directory "/var/www/vhost/music">
 21         Require all granted
 22 </Directory>

在这里插入图片描述
测试:
##先再测试主机上做好本地解析,给apache服务器的不同站点配置不同的默认发布文件

vim /etc/hosts
172.25.254.121  news.westos.com music.westos.com www.westos.com

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

四、apache内部的访问控制(用户权力的管理)

1.针对于主机的访问控制

vim /etc/httpd/conf.d/vhost.conf
  5 <Directory "/var/www/html/test">
  6         Order Deny,Allow		  ##列表读取顺序,后读取的列表会覆盖先读取内容的重复部分(即黑名单或者白名单)
  7         Allow from 172.25.254.44    
  8         Deny from all
  9 </Directory>
##此处即为白名单,只允许172.25.254.44访问

在这里插入图片描述
测试:
在172.25.254.121主机上:(只能看到测试页,看不到默认发布文件的内容)
在这里插入图片描述
在172.25.254.6主机上:
在这里插入图片描述
2.针对用户方式的访问控制(即访问的时候会让你输入指定的用户名和密码,这个用户名和密码可以有多个)

htpasswd -cm	/etc/httpd/userpass	admin                ##建立一个用户admin,会生成关于用户的密码文件userpass,可以在生成时写成.userpass 将文件隐藏起来
htpasswd -m 	/etc/httpd/userpass	admin1               ##给访问添加一个用户,原来的用户仍然可以使用

在这里插入图片描述
在这里插入图片描述

vim  /etc/httpd/conf.d/vhost.conf               ##编辑配置文件
 10 <Directory "/var/www/html/admin">
 11         AuthUserFile /etc/httpd/userpass    ##建立用户时指定的文件
 12         AuthName "Please input your name and password"            ##登陆时的提示语
 13         AuthType basic                      ##加密方式
 14         #Require user  admin admin     		##可以用空格隔开指定多个用户生效
 15         Require valid-user           		##允许所有文件中的所有用户登陆
 16 </Directory>
systemctl restart httpd

在这里插入图片描述
测试:
在这里插入图片描述在这里插入图片描述
##清除缓存再用另一个用户尝试
在这里插入图片描述

五、apache支持的语言

1.html(即上文所写内容,超文本标记语言)
2.php(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”,是一种通用开源脚本语言。)

vim /var/www/html/index.php         ##编写一个php脚本

<?php
	phpinfo();
?>

yum install php -y                  ##安装php运行必须的环境
systemctl restart httpd             ##重启服务

测试:
在这里插入图片描述
3.cgi(Common Gateway Interface,简称CGI。在物理上是一段程序,运行在服务器上,提供同客户端HTML页面的接口。)

mkdir -p /var/www/html/cgi                                        ##创建一个cgi的默认发布目录
semanage fcontext -l | grep http                                  ##查看可执行文件的安全上下文
semanager fcontent -a -t  httpd_sys_script_exec_t '/var/www/html/cgi(/.*)?'
restorecon -RvvF /var/www/html/cgi/

在这里插入图片描述

vim /var/www/html/cgi/index.cgi                 ##编辑一个默认发布文件(格式在apache手册里)           

#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;                                   ##脚本内容为显示当前时间

chmod 755 /var/www/html/cgi/index.cgi           ##给脚本赋予可执行权限
/var/www/html/cgi/index.cgi	                    ##执行下脚本确保脚本运行正常

在这里插入图片描述

vim /etc/httpd/conf.d/vhost.conf                ##格式在apache手册里
 
 17 <Directory "/var/www/html/cgi">				##默认发布目录
 18         Options +ExecCGI                    ##允许cgi文件运行
 19         AddHandler cgi-script .cgi          ##cgi的文件格式.cgi
 20 </Directory>

systemctl restart httpd                         ##重启服务

在这里插入图片描述
##测试:
在这里插入图片描述
4.wsgi(WSGI是Web Server Gateway Interface的缩写。)
##PythonWeb服务器网关接口(Python Web Server Gateway Interface,缩写为WSGI)是Python应用程序或框架和Web服务器之间的一种接口,已经被广泛接受, 它已基本达成它的可移植性方面的目标。

yum install mod_wsgi -y                    	      ##安装wsgi需要的服务软件
systemctl restart httpd                           ##安装wsgi后重启服务,否则后面可能会有问题
vim /var/www/html/cgi-bin/webapp.wsgi             ##编写一个python程序(本次为监控内存使用)

#!/usr/bin/env python
import time

def application (environ, start_response):
  response_body = 'UNIX EPOCH time is now: %s\n' % time.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]
vim /etc/httpd/conf.d/vhost.conf                  ##编辑配置文件添加如下内容

 35 <VirtualHost *:80>
 36         ServerName wsgi.westos.com
 37         WSGIScriptAlias /   /var/www/html/cgi-bin/webapp.wsgi         ##默认发布目录访问文件
 38 </VirtualHost>

systemctl restart httpd                          ##重启服务

在这里插入图片描述
测试:
先在测试主机上做好本地解析

vim /etc/hosts
172.25.254.121    wsgi.westos.com

在这里插入图片描述

六、https

HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer 或 Hypertext Transfer Protocol Secure,超文本传输安全协议),是以安全为目标的HTTP通道,简单讲是HTTP的安全版。即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL。

HTTPS和HTTP的区别主要为以下四点:
一、https协议需要到ca申请证书,一般免费证书很少,需要交费。
二、http是超文本传输协议,信息是明文传输,https 则是具有安全性的ssl加密传输协议。
三、http和https使用的是完全不同的连接方式,用的端口也不一样,前者是80,后者是443。
四、http的连接很简单,是无状态的;HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、身份认证的网络协议,比http协议安全。

1.环境部署

yum install mod_ssl -y                          ##SSL(Secure Sockets Layer 安全套接层),为数据通讯提供安全支持
yum install crypto-utils -y                     ##SSL所需的证书和访问的钥匙组件

firewall-cmd --permanent --add-service=https    ##允许防火墙通过https服务 
firewall-cmd --reload                           ##让火墙重新读取策略
systemctl restart httpd                         ##重启apache

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2.生成证书和钥匙并加到指定的网址上

genkey www.westos.com                          ##给www.westos.com加证书和钥匙,执行该命令后将进入图形界面设置
vim /etc/httpd/conf.d/ssl.conf                 ##将生成的证书和钥匙的文件位置写入配置文件 

101 SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
109 SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key

systemctl restart httpd                       ##重启apache

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
测试:
先在测试主机上做好本地解析,并清空浏览器的缓存(不同浏览器方法不同)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.设定https虚拟主机并设定网页重写

(1)设定虚拟主机

vim /etc/httpd/conf.d/vhost-https.conf

  1 <VirtualHost *:443>                                              ##https默认使用443端口,与http的80不同
  2         ServerName login.westos.com
  3         DocumentRoot /var/www/html/login/html                    ##默认发布目录
  4         CustomLog logs/login.logs combined                       ##四种日志集合
  5         SSLEngine on                                             ##允许加密访问
  6         SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
  7         SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
  8 </VirtualHost>
  9 
 10 <Directory "/var/www/html/login/html">
 11         Require all granted                                      ##开放安全设定
 12 </Directory>

mkdir -p /var/www/html/login/html                                    ##建立一个默认发布目录
vim /var/www/html/login/html/index.html                              ##写一个默认发布文件

systemctl restart httpd                                              ##重启apache

在这里插入图片描述
在这里插入图片描述
测试:
在测试主机做好本地解析
在这里插入图片描述
(2)https的网页重写
##当我们在浏览器输入http://login.westos.com时,默认访问80端口,而生活中我们输入http://www.baidu.con这种网址时会自动变成https://www.baidu.com,我们要做的就是在用户输入http时自动跳转到https加密

  1 <VirtualHost *:443>
  2         ServerName login.westos.com
  3         DocumentRoot /var/www/html/login/html
  4         CustomLog logs/login.logs combined
  5         SSLEngine on
  6         SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
  7         SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
  8 </VirtualHost>
  9 
 10 <Directory "/var/www/html/login/html">
 11         Require all granted
 12 </Directory>
 13 
 14 <VirtualHost *:80>
 15         ServerName login.westos.com
 16         RewriteEngine on                   						      ##允许网页重写服务
 17         RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]     ##$1即为$前面的字符^(/.*),表示所有
 18 </VirtualHost>
 
systemctl restart httpd 

^(/.*)$				##客户在浏览器地址栏中输入的所有字符
https://			##强制客户加密访问
%{HTTP_HOST}		##客户请求主机
$1					##"$1"标示 ^(/.*)$的值
[redirect=301]		##301永久重写 302临时

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值