|
|

楼主 |
发表于 2005-4-15 11:04:35
|
显示全部楼层
配置apache 的虚拟机和https服务
有两种方式可以实现https(加密的http)服务:
1、安装apache-ssl ,方式简单,但定制似乎困难。
2、安装apache和mod-ssl(我使用apache和mod-ssl,所以不用安装apache-ssl)
下面我按照2的搞法来配置:
先安装apache和libapache-mod-ssl:
apt-get install apache libapache-mod-ssl
https服务的关键是apache服务器的私钥和证书,因为mod-ssl需要这两个东西来向用户证明自己的身份。生成apache证书之前,当然也需要一个根证书,由于你没有合法的正式的根证书,只好自己凑合一个自签名的根证书了。
可以这样做,同时生成根证书和服务器证书:
dpkg-reconfigure libapache-mod-ssl
按照提示一步一步做就可以了,
当然,你也可以手动用openssl一步一步来生成自签名证书和服务器证书,稍微麻烦一点,不过还是有用的:
1、首先,看看你的/etc/ssl/openssl.conf的配置参数,以便知道你生成的证书类型、位置,或者你可以修改一些默认参数,比如国家代码、省份、城市、组织名称等等。具体可以查看openssl文档。
2、然后生成自签名根证书:
tac:/etc/ssl# openssl req -x509 -newkey rsa -out cacert.pem -outform PEM
Generating a 1024 bit RSA private key
...............++++++
.....................................++++++
writing new private key to 'privkey.pem'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
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) [CN]:
State or Province Name (full name) [Hunan]:
Locality Name (eg, city) [Changsha]:
Organization Name (eg, company) [SecureFree Orgnization]:
Organizational Unit Name (eg, section) [SI]:
Common Name (eg, YOUR name) []:Jing
Email Address []:neixian@hotmail.com
注意,上面输入的pass phrase是根证书私钥的加密密码,绝对不能泄漏和忘记。
可以用下面的明令来查看该证书的内容,
openssl x509 -in cacert.pem -text -noout
可以看到,里边的内容,如密码长度(modules)等,与openssl.conf里边的配置是一致的,
3、有了根证书,我们可以发放服务器证书了,先生成一个证书请求:
openssl req -newkey rsa:1024 -keyout server.key -keyform PEM -out serverreq.pem
可以用下面的命令来查看该证书请求的内容:
openssl req -in serverreq.pem -text -noout
4、最后就是形成apache能够使用的服务器证书了:
由于生成的任何一个证书,都要在newcert下面放置他们,并给serial文件内容增加1,并且修改 index.txt 文件的内容,所以,我们需要创建这个文件夹和这两个文件:
mkdir myCA/newcerts
touch myCA/serial
touch myCA/index.txt
echo '01' > myCA/serial
然后,生成服务器证书:
openssl ca -in serverreq.pem
期间,会要求你输入根证书的私钥密码,因为它需要根证书的私钥来对证书签名。并且,会提示证书的一些信 息,让你确认正误。如果没有问题,敲入y便生成了证书。
可以查看,serial文件,的确增加了1,index.txt文件也包含了证书的相关信息,如序列号和有效性信息。
显然,这个证书和第三步生成的私钥 server.key,正是apache服务器利用mod-ssl模块启动https服务的东西。
5、配置虚拟服务器
一般,而言,我们会让https服务使用443这个端口,并且该服务单独使用一个虚拟的服务器,所以,我们需要 在apache里边配置这样一个"per IP virtual host"(因为https服务不能使用per name virtual host,具体 原因请查询mod-ssl网站:www.mod-ssl.org)
由于我只有一块网卡,首先,当然是配置单网卡双地址,编辑/etc/network/interfaces
auto eth0 eth0:0
iface eth0 inet static
address 192.168.9.77
netmask 255.255.0.0
network 192.168.0.0
broadcast 192.168.255.255
gateway 192.168.1.254
iface eth0:0 inet static
address 192.168.9.22
netmask 255.255.0.0
network 192.168.0.0
broadcast 192.168.255.255
显然,192.168.9.22是虚拟网卡地址。
我们在/etc/apache/http.conf里边,可以进行如下配置,让虚拟机工作:
<VirtualHost 192.168.9.22:80>
ServerAdmin webmaster@securefree.org
DocumentRoot /var/www/openca
ServerName ca.securefree.org
ErrorLog /var/log/securefree.org.log
ScriptAlias /cgi-bin /usr/lib/cgi-bin/openca
<Directory /var/www/openca>
Options Indexes Includes FollowSymLinks MultiViews
AllowOverride FileInfo AuthConfig Limit
order deny,allow
deny from all
allow from 127.0.0.0/255.0.0.0
allow from 192.168.0.0/255.255.0.0
</Directory>
<Directory /var/www/openca/cgi-bin>
AllowOverride FileInfo AuthConfig Limit
Options ExecCGI
order deny,allow
deny from all
allow from 127.0.0.0/255.0.0.0
allow from 192.168.0.0/255.255.0.0
</Directory>
</VirtualHost>
如果在浏览器地址栏输入该虚拟地址,应该可以看到网页信息了。
现在要让apache运行ssl模块,配置如下:(所有配置都是从缺省配置中拷贝,然后按需作局部修改)
<IfModule mod_ssl.c>
# These will make apache listen to port 443 in addition to the
# standard port 80. HTTPS requests use port 443.
Listen 443
# Some MIME-types for downloading Certificates and CRLs
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl .crl
# Semaphore:
# Configure the path to the mutual exclusion semaphore the
# SSL engine uses internally for inter-process synchronization.
SSLMutex file:/var/run/mod_ssl_mutex
# Inter-Process Session Cache:
# Configure the SSL Session Cache: First either `none'
# or `dbm:/path/to/file' for the mechanism to use and
# second the expiring timeout (in seconds).
#SSLSessionCache dbm:/var/run/mod_ssl_scache
#SSLSessionCacheTimeout 300
SSLSessionCache none
# Pseudo Random Number Generator (PRNG):
# Configure one or more sources to seed the PRNG of the
# SSL library. The seed data should be of good random quality.
SSLRandomSeed startup file:/dev/urandom 512
SSLRandomSeed connect file:/dev/urandom 512
# Logging:
# The home of the dedicated SSL protocol logfile. Errors are
# additionally duplicated in the general error log file. Put
# this somewhere where it cannot be used for symlink attacks on
# a real server (i.e. somewhere where only root can write).
# Log levels are (ascending order: higher ones include lower ones):
# none, error, warn, info, trace, debug.
SSLLog /var/log/apache/ssl_engine.log
SSLLogLevel info
<VirtualHost 192.168.9.22:443>
SSLEngine On
# SSLCertificateFile /etc/apache/ssl.crt/server.crt
# SSLCertificateKeyFile /etc/apache/ssl.key/server.key
SSLCertificateFile /etc/ssl/myCA/newcerts/01.pem
SSLCertificateKeyFile /etc/ssl/myCA/server.key
SSLOptions +StdEnvVars +ExportCertData +StrictRequire
ServerAdmin webmaster@securefree.org
DocumentRoot /var/www/openca
ServerName ca.securefree.org
ErrorLog /var/log/securefree.org.log
ScriptAlias /cgi-bin /usr/lib/cgi-bin/openca
<Directory /var/www/openca>
Options Indexes Includes FollowSymLinks MultiViews
AllowOverride FileInfo AuthConfig Limit
order deny,allow
deny from all
allow from 127.0.0.0/255.0.0.0
allow from 192.168.0.0/255.255.0.0
</Directory>
<Directory /var/www/openca/cgi-bin>
AllowOverride FileInfo AuthConfig Limit
Options ExecCGI
order deny,allow
deny from all
allow from 127.0.0.0/255.0.0.0
allow from 192.168.0.0/255.255.0.0
</Directory>
</VirtualHost>
</IfModule>
注意如下配置:
SSLEngine On
# SSLCertificateFile /etc/apache/ssl.crt/server.crt
# SSLCertificateKeyFile /etc/apache/ssl.key/server.key
SSLCertificateFile /etc/ssl/myCA/newcerts/01.pem
SSLCertificateKeyFile /etc/ssl/server.key
SSLOptions +StdEnvVars +ExportCertData +StrictRequire
星号注释掉的证书和私钥,是dpkg-reconfigure libapache-mod-ssl自动生成的,而下面两个是我们上述手动一步 一步生成的。最后一行,是特殊应用要用到的,里如openca,好让服务器和客户端协商加密密钥长度用的(否 则,如果服务器使用加密长度128位,而客户端不能与之协商而使用了56位,那么会话将无法建立。)。
重起apache:/etc/init.d/apache restart,我们就可以使用https服务了。 |
|