浙江雁荡山 发表于 2018-11-19 06:57:45

Linux下完整编译Apache+OpenSSL实现HTTPS

  为了使Apache支持https访问,系统需要安有apache、openssl、mod_ssl.so
  

  1、安装openssl
  yum install -y openssl openssl-devel
  

  2、apache下载地址,安装mod_ssl.so
  wget http://www.lishiming.net/data/attachment/forum/httpd-2.2.24.tar.bz2
  tar -jxf apache-2.2.24.tar.bz2
  cd apache-2.2.24
  

  ./configure --prefix=/usr/local/apache2.2.24 --enable-so --enable-rewrite=shared --enable-cgi --enable-pcre --enable-ssl=shared --enable-mods-shared=all --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/
  

  注:
  --enable-so    #支持动态共享模块,如果没有此功能,php 无法与 apache 一起工作,必须安装
  --enable-rewrite    #支持 url 重写
  --enable-cgi    #支持 cgi
  --enable-pcre    #支持pcre
  --enable-ssl    #启用 ssl 功能,不安装无法启用 https
  --enable-mods-shared=all    #编译所有模块
  --with-apr=/usr/local/apr    #指定apr库文件路径
  --with-apr-util=/usr/local/apr-util/    #指定apr-util库文件路径
  --with-mpm=worker    #指定apache工作模式
  参加后面加上shared开启所有的共享(动态)编译模式,如果不加shared,则为静态编译模式
  

  注意:mpm_开头的就是apache的mp工作模式,2.2版本的httpd默认的mpm工作模式为prefork。除了prefork外,常见的工作模式还有worker以及event。2.4版本的httpd默认是event工作模式。关于这几种工作模式的区别,可以参考一下 http://www.cnblogs.com/fnng/archive/2012/11/20/2779977.html

  

  3、复制/usr/local/apache2.2.24/bin/apachectl到/etc/init.d/
  cp -vRp /usr/local/apache2.2.24/bin/apachectl /etc/init.d/httpd
  chmod +x /etc/init.d/httpd
  执行以下sed命令,直接修改/etc/init.d/httpd启动文件
  sed -i '2s/\#/\#chkconfig\:2345 10 90/g' /etc/init.d/httpd
  sed -i '2 a \#description\:Activates\/DeactivatesApache Web Server' /etc/init.d/httpd
  chkconfig --add httpd    添加服务
  chkconfig --level 2345 httpd on    开机自启动
  

  4、修改httpd.conf配置文件

  添加一行:ServerName localhost:80
  

  执行以下命令,检查httpd.conf配置文件是否有错误
  /usr/local/apache2.2.24/bin/apachectl -t
  

  执行以下命令,检查apache编译后加载的模块
  /usr/local/apache2.2.24/bin/apachectl -M
  ssl_mod(shared)    为共享(动态)模块
  ssl_mod(static)    为静态模块,不共享
  

  5、启动apache服务
  service httpd start
  或
  /etc/init.d/httpd start
  或
  /usr/local/apache2.2.24/bin/apachectl start
  

  netstat -lnapt |grep httpd    查看apache服务是否启动
  

  以上的操作,只是开启了apache的80端口,以下操作为apache支持https访问(443)端口
  6、生成密钥和证书

  直接进入到Apache安装目录下的conf目录;
  建立三个文件(不然为网站服务器签署证书时候会报错):
  mkdir newcerts
  echo "01" > serial
  touch index.txt
  建立服务器密钥:
  可以通过 openssl --help    查看命令的帮助
  openssl genrsa -des3 1024 > server.key

  输入口令:******
  确定口令:******
  从密钥中删除密码(以避免系统启动后被询问口令):
  openssl rsa -in server.key > server2.key

  输入server.key口令:******
  替换文件:

  mv server2.key server.key
  建立服务器密钥请求文件:
  openssl req -new -key server.key -out server.csr
  建立服务器证书:
  openssl x509 -in server.csr -out server.crt -req -signkey server.key -days 365
  

  7、Apache添加SSL支持

  打开apache安装目录下conf目录中的httpd.conf文件,找到   
#LoadModule ssl_module modules/mod_ssl.so   
#Include conf/extra/httpd_ssl.conf   
删除行首的配置语句注释符号“#”
  打开apache安装目录下conf/extra目录中的httpd-ssl.conf文件   
在配置文件中查找以下配置语句   
SSLCertificateFile conf/ssl.crt/server.crt         将服务器证书配置到该路径下
SSLCertificateKeyFile conf/ssl.key/server.key    将服务器证书私钥配置到该路径下
  

  8、打开apache安装目录下conf/extra目录中的httpd-ssl.conf文件
  修改端口 Listen 443
  DocumentRoot "/usr/local/apache2/htdocs"    ssl的访问站点要跟http访问的站点目录要一致,否则https访问失败
  打开apache安装目录下conf目录中的httpd.conf文件
  DocumentRoot "/usr/local/apache2/htdocs"
  

  9、重启apache服务
  10、输入https://ip/网站进行验证,则表示已经支持ssl



页: [1]
查看完整版本: Linux下完整编译Apache+OpenSSL实现HTTPS