手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux下配置Apache实现HTTPS访问

[日期:2018-08-18] 来源:longlei  作者:Linux [字体: ]

Linux下编译安装Apache并且配置HTTPS访问。https是超文本传输安全协议,是一种网络安全传输协议。http协议传输的数据都没有加密,一些私密的信息不安全,https经由超文本传输协议(http)进行通信,利用SSL/TLS来加密数据包,https开发的主要目的就是为了保护数据传输的安全性。

1、软件环境

HTTPS是以安全为目标的HTTP通道,简单讲是HTTP的安全版。谷歌已经制定了一项长远的计划,它的最终目标是将所有通过HTTP协议呈现的网页标为“不安全”,对于站长来说,部署SSL证书来迁移到HTTPS是一个现实和重要的问题

操作系统:CentOS 7.3

Apache版本:编译安装的Apache 2.4.7

IP地址:192.168.115.120

对应的测试域名:www.awstats.com

关闭selinux和关闭防火墙,也可以提前设置好防火墙的规则

Windows的hosts文件中需要添加”192.168.115.120 www.awstats.com”域名解析

使用Windows浏览器测试是否成功

2、给Apache安装ssl模块

为了使Apache支持https访问,系统需要安有apache、openssl、mod_ssl.so

Linux系统默认安装了openssl,所有就不需要安装了,由于某些原因,apache的ssl模块在最初安装时未能包含进去,在之后又不能重新安装apache,这时就需要在不重新编译apache的情况下新增编译模块,可采用以下方法

(1)进入Apache的源码解压的/modules/ssl目录执行安装命令

[root@www modules]# whereis openssl

openssl: /usr/bin/openssl /usr/lib64/openssl /usr/include/openssl /usr/share/man/man1/openssl.1ssl.gz

[root@www ~]# cd /root/httpd-2.4.7/modules/ssl

[root@www ssl]# apxs -i -c -a -D HAVE_OPENSSL=1 -I /usr/include/openssl -lcrypto -lssl -ldl *.c

/usr/local/apr/build-1/libtool --silent --mode=compile gcc -std=gnu99 -prefer-pic  -DLINUX -D_REENTRANT -D_GNU_SOURCE -g -O2 -pthread -I/usr/local/apache2/include  -I/usr/local/apr/include/apr-1  -I/usr/local/apr-util/include/apr-1  -I/usr/include/openssl -DHAVE_OPENSSL=1  -c -o mod_ssl.lo mod_ssl.c && touch mod_ssl.slo

In file included from mod_ssl.c:27:0:

ssl_private.h:85:30: 致命错误:openssl/opensslv.h:没有那个文件或目录

 #include <openssl/opensslv.h>

                              ^

编译中断。

apxs:Error: Command failed with rc=65536

上面出现编译报错,说缺少openssl某个文件

解决方法:

[root@www ssl]# yum -y install openssl-devel

上面安装完成之后,接着执行之前的编译模块命令

[root@www ssl]# apxs -i -c -a -D HAVE_OPENSSL=1 -I /usr/include/openssl -lcrypto -lssl -ldl *.c

…………………………………………………………………………………………………………………省略若干

libtool: install: chmod 644 /usr/local/apache2/modules/mod_ssl.a

libtool: install: ranlib /usr/local/apache2/modules/mod_ssl.a

libtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/sbin" ldconfig -n /usr/local/apache2/modules

----------------------------------------------------------------------

Libraries have been installed in:

  /usr/local/apache2/modules

If you ever happen to want to link against installed libraries

in a given directory, LIBDIR, you must either use libtool, and

specify the full pathname of the library, or use the '-LLIBDIR'

flag during linking and do at least one of the following:

  - add LIBDIR to the 'LD_LIBRARY_PATH' environment variable

    during execution

  - add LIBDIR to the 'LD_RUN_PATH' environment variable

    during linking

  - use the '-Wl,-rpath -Wl,LIBDIR' linker flag

  - have your system administrator add LIBDIR to '/etc/ld.so.conf'

See any operating system documentation about shared libraries for

more information, such as the ld(1) and ld.so(8) manual pages.

----------------------------------------------------------------------

chmod 755 /usr/local/apache2/modules/mod_ssl.so

