1、下載源碼包解壓編譯
啟動多個,請看:在linux系統下安裝兩個nginx以及啟動
查看nginx包路徑:http://nginx.org/download/,兩種下載方式:
1、在官網下載使用Xftp上傳到linux上(不推薦使用)
2、(推薦)在版本上選好,直接命令下載,如下:(下載nginx-1.16.1.tar.gz版本)建議到home目錄執行該命令,方便找到
wget http://nginx.org/download/nginx-1.16.1.tar.gz
已安裝好

3、解壓
tar xvf nginx-1.16.1.tar.gz -C /usr/local/src/
4、安裝相應的開發工具
yum groupinstall "Development tools" yum -y install gcc wget gcc-c++ automake autoconf libtool libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed pcre-devel openssl-devel
5、進入nginx目錄進行編譯安裝
1、進入目錄
cd /usr/local/src/nginx-1.16.1
2、執行以下命令,直接粘貼即可
./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 \ --http-client-body-temp-path=/var/tmp/nginx/client \ --http-proxy-temp-path=/var/tmp/nginx/proxy \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ --http-scgi-temp-path=/var/tmp/nginx/scgi \ --user=nginx \ --group=nginx \ --with-pcre \ --with-http_v2_module \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_sub_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_stub_status_module \ --with-http_auth_request_module \ --with-mail \ --with-mail_ssl_module \ --with-file-aio \ --with-ipv6 \ --with-http_v2_module \ --with-threads \ --with-stream \ --with-stream_ssl_module

3、完成編譯安裝
make && make install
mkdir -pv /var/tmp/nginx/client
6、添加SysV啟動腳本
1、創建文件
vi /etc/init.d/nginx
2.按i進入編輯狀態
#!/bin/sh
#
# 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
# config: /etc/sysconfig/nginx
# 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/subsys/nginx
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
killall -9 nginx
}
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
7.賦予腳本執行權限
chmod +x /etc/init.d/nginx
8.添加nginx服務進程用戶
groupadd -r nginx
useradd -r -g nginx nginx
9、添加至服務管理列表,設置開機自啟
chkconfig --add nginx
chkconfig nginx on
10、啟動nginx
關閉防火牆:systemctl stop firewalld
service nginx start


2、詳細配置
| 配置選項 | 說明 |
| --prefix | nginx的安裝目錄,默認為/usr/local/nginx |
| --sbin-path | nginx可執行文件路徑,若沒有設置則依賴於--prefix |
| --conf-path | 設置nginx.conf配置文件路徑,nginx啟動時可以通過-c參數指定配置文件 |
| --error-log-path | 錯誤日志路徑 |
| --http-log-path | http主請求日志文件 |
| --pid-path | 存放nginx進程的pid號 |
| --lock-path | 共享存儲器互斥鎖文件路徑 |
| --http-client-body-temp-path | 客戶端收到請求后,臨時存放請求體目錄 |
| --http-proxy-temp-path | 使用代理后,通過該項設置存放請求體路徑 |
| --http-fastcgi-temp-path | 設置FastCGI臨時文件的目錄 |
| --http-uwsgi-temp-path | 設置uWSGI臨時文件的目錄 |
| --http-scgi-temp-path | 設置SCGI臨時文件的目錄 |
| --user | 指定nginx運行的用戶 |
| --group | 指定nginx運行的用戶組 |
| --with-pcre | 設置PCRE庫的源碼路徑 |
| --with-http_v2_module | 用來支持 HTTP 2.0 的 |
| --with-http_ssl_module | 如果需要對流量進行加密,可以使用該選項,再URLs中開始部分將會是https(需要OpenSSL庫) |
3.nginx.conf配置
#user nobody; worker_processes 1; #工作進程:數目。根據硬件調整,通常等於cpu數量或者2倍cpu數量。 #錯誤日志存放路徑 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; # nginx進程pid存放路徑 events { worker_connections 1024; # 工作進程的最大連接數量 } http { include mime.types; #指定mime類型,由mime.type來定義 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 logs/access.log main; #用log_format指令設置日志格式后,需要用access_log來指定日志文件存放路徑 sendfile on; #指定nginx是否調用sendfile函數來輸出文件,對於普通應用,必須設置on。 如果用來進行下載等應用磁盤io重負載應用,可設着off,以平衡磁盤與網絡io處理速度,降低系統uptime。 #tcp_nopush on; #此選項允許或禁止使用socket的TCP_CORK的選項,此選項僅在sendfile的時候使用 #keepalive_timeout 0; #keepalive超時時間 keepalive_timeout 65; #gzip on; #開啟gzip壓縮服務 #虛擬主機 server { listen 80; #配置監聽端口號 server_name localhost; #配置訪問域名,域名可以有多個,用空格隔開 #charset koi8-r; #字符集設置 #access_log logs/host.access.log main; location / { root html; index 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 html; } # 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$ { #請求的url過濾,正則匹配,~為區分大小寫,~*為不區分大小寫。 # root html; #根目錄 # fastcgi_pass 127.0.0.1:9000; #請求轉向定義的服務器列表 # fastcgi_index index.php; # 如果請求的Fastcgi_index URI是以 / 結束的, 該指令設置的文件會被附加到URI的后面並保存在變量$fastcig_script_name中 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #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; # ssl_prefer_server_ciphers on; # # location / { # root html; # index index.html index.htm; # } #} }
