安裝與設置成服務
1、下載
# wget http://nginx.org/download/nginx-1.14.2.tar.gz
2、解壓
# tar xvf nginx-1.9.9.tar.gz -C /usr/local/src
3、安裝相應的開發工具
# 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
4、進入nginx目錄進行編譯
# cd /usr/local/src/nginx-1.9.9/
# ./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
5、完成編譯
# make && make install
# mkdir -pv /var/tmp/nginx/client
6、添加sys啟動腳本
1.創建文件
# vim /etc/init.d/nginx
2.按i編譯
#!/bin/sh
#
# nginx - this/>#
# 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、添加至服務管理列表設置開機自啟
# chkconfig --add nginx
# chkconfig nginx on
9、啟動
# service nginx start
10、查看運行情況
# netstat -tnlp
配置項
更多參數幫助可以在nginx目錄下執行如下命令查看
# ./configure --help
配置選項 說明
--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庫)
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>
#
#location ~ \\.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP>
#
#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;
# }
#}
}
閱讀更多 菜猿子 的文章