Apache HTTP server

1.什么是apache?

Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行
Apache是世界使用排名第一的Web服务器软件,它可以运行在几乎所有广泛使用的计算机平台上
由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一,也叫网页服务器软件 
它快速、可靠并且可通过简单的API扩充,将Perl/Python等解释器编译到服务器中
同时Apache音译为阿帕奇,是北美印第安人的一个部落,叫阿帕奇族,在美国的西南部,也是一个基金会的名称、一种武装直升机等

什么是http://协议?

HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写
是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送协议
HTTP是基于TCP/IP通信协议来传递数据(HTML 文件, 图片文件, 查询结果等)
HTTP是一个属于应用层的面向对象的协议,由于其简捷、快速的方式,适用于分布式超媒体信息系统
HTTP协议工作于客户端-服务端架构为上,浏览器作为HTTP客户端通过URL向HTTP服务端即WEB服务器发送所有请求
Web服务器根据接收到的请求后,向客户端发送响应信息

2.apache的安装部署

关闭selinux

在这里插入图片描述
在这里插入图片描述
yum install httpd -y ##安装apache软件在这里插入图片描述
在这里插入图片描述

yum install httpd-manual  ##安装apache的手册

在这里插入图片描述

systemctl start httpd
systemctl enable httpd
firewall-cmd --permanent --add-service=http  ##火墙开启http服务
firewall-cmd --reload
firewall-cmd --list-all    

在这里插入图片描述

注:
/var/www/html ##apache的根目录,默认发布目录
/var/www/html/index.html ##apache的默认发布文件

vim /var/www/html/index.html
<h1>hello westos</h1>

在这里插入图片描述
测试:
http://172.25.254.121
在这里插入图片描述
http://172.25.254.121/manual
在这里插入图片描述

3.apache的基础信息

