一般我們都需要先裝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.gz
tar -zxvf pcre-8.21.tar.gz
cd pcre-8.21
./configure
make
make install
3.安裝zlib庫
cd /usr/local/
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8
./configure
make
make install
4.安裝ssl
cd /usr/local/
wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
tar -zxvf openssl-1.0.1c.tar.gz
./config --prefix=/usr/local/ssl shared zlib-dynamicmake
make install
5.安裝nginx
Nginx 一般有兩個版本,分別是穩定版和開發版,您可以根據您的目的來選擇這兩個版本的其中一個,下面是把Nginx 安裝到 /usr/local/nginx 目錄下的詳細步驟:
cd /usr/local/
wget http://nginx.org/download/nginx-1.2.8.tar.gz
tar -zxvf nginx-1.2.8.tar.gz
cd 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_module
make
make 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/ssl
mkdir myca
cp misc openssl.cnf myca -rf
cd myca
cd misc
./CA.sh -newca
#產生一個demoCA文件夾
cd demoCA
touch serial
echo 01 > serial
cp ../../openssl.cnf .
vim openssl.cnf +42
在42行:將dir = ./demoCA修改為 dir = ./
#產生CA自簽名證書,這里產生的證書會自動同步到/etc/pki/CA目錄下
openssl genrsa -out ./private/ca.key -rand ./private/.rnd -des 2048
openssl req -new -x509 -days 3650 -key ./private/ca.key -out ./private/ca.crt -config openssl.cnf
openssl x509 -in ./private/ca.crt -noout -text
#產生server的證書過程
openssl genrsa -out ./private/server.key 1024
openssl 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.crt
openssl 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.cnf
openssl ca -in ./newcerts/proxy.csr -cert ./private/ca.crt -keyfile ./private/ca.key -config openssl.cnf -policy policy_anything -out ./certs/proxy.crt
openssl x509 -in ./certs/proxy.crt -noout -text
#產生client的證書過程
openssl genrsa -out ./private/client.key 1024
openssl req -new -key ./private/client.key -out ./newcerts/client.csr -config openssl.cnf
openssl ca -in ./newcerts/client.csr -cert ./private/ca.crt -keyfile ./private/ca.key -config openssl.cnf -policy policy_anything -out ./certs/client.crt
openssl 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.attr
8.重啟
/usr/local/nginx/sbin/nginx –s reload
停止: ps –ef |grep nginx
killall nginx
Kill –QUIT 進程號
Kill –TERM 進程
Pkill –q nginx
9.修改配置文件
cd /usr/local/nginx/conf
vi 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 www.mynginx20140416.com;
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;
}