linux下安裝apache支持ssl

    最近須要搭建一個基於apache上的https安全服務支持,因沒有接觸過,因而在網上搜了些資料,因而總結便於後人更加容易搭建。linux

    最近在研究linux下的apache-ssl配置,寫點我的當心得,新人發博,敬請見諒。apache

如下是基於網上的文章:http://honghuihun.iteye.com/blog/1137204瀏覽器

我只在此針對遇到的問題作些補充。安全

====================================================
軟件環境

1)Apache Httpd 2.2.15 (http://httpd.apache.org ) 服務器

    httpd2.x以上的版自帶mod_ssl模塊。
dom

2)OpenSSL 0.9.8r (http://www.openssl.org/source )
3)SSL-Tools (http://www.openssl.org/contrib/ssl.ca-0.1.tar.gz )

1. OpenSSL
#tar zxvf openssl-0.9.8r.tar.gz
#cd openssl-0.9.8r
#./config
#make
#make install

此舉將安裝最新的OpenSSL到/usr/local/ssl目錄中,無需理會系統中已有版本的OpenSSL,也不要去卸載它,不然會致使不少的應用程序沒法正常執行,例如X窗口沒法進入等錯誤。工具

2. Apache Httpd
#tar zxvf httpd-2.2.15.tar.gz
#cd httpd-2.2.15
#./configure --prefix=/usr/local/apache/httpd --enable-ssl=static --with-ssl=/usr/local/ssl
#make
#make install
此步驟在/apache/httpd目錄中安裝httpd服務(經過參數--prefix指定),同時使用--with-ssl指定剛纔所安裝OpenSSL的路徑,用於將mod_ssl靜態的編譯到httpd服務中。測試


可能遇到的問題:
ui

./configure --prefix=/ytxt/apache2/  --enable-ssl   --enable-rewrite  --enable-so   --with-ssl=/usr/local/openssl/
spa

報錯:

configure: error: ... Error, SSL/TLS libraries were missing or unusable

解決:

執行以下一句設置環境變量:

export LDFLAGS=-ldl



3.製做證書 (這是可選部分)
咱們必須手工來生成SSL用到的證書,對證書不熟悉的人,有一個工具可使用:http://www.openssl.org/contrib/ssl.ca-0.1.tar.gz  。下面是如何經過這個工具來生成證書的過程:

#cp ssl.ca-0.1.tar.gz /usr/local/apache/httpd/conf
#cd /usr/local/apache/conf
#tar zxvf ssl.ca-0.1.tar.gz
#cd ssl.ca-0.1
#./new-root-ca.sh (生成根證書)
No Root CA key round. Generating one
Generating RSA private key, 1024 bit long modulus
...........................++++++
....++++++
e is 65537 (0x10001)
Enter pass phrase for ca.key: (輸入一個密碼)
Verifying - Enter pass phrase for ca.key: (再輸入一次密碼)
......
Self-sign the root CA... (簽署根證書)
Enter pass phrase for ca.key: (輸入剛剛設置的密碼)
........
........ (下面開始簽署)
Country Name (2 letter code) [MY]:CN
State or Province Name (full name) [Perak]:GanSu//隨你喜歡
Locality Name (eg, city) [Sitiawan]:LanZhou//隨你喜歡
Organization Name (eg, company) [My Directory Sdn Bhd]:lzu//隨你喜歡
Organizational Unit Name (eg, section) [Certification Services Division]:lzu//隨你喜歡
Common Name (eg, MD Root CA) []:dslab//隨你喜歡
Email Address []:sunyanmeng@gmail.com//隨你喜歡
這樣就生成了ca.key和ca.crt兩個文件,下面還要爲咱們的服務器生成一個證書:
# ./new-server-cert.sh server (這個證書的名字是server)
......
......
Country Name (2 letter code) [MY]:CN
State or Province Name (full name) [Perak]:GanSu
Locality Name (eg, city) [Sitiawan]: LanZhou
Organization Name (eg, company) [My Directory Sdn Bhd]:lzu
Organizational Unit Name (eg, section) [Secure Web Server]:lzu
Common Name (eg, www.domain.com) []:localhost
Email Address []:sunyanmeng@gmail.com
這樣就生成了server.csr和server.key這兩個文件。
還須要簽署一下才能使用的:
# ./sign-server-cert.sh server
CA signing: server.csr -> server.crt:
Using configuration from ca.config
Enter pass phrase for ./ca.key: (輸入上面設置的根證書密碼)
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName:PRINTABLE:'CN'
stateOrProvinceName:PRINTABLE:'GanSu'
localityName:PRINTABLE:'LanZhou'
organizationName:PRINTABLE:'lzu'
organizationalUnitName:PRINTABLE:'lzu'
commonName:PRINTABLE:'localhost'
emailAddress :IA5STRING:'sunyanmeng@gmail.com '
Certificate is to be certified until Jan 19 21:59:46 2011 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
CA verifying: server.crt <-> CA cert
server.crt: OK

下面要按照ssl.conf裏面的設置,將證書放在適當的位置。

# chmod 400 server.key
# cd ..
# mkdir ssl.key
# mv ssl.ca-0.1/server.key ssl.key
# mkdir ssl.crt
# mv ssl.ca-0.1/server.crt ssl.crt


ssl.conf的配置文件爲httpd-ssl.conf 此文件是在apache_dir/conf/extra下。

    1)httpd-ssl.conf主要修改的信息爲:

SSLCertificateFILE /usr/local/apache/httpd/conf/ssl.crt/server.crt
SSLCertificateKeyFile /usr/local/apache/httpd/conf/ssl.key/server.key

    2)httpd.conf 須要放開ssl支持:

Include conf/extra/httpd-ssl.conf前面的註解去掉。


文件目錄以下:

root@mengfh-virtual-machine:/usr/local/apache/httpd/conf# ls
extra  httpd.conf  magic  mime.types  original  ssl.ca-0.1  ssl.ca-0.1.tar.gz  ssl.crt  ssl.key
root@mengfh-virtual-machine:/usr/local/apache/httpd/conf# ls -tRl extra/
extra/:
總計 56
-rw-r--r-- 1 root root 11659 2014-03-18 14:52 httpd-ssl.conf
-rw-r--r-- 1 root root  1503 2014-03-18 11:43 httpd-vhosts.conf
-rw-r--r-- 1 root root   817 2014-03-18 11:43 httpd-userdir.conf
-rw-r--r-- 1 root root  2201 2014-03-18 11:43 httpd-multilang-errordoc.conf
-rw-r--r-- 1 root root  3789 2014-03-18 11:43 httpd-mpm.conf
-rw-r--r-- 1 root root   928 2014-03-18 11:43 httpd-manual.conf
-rw-r--r-- 1 root root  5078 2014-03-18 11:43 httpd-languages.conf
-rw-r--r-- 1 root root  1103 2014-03-18 11:43 httpd-info.conf
-rw-r--r-- 1 root root  2344 2014-03-18 11:43 httpd-default.conf
-rw-r--r-- 1 root root  1743 2014-03-18 11:43 httpd-dav.conf
-rw-r--r-- 1 root root  2855 2014-03-18 11:43 httpd-autoindex.conf
root@mengfh-virtual-machine:/usr/local/apache/httpd/conf#

-----------------------------------------------------------------------------------------------而後就能夠啓動啦!# cd /usr/local/apache注意,apache2.2以後不支持startssl,因此只用start便可# ./bin/apachectl start4. 測試HTTP服務使用瀏覽器打開地址:https://127.0.0.1   完畢!!

相關文章
相關標籤/搜索