一、yum安裝nginx
訪問nginx的官方網站:http://www.nginx.org/
Nginx版本類型
Mainline version:? 主線版,即開發版
Stable version:? ? ? 最新穩定版,生產環境上建議使用的版本
Legacy versions:? ? 遺留的老版本的穩定版
yum配置的官網:http://nginx.org/en/linux_packages.html
配置nginx的yum源
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@wenmao ~]# yum -y install nginx
-V查看版本及配置
[root@wenmao ~]# nginx -V?
-v查看版本
[root@wenmao ~]# nginx -v?
關閉防火墻和selinux
[root@wenmao ~]# getenforce
Enforcing
[root@wenmao ~]# setenforce 0
[root@wenmao ~]# systemctl stop firewalld
[root@wenmao ~]# systemctl disable firewalld
啟動并設置開機啟動
[root@wenmao ~]# systemctl start nginx
[root@wenmao ~]# systemctl enable nginx
二、編譯安裝nginx
1、安裝編譯環境
[root@wenmao ~]# yum -y install gcc gcc-c++
2、安裝pcre軟件包(使nginx支持http rewrite模塊)
[root@wenmao ~]#?yum install -y pcre pcre-devel
3、安裝openssl-devel(使nginx支持ssl)
[root@wenmao ~]#?yum install -y openssl openssl-devel
4、安裝zlib
[root@wenmao ~]#?yum install -y zlib zlib-devel
5、創建用戶nginx
[root@wenmao ~]# useradd nginx
[root@wenmao ~]#?passwd nginx
6、安裝nginx
[root@wenmao ~]#?wget http://nginx.org/download/nginx-1.16.0.tar.gz
[root@wenmao ~]#?tar xzf nginx-1.16.0.tar.gz -C /usr/local/
[root@wenmao ~]#?cd /usr/local/nginx-1.16.0/
[root@wenmao ~]#?./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@wenmao ~]#?make && make install
7、Nginx 編譯參數
查看 nginx 安裝的模塊
[root@wenmao ~]# /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配置文件是否正確
[root@wenmao ~]#?/usr/local/nginx/sbin/nginx -t
[root@wenmao ~]#?mkdir -p /tmp/nginx
[root@wenmao ~]# mkdir /usr/local/nginx/logs
10、啟動nginx服務
[root@wenmao ~]#?/usr/local/nginx/sbin/nginx
11、通過 nginx 命令控制 nginx 服務
nginx -c /path/nginx.conf # 以特定目錄下的配置文件啟動nginx:
nginx -s reload? ? ? ? ? ? # 修改配置后重新加載生效
nginx -s stop? # 快速停止nginx
nginx -t? ? # 測試當前配置文件是否正確
nginx -t -c /path/to/nginx.conf? # 測試特定的nginx配置文件是否正確