1、nginx部署-Yum安裝
訪問nginx的官方網站:http://www.nginx.org/
Nginx版本類型
Mainline version: 主線版,即開發版
Stable version: 最新穩定版,生產環境上建議使用的版本
Legacy versions: 遺留的老版本的穩定版
這裏我們用穩定版本
1、安裝編譯環境
yum -y install gcc gcc-c++
2、安裝pcre軟件包(使nginx支持http rewrite模塊)
yum install -y pcre pcre-devel
3、安裝openssl-devel(使nginx支持ssl)
yum install -y openssl openssl-devel
4、安裝zlib
yum install -y zlib zlib-devel
5、創建用戶nginx
useradd nginx
passwd nginx
6、安裝nginx
[root@localhost ~]# wget http://nginx.org/download/nginx-1.16.0.tar.gz
[root@localhost ~]# tar xzf nginx-1.16.0.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/nginx-1.16.0/
[root@localhost nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream
[root@localhost nginx-1.16.0]# make && make install
7、Nginx 編譯參數
# 查看 nginx 安裝的模塊
[root@localhost ~]#/usr/local/nginx/sbin/nginx -V
# 模塊參數具體功能
--with-cc-opt='-g -O2 -fPIE -fstack-protector //設置額外的參數將被添加到CFLAGS變量。(FreeBSD或者ubuntu使用)
--param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2'
--with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now'
--prefix=/usr/local/nginx //指向安裝目錄
--conf-path=/etc/nginx/nginx.conf //指定配置文件
--http-log-path=/var/log/nginx/access.log //指定訪問日誌
--error-log-path=/var/log/nginx/error.log //指定錯誤日誌
--lock-path=/var/lock/nginx.lock //指定lock文件
--pid-path=/run/nginx.pid //指定pid文件
--http-client-body-temp-path=/var/lib/nginx/body //設定http客戶端請求臨時文件路徑
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi //設定http fastcgi臨時文件路徑
--http-proxy-temp-path=/var/lib/nginx/proxy //設定http代理臨時文件路徑
--http-scgi-temp-path=/var/lib/nginx/scgi //設定http scgi臨時文件路徑
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi //設定http uwsgi臨時文件路徑
--with-debug //啓用debug日誌
--with-pcre-jit //編譯PCRE包含“just-in-time compilation”
--with-ipv6 //啓用ipv6支持
--with-http_ssl_module //啓用ssl支持
--with-http_stub_status_module //獲取nginx自上次啓動以來的狀態
--with-http_realip_module //允許從請求標頭更改客戶端的IP地址值,默認爲關
--with-http_auth_request_module //實現基於一個子請求的結果的客戶端授權。如果該子請求返回的2xx響應代碼,所述接入是允許的。如果它返回401或403中,訪問被拒絕與相應的錯誤代碼。由子請求返回的任何其他響應代碼被認爲是一個錯誤。
--with-http_addition_module //作爲一個輸出過濾器,支持不完全緩衝,分部分響應請求
--with-http_dav_module //增加PUT,DELETE,MKCOL:創建集合,COPY和MOVE方法 默認關閉,需編譯開啓
--with-http_geoip_module //使用預編譯的MaxMind數據庫解析客戶端IP地址,得到變量值
--with-http_gunzip_module //它爲不支持“gzip”編碼方法的客戶端解壓具有“Content-Encoding: gzip”頭的響應。
--with-http_gzip_static_module //在線實時壓縮輸出數據流
--with-http_image_filter_module //傳輸JPEG/GIF/PNG 圖片的一個過濾器)(默認爲不啓用。gd庫要用到)
--with-http_spdy_module //SPDY可以縮短網頁的加載時間
--with-http_sub_module //允許用一些其他文本替換nginx響應中的一些文本
--with-http_xslt_module //過濾轉換XML請求
--with-mail //啓用POP3/IMAP4/SMTP代理模塊支持
--with-mail_ssl_module //啓用ngx_mail_ssl_module支持啓用外部模塊支持
8、修改配置文件/etc/nginx/nginx.conf
# 全局參數設置
user nginx; #指定用戶
worker_processes 4; #設置nginx啓動進程的數量,一般設置成與邏輯cpu數量相同
error_log logs/error.log; #指定錯誤日誌
worker_rlimit_nofile 10240; #設置一個nginx進程能打開的最大文件數
pid /var/run/nginx.pid;
events {
worker_connections 1024; #設置一個進程的最大併發連接數
}
# http 服務相關設置
http {
include mime.types;
default_type application/octet-stream;
log_format main 'remote_addr - remote_user [time_local] "request" '
'status body_bytes_sent "$http_referer" '
'"http_user_agent" "http_x_forwarded_for"';
access_log /var/log/nginx/access.log main; #設置訪問日誌的位置和格式
sendfile on; #是否調用sendfile函數輸出文件,一般設置爲on,若nginx是用來進行磁盤IO負載應用時,可以設置爲off,降低系統負載
gzip on; #是否開啓gzip壓縮,將註釋去掉開啓
keepalive_timeout 65; #設置長連接的超時時間
# 虛擬服務器的相關設置
server {
listen 80; #設置監聽的端口
server_name localhost; #設置綁定的主機名、域名或ip地址
# charset koi8-r; # 設置編碼字符
charset utf-8;
location / {
root /var/www/nginx; #設置服務器默認網站的根目錄位置,需要手動創建
index index.html index.htm; #設置默認打開的文檔
}
error_page 500 502 503 504 /50x.html; #設置錯誤信息返回頁面
location = /50x.html {
root html; #這裏的絕對位置是/usr/local/nginx/html
}
}
}
nginx.conf的組成:nginx.conf一共由三部分組成,分別爲:全局塊、events塊、http塊。在http塊中又包含http全局塊、多個server塊。每個server塊中又包含server全局塊以及多個location塊。在統一配置塊中嵌套的配置快,各個之間不存在次序關係。
檢測nginx配置文件是否正確
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
[root@localhost ~]# mkdir -p /tmp/nginx
[root@localhost ~]# mkdir /usr/local/nginx/logs #新建日誌目錄
10、啓動nginx服務
[root@localhost ~]# /usr/local/nginx/sbin/nginx 這裏編譯安裝只能打絕對路徑
設置軟鏈接 或者在/etc/profile 中設置環境變量
這裏我做軟鏈接 ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
然後可以啓動
[root@localhost ~]#nginx
11、通過 nginx 命令控制 nginx 服務
nginx -c /path/nginx.conf # 以特定目錄下的配置文件啓動nginx:
nginx -s reload # 修改配置後重新加載生效
nginx -s stop # 快速停止nginx
nginx # 快速啓動nginx
nginx -t # 測試當前配置文件是否正確
nginx -t -c /path/to/nginx.conf # 測試特定的nginx配置文件是否正確
12 shell 腳本實現一鍵安裝
#!/usr/bin/bash
#install nginx
#by fu 2019.03.09
yum -y install gcc gcc-c++ pcre pcre-devel openssl openssl-devel wget
rpm -q gcc gcc-c++ pcre pcre-devel openssl openssl-devel wget
if [ $? -ne 0 ];then
exit
fi
useradd nginx
echo "nginx" | passwd --stdit nginx #設置密碼
wget http://nginx.org/download/nginx-1.16.1.tar.gz
if [ $? -ne 0 ];then
exit
fi
tar -xzf nginx-1.16.1.tar.gz -C /usr/local
cd /usr/local/nginx-1.16.1
./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream
#以上內容必須在一行 可以加上換上符\
make && make install
sed -ri '/^#user/c user nginx;' /etc/nginx/nginx.conf
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
mkdir -p /tmp/nginx
mkdir /usr/local/nginx/logs
nginx #啓動