什么是Nginx?
Nginx是一個高性能的HTTP和反向代理服務器,也是一個IMAP/POP3/SMTP 代理服務器。
Nginx特點
- 占有內存少
- 并發能力強
Nginx相對于apache的優缺點
1.Nginx相對于apache的優點
- 輕量級,同樣起web服務,比apache占用更少的內存及資源
- 抗并發,nginx處理請求是異步非阻塞的,而apache則是阻塞型的,在高并發下nginx能保持低資源低消耗高性能
- 高度模塊化的設計,編寫模塊相對簡單
- 社區活躍,各種高性能模塊出品迅速啊
2.apache相對于Nginx的優點
- rewrite ,比nginx 的rewrite 強大
- 模塊超多,基本想到的都可以找到
- 少bug ,nginx 的bug 相對較多
3.Nginx 配置簡潔, Apache 復雜
4.最核心的區別在于apache是同步多進程模型,一個連接對應一個進程;nginx是異步的,多個連接(萬級別)可以對應一個進程
Tengine 是nginx的加強版,封裝版,淘寶開源
Tengine安裝流程
安裝之前準備
1、依賴 gcc openssl-devel pcre-devel zlib-deve
安裝:yum install gcc openssl-devel pcre-devel zlib-devel
2.創建用戶和用戶組。為了方便nginx運行而不影響linux安全
創建組:groupadd -r nginx
創建用戶:useradd -r -g nginx -M nginx
-M 表示不創建用戶的家目錄。
3.安裝
Tengine壓縮包自行下載,解壓后在Tengine目錄執行后續步驟
以下代碼為實際安裝時所用代碼
./configure \
--prefix=/opt/to/soft/tengine-2.1.0/ \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--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 \
--with-pcre
以下代碼為示意代碼
./configure \
--prefix=/usr \ //安裝位置
--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 \ //http請求日志路徑
--pid-path=/var/run/nginx/nginx.pid \ //pid進程號路徑
--lock-path=/var/lock/nginx.lock \ //鎖文件路徑
--user=nginx \ //若加上 --user和--group 則只有指定用戶可以啟動,若不加則沒有限制
--group=nginx \
--with-http_ssl_module \ //可插拔模塊 |
--with-http_flv_module \ //用哪個模塊 |
--with-http_stub_status_module \ //就with |
--with-http_gzip_static_module \ //哪個模塊 |
--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 \ //徑
--with-pcre
4、make && make install
5、添加安裝的tengine到注冊表,具體內容見nginx
- 注意修改路徑,nginx文件必須是在/etc/init.d 下面touch或者vi來新建,不可以用rz sz或者其他文件上傳工具進行上傳,因為會出現編碼問題
-
修改nginx文件的執行權限
- chomod a+x nginx
-
添加該文件到系統服務中去
- chkconfig --add nginx
-
查看是否添加成功
- chkconfig --list nginx
- 啟動,停止,重新裝載
- service nginx start|stop|reload
以下代碼為nginx
#!/bin/bash
#
# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve
# 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="/opt/to/soft/tengine-2.1.0/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/opt/to/soft/tengine-2.1.0/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' -`
# 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: "
# -HUP是nginx平滑重啟參數
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