Linux系统之Apache

本文详细介绍了Apache HTTP Server的安装、配置与管理,包括基本设定、服务端口、虚拟主机、语言支持、https加密及自签名证书的创建,以及从http到https的网页重写。Apache作为流行的Web服务器软件,其安全性和多平台特性使得它在各种场景下得到广泛应用。

什么是Apache

Apache HTTP Server(简称Apache):是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其多平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩展,将Perl/Python等解释器编译到服务器中。
Apache的默认服务端口为:80

##Apache的默认设定

默认发布目录:/var/www/html/
默认访问页面:index.html 里面可以写东西,就会显示在网页
默认端口:80
默认安全上下文:http_sys_content_t
程序开启默认用户:apache
apache日志:/etc/httpd/logs/*
主配置目录:/etc/httpd/
主配置文件:/etc/httpd/conf/httpd.conf
子配置目录:/etc/httpd/conf.d
子配置文件:/etc/httpd/conf.d/*.conf

安装httpd服务

[root@http ~]# yum install httpd -y
Loaded plugins: langpacks
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-45.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package         Arch             Version                 Repository       Size
================================================================================
Installing:
 httpd           x86_64           2.4.6-45.el7            rhel7           1.2 M

Transaction Summary
================================================================================
Install  1 Package

Total download size: 1.2 M
Installed size: 3.7 M
Downloading packages:
httpd-2.4.6-45.el7.x86_64.rpm                              | 1.2 MB   00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : httpd-2.4.6-45.el7.x86_64                                    1/1 
  Verifying  : httpd-2.4.6-45.el7.x86_64                                    1/1 

Installed:
  httpd.x86_64 0:2.4.6-45.el7                                                   

Complete!

##开启httpd
[root@http ~]# systemctl start httpd
##查看http服务端口
[root@http ~]# netstat -antuple | grep httpd
tcp6 0 0 :::80 ::? LISTEN 0 91836 3028/httpd
##进入http服务默认端口
[root@dhcp ~]# cd /var/www/html/
##编写http服务默认发布页面,该页面的内容访问时会出现
[root@http html]# vim index.html

<h1>hello wold!</h1>

##让火墙允许http服务通过
[root@http html]# firewall-cmd --permanent --add-service=http
success
##让火墙允许https服务通过
[root@http html]# firewall-cmd --permanent --add-service=https
success
##重启火墙服务
[root@http html]# firewall-cmd --reload
success
##查看火墙服务的状态
[root@http html]# firewall-cmd --list-all
public (default, active)
interfaces: eth0
sources:
services: dhcp dhcpv6-client ssh http https
ports:
masquerade: no
forward-ports:
icmp-blocks:
rich rules:

##验证是否成功
在这里插入图片描述

Apache的基本配置

##修改默认发布文件
##注释掉原来的 添加新的默认发布文件为cai.html
[root@http ~]# vim /etc/httpd/conf/httpd.conf
在这里插入图片描述
[root@http ~]# vim /var/www/html/cai.html
[root@http ~]# systemctl restart httpd.service
[root@http ~]# ls /var/www/html/
cai.html index.html
[root@http ~]# cat /var/www/html/cai.html
cai.westos.com
[root@http ~]# cat /var/www/html/index.html
hello wold!

测试:在网页上输入172.25.254.108后出现cai.html文件内容
在这里插入图片描述

##修改默认发布目录
##修改默认发布目录为/httphome/http/html,默认发布页面为tian.html
[root@http ~]# vim /etc/httpd/conf/httpd.conf
在这里插入图片描述
[root@http ~]# mkdir -p /httphome/http/html
[root@http ~]# vim /httphome/http/html/tian.html
[root@http ~]# systemctl restart httpd.service
[root@http ~]#cat/httphome/http/html/tian.html
tian.westos.com
测试:在网页上输入172.25.254.108/tian.html出现生日定后的页面
在这里插入图片描述
##Apache的端口设定
在Apache服务中,默认是80端口开放服务
我们可以通过修改配置文件修改默认端口
[root@http ~]# vim /etc/httpd/conf/httpd.conf
在这里插入图片描述
[root@http ~]# vim /etc/httpd/conf/httpd.conf
[root@http ~]# systemctl restart httpd.service
[root@http ~]# netstat -antuple | grep httpd
tcp6 0 0 :::8080 ::? LISTEN 0 133427 32724/httpd
[root@http ~]# firewall-cmd --permanent --add-port=8080/tcp
success
[root@http ~]# firewall-cmd --reload
success

测试:
默认为80端口,如果不加修改后的端口号,页面报错
在这里插入图片描述

添加8080端口号,页面出现
在这里插入图片描述

##apache的访问控制(设定ip的访问和设定用户的访问)
第一种:设定ip的访问
[root@http ~]# vim /etc/httpd/conf/httpd.conf
在这里插入图片描述
[root@http ~]# systemctl restart httpd.service

测试:
在ip为172.25.254.8的物理机中访问westos目录,被拒绝
在这里插入图片描述
在ip为172.25.254.108的物理机中,访问westos目录,成功
在这里插入图片描述

注意:如果顺序写错会被后写的覆盖

第二种:设定用户的访问

注意:htpasswd命令的参数:

-c:创建一个加密文件;
-m:默认采用MD5算法对密码进行加密;
-n:不更新加密文件,只将加密后的用户密码显示在屏幕上;
-d:采用CRYPT算法对密码进行加密
-p:不对密码进行加密,即明文密码
-s:采用SHA算法对密码进行加密
-b:在命令行一并输入用户名和密码而不是根据提示输入密码
-D:删除指定用户;

###建立登陆的http用户以及密码。名字为: westosuser。第一次建立要加c,第二次不用
[root@http ~]# htpasswd -cm westosuser admin
New password:
Re-type new password:
Adding password for user admin
[root@http ~]# htpasswd -m westosuser admin1
New password:
Re-type new password:
Adding password for user admin1
##成功后在当前文件下生成密码文件westosuser
[root@http ~]# ls
anaconda-ks.cfg Documents Music Public Videos
Desktop Downloads Pictures Templates westosuser
##查看密码
[root@http ~]# cat westosuser
admin:apr1apr1apr1RB5ouU89iDnH3I586.wVwueUHg.2u1admin1:iDnH3I586.wVwueUHg.2u1 admin1:iDnH3I586.wVwueUHg.2u1admin1:apr1$5ob.QjZP$4OsNyVZV9IOBOwP1Ks1s3/
##编辑主配置文件
[root@http ~]# vim /etc/httpd/conf/httpd.conf
<Directory “/var/www/html/westos”> ##需要加密的目录
AuthUserFile /etc/httpd/conf/westosuser ##用户认证文件
AuthType basic ##认证类型
AuthName “password is 123”##用户认证提示信息
Require user admin ##只允许认证文件中admin用户访问二写一
Require valid-user ##认证用户,认证文件中有的用户可以通过(以上两个参数选其一)

##移动密码文件
[root@http ~]# cp -p westosuser /etc/httpd/conf/westosuser
##查看是否移动成功
[root@http ~]# ls /etc/httpd/conf/
httpd.conf magic westosuser
##重启服务
[root@http ~]# systemctl restart httpd.service
在这里插入图片描述

测试:
##访问页面需要密码
在这里插入图片描述
##密码输入正确后才可以访问
在这里插入图片描述

apache的虚拟主机

功能:可以让我们的一台apache服务器在被访问不同域名的时候显示不同的主页
##建立虚拟主机页面music和news
[root@http ~]# mkdir -p /var/www/virhome/music.westos.com/html
[root@http ~]# mkdir -p /var/www/virhome/news.westos.com/html
[root@http ~]# echo “music.westos.com” > /var/www/virhome/music.westos.com/html/index.html
[root@http ~]# echo “news.westos.com” > /var/www/virhome/news.westos.com/html/index.html
[root@http ~]# cat /var/www/virhome/music.westos.com/html/index.html
“music.westos.com”
[root@http ~]# cat /var/www/virhome/news.westos.com/html/index.html “news.westos.com”
在这里插入图片描述
##编写解析文件,使页面对应起来
[root@http ~]# vim /etc/httpd/conf.d/default.conf

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


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


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

在这里插入图片描述
##在本地解析文件中编写解析
[root@http ~]# vim /etc/hosts
[root@http ~]# tail -n 1 /etc/hosts
172.25.254.108 www.westos.com music.westos.com news.westos.com
##测试:重启服务后进行测试
[root@http ~]# systemctl restart httpd
[root@http ~]# curl news.westos.com
“news.westos.com”
[root@http ~]# curl music.westos.com
“music.westos.com”

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

apache语言支持

1)html语言
超文本标记语言(Hyper Text Markup Language),标准通用标记语言下的一个应用。HTML 不是一种编程语言,而是一种标记语言 (markup language),是网页制作所必备的。“超文本”就是指页面内可以包含图片、链接,甚至音乐、程序等非文字元素。超文本标记语言(或超文本标签语言)的结构包括“头”部分、和“主体”部分,其中“头”部提供关于网页的信息,“主体”部分提供网页的具体内容。
html语言(一般以".html"结尾),Apache默认支持该语言。
2)php语言
PHP即“超文本预处理器”,是一种通用开源脚本语言。PHP是在服务器端执行的脚本语言,与C语言类似,是常用的网站编程语言。
一般以".php结尾"
##安装php
[root@http /]# yum install php

[root@http /]# yum install php
Loaded plugins: langpacks
Resolving Dependencies
--> Running transaction check
---> Package php.x86_64 0:5.4.16-42.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package       Arch             Version                   Repository       Size
================================================================================
Installing:
 php           x86_64           5.4.16-42.el7             rhel7           1.4 M

Transaction Summary
================================================================================
Install  1 Package

Total download size: 1.4 M
Installed size: 4.4 M
Is this ok [y/d/N]: y
Downloading packages:
php-5.4.16-42.el7.x86_64.rpm                               | 1.4 MB   00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : php-5.4.16-42.el7.x86_64                                     1/1 
  Verifying  : php-5.4.16-42.el7.x86_64                                     1/1 

Installed:
  php.x86_64 0:5.4.16-42.el7                                                    

Complete!

##编辑/var/www/html/index.php文件

[root@http /]# vim /var/www/html/index.php 
[root@http /]# cat /var/www/html/index.php 
<?php
	phpinfo();
?>

##重启服务,并在浏览器输入IP/index.php进行测试
在这里插入图片描述

3)cgi语言
公共网关接口 CGI 程序是存放在 HTTP 服务器上,为用户和HTTP服务器之外的其他应用程序提供互相“交谈”手段的软件

[root@http ~]# mkdir /var/www/html/cgi
[root@http ~]# vim /var/www/html/cgi/index.cgi
[root@http ~]# chmod 775 /var/www/html/cgi/index.cgi 
[root@http ~]# vim /etc/httpd/conf.d/default.conf
[root@http ~]# systemctl restart httpd.service 
[root@http ~]# tail -n4 /etc/httpd/conf.d/default.conf
<Directory /var/www/html>
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>

在这里插入图片描述

4)python语言(python脚本语言需要网络服务器网关接口即WSGI)
WSGI是Web Server Gateway Interface的缩写。以层的角度来看,WSGI所在层的位置低于CGI。但与CGI不同的是WSGI具有很强的伸缩性且能运行于多线程或多进程的环境下,这是因为WSGI只是一份标准并没有定义如何去实现。

[root@http ~]# mkdir /var/www/wsgi
[root@http ~]# yum install mod_wsgi.x86_64 -y
Loaded plugins: langpacks
Resolving Dependencies
--> Running transaction check
---> Package mod_wsgi.x86_64 0:3.4-12.el7_0 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package           Arch            Version                 Repository      Size
================================================================================
Installing:
 mod_wsgi          x86_64          3.4-12.el7_0            rhel7           76 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 76 k
Installed size: 197 k
Downloading packages:
mod_wsgi-3.4-12.el7_0.x86_64.rpm                           |  76 kB   00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mod_wsgi-3.4-12.el7_0.x86_64                                 1/1 
  Verifying  : mod_wsgi-3.4-12.el7_0.x86_64                                 1/1 

Installed:
  mod_wsgi.x86_64 0:3.4-12.el7_0                                                

Complete!
[root@http ~]# vim /var/www/wsgi/index.wsgi
[root@http ~]# cat /var/www/wsgi/index.wsgi
#!/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]
[root@http ~]# vim /etc/httpd/conf.d/default.conf 
[root@http ~]# systemctl restart httpd.service 
[root@http ~]# vim /etc/hosts
[root@http ~]# vim /etc/httpd/conf.d/default.conf 
[root@http ~]# systemctl restart httpd.service 
Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.
[root@http ~]# vim /etc/httpd/conf.d/default.conf 
[root@http ~]# systemctl restart httpd.service 
[root@http ~]# vim /etc/httpd/conf.d/default.conf 
[root@http ~]# systemctl restart httpd.service 

在这里插入图片描述

https加密

##什么是https
HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer 或 Hypertext Transfer Protocol Secure,超文本传输安全协议),是以安全为目标的HTTP通道,简单讲是HTTP的安全版。
HTTPS在HTTP的基础上加入了SSL/TLS协议,SSL/TLS依靠证书来验证服务器的身份,并为浏览器和服务器之间的通信加密。
HTTPS协议是由SSL/TLS+HTTP协议构建的可进行加密传输、身份认证的网络协议,要比http协议安全

##https和http的区别

1、https协议需要到CA申请证书,一般免费证书较少,因而需要一定费用。

2、http是超文本传输协议,信息是明文传输,https则是具有安全性的ssl/tls加密传输协议。

3、http和https使用的是完全不同的连接方式,用的端口也不一样,前者是80,后者是443。

4、http的连接很简单,是无状态的;HTTPS协议是由SSL/TLS+HTTP协议构建的可进行加密传输、身份认证的网络协议,比http协议安全。

##apache服务加密

[root@http0 ~]# yum install mod_ssl.x86_64 -y
已加载插件:langpacks
正在解决依赖关系
--> 正在检查事务
---> 软件包 mod_ssl.x86_64.1.2.4.6-45.el7 将被 安装
--> 正在处理依赖关系 openssl-libs >= 1:1.0.1e-37,它被软件包 1:mod_ssl-2.4.6-45.el7.x86_64 需要
--> 正在检查事务
---> 软件包 openssl-libs.x86_64.1.1.0.1e-34.el7 将被 升级
--> 正在处理依赖关系 openssl-libs(x86-64) = 1:1.0.1e-34.el7,它被软件包 1:openssl-1.0.1e-34.el7.x86_64 需要
---> 软件包 openssl-libs.x86_64.1.1.0.1e-60.el7 将被 更新
--> 正在检查事务
---> 软件包 openssl.x86_64.1.1.0.1e-34.el7 将被 升级
---> 软件包 openssl.x86_64.1.1.0.1e-60.el7 将被 更新
--> 解决依赖关系完成

依赖关系解决

================================================================================
 Package             架构          版本                   源               大小
================================================================================
正在安装:
 mod_ssl             x86_64        1:2.4.6-45.el7         rhel_dvd        105 k
为依赖而更新:
 openssl             x86_64        1:1.0.1e-60.el7        rhel_dvd        713 k
 openssl-libs        x86_64        1:1.0.1e-60.el7        rhel_dvd        958 k

事务概要
================================================================================
安装  1 软件包
升级           ( 2 依赖软件包)

总下载量:1.7 M
Downloading packages:
No Presto metadata available for rhel_dvd
(1/3): mod_ssl-2.4.6-45.el7.x86_64.rpm                     | 105 kB   00:00     
(2/3): openssl-libs-1.0.1e-60.el7.x86_64.rpm               | 958 kB   00:00     
(3/3): openssl-1.0.1e-60.el7.x86_64.rpm                    | 713 kB   00:00     
--------------------------------------------------------------------------------
总计                                                11 MB/s | 1.7 MB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在更新    : 1:openssl-libs-1.0.1e-60.el7.x86_64                         1/5 
  正在更新    : 1:openssl-1.0.1e-60.el7.x86_64                              2/5 
  正在安装    : 1:mod_ssl-2.4.6-45.el7.x86_64                               3/5 
  清理        : 1:openssl-1.0.1e-34.el7.x86_64                              4/5 
  清理        : 1:openssl-libs-1.0.1e-34.el7.x86_64                         5/5 
  验证中      : 1:openssl-libs-1.0.1e-60.el7.x86_64                         1/5 
  验证中      : 1:openssl-1.0.1e-60.el7.x86_64                              2/5 
  验证中      : 1:mod_ssl-2.4.6-45.el7.x86_64                               3/5 
  验证中      : 1:openssl-libs-1.0.1e-34.el7.x86_64                         4/5 
  验证中      : 1:openssl-1.0.1e-34.el7.x86_64                              5/5 

已安装:
  mod_ssl.x86_64 1:2.4.6-45.el7                                                 

作为依赖被升级:
  openssl.x86_64 1:1.0.1e-60.el7       openssl-libs.x86_64 1:1.0.1e-60.el7      

完毕!
[root@http0 ~]# systemctl restart httpd
[root@http0 ~]# cd /etc/
Display all 140 possibilities? (y or n)
[root@http0 ~]# cd /etc/httpd/conf.d/
[root@http0 conf.d]# ls
autoindex.conf  README  ssl.conf  userdir.conf  welcome.conf
[root@http0 conf.d]# systemctl start firewalld.service 
[root@http0 conf.d]# firewall-cmd --permanent --add-service=httpd
Error: INVALID_SERVICE: httpd
[root@http0 conf.d]# firewall-cmd --permanent --add-service=https
success
[root@http0 conf.d]# firewall-cmd --reload 
success
[root@http0 conf.d]# firewall-cmd --list-all
public (default, active)
  interfaces: eth0 eth1 team0
  sources: 
  services: dhcpv6-client http https ssh
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 
	

[root@http0 conf.d]# netstat -antlupe | grep httpd
tcp6       0      0 :::443                  :::*                    LISTEN      0          373987     7305/httpd          
tcp6       0      0 :::80                   :::*                    LISTEN      0          373979     7305/httpd          

测试:在浏览器中输入https://172.25.70.250,出现以下界面
在这里插入图片描述
但是我们并没有证书,所以需要制作自己的证书和密钥

自签名证书

##安装自签名证书软件
[root@http0 conf.d]# yum install crypto-utils.x86_64 -y
已加载插件:langpacks
正在解决依赖关系
--> 正在检查事务
---> 软件包 crypto-utils.x86_64.0.2.4.1-42.el7 将被 安装
--> 正在处理依赖关系 perl(Newt),它被软件包 crypto-utils-2.4.1-42.el7.x86_64 需要
--> 正在检查事务
---> 软件包 perl-Newt.x86_64.0.1.08-36.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

================================================================================
 Package             架构          版本                   源               大小
================================================================================
正在安装:
 crypto-utils        x86_64        2.4.1-42.el7           rhel_dvd         78 k
为依赖而安装:
 perl-Newt           x86_64        1.08-36.el7            rhel_dvd         64 k

事务概要
================================================================================
安装  1 软件包 (+1 依赖软件包)

总下载量:143 k
安装大小:347 k
Downloading packages:
(1/2): crypto-utils-2.4.1-42.el7.x86_64.rpm                |  78 kB   00:00     
(2/2): perl-Newt-1.08-36.el7.x86_64.rpm                    |  64 kB   00:00     
--------------------------------------------------------------------------------
总计                                               912 kB/s | 143 kB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : perl-Newt-1.08-36.el7.x86_64                                1/2 
  正在安装    : crypto-utils-2.4.1-42.el7.x86_64                            2/2 
  验证中      : crypto-utils-2.4.1-42.el7.x86_64                            1/2 
  验证中      : perl-Newt-1.08-36.el7.x86_64                                2/2 

已安装:
  crypto-utils.x86_64 0:2.4.1-42.el7                                            

作为依赖被安装:
  perl-Newt.x86_64 0:1.08-36.el7                                                

完毕!
##对www.westos.com主机生成证书和密钥!
[root@http0 ~]# genkey www.westos.com
/usr/bin/keyutil -c makecert -g 1024 -s "CN=www.westos.com, OU=hahaha, O=westos, L=Xi'an, ST=ShaanXi, C=CA" -v 1 -a -z /etc/pki/tls/.rand.10645 -o /etc/pki/tls/certs/www.westos.com.crt -k /etc/pki/tls/private/www.westos.com.key
cmdstr: makecert

cmd_CreateNewCert
command:  makecert
keysize = 1024 bits
subject = CN=www.westos.com, OU=hahaha, O=westos, L=Xi'an, ST=ShaanXi, C=CA
valid for 1 months
random seed from /etc/pki/tls/.rand.10645
output will be written to /etc/pki/tls/certs/www.westos.com.crt   //锁头
output key written to /etc/pki/tls/private/www.westos.com.key   //密钥


Generating key. This may take a few moments...

Made a key
Opened tmprequest for writing
/usr/bin/keyutil Copying the cert pointer
Created a certificate
Wrote 882 bytes of encoded data to /etc/pki/tls/private/www.westos.com.key 
Wrote the key to:
/etc/pki/tls/private/www.westos.com.key

##告诉你密钥和证书生成的位置:选择Next
在这里插入图片描述
在这里插入图片描述

##正在生成:加密位数,加密的位数越长,时间就越长,会随即收取在键盘输入的字符进行加密,如果加密字符不够,会止步不前,所以要确保加密字符足够!敲击键盘。
在这里插入图片描述
在这里插入图片描述

##是否发送给CA机构进行认证:不发
在这里插入图片描述
##是否要输入密码:每次使用都要输密码——不输
在这里插入图片描述

##填写该证书的信息

在这里插入图片描述

##在配置文件中写入锁和密码,使Apach知道
[root@http0 ~]# vim /etc/httpd/conf.d/ssl.conf
SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
在这里插入图片描述
##重启服务
[root@http0 ~]# systemctl restart httpd.service
##测试:点击高级—>添加列外—>查看证书状态
在这里插入图片描述

网页重写(http->https)

在客户端输入ip(80端口)自动跳转到为https://ip(443端口)

##进入配置文件中配置https服务
[root@http0 ~]# cd /etc/httpd/conf.d/
[root@http0 conf.d]# vim default.conf

<VirtualHost *:443>            ##建立一个443端口虚拟主机,https需要开启443端口
       ServerName      "bbs.westos.com"
       DocumentRoot    "/var/www/virhome/bbs.westos.com/html"   ##建立默认发布目录
       CustomLog       "logs/news.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/virhome/bbs.westos.com/html">           ##授权
       Require all granted
</Directory>

在这里插入图片描述
##建立虚拟主机/bbs.westos.com的默认发布目录
[root@http0 conf.d]# mkdir /var/www/virhome/bbs.westos.com/html/ -p
##建立虚拟主机/bbs.westos.com的默认发布页面
[root@http0 conf.d]# vim /var/www/virhome/bbs.westos.com/html/index.html
[root@http0 conf.d]# cat /var/www/virhome/bbs.westos.com/html/index.html
bbs.westos.com
##重启服务
[root@http0 conf.d]# systemctl restart httpd.service
##把bbs.westos.com加入解析
[root@foundation70 ~]# tail -n 1 /etc/hosts
172.25.254.170 music.westos.com news.westos.com www.westoswsgi.com bbs.westos.com
测试:
使用https://bbs.westos.com输入网页 发生变化
在这里插入图片描述
但使用http://bbs.westos.com输入网页 是原来的
在这里插入图片描述
说明https的搭建成功,但并不会自动跳转

##修改配置文件,使其自动跳转
[root@http0 conf.d]# vim default.conf

<VirtualHost *:80>      ##建立一个80端口的虚拟主机
       ServerName      "bbs.westos.com"
        RewriteEngine   On       ##打开网页重写的功能开启
        RewriteRule     ^(/.*)$ https://%{HTTP_HOST}$1 [REDIRECT=301]              ##(状态码,永久重定向:永久跳转)
                     客户端输入的内容       客户端主机       
</VirtualHost>

在这里插入图片描述
##重启服务
[root@http0 conf.d]# systemctl restart httpd.service
##测试
在真机输入:bbs.westos.com 可以自动跳转到 https://bbs.westos.com

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值