服務器維護CentOS 6.8下源碼安裝Nginx
2020-07-01 10:28 作者:艾銻無限 瀏覽量:
服務器維護CentOS 6.8下源碼安裝Nginx
服務器維護小知識1. 背景
服務器維護小知識介紹:
Nginx是一款高性能的HTTP和反向代理服務器,能夠選擇高效的epoll(linux2.6內核)、kqueue(freebsd)、eventport(solaris10)作為網絡I/O模型,能夠支持高達50000個并發連接數的響應,而內存、CPU等系統資源消耗卻非常低、運行非常穩定。
服務器維護小知識選擇的理由:
* 支持高并發連接:nginx使用高效的多路復用模型(epoll/linux, kqueue/freebsd, eventport/solaris)
* 內存消耗少:在服務器3W并發連接下,開啟10個Nginx進程消耗150MB內存(15MB*10)
* 成本低廉:購買F5 BIG-IP、NetScaler等負載均衡交換機需要幾十萬RMB,而開源Nginx替代這些商業設備。
* 其他理由:網絡配置簡單;支持rewrite重寫規則,能夠根據域名、URL的不同、將HTTP請求分到不同的后端服務器群組;內置的健康檢查功能;節省帶寬,支持GZIP壓縮,可以添加瀏覽器本地緩存的Header頭;支持熱部署,能夠在不間斷服務的情況下、對軟件版本進行升級
服務器維護小知識服務器維護小知識應用范圍:
* Web服務: 設置多虛擬主機的服務并配合fast-cgi或tomcat支持動態網頁
Nginx是近年來比較火的一個www服務的軟件,與Apache和lighttpd以及tomcat等功能類似,但是nginx要比前者有著卓越的性能,比如:采用了epoll模型,內存消耗小等優點;
* 反向代理, 多虛擬主機的代理:
指以代理服務器來接受Internet上的連接請求,然后將請求轉發給內部網絡上的服務器,并將從服務器上得到的結果返回給Internet上請求連接的客戶端;
* 七層的負載均衡: 單多虛擬主機不同服務器之間的訪問;
負載均衡是由多臺服務器以對稱的方式組成一個服務器集合,每臺都是等價地位,通過某種負載分擔技術,將外部發送來的請求均勻分配到對稱結構中某一臺服務器上,來接收到請求的服務器獨立地回應客戶的請求;
* 正向代理: 代理上網
代理內部網絡對Internet的鏈接請求,客戶機必須指定代理服務器,并將本來要直接發送到web服務器上的http請求發送到代理服務器中,由代理服務器請求并返回響應內容;
* 緩存服務
為proxy和fastcgi做緩存服務,提高訪問速度,相當于squid功能;
服務器維護小知識2. 環境
[root@nginx ~]# cat /etc/
RedHat-release
CentOS release 6.8 (Final)
[root@nginx ~]# uname -r
2.6.32-504.el6.x86_64
服務器維護小知識3. 安裝
* 臨時關閉selinux(可選)
[root@nginx ~]# setenforce 0
* 關閉iptables(可選)
[root@nginx ~]# service iptables stop
* 創建www用戶
[root@nginx ~]# useradd -r -s /sbin/nologin -M www
* 安裝pcre庫依賴
[root@nginx ~]# yum install pcre pcre-devel -y
* 安裝ssl庫依賴
[root@nginx ~]# yum install openssl openssl-devel -y
* 進入下載目錄
cd /usr/local/src
* 下載nginx源碼包
wget
http://nginx.org/download/nginx-1.11.10.tar.gz
* 解壓nginx源碼包
tar zxvf nginx-1.11.10.tar.gz
* 進入nginx包目錄
cd nginx-1.11.10
* 指定安裝目錄、用戶、模塊
[root@nginx ~]# ./configure --prefix=/usr/local/nginx-1.11.10 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
* 編譯并安裝
[root@nginx ~]# make && make install
* 做nginx軟鏈接
[root@nginx ~]# ln -s /usr/local/nginx-1.11.10 /usr/local/nginx
服務器維護小知識4. 創建啟動腳本
* /etc/init.d/nginx
#!/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: /usr/local/nginx/conf/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/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6
make_dirs 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
* 改變nginx腳本文件權限
[root@nginx ~]# chmod 755 /etc/init.d/nginx
* 添加進service管理服務并設置開機啟動
[root@nginx ~]# chkconfig --add nginx
[root@nginx ~]# chkconfig nginx on
服務器維護小知識5. 服務啟動測試
[root@nginx ~]# service nginx start
可以看到80默認的80端口nginx已經開始監聽
服務器維護小知識6. 訪問測試
* 通過瀏覽器測試, 此nginx宿主機ip為192.168.222.128
訪問成功,nginx已經成功返回頁面