2019年6月

nodeJS使用阿里云免费SSL证书简例

云友“ leonkwok1992 ”在问答里想知道在nodeJS里怎么实现https访问,所以写此帖。

环境:Debian 8,阿里云公共镜像。

步骤:

1.安装 nodejs
apt-get install nodejs
bbs-aliyun-dongshan3-306848-1.png

2.编辑 web.js 文件,内容如下:

var https = require('https');
var fs = require('fs');
var options = {
  key: fs.readFileSync('213949634960268.key'),
  cert: fs.readFileSync('213949634960268.pem')
};
var a = https.createServer(options, function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}).listen(443);

bbs-aliyun-dongshan3-306848-2.png

3.将从阿里云证书服务下载的文件,213949634960268.key 和 213949634960268.pem 上传到与 web.js 同一个路径中

4.运行(本例以root身份运行,图简单)
nodejs web.js
bbs-aliyun-dongshan3-306848-3.png

5.在火狐浏览器访问测试,https内容显示正常
bbs-aliyun-dongshan3-306848-4.png

参考:

在Debian 8 系统里安装python3

云友“seiya0613 ”在云栖里问怎么在Debian 8里安装python3,所以写此帖。

环境:阿里云公共镜像,debian 8 64位

过程:

1.默认,运行的python版本是2.7
bbs-aliyun-dongshan3-306726-1.png

2.安装python3
apt-get install python3
bbs-aliyun-dongshan3-306726-2.png

3.即使安装了python3,默认还是使用python2

4.使用 update-alternatives 来管理python的使用版本
update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2

  1. 现在运行python,默认是使用3.4版本了
    python --version
    bbs-aliyun-dongshan3-306726-3.png

参考:

在Debian 8 系统里设置apache web的虚拟主机

云友“bokie ”提到,不想让他人的域名访问到自己ECS服务器上的站点内容。

因为安装Apache后,在浏览器地址栏里输入ECS的公网地址,访问到的页面是默认站点的内容,如果这时有他人的域名解析到这个公网IP地址,也会访问到相同的内容。

本例环境:Debian 8,通过apt-get安装了apache web,yun.anqun.org 和 portal.anqun.org 两个域名均解析到了ECS的公网IP地址:121.43.110.72

过程:

1.打开apache的虚拟主机配置文件目录,/etc/apache2/sites-enabled ,里边有一个默认站点的配置文件,000-default.conf,内容如下:

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

其中没有设置ServerName,且提示这个站点将作为默认站点,即如果当请求域名不存在时,将显示该站点的内容。

2.现在在 /etc/apache2/sites-enabled 目录里,新创建一个虚拟站点 yun.anqun.org 的配置文件 111-yun_anqun_org.conf ,简化后的内容如下:

<VirtualHost *:80>
        ServerName yun.anqun.org
        DocumentRoot /var/www/html/yun.anqun.org
</VirtualHost>

3.保存后重启apache2服务,在浏览器里分别访问 http://yun.anqun.org/ , http://portal.anqun.org/, http://121.43.110.72/ ,结果显示,后两个网址访问到的都是默认站点的内容

bbs-aliyun-dongshan3-305413-1.png

bbs-aliyun-dongshan3-305413-2.png

bbs-aliyun-dongshan3-305413-3.png

所以,如果想不让他人的域名访问到自己的站点内容,可将默认站点设置为其它内容(或空白)即可。

在 Windows 2008 R2 64位系统为 wampserver apache 不同网站配置不同SSL证书(SNI)

有云友 提问,想为 wampserver 套件里的 apache 配置SSL证书安全访问。

环境:Windows 2008 R2 64位中文系统 , wampserver2.5-Apache-2.4.9-Mysql-5.6.17-php5.5.12-64b

过程:

1.安装好wamp,本例是安装到 C:\wamp 且测试可用,启用 mod_ssl模块
bbs-aliyun-dongshan3-305369-1.png

bbs-aliyun-dongshan3-305369-2.png

2.将下载好的证书放到 apache 目录下,如我将 yun.anqun.org 的SSL证书保存在 C:\wamp\bin\apache\apache2.4.9\conf\ssl\yun\ 里

3.编辑 httpd.conf 配置文件(C:\wamp\bin\apache\apache2.4.9\conf\httpd.conf),将约在530行的,关于ssl配置的文件启用;
bbs-aliyun-dongshan3-305369-3.png

4.编辑 httpd-ssl.conf 文件,按实际情况配置虚拟主机的ssl内容,如本例里的 yun.anqun.org 及 portal.anqun.org 的简要配置内容如下(如果apache未加载mod_socache_shmcb块,还需要先注释掉SSLSessionCache的设置,约在第73行):
bbs-aliyun-dongshan3-305369-4.png

5.保存配置文件后,重启apache,在Chrome浏览器里测试访问,显示结果正常
bbs-aliyun-dongshan3-305369-5.png

bbs-aliyun-dongshan3-305369-6.png

在 Debian 8 系统 为 tomcat8 配置https站点访问

在“云栖问答 ”里看到有人提到在tomcat里配置https访问时,可能会遇到问题,所以写此帖。

环境:“经典网络”类型ECS,Debian 8系统,Tomcat 8,JDK 1.7

过程:

1.安装相应的软件或组件,
apt-get install default-jdk tomcat8 tomcat8-examples
bbs-aliyun-dongshan3-305110-1.png

2.将在阿里云申请的,适合tomcat的证书文件上传到合适的目录,如本例是保存在:/etc/tomcat8/cert/213949634960268.pfx

3.编辑 tomcat 的配置文件 /etc/tomcat8/server.xml ,添加相应的https访问配置内容,如本例约在第92号添加了:

<Connector port="8443"
    protocol="HTTP/1.1"
    SSLEnabled="true"
    scheme="https"
    secure="true"
    keystoreFile="/etc/tomcat8/cert/213949634960268.pfx"
    keystoreType="PKCS12"
    keystorePass="213949634960268"
    clientAuth="false"
    SSLProtocol="TLSv1+TLSv1.1+TLSv1.2"
    />
````
其中关键是port,keystoreFile 和 keystorePass,请根据自己的实际情况修改。因为在Debian里的tomcat默认运行账户是tomcat8,是无权限直接使用443端口的,所以本例的port用8443 
![bbs-aliyun-dongshan3-305110-2.png][2]
 
4.重启tomcat服务 
systemctl restart tomcat8
 
6.在火狐浏览器访问,如 https://yun.anqun.org:8443,测试正常 
![bbs-aliyun-dongshan3-305110-3.png][3] 
 
参考:  
* https://tomcat.apache.org/tomcat-8.0-doc/ssl-howto.html 
* https://confluence.atlassian.com/confkb/permission-denied-error-when-binding-a-port-290750651.html 


  [1]: https://liujia.anqun.org/usr/uploads/2019/06/09/81045930862347.png
  [2]: https://liujia.anqun.org/usr/uploads/2019/06/09/81045765975983.png