[activating module `ssl' in /usr/local/apache2/conf/httpd.conf]

apxs命令参数说明:

-i  此选项表示需要执行安装操作,以安装一个或多个动态共享对象到服务器的modules目录中。

-a  此选项自动增加一个LoadModule行到httpd.conf文件中,以激活此模块,或者,如果此行已经存在,则启用之。

-A  与 -a 选项类似,但是它增加的LoadModule命令有一个井号前缀(#),即此模块已经准备就绪但尚未启用。

-c  此选项表示需要执行编译操作。它首先会编译C源程序(.c)files为对应的目标代码文件(.o),然后连接这些目标代码和files中其余的目标代码文件(.o和.a),以生成动态共享对象dsofile 。如果没有指定 -o 选项,则此输出文件名由files中的第一个文件名推测得到,也就是默认为mod_name.so

(2)查看安装的ssl模块

[root@www ssl]# ls -l /usr/local/apache2/modules/ | grep ssl

-rwxr-xr-x 1 root root 945280 5月  16 15:20 mod_ssl.so

3、申请证书或自己生成自签名证书

我们可以使用openssl工具自己生成证书或者到各大厂商去申请免费证书,可满足个人网站的需求,如企业网站,建议购买企业收费证书。

这里我们就自己生成一个自签名证书

首先,生成2048位的加密私钥

[root@www ~]# openssl genrsa -out server.key 2048

Generating RSA private key, 2048 bit long modulus

....................+++

..+++

e is 65537 (0x10001)

然后,生成证书签名请求(CSR),这里需要填写许多信息,如国家,省市,公司等

[root@www ~]# openssl req -new -key server.key -out server.csr

You are about to be asked to enter information that will be incorporated

into your certificate request.

What you are about to enter is what is called a Distinguished Name or a DN.

There are quite a few fields but you can leave some blank

For some fields there will be a default value,

If you enter '.', the field will be left blank.

-----

Country Name (2 letter code) [XX]:cn

State or Province Name (full name) []:bj

Locality Name (eg, city) [Default City]:bj

Organization Name (eg, company) [Default Company Ltd]:ll

Organizational Unit Name (eg, section) []:lz

Common Name (eg, your name or your server's hostname) []:www.awstats.com

注意这里的主机名一定要和httpd.conf文件中的ServerName www.awstats.com保持一致,否则会报错。

Email Address []:123456@163.com

Please enter the following 'extra' attributes

to be sent with your certificate request

A challenge password []:123456

An optional company name []:

最后,生成类型为X509的自签名证书。有效期设置3650天,即有效期为10年

[root@www ~]# openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt

Signature ok

subject=/C=cn/ST=bj/L=bj/O=ll/OU=lz/CN=www.awstats.com/emailAddress=123456@163.com

Getting Private key

4、复制秘钥和证书文件到指定位置

[root@www ~]# cp server.crt server.key /usr/local/apache2/conf

5、修改httpd-ssl.conf文件指定相关秘钥和证书位置

[root@www ~]# vim /usr/local/apache2/conf/extra/httpd-ssl.conf

<VirtualHost _default_:443>

DocumentRoot "/usr/local/apache2/htdocs"

ServerName www.awstats.com:443

ServerAdmin you@example.com

ErrorLog "/usr/local/apache2/logs/error_log"

TransferLog "/usr/local/apache2/logs/access_log"

SSLEngine on

SSLCertificateFile "/usr/local/apache2/conf/server.crt"

SSLCertificateKeyFile "/usr/local/apache2/conf/server.key"

……………………………………………..省略若干

</VirtualHost>

6、修改Apache主配置文件并开启相关模块

[root@www ~]# vim /usr/local/apache2/conf/httpd.conf

打开相关的注释,启用需要的模块

LoadModule rewrite_module modules/mod_rewrite.so

LoadModule ssl_module        modules/mod_ssl.so

LoadModule socache_shmcb_module modules/mod_socache_shmcb.so

修改主机名

ServerName www.awstats.com

下面的需要添加进来

RewriteEngine on

RewriteCond %{SERVER_PORT} !^443$

RewriteCond %{REQUEST_URI} !^/tz.php

RewriteRule (.*) https://%{SERVER_NAME}/$1 [R]

7、相关说明

配置文件参数

说明

LoadModule

加载SSL模块

Listen

监听443端口

DocumentRoot

网页目录

ServerName

站点域名

SSLEngine   on

启用SSL功能

SSLCertificateFile

证书文件

SSLCertificateKeyFile

私钥文件

SSLCertificateChainFile

证书链文件

8、重启Apache并检查相应端口是否开启

[root@www ~]# httpd -t

Syntax OK

[root@www ~]# systemctl restart httpd

[root@www ~]# ss -ant

State      Recv-Q Send-Q        Local Address:Port      Peer Address:Port                                 

LISTEN      0      128                :::80            :::*                 

LISTEN      0      128                :::22            :::*                 

LISTEN      0      100                ::1:25            :::*                 

LISTEN      0      128                :::443            :::*     

9、在Windows浏览器测试访问域名是否能自动成功跳转到https

在浏览器地址栏输入www.awstats.com域名,然后回车

上面回车之后,会自动跳转到下面的页面,我们点击“高级”选项会展开相关内容

上面点击“高级”选项之后,会出现下面的页面,我们点击下面的红框部分就可以进入下一步

上面点击红框部分之后,就会出现下面的网页了。因为是自己生成的证书不被浏览器识别,所以会出现下面的页面,说此站点不安全,我们直接不用管点击详细信息并接着点击“转到此网页(不推荐)”选项即可进入我们需要的网页了


下面是我们需要访问的最终页面,如果是在各大厂商申请的证书就不会出现以上提示不安全的信息,会直接进入我们下面的界面。


二、yum安装的Apache配置https方法

1、安装相关软件

[root@localhost ~]# yum -y install httpd

[root@localhost ~]# yum -y install mod_ssl

2、修改相关的配置文件

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf

去掉下面几行注释

Include conf.modules.d/*.conf

ServerName www.awstats.com

IncludeOptional conf.d/*.conf

下面的需要添加在主配置文件中或虚拟主机中

RewriteEngine on

RewriteCond %{SERVER_PORT} !^443$

RewriteCond %{REQUEST_URI} !^/tz.php

RewriteRule (.*) https://%{SERVER_NAME}/$1 [R]

[root@localhost conf.modules.d]# vim /etc/httpd/conf.modules.d/00-ssl.conf

去掉下面的注释

LoadModule ssl_module modules/mod_ssl.so

[root@localhost ~]# vim /etc/httpd/conf.modules.d/00-base.conf

去掉下面的注释

LoadModule rewrite_module modules/mod_rewrite.so

3、生成相关的证书及秘钥文件

[root@localhost ~]# openssl genrsa -out server.key 2048

Generating RSA private key, 2048 bit long modulus

.................................................................+++

..........................+++

e is 65537 (0x10001)

[root@localhost ~]# openssl req -new -key server.key -out server.csr

You are about to be asked to enter information that will be incorporated

into your certificate request.

What you are about to enter is what is called a Distinguished Name or a DN.

There are quite a few fields but you can leave some blank

For some fields there will be a default value,

If you enter '.', the field will be left blank.

-----

Country Name (2 letter code) [XX]:cn

State or Province Name (full name) []:bj

Locality Name (eg, city) [Default City]:bj

Organization Name (eg, company) [Default Company Ltd]:ll

Organizational Unit Name (eg, section) []:lz

Common Name (eg, your name or your server's hostname) []:www.awstats.com

Email Address []:123456@163.com

Please enter the following 'extra' attributes

to be sent with your certificate request

A challenge password []:123456

An optional company name []:

[root@localhost ~]# openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt

Signature ok

subject=/C=cn/ST=bj/L=bj/O=ll/OU=lz/CN=www.awstats.com/emailAddress=123456@163.com

Getting Private key

4、修改配置文件中对应的秘钥文件

[root@localhost ~]# vim /etc/httpd/conf.d/ssl.conf

去掉相关注释并更改证书和私钥文件

SSLEngine on

SSLCertificateFile /etc/pki/tls/certs/server.crt

SSLCertificateKeyFile /etc/pki/tls/private/server.key

复制生成的秘钥文件到ssl.conf指定的位置

[root@localhost ~]# cp /root/server.crt  /etc/pki/tls/certs/

[root@localhost ~]# cp /root/server.key  /etc/pki/tls/private/

5、验证配置是否有问题并重启Apache服务

[root@localhost ~]# httpd -t

Syntax OK

[root@localhost ~]# systemctl restart httpd

6、在Windows浏览器测试访问域名是否能自动成功跳转到https

打开浏览器,输入IP或域名会自动跳转到https,说明配置成功,因为是自己生成的证书不被浏览器识别,所以会出现下面的页面,说此站点不安全,我们直接不用管点,击详细信息

上面点击详细信息之后,或展开下面的页面,我们点击“转到此网页(不推荐)”即可进入我们需要访问的网站页面。

Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址https://www.linuxidc.com/Linux/2018-08/153555.htm

linux
相关资讯       https  Apache实现HTTPS 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款