Nginx的安裝
一、前言
在上一篇博客中,講述了在服務器上安裝多個tomcat,現在這篇博客要講是安裝nginx
二、配置Nginx的安裝環境
安裝Nginx需要先將官網下載的源碼進行編譯,編譯依賴gcc環境,如果沒有gcc環境,需要安裝gcc:yum install gcc-c++ ,安裝完gcc后,才可以進行下一步的安裝
三、編譯安裝
1. 解壓縮
將Nginx安裝包nginx-1.8.0.tar.gz拷貝至服務器上
解壓縮安裝包:
`tar -zxvf nginx-1.8.0.tar.gz`
`cd nginx-1.8.0`
2. 配置安裝參數
參數如下:
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
--with-http_ssl_module
注:上邊將臨時文件目錄指定為/var/temp/nginx,需要在/var下創建temp及nginx目錄
3. 編譯安裝
make
make install
四、啟動Nginx
cd /usr/local/nginx/sbin/
./nginx
五、停止Nginx
cd /usr/local/nginx/sbin
./nginx -s stop
此方式相當于先查出nginx進程id再使用kill命令強制殺掉進程。
方式2,完整停止(建議使用):
cd /usr/local/nginx/sbin
./nginx -s quit
此方式停止步驟是待nginx進程處理任務完畢進行停止。
六、重啟Nginx
方式1,先停止再啟動(建議使用):
對nginx進行重啟相當于先停止nginx再啟動nginx,即先執行停止命令再執行啟動命令。
如下:
./nginx -s quit./nginx
方式2,重新加載配置文件:
當nginx的配置文件nginx.conf修改后,要想讓配置生效需要重啟nginx,使用-s reload不用先停止nginx再啟動nginx即可將配置信息在nginx中生效,如下:
./nginx -s reload
七、開機自啟Nginx
1. 編寫shell文件
vi /etc/init.d/nginx
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL

2. 設置文件訪問權限
`chmod a+x /etc/init.d/nginx` (a+x ==> all user can execute 所有用戶可執行)
3. 加入到自動列表中
vi /etc/rc.local
加入一行 /etc/init.d/nginx start 保存并退出,下次重啟會生效。

浙公網安備 33010602011771號