1.安裝Nginx前期準備:
1)Nginx的配置及運作需要pcre、zlib等軟體包的支援,是以應預先安裝這些軟體的開發包(devel),以便提供相應的庫和頭檔案,確定Ngnix的安裝順利完成。
Yum –y install pcre-devel zlib-devel
2)
建立運作使用者、組,建議為其建立專門的使用者賬号,以便更準确地控制其通路權限,增加靈活性、降低安全風險。
useradd –M –s /sbin/nologin nginx
3)編譯安裝Nginx
如果在編譯的時候出現下面的狀況,是因為gcc沒有安裝,不信的話,可以
Rpm –ql | grep gcc* 看看
在安裝gcc的時候如果出現下面情況是因為gpgcheck=1,而你的key又導入失敗,這時key不能用,要麼就找個新的key位址,要不就把gpgcheck=0,就ok了
做完這步就可以編譯安裝Nginx了
3)
編譯安裝Nginx
[root@68Q /]# tar zxvf nginx-1.4.2.tar.gz
[root@68Q /]# cd nginx-1.4.2
[root@68Q nginx-1.4.2]#
[root@68Q nginx-1.4.2]# ./configure --prefix=/usr/local/nginx--user=nginx --group=nginx
[root@68Q nginx-1.4.2]# make && make install
為了使Nginx伺服器的運作更加友善,可以為nginx建立連結檔案,以便管理者直接執行”nginx”指令就可以調用nginx的主程式。
[root@68Q nginx-1.4.2]# ln -s /usr/local/nginx/sbin/nginx/usr/local/sbin/

1.Nginx 的運作控制
1)檢查配置檔案
與Apache的主程式httpd類似,nginx的主程式也提供了“-t”選項用來對配置檔案進行檢查,以便找出不當或錯誤的配置。配置檔案nginx.conf預設位于安裝目錄下的conf/子目錄中,若要檢查其他位置的配置檔案,可使用“-c”選項來指定路徑。
[[email protected]]# nginx -t
nginx:the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx:configuration file /usr/local/nginx/conf/nginx.conf test is successful
2)
啟動nginx
[root@68Q nginx-1.4.2]# nginx
通過檢查nginx程式的監聽狀态,或者在浏覽器中通路此web服務(預設頁面将顯示“Welcom to nginx!”),可以确認nginx服務是否正常運作。
[[email protected]]# netstat -anpt | grep nginx
tcp00 0.0.0.0:800.0.0.0:*LISTEN5443/nginx
[[email protected]]# elinks http://localhost
Welcome to nginx!
Welcome tonginx!
If you see this page, the nginx web serveris successfully installed and
working. Further configuration isrequired.
For online documentation and support pleaserefer to nginx.org.
Commercial support is available atnginx.com.
Thank you forusing nginx.
通路成功
主程式nginx支援标準的程序信号,通過kill或killall指令發送HUP信号表示重載配置,QUIT信号表示退出程序。KILL信号表示殺死程序。(例如,若使用killall指令,重載配置、停止服務的操作分别如下(通過-s指定信号種類)
[[email protected]]# killall -s HUP nginx選項-s HUP 等同于 -1
[[email protected]]# killall -s QUIT nginx選項-s QUIT 等同于 -3
當nginx程序運作時,PID号預設存放在logs/目錄下的nginx.pid檔案中,是以若要改用kill指令,也可以根據nginx.pid檔案中的PID号來進行控制。
3)使用nginx服務腳本
[root@68Q nginx-1.4.2]# vi /etc/init.d/nginx
[[email protected]]# vi /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
# 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
# pidfile: /usr/local/nginx/logs/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"
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 -eq 0 ] && rm -f$lockfile
restart() {
configtest || return $?
stop
start
reload() {
echo -n $"Reloading $prog:"
killproc $nginx -HUP
RETVAL=$?
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
restart|configtest)
reload)
rh_status_q || exit 7
force-reload)
force_reload
status)
rh_status
condrestart|try-restart)
;;
*)
echo $"Usage: $0{start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
[[email protected]]# chmod +x /etc/init.d/nginx
[[email protected]]# chkconfig --add nginx
通過腳本,nginx就可以正常的使用了。