Linux下安装Apache Openssl

来源:互联网 发布:timeline软件下载 编辑:程序博客网 时间:2024/05/12 09:12
1.下载
Apache: http://httpd.apache.org/download.cgi
Openssl: https://www.openssl.org/source/
pcre: https://sourceforge.net/projects/pcre/
apr 和 apr-util: http://apr.apache.org/download.cgi


2.创建pcre apr apr-util Openssl Apache的安装文件目录(文件夹)
cd /usr/local
mkdir pcre
mkdir web
mkdir apr
mkdir apr-util
mkdir openssl
mkdir apache
cd web
mkdir apr
修改当前文件夹及子文件的拥有者为当前的操作用户
chown -R currentUser pcre //currentUser指当前的操作用户(登陆用户),其他的几个安装目录同理,这里省略写


3.安装pcre
解压:unzip -o pcre-8.39.zip
进入pcre-8.39目录配置  ./configure --prefix=/usr/local/pcre
安装  make && make install


注:
$./configure --prefix=/usr/local/pcre
若出现configure: error: You need a C++ compiler for C++ support.
则 
Ubuntu: sudo apt-get install build-essential
(如果安装build-essential有提示缺少依赖包,刚相应地用apt-get安装依赖包)
Redhat: yum -y install gcc-c++  




4.安装apr
解压:tar zxvf apr-1.5.2.tar.gz
进入apr-1.5.2目录配置  ./configure --prefix=/usr/local/web/apr
安装  make && make install


5.安装apr-util
解压:tar zxvf apr-util-1.5.4.tar.gz
进入apr-util-1.5.4目录配置  ./configure --prefix=/usr/local/apr-util
安装  make && make install


6.安装Openssl
解压:tar zxvf openssl-1.0.2h.tar.gz
进入openssl-1.0.2h目录配置  ./configure --prefix=/usr/local/openssl
安装  make && make install


7.安装Apache
解压:tar zxvf httpd-2.4.23.tar.gz
进入httpd-2.4.23目录配置 
./configure --prefix=/usr/local/apache --enable-ssl   --enable-rewrite  --enable-so --with-apr=/usr/local/apr --with-apr-util=/usr/local/web/apr-util --with-pcre=/usr/local/pcre --with-ssl=/usr/local/openssl
安装  make && make install




8.创建证书
1)在/usr/local/apache/conf/下面建个目录ssl
mkdir ssl
2)cp /openssl的安装目录/ssl/misc/CA.sh /usr/local/apache/conf/ssl/
用CA.sh来创建证书
3)./CA.sh -newca   //建立主证书
openssl genrsa -des3 -out server.key 1024    //产生服务器私钥,1024:表示生成1024位的密码
openssl req -days 365 -new -key server.key -out server.csr      //生成服务器证书, -days 365 参数是证书有效期时间 
cp server.csr newreq.pem
./CA.sh -sign     //为服务器证书签名
cp newcert.pem server.crt //生成的crt证书,会在ssl的配置文件中引用到
4)集中所有证书和私钥到一起 
cp demoCA/cacert.pem cacert.pem
cp cacert.pem ca.crt


注:
在生成证书时,有一项Common Name (eg, YOUR name) []: 表示的是证书对应的域名,这个要与后边配置文件中ServerName保持一致,这个域名也可以使用*代表泛型,例如, *.test.com




9.配置
1)修改/url/local/apache/conf/httpd.conf
#ServerName www.example.com:80
去掉#,并将域名修改为与上面证书对应的域名一致
#Include conf/extra/httpd-ssl.conf
去除#
#LoadModule ssl_module modules/mod_ssl.so
#LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
去除#


2)修改/url/local/apache/conf/extra/httpd-conf.conf
#ServerName www.example.com:80
去掉#,并将域名修改为与上面证书对应的域名一致


SSLCertificateFile "/usr/local/apache/conf/ssl/server.crt"
SSLCertificateKeyFile "/usr/local/apache/conf/ssl/server.key"


SSLCACertificatePath "/usr/local/apache/conf/ssl"
SSLCACertificateFile "/usr/local/apache/conf/ssl/cacert.pem"
//这两句是不是必要的,没有测试


#SSLVerifyClient require
//这个表示客户端浏览器是否一定要进行证书的验证,如果去掉#,则需要在客户端安装客户端证书
这里直接用服务器的证书去安装
openssl pkcs12 -export -clcerts -in server.crt -inkey server.key -out client.pfx
将client.pfx安装到客户端


httpd.conf 与 httpd-conf.conf 的配置分别走的是http 和 https 通道




10.启动与停止
 /usr/local/apache/bin/apachectl -D SSL -k start
 /usr/local/apache/bin/apachectl -D SSL -k stop


注:
启动时会要求输入证书的密码




11.测试
如果是已申请域名,那么在浏览器中直接输入域名访问即可。
如果是测试的域名,刚可以在C:\Windows\System32\drivers\etc\host 中添加域名地址转换,使其通过域名能访问到服务器的地址。
访问时一定要用https, https才是走的ssl通道
0 0