Apache配置实现https访问php
HTTPS是以安全为目标的HTTP通道,简单讲是HTTP的安全版。谷歌已经制定了一项长远的计划,它的最终目标是将全部经过HTTP协议呈现的网页标为“不安全”,对于站长来讲,部署SSL证书来迁移到HTTPS是一个现实和重要的问题linux
操做系统:CentOS 7.3apache
Apache版本:编译安装的Apache 2.4.7vim
IP地址:192.168.115.120浏览器
对应的域名:www.awstats.com安全
关闭selinux和关闭防火墙,也能够提早设置好防火墙的规则服务器
Windows的hosts文件中须要添加”192.168.115.120 www.awstats.com”域名解析app
使用Windows浏览器测试是否成功ide
为了使Apache支持https访问,系统须要安有apache、openssl、mod_ssl.so工具
Linux系统默认安装了openssl,全部就不须要安装了,因为某些缘由,apache的ssl模块在最初安装时未能包含进去,在以后又不能从新安装apache,这时就须要在不从新编译apache的状况下新增编译模块,可采用如下方法
[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
[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
咱们可使用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
[root@www ~]# cp server.crt server.key /usr/local/apache2/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>
[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]
配置文件参数 |
说明 |
LoadModule |
加载SSL模块 |
Listen |
监听443端口 |
DocumentRoot |
网页目录 |
ServerName |
站点域名 |
SSLEngine on |
启用SSL功能 |
SSLCertificateFile |
证书文件 |
SSLCertificateKeyFile |
私钥文件 |
SSLCertificateChainFile |
证书链文件 |
[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 :::*
在浏览器地址栏输入www.awstats.com域名,而后回车
上面回车以后,会自动跳转到下面的页面,咱们点击“高级”选项会展开相关内容
上面点击“高级”选项以后,会出现下面的页面,咱们点击下面的红框部分就能够进入下一步
上面点击红框部分以后,就会出现下面的网页了。由于是本身生成的证书不被浏览器识别,因此会出现下面的页面,说此站点不安全,咱们直接不用管点击详细信息并接着点击“转到此网页(不推荐)”选项便可进入咱们须要的网页了
下面是咱们须要访问的最终页面,若是是在各大厂商申请的证书就不会出现以上提示不安全的信息,会直接进入咱们下面的界面。
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# yum -y install mod_ssl
[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
[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
[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/
[root@localhost ~]# httpd -t
Syntax OK
[root@localhost ~]# systemctl restart httpd
打开浏览器,输入IP或域名会自动跳转到https,说明配置成功,由于是本身生成的证书不被浏览器识别,因此会出现下面的页面,说此站点不安全,咱们直接不用管点,击详细信息
上面点击详细信息以后,或展开下面的页面,咱们点击“转到此网页(不推荐)”便可进入咱们须要访问的网站页面。