主配置目录/etc/httpd/conf
主配置文件/etc/httpd/conf/httpd.conf
子配置目录/etc/httpd/conf.d/
子配置文件/etc/httpd/conf.d/*.conf
默认发布目录/var/www/html
默认发布文件index.html
默认端口80
默认安全上下文httpd_sys_content_t
程序开启默认用户apache
apache日志/etc/httpd/logs/*
  • 修改默认端口:

vim /etc/httpd/conf/httpd.conf ##编写配置文件
43 Listen 8080 ##修改默认端口为8080
在这里插入图片描述
firewall-cmd --permanent --add-port=8080/tcp ##给火墙添加8080端口
firewall-cmd --reload ##重新加载
在这里插入图片描述
systemctl restart httpd ##重启服务
netstat -pan | grep httpd ##查看端口信息
在这里插入图片描述
在浏览器测试8080端口:
在这里插入图片描述

  • 修改默认发布文件:

默认发布文件就是访问apache时没有指定文件名时默认访问的文件;
这个文件可以指定多个,有访问顺序
vim /etc/httpd/conf/httpd.conf ##编写配置文件
164 DirectoryIndex westos index.html ##当westos不存在时访问index.html
在这里插入图片描述
编写/var/www/html/westos文件
在这里插入图片描述
在浏览器测试:
在这里插入图片描述

  • 修改默认发布目录:

vim /etc/httpd/conf/httpd.conf ##编写配置文件
在这里插入图片描述
建立/westos/html目录,将index.html移动至此目录下
在这里插入图片描述
用浏览器测试:
在这里插入图片描述

  • 访问控制

      vim /var/www/html/index.html
      <h1>172.25.254.121</h1>
      mkdir /var/www/html/zhang
      vim /var/www/html/zhang/index.html
      <h1>how are you</h1>    
    

    在这里插入图片描述

vim/etc/httpd/conf/httpd.conf

120 <Directory "/var/www/html/zhang">
121         Order Allow,Deny    ##先读allow,再读deny
122         Allow from All
123         Deny from 172.25.254.221  ##读到deny会覆盖allow
124 < /Directory>   

在这里插入图片描述

systemctl restart httpd

在浏览器测试:
在这里插入图片描述

vim/etc/httpd/conf/httpd.conf

在这里插入图片描述

systemctl restart httpd

在这里插入图片描述

  • 指定用户访问:
    htpasswd -cm westosuser admin ##第一次添加用户时加-c参数
    htpasswd -m westosuser admin1 ##第二次添加用户
    cat westosuser ##查看密码
    在这里插入图片描述

vim /etc/httpd/conf/httpd.conf

<Directory "/var/www/html/westos">
    AuthUserFile   /etc/httpd/conf/westosuser
    AuthType       basic
    AuthName       "Plesae input your name and password!!!"
    Require user   admin       ##允许admin用户加密访问
   #Require        valid-user  ##所有的加密用户都可访问
</Directory>    

在这里插入图片描述

systemctl restart httpd

在这里插入图片描述
输入刚建立的用户及密码进入:
在这里插入图片描述
修改参数,所有用户可进入
在这里插入图片描述

  • 虚拟主机搭建
    vim /etc/hosts ##添加解析
    在这里插入图片描述
    在浏览器输入www.westos.com即可查看
    在这里插入图片描述

添加news和music的目录及编写.html文件

mkdir /var/www/virtual/news/html -p
mkdir /var/www/virtual/music/html -p
vim /var/www/virtual/news/html/index.html
cp /var/www/virtual/news/html/index.html /var/www/virtual/music/html/index.html
vim /var/www/virtual/music/html/index.html    

在这里插入图片描述

cd /etc/httpd/conf.d/
vim default.conf ##编写虚拟主机配置文件

<VirtualHost _default_:80>
        DocumentRoot   /var/www/html
        CustomLog      logs/default.log combined
</VirtualHost>   

在这里插入图片描述

vim news.conf ##编辑news虚拟站点的基本信息和授权信息

<VirtualHost *:80>
        ServerName     news.westos.com
        DocumentRoot   /var/www/virtual/news/html
        CustomLog      logs/new.log combined
</VirtualHost>
<Directory "/var/www/virtual/news/html">
          Require all granted
</Directory>      

在这里插入图片描述

cp news.conf music.conf
vim music.conf ##编辑music虚拟站点的基本信息和授权信息

<VirtualHost *:80>
        ServerName     music.westos.com
        DocumentRoot   /var/www/virtual/music/html
        CustomLog      logs/new.log combined
</VirtualHost>
<Directory "/var/www/virtual/music/html">
          Require all granted
</Directory>      

在这里插入图片描述

systemctl restart httpd
用浏览器访问music.westos.com/news.westos.com
在这里插入图片描述

在这里插入图片描述

apache支持的语言

  • php超文本预处理器

vim /var/www/html/index.php ##编辑php默认发布文件

<?php
      phpinfo();
?>

在这里插入图片描述

yum install php -y

在这里插入图片描述

在这里插入图片描述

systemctl restart httpd
在这里插入图片描述在浏览器可看到php表格
在这里插入图片描述

systenctl restart httpd
mkdir /var/www/html/cgi
vim index.cgi

#! /usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;  

在这里插入图片描述

chmod 775 index.cgi ##更改权限
./index.cgi ##执行文件
在这里插入图片描述

vim /etc/httpd/conf.d/default.conf

<VirtualHost _default_:80>
        DocumentRoot   /var/www/html
        Custommlog     logs/default.log combined
</VirtualHost>
<Directory "/var/www/html/cgi">
     Options +ExecCGI
     AddHandler cgi-script .cgi
</Directory>      

在这里插入图片描述

systemctl restart httpd
在浏览器访问http://172.25.254.121/cgi/index.cgi
在这里插入图片描述

  • https 安全超文本传输协议,在http协议上增加ssl加密传送信息

yum install mod_ssl -y
在这里插入图片描述
systemctl restart httpd
增加火墙功能
在这里插入图片描述
在浏览器访问https://172.25.254.121 此时的加密未得到任何认证
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 自签名证书:

yum install -y crypto-utils
在这里插入图片描述
genkey www.westos.com ##对域名主机生成证书和密钥,进入图形安装
在这里插入图片描述
进入加密页面
在这里插入图片描述
选择钥匙大小
在这里插入图片描述

在这里插入图片描述
是否给CA发送证书申请,选择否
在这里插入图片描述
给私钥加密,选next
在这里插入图片描述
填写基本信息
在这里插入图片描述

vim /etc/httpd/conf.d/ssl.conf ##通过配置文件告诉apache生成的密钥和证书文件

101 SSLCentificateFile /etc/pki/tls/certs/www.westos.com.crt
108 SSLCentificateKeyFile /etc/pki/tls/private/www.westos.com.key 

在这里插入图片描述

systemctl restart httpd
在浏览器可查看自签名证书的发布信息
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 网页重写(客户在浏览器输入80端口可跳转到443端口)

cd /etc/httpd/conf.d/
cp news.conf login.conf
vim login.conf

<VirtualHost *:443>
     ServerName    login.westos.com
     DocumentRoot  /var/www/virtual/login/html
     CustomLog     logs/login.log combined
     SSLEngine on
     SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
     SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
</VirtualHost>
<Directory "/var/www/virtual/login/html">
     Require all granted
</Directory>          

在这里插入图片描述

vim /etc/hosts

172.25.254.121 login.westos.com

在这里插入图片描述

mkdir -p /var/www/virtual/login/html
vim /var/www/virtual/login/html/index.html

<h1>login page</h1>

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

systemctl restart httpd
在浏览器查看
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 网页重写

vim login.conf

<VirtualHost *:80>
      ServerName    login.westos.com
      RewriteEngine On
      RewriteRule   ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</VirtualHost>    

在这里插入图片描述

注:
#^(/.*)$表示客户在浏览器中输入的所有字符
# 1 表 示 ( / . ∗ ) 1表示^(/.*) 1(/.)的值
#redirect=301表示转换时永久的
#%{HTTP_HOST}表示客户请求主机

systemctl restart httpd
在这里插入图片描述

##wsgi

mkdir /var/www/wsgi
cd /var/www/wsgi/
从172.25.254.250/RHCEPACKAGES/materials上下载script.wsgi
cd /etc/httpd/conf.d/
vim vhost.conf
<VirtualHost *:80>
        ServerName www.westoswsgi.com
        WSGIScriptAlias / /var/www/wsgi/script.wsgi
</VirtualHost>

yum install mod_wsgi -y

vim /etc/hosts
172.25.254.121 www.westoswsgi.com

用浏览器访问www.westoswsgi.com
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值