Apache如何配置https以及80重定向443(一文搞懂)

最近公司项目考虑到安全性要使用https,于是领导就把这个任务交给了我,今天就一次性搞懂https如何配置。

在这里插入图片描述

一、HTTP和HTTPS概念

超文本传输协议HTTP协议被用于在Web浏览器和网站服务器之间传递信息,HTTP协议以明文方式发送内容,不提供任何方式的数据加密,如果攻击者截取了Web浏览器和网站服务器之间的传输报文,就可以直接读懂其中的信息,因此,HTTP协议不适合传输一些敏感信息,比如:信用卡号、密码等支付信息。

为了解决HTTP协议的这一缺陷,需要使用另一种协议:安全套接字层超文本传输协议HTTPS,为了数据传输的安全,HTTPS在HTTP的基础上加入了SSL/TLS协议,SSL/TLS依靠证书来验证服务器的身份,并为浏览器和服务器之间的通信加密。

也就是说https在进行数据加密传输、身份认证等网络协议中要比http安全的多。

二、HTTP和HTTPS区别

  1. https协议是需要到CA申请证书的,一般免费证书较少,因而需要一定费用,在后面的配置中,证书是公司已经申请好的;
  2. http是超文本传输协议,信息是明文传输,https则是具有安全性的ssl/tls加密传输协议;
  3. http和https的连接方式是不同的,用的端口也不一样,前者是80,后者是443;
  4. http的连接很简单,是无状态的;HTTPS协议是由SSL/TLS+HTTP协议构建的可进行加密传输、身份认证的网络协议,比http协议安全。

三、Apache安装

因为公司使用的是Apache服务,所以我就用它来演示,考虑到一些小伙伴可能没有使用过Apache服务,这里我带领大家一起来安装一下。

1. 通过ssh连接到我们的服务器

ssh iyongbao@10.xx.xx.xx
password:

iyongbao是我们服务器的主机名,@后面是服务器ip

2. 使用yum安装apache

  1. 通过yum来下载安装apache

    [root@iyongbao ~]# yum install httpd -y
    
    # 启动apache
    [root@iyongbao ~]# systemctl start httpd
    
    # 设置开机自启动
    [root@iyongbao ~]# systemctl enable httpd
    
    # 查看服务是否启动
    [root@iyongbao ~]# netstat -lntp |grep http
    tcp6 0    0    :::443    :::*      LISTEN      1368511/httpd
    tcp6 0    0    :::80     :::*      LISTEN      1368511/httpd
    

    因为我这里是配置好https服务了,所以端口有一个443在运行。

  2. 访问网站

    通过域名ip就可以访问到apache的默认页,apache的默认端口80,后面我们会让80强制跳转到443

    apache-index.PNG

  3. apache项目路径

上边显示的内容是apache的默认页,我们可以找到它的路径。

# 配置web页面(apache默认web路径)
[root@iyongbao ~]# cat /var/www/html/index.html
  1. apache配置文件
# 通过配置这个文件可以改变我们的端口和项目路径
[root@iyongbao ~]# cat /etc/httpd/conf/httpd.conf

# DocumentRoot: The directory out of which you will serve your
DocumentRoot "/var/www/html"

注意:当你修改了配置文件之后记得要重启

[root@iyongbao ~]# systemctl restart httpd

四、配置证书

证书如果是工作中,一般公司都会去申请,这里我也是直接使用申请下来的。

zhengshu.PNG

1. 安装ssl证书模块

# 安装mod_ssl模块
[root@iyongbao ~]# yum install mod_ssl -y

#查看安装的mod_ssl
[root@iyongbao ~]# rpm -qa mod_ssl
mod_ssl-2.4.57-5.el9.x86_64

# 创建证书的存放目录
[root@iyongbao ~]# mkdir /etc/httpd/ssl
[root@iyongbao ~]# ll /etc/httpd/

2. 配置https

当我们安装好mod_ssl模块后,在我们的/etc/httpd/conf.d目录下会生成一个ssl.conf,下面我们来配置一下这个文件。

# ssl.conf
[root@iyongbao ~]# egrep '^[^#]' /etc/httpd/conf.d/ssl.conf
Listen 443 https

