Nginx服務安裝從小白到精通你只差這個文件(包括各種依賴包的解釋)
安裝nginx,升級 用戶認證 auth_basic_user_file 虛擬主機 加密 反向代理: 源碼包安裝nginx
yum –y install gcc pcre-devel(支持正則表達) openssl-devel(支持認證加密) #常見依賴包
useradd –s /sbin/nologin nginx #為程序創建用戶
tar -xf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --help | grep with #查看功能
./configure \
--prefix=/usr/local/nginx \ #指定安裝路徑文件
--user=nginx \ #指定用戶
--group=nginx \ #指定用戶組
--with-http_ssl_module #SSL加密功能
make
make install
cp /usr/local/nginx/sbin/nginx /usr/sbin/ #添加快捷命令
首先編譯安裝
./configure......
make
C語言源碼編譯成二進制可執行程序 庫文件等
綠色的nginx
make install
註意:原來有則會覆蓋掉原來數據
升級Nginx
編譯新版本nginx
tar -zxvf nginx-1.9.0.tar.gz
# cd nginx-1.9.0 [root@svr5 nginx-1.9.0]# ./configure > --prefix=/usr/local/nginx \ > --user=nginx \ > --group=nginx \ > --with-http_ssl_module #會在目錄下生成objs文件夾 這是編譯文件 # make #會在objs裏生成nginx程序文件著就是升級的程序
註意: 這裏繼續make install會覆蓋安裝 會刪除軟件下所有目錄所有數據!
# mv /usr/local/nginx/sbin/nginx >/usr/local/nginx/sbin/nginxold #將舊版本備份起來以便升級失敗後還原,重要
# cp objs/nginx /usr/local/nginx/sbin/ #拷貝新版本
# /usr/local/nginx/sbin/nginx -s stop #停服務
# /usr/local/nginx/sbin/nginx #啟動 重啟是 -s reload
nginx命令
/usr/local/nginx/sbin/nginx #啟動服務
/usr/local/nginx/sbin/nginx -s stop #關閉服務
/usr/local/nginx/sbin/nginx -s reload #重新加載 配置文件
/usr/local/nginx/sbin/nginx –V #查看軟件信息
......
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module
#有軟件安裝配置 模塊信息
nginx服務默認通過TCP 80端口監聽客戶端請求:
netstat -anptu | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10441/nginx
配置文件
註意: 每段後面都有;分號結尾,一個server代表一個網站;
配置文件中 / 根 代表/usr/local/nginx/下
虛擬主機不能完全一樣
vim /usr/local/nginx/conf/nginx.conf #全局配置文件
註意:
每段後面都有;分號結尾
一個server{}代表一個網站
基於域名
基於IP
基於端口
相同條件由於多個虛擬網站第一個優先顯示 #如果不想讓人用IP訪問可在第一個放一個空頁面 用IP就無法訪問了
http{
server {
listen 80; #等同於listen 192.168.4.5:80; #一個IP也就是一張網卡
server_name www.haha.com;
location / {
root html; #網站目錄
index index.html index.htm;
}
}
server {
listen 80; #可寫 listen 192.168.4.5:80;
server_name localhost;
auth_basic "Input Password:"; #認證提示符
auth_basic_user_file "/usr/local/nginx/pass"; #認證密碼文件用戶,是nginx獨立創建的
location / {
root web; #目錄不要和上面的重復不然會一樣 出去要創建
index index.html index.htm;
}
}
}
#pid logs/nginx.pid; #記錄nginx的進程pid文件 此文件可用於判斷nginx是否啟動
配置認證用戶
#yum install -y httpd-tools #安裝創建網站用戶的工具
htpasswd -cm /usr/local/nginx/pass tom #-c是創建密碼文件 以有可以不用加-c m是md5加密方式 可以不寫默認也是md5
New password:
Re-type new password:
Adding password for user tom
htpasswd /usr/local/nginx/pass jarry
nginx -s reload
日誌文件 logs
https
對稱密鑰:單機加密 AES,DES
加密解密同一個 比如RAR壓縮密碼(AES)
非對稱密鑰:網絡加密 RSA,DSA
公鑰 私鑰 證書
擴展
md5sum a.txt #查看文加md5值 內容改變md5校驗值就會變(數據安全)
SSL虛擬主機
源碼安裝Nginx時必須使用--with-http_ssl_module參數
1.生成私鑰與證書
cd /usr/local/nginx/conf
openssl genrsa -out cert.key #生成私鑰 也以這樣寫openssl genrsa > cert.key
openssl req -new -x509 -key cert.key -out cert.pem #生成證書 同樣 -out 可 > 替換
2.修改Nginx配置文件,設置加密網站的虛擬主機
vim /usr/local/nginx/conf/nginx.conf #在尾部有模版
… …
server {
listen 443 ssl;
server_name www.cc.com;
ssl_certificate cert.pem; # 公鑰 註意在conf下
ssl_certificate_key cert.key; # 私鑰
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
Nginx反向代理
調度器 client----> proxy----->web1,web2,web3...(web高可用)
proxy:
調度
負載均衡
健康檢查(自動感知 後臺ping)
1.安裝nginx
2.修改配置
vim /usr/local/nginx/conf/nginx.conf
http {
......
upstream webserver { #定義集群 可以定義多個集群
ip_hash; #會話保持
server 192.168.2.100; #web服務器的ip
server 192.168.2.200 weight=2 max_fails=3 fail_timeout=30;
server 192.168.1.1 down;
#weight默認1會在此會連續調用兩次數
#max_fails錯誤連接次數
#fail_timeout連接失敗後(ping)等待時間
#down 停用服務器(維修)
#ip_hash 會話保持,根據客戶端(同一IP)只會訪問同一後端服務器防止賬號重復登陸的問題
}
server {
listen 80;
server_name www.test.com;
location / {
proxy_pass http://webserver; #調用集群 寫在location裏,下面的配置就無效拉
root html;
index index.html index.htm;
}
......
}
3.起服務
nginx -s reload
看完以後有何感想 評論留下你的感想 有贊更帥
Nginx服務安裝從小白到精通你只差這個文件(包括各種依賴包的解釋)