一般我们都需要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩。
1.选定源码目录选定目录 /usr/local/cd /usr/local/2.安装PCRE库cd /usr/local/wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gztar -zxvf pcre-8.21.tar.gzcd pcre-8.21./configuremakemake install3.安装zlib库cd /usr/local/ wget http://zlib.net/zlib-1.2.8.tar.gztar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8./configuremakemake install4.安装sslcd /usr/local/wget http://www.openssl.org/source/openssl-1.0.1c.tar.gztar -zxvf openssl-1.0.1c.tar.gz./config --prefix=/usr/local/ssl shared zlib-dynamicmakemake install5.安装nginxNginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把Nginx 安装到 /usr/local/nginx 目录下的详细步骤:cd /usr/local/wget http://nginx.org/download/nginx-1.2.8.tar.gztar -zxvf nginx-1.2.8.tar.gzcd nginx-1.2.8 这步是关键,如果不加的话在配置nginx.conf的时候会报类似这样的错误:nginx: [emerg] unknown directive "ssl" in /usr/local/nginx/conf/vhost/nginx.conf:8 nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed ./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_modulemakemake install--with-pcre=/usr/src/pcre-8.21 指的是pcre-8.21 的源码路径。--with-zlib=/usr/src/zlib-1.2.7 指的是zlib-1.2.7 的源码路径。6.启动/usr/local/nginx/sbin/nginx –t //测试/usr/local/nginx/sbin/nginx //启动提示错误:/usr/local/nginx/sbin/nginx: error while loading shared libraries:libpcre.so.1: cannot open shared object file: No such file or directory解决方法:确认已经安装PCRE: cd /lib ls *pcre* libpcre.so.0 libpcre.so.0.0.1 find / -type f -name *libpcre.so.* 添加软链接: ln -s /lib/libpcre.so.0.0.1 /lib/libpcre.so.1 前面在一般的linux上可以解决此问题.注: 在有的操作系统上面,安装pcre后,安装的位置为/usr/local/lib/*pcre*在redhat 64位机器之上有这样的情况.在redhat 64位机器上, nginx可能读取的pcre文件为/lib64/libpcre.so.1文件.所以在改用下面的软连接: ln -s /usr/local/lib/libpcre.so.1 /lib64/ 检查是否启动成功:netstat -ano|grep 80 有结果输入说明启动成功打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。7.生成证书:
这步需要找到openssl安装目录下的misc文件夹 和openssl.cnf文件前面安装openssl时通过--prefix指定的安装目录是/usr/local/ssl所以misc在 /usr/local/ssl/ssl 下 openssl.cnf 也在其中。新建一个文件夹,myca 将两个文件拷贝到myca下#生成工作目录产生CA凭证ca.crt为自签名证书;server.crt,server.key为服务器端的证书和私钥文件;proxy.crt,proxy.key为代理服务器端的证书和私钥文件;client.crt,client.key为客户端的证书和私钥文件。具体步骤:
cd /usr/local/sslmkdir mycacp misc openssl.cnf myca -rfcd mycacd misc
./CA.sh -newca#产生一个demoCA文件夹cd demoCAtouch serialecho 01 > serialcp ../../openssl.cnf .
vim openssl.cnf +42在42行:将dir = ./demoCA修改为 dir = ./#产生CA自签名证书,这里产生的证书会自动同步到/etc/pki/CA目录下
openssl genrsa -out ./private/ca.key -rand ./private/.rnd -des 2048openssl req -new -x509 -days 3650 -key ./private/ca.key -out ./private/ca.crt -config openssl.cnfopenssl x509 -in ./private/ca.crt -noout -text#产生server的证书过程
openssl genrsa -out ./private/server.key 1024openssl req -new -key ./private/server.key -out ./newcerts/server.csr -config openssl.cnf//这一步如果产生错误,请看后面的解决方法openssl ca -in ./newcerts/server.csr -cert ./private/ca.crt -keyfile ./private/ca.key -config openssl.cnf -policy policy_anything -out ./certs/server.crtopenssl x509 -in ./certs/server.crt -noout -text#产生proxy的证书过程openssl genrsa -out ./private/proxy.key 1024//这步要是产生错误,请看后面的解决方法openssl req -new -key ./private/proxy.key -out ./newcerts/proxy.csr -config openssl.cnfopenssl ca -in ./newcerts/proxy.csr -cert ./private/ca.crt -keyfile ./private/ca.key -config openssl.cnf -policy policy_anything -out ./certs/proxy.crtopenssl x509 -in ./certs/proxy.crt -noout -text#产生client的证书过程openssl genrsa -out ./private/client.key 1024openssl req -new -key ./private/client.key -out ./newcerts/client.csr -config openssl.cnfopenssl ca -in ./newcerts/client.csr -cert ./private/ca.crt -keyfile ./private/ca.key -config openssl.cnf -policy policy_anything -out ./certs/client.crtopenssl x509 -in ./certs/client.crt -noout -text#产生一般错误的解决方法出现:I am unable to access the ./demoCA/newcertsdirectory ./demoCA/newcerts:Nosuch file or directory解决:修改openssl.cnf在42行:dir= ./demoCA修改为 dir= ./出现:failed to update database TXT_DB errornumber 2解决:修改index.txt.attr将unique_subject = yes修改为 unique_subject= no vim /etc/pki/CA/index.txt.attr8.重启/usr/local/nginx/sbin/nginx –s reload停止: ps –ef |grep nginxkillall nginx
Kill –QUIT 进程号Kill –TERM 进程Pkill –q nginx9.修改配置文件cd /usr/local/nginx/confvi nginx.conf============================nginx.conf==========================#user nobody; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { #本机的ip地址:https的端口号443 listen 192.168.62.128:443; ssl on; #要使用生成的服务器的证书和key,使用绝对路径 ssl_certificate /etc/pki/CA/certs/server.crt; ssl_certificate_key /etc/pki/CA/private/server.key; ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; #定义在浏览器里输入的网址 server_name ; location / { root /nginxhome; #/nginxhome 下的两个文件,index.html 和index.htm 里面是连接后显示的内容 index index.html index.htm; } location =/50x.html { root html; } } } 10.配置完成后重启服务器本机直接输入定义的https://www.mynginx20140416.com其他机器的浏览器输入:ip地址:443如果访问不了的话,使用setup命令关掉服务器的防火墙。使用curl 测试Curl –k https://192.168.62.128:443显示你在/nginxhome目录下的index.html中输入的内容证明搭建成功了向服务器传送文件Curl –T localfile –k https://192.168.62.128:443如果出现 <html><head><title>405Not Allowed</title></head><bodybgcolor="white"><center><h1>405Not Allowed</h1></center><hr><center>nginx/1.1.19</center></body></html>说明需要加入PUT GET 等方法在安装编译nginx时加入 --with-http_dav_module这个模块./configure --with-http_dav_module在/usr/local/nginx/conf/nginx.conf中修改location / { root /var/www; dav_methods PUT;}