SSLPassPhraseDialog exec:/usr/libexec/httpd-ssl-pass-dialog

SSLSessionCache shmcb:/run/httpd/sslcache(512000)
SSLSessionCacheTimeout 300

SSLRandomSeed startup file:/dev/urandom  256
SSLRandomSeed connect builtin

<VirtualHost _default_:443>
    ServerName utouch-vn-qms-api.ces.myfiinet.com
    ErrorLog logs/ssl_error_log
    TransferLog logs/ssl_access_log
    LogLevel warn
    
    SSLEngine on #启用SSL功能
    SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
    SSLCipherSuite HIGH:!aNULL:!MD5:!SEED:!IDEA	
    
    # 配置证书
    SSLCertificateFile /etc/httpd/www.iyognbao.cn.crt
    SSLCertificateKeyFile /etc/httpd/www.iyongbao.cn.key
    SSLCertificateChainFile /etc/httpd/ssl/root_url.crt
    
    # 配置项目的路径
    <Directory /var/www/my-project>
        Options Indexes FollowSymLinks
        AllowOverride All
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>    

#重启apache服务 
[root@iyongbao ~]# systemctl restart httpd

3. https访问网站

配置好就可以使用https去访问我们的网站了。

https://www.xxxxxx.cn/

安全.png

可以看到我们的网站已经成功上锁了,到此https配置成功。

五、Apache 80跳转443

当我们不以https://开头时,默认是去访问apache80(http)端口,但是为了安全我们要http去跳转到https上,已达到安全的目的。

修改ssl.conf文件

# /etc/httpd/conf.d/ssl.conf
<VirtualHost *:80>
    ServerName www.iyongbao.cn
    RewriteEngine on
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^/?(.*)$ https://%{SERVER_NAME}/$1 [L,R]
</VirtualHost>

#重启apache服务 
[root@iyongbao ~]# systemctl restart httpd

六、总结

这篇文章意在让大家了解https是如何配置的,对于技术其实也是一种增长,虽然我们不需要经常配置它,但是掌握了肯定没有坏事。

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想将 Debian 8.0 上的 Apache 服务器的默认 HTTPS 端口重定向443 端口,可以按照以下步骤操作: 1. 确保你已经安装了 OpenSSL 和 Apache2。 ``` sudo apt-get update sudo apt-get install openssl apache2 ``` 2. 生成一个自签名 SSL 证书。这个证书将用于加密 HTTPS 通信。 ``` sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/apache-selfsigned.key -out /etc/ssl/certs/apache-selfsigned.crt ``` 该命令将要求你输入一些证书信息,例如国家、组织、通用名称等。对于测试目的,可以填写任意值。 3. 将 Apache SSL 配置文件拷贝到 /etc/apache2/sites-available/ 目录下。 ``` sudo cp /etc/apache2/sites-available/default-ssl.conf /etc/apache2/sites-available/default-ssl.conf.bak sudo cp /etc/apache2/sites-available/default-ssl.conf /etc/apache2/sites-available/default-ssl.conf.orig ``` 4. 编辑 default-ssl.conf 配置文件,修改其中的 SSLCertificateFile 和 SSLCertificateKeyFile 配置项,分别指向刚才生成的自签名证书和私钥文件。 ``` sudo nano /etc/apache2/sites-available/default-ssl.conf ``` 将以下两行配置项修改为: ``` SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key ``` 5. 启用 SSL 模块和默认 SSL 虚拟主机。 ``` sudo a2enmod ssl sudo a2ensite default-ssl ``` 6. 重新启动 Apache 服务。 ``` sudo systemctl restart apache2 ``` 7. 编辑 /etc/apache2/sites-available/000-default.conf 文件,添加以下重定向规则: ``` <VirtualHost *:80> ServerName example.com Redirect permanent / https://example.com/ </VirtualHost> ``` 这里的 example.com 指代你自己的域名。 8. 重新启动 Apache 服务。 ``` sudo systemctl restart apache2 ``` 现在,当用户访问 http://example.com 时,将会被重定向https://example.com,并且所有的 HTTPS 通信将会使用自签名证书进行加密。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值