写自:20230127 18:50
(1) 在Apache的安装目录下创建cert目录,并且将下载的全部文件拷贝到cert目录中。如果申请证书时是自己创建的CSR文件,请将对应的私钥文件放到cert目录下并且命名为214252370570514.key;
(2) 打开 apache 安装目录下 conf 目录中的 httpd.conf 文件,找到以下内容并去掉“#”:
#LoadModule ssl_module modules/mod_ssl.so (如果找不到请确认是否编译过 openssl 插件)
#Include conf/extra/httpd-ssl.conf
(3) 打开 apache 安装目录下 conf/extra/httpd-ssl.conf 文件 (也可能是conf.d/ssl.conf,与操作系统及安装方式有关), 在配置文件中查找以下配置语句:
# 添加 SSL 协议支持协议,去掉不安全的协议
SSLProtocol all -SSLv2 -SSLv3
# 修改加密套件如下
SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM
SSLHonorCipherOrder on
# 证书公钥配置
SSLCertificateFile cert/public.pem
# 证书私钥配置
SSLCertificateKeyFile cert/214252370570514.key
# 证书链配置,如果该属性开头有 '#'字符,请删除掉
SSLCertificateChainFile cert/chain.pem
(4) 重启 Apache。
(5) 通过 https 方式访问您的站点,测试站点证书的安装配置,如遇到证书不信任问题,
我的配置中直接在最后加上,配置文件不管他之前的设置在httpd-ssl.conf
# 添加 SSL 协议支持协议,去掉不安全的协议
SSLProtocol all -SSLv2 -SSLv3
# 修改加密套件如下
SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM
SSLHonorCipherOrder on
# 证书公钥配置
SSLCertificateFile cert/public.pem
# 证书私钥配置
SSLCertificateKeyFile cert/214252370570514.key
# 证书链配置,如果该属性开头有 '#'字符,请删除掉
SSLCertificateChainFile cert/chain.pem
在apache 的安装目录下找到 httpd-ssl.conf 文件
# General setup for the virtual host
DocumentRoot "C:/xamppp/www/ifanatic-app/public"
ServerName ifanatic.cn
ServerAlias www.ifanatic.cn
ServerAdmin 3046526371@qq.com
后续:
/opt/lampp/etc/extra 中有httpd-ssl.conf 和httpd-vhost.conf 文件。
这样的没有问题的在本地已经测试OK
友情提醒:Linux禁止使用集成开发的工具安装,分开装,有问题可以找,不然有问题都找不到,浪费时间.
下面是nginx的证书配置,其实比apache简单一下
加上这个 return 301 https://blog.ifanatic.cn$request_uri;
server {
listen 80;
server_name ifanatic.cn;
return 301 https://ifanatic.cn$request_uri;
}
server {
listen 443 ssl;
server_name ifanatic.cn;
ssl_certificate /path/to/cert-crt.crt;
ssl_certificate_key /path/to/cert-key.key;
}
----
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid ilogs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name blog.ifanatic.cn;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root www/blog/public;
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite . /index.php last;
}
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root www/blog;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root www/blog/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
server {
listen 443 ssl;
server_name blog.ifanatic.cn;
ssl_certificate cert/214279090710514.pem;
ssl_certificate_key cert/214279090710514.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root www/blog/public;
index index.html index.htm index.php;
if (!-e $request_filename) {
rewrite . /index.php last;
}
}
location ~ \.php$ {
root www/blog/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
include /etc/nginx/vhost/vhost_shop.ifanatic.cn.conf;
}
如何设置http自动跳转到https?apache环境下,配置好https后,需要设置url重定向规则,使网站页面的http访问都自动转到https访问。
<Directory “C:/www”>
…
</Directory>
修改其中的 AllowOverride None 为 AllowOverride All
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_URI} !^/tz.php
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R]
解释:
%{SERVER_PORT} —— 访问端口
%{REQUEST_URI} —— 比如如果url是 http://localhost/tz.php,则是指 /tz.php
%{SERVER_NAME} —— 比如如果url是 http://localhost/tz.php,则是指 localhost
以上规则的意思是,如果访问的url的端口不是443,且访问页面不是tz.php,则应用RewriteRule这条规则。
这样便实现了:访问了 http://localhost/index.php 或者 http://localhost/admin/index.php
等页面的时候会自动跳转到 https://localhost/index.php 或者 https://localhost/admin/index.php,
但是访问 http://localhost/tz.php 的时候就不会做任何跳转,
也就是说 http://localhost/tz.php 和 https://localhost/tz.php 两个地址都可以访问。