RHEL/CentOS 安裝 nginx
安裝類型
- rpm包
- 源碼編譯
1 方法一:rpm包安裝
採用官方nginx源安裝方法支持的環境
如果不支持,可以改爲epel源
系統 | 版本 | 支持的平臺 |
---|---|---|
RHEL/CentOS | 6.x | x86_64, i386 |
RHEL/CentOS | 7.4+ | x86_64, ppc64le |
也可以源碼編譯安裝或直接yum安裝。
域名解析請提前設置好
nginx版本選擇
nginx的軟件有三種版本,穩定版、開發版、及歷史穩定版
選擇標準如下:
- 開發版更新快,新功能多,但bug多
- 穩定版的更新很慢,但是bug較少,可以做爲企業生產環境的首選。
但是在實際工作中,儘量避免使用最新的穩定版,應該選擇比已出版本來的最新版本晚6-10個月的版本比較好
官網的解釋是這樣的:
- Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以說是開發版
- Stable version:最新穩定版,生產環境上建議使用的版本
- Legacy versions:遺留的老版本的穩定版
1.1 配置nginx源
配置nginx官方源(推薦)
[root@node1 ~]# vim /etc/yum.repos.d/nginx.repo
添加:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
或者 配epel源
備份(如有配置其他epel源)(推薦阿里epel源)
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
下載新repo 到/etc/yum.repos.d/
epel(RHEL 7)
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
epel(RHEL 6)
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
1.2 yum安裝nginx
[root@node1 ~]# yum list | grep nginx
[root@node1 ~]# yum install nginx
1.3 查看版本
[root@node1 ~]# nginx -v
1.4 查看nginx 編譯的參數
以下爲官方默認編譯參數:
[root@node1 ~]# nginx -V
./configure --prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=nginx \
--group=nginx \
--with-compat \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' \
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
2 方法二:源碼編譯安裝nginx
2.1 安裝依賴
- 安裝
gcc
、gcc-c++
、C/C++
語言編譯環境
yum install gcc gcc-c++ autoconf automake make -y
或者
yum groupinstall "Development Tools" "Server Platform Development" -y
yum
安裝nginx
依賴庫
yum install pcre-devel zlib-devel openssl openssl-devel libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed libtool patch -y
- 創建一個名爲nginx且沒有登錄權限的用戶和一個名爲nginx的用戶組
groupadd -r nginx
useradd -r -g nginx -M -s /sbin/nologin -d /usr/local/nginx nginx
id nginx
添加新用戶參數:
-r: 添加系統用戶( 這裏指將要被創建的系統用戶nginx
)
-g: 指定要創建的用戶所屬組( 這裏指添加到新系統用戶nginx
到nginx
系統用戶組 )
-s: 新帳戶的登錄shell
( /sbin/nologin
這裏設置爲將要被創建系統用戶nginx
不能用來登錄系統 )
-d: 新帳戶的主目錄( 這裏指定將要被創建的系統用戶nginx
的家目錄爲 /usr/local/nginx
)
-M: 不要創建用戶的主目錄( 也就是說將要被創建的系統用戶nginx
不會在 /home
目錄下創建 nginx
家目錄 )
2.2 下載、解壓
源碼包下載地址:https://nginx.org/en/download.html
下載穩定版(Stable version),例如我下載1.14.0
[root@node1 ~]# wget https://nginx.org/download/nginx-1.14.0.tar.gz
解壓
[root@node1 ~]# tar xf nginx-1.14.0.tar.gz
2.3 配置編譯參數
使用該configure命令配置構建。它定義了系統的各個方面,包括允許nginx用於連接處理的方法。最後它創造了一個Makefile。
官方參數說明:https://nginx.org/en/docs/configure.html
爲方便以後支持tcp轉發,推薦編譯時添加stream模塊,需要手動添加參數:--with-stream,
[root@node1 ~]# ./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--user=nginx \
--group=nginx \
--with-compat \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-ld-opt="-Wl,-E"
2.4 編譯
[root@node1 ~]# make
2.5 安裝
[root@node1 ~]# make install
3 啓動與停止nginx服務
3.1 CentOS 6.*
- 配置Nginx到系統服務
配置Nginx啓動腳本
vim /etc/init.d/nginx
編寫腳本文件
#! /bin/bash
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
#
# processname: nginx
# config: /etc/nginx/nginx.conf
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/nginx.lock
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n "Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n "Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n "Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
;;
esac
- 改變文件權限
chmod +x /etc/init.d/nginx
- 添加到系統服務
chkconfig --add nginx
- 設置系統爲開機自啓動
[root@node1 ~]# chkconfig --list|grep nginx
nginx 0:關閉 1:關閉 2:關閉 3:關閉 4:關閉 5:關閉 6:關閉
[root@node1 ~]# chkconfig nginx on
[root@node1 ~]# chkconfig --list|grep nginx
nginx 0:關閉 1:關閉 2:啓用 3:啓用 4:啓用 5:啓用 6:關閉
[root@node1 ~]#
- 啓動
[root@node1 ~]# service nginx start
- 停止
[root@node1 ~]# service nginx stop
- 重啓
[root@node1 ~]# service nginx restart
- 查看運行狀態
[root@node1 ~]# service nginx status
nginx (pid 23961) 正在運行...
[root@node1 ~]#
3.2 CentOS 7.*
- 配置 systemd Nginx 服務和設置 Nginx 自啓動
centos 7 中採用 systemd 來管理系統,我們來爲 nginx 創建服務文件,來實現通過 systemd 來管理 nginx 。
創建 systemd 服務文件: /lib/systemd/system/nginx.service,內容如下:
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
- 添加開機自啓動
[root@node1 ~]# systemctl list-unit-files | grep nginx.service
nginx.service disabled
[root@node1 ~]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@node1 ~]# systemctl list-unit-files | grep nginx.service
nginx.service enabled
[root@node1 ~]#
- 啓動
[root@node1 ~]# systemctl start nginx.service
- 停止
[root@node1 ~]# systemctl stop nginx.service
- 重啓
[root@node1 ~]# systemctl restart nginx.service
- 查看運行狀態
[root@node1 ~]# systemctl status nginx.service
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2018-09-29 11:34:01 CST; 5h 37min ago
Docs: http://nginx.org/en/docs/
Main PID: 26114 (nginx)
CGroup: /system.slice/nginx.service
├─26114 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─26115 nginx: worker process
Sep 29 11:34:01 node1 systemd[1]: Starting nginx - high performance web server...
Sep 29 11:34:01 node1 systemd[1]: Started nginx - high performance web server.
[root@node1 ~]#
4 常用參數
4.1 參數
nginx version: nginx/1.14.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : 幫助
-v : 顯示版本並退出
-V : 顯示版本並配置選項然後退出
-t : 測試配置並退出
-T : 測試配置,轉儲並退出
-q : 在配置測試期間抑制非錯誤消息
-s signal : 向主進程發送信號:stop(停止),quit(退出),reopen(重新打開),reload(重新加載)
-p prefix : 設置前綴路徑(默認值:/ etc/nginx/)
-c filename : 指定配置文件啓動(默認值:/etc/nginx/nginx.conf)
-g directives : 從配置文件中設置全局指令;
4.2 檢查配置文件
測試配置文件是否正確,在運行時需要重新加載配置的時候,此命令非常重要,用來檢測所修改的配置文件是否有語法錯誤。
[root@node1 ~]# nginx -t -c /etc/nginx/nginx.conf
4.3 修改Nginx配置文件
[root@lightserver ~]$ vi /etc/nginx/nginx.conf
# 運行用戶
user nginx;
# 啓動進程, 通常設置成和cpu的數據相等
worker_processes 1;
# 全局錯誤日誌及PID文件
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
# 工作模式及連接數上限
events {
#epoll是多路複用IO(I/O Multiplexing)中的一種方式,
#僅用於linux2.6以上內核,可以大大提高nginx的性能
use epoll;
#單個後臺worker process進程的最大併發鏈接數
worker_connections 1024;
}
http {
#設定mime類型,類型由mime.type文件定義
include /etc/nginx/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;
#
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
#sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,
#對於普通應用,必須設爲 on,
#如果用來進行下載等應用磁盤IO重負載應用,可設置爲 off,
#以平衡磁盤與網絡I/O處理速度,降低系統的uptime.
sendfile on;
#tcp_nopush on;
#連接超時時間
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
#開啓gzip壓縮
gzip on;
gzip_disable "MSIE [1-6]";
#設定請求緩衝
client_header_buffer_size 128k;
large_client_header_buffers 4 128k;
include /etc/nginx/conf.d/*.conf;
#設定虛擬主機配置
server {
#偵聽80端口
listen 80;
#定義使用 本地 訪問
server_name localhost;
#charset koi8-r;
#設定本虛擬主機的訪問日誌
access_log logs/nginx.dev.access.log main;
#默認請求
location / {
#定義服務器的默認網站根目錄位置
root /data/www/html;
#定義首頁索引文件的名稱
index index.php index.html index.htm;
}
# 定義錯誤提示頁面
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /data/www/html;
}
#靜態文件,nginx自己處理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
#過期30天,靜態文件不怎麼更新,過期可以設大一點,
#如果頻繁更新,則可以設置得小一點。
expires 30d;
}
#PHP 腳本請求全部轉發到 FastCGI處理. 使用FastCGI默認配置
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
# root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#禁止訪問 .htxxx 文件
location ~ /\.ht {
deny all;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# 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;
# }
#}
}
新建指定目錄,用於存放http的server段:
mkdir -pv /etc/nginx/conf.d/
可以把server段剪切到/etc/nginx/conf.d/目錄下,以 ".conf" 結尾。
參考
https://segmentfault.com/a/1190000016498647