1.安裝依賴(lài)
[root@web src]# yum install pcre zlibopenssl -y
2.源碼安裝Nginx
(1).解壓源碼包
[root@web src]# tar xfnginx-1.4.2.tar.gz
[root@web src]# cd nginx-1.4.2
(2).創(chuàng)建軟鏈接
[root@web local]# ln -sv nginx-1.4.2nginx
"nginx" ->"nginx-1.4.2"
[root@web local]# cd nginx
[root@web nginx]# ll
總用量588
drwxr-xr-x 6 1001 10014096 8月29 17:32 auto
-rw-r--r-- 1 1001 1001 222366 7月17 20:51 CHANGES
-rw-r--r-- 1 1001 1001 338680 7月17 20:51 CHANGES.ru
drwxr-xr-x 2 1001 10014096 8月29 17:32 conf
-rwxr-xr-x 1 1001 10012369 7月17 20:51 configure
drwxr-xr-x 3 1001 10014096 8月29 17:32 contrib
drwxr-xr-x 2 1001 10014096 8月29 17:32 html
-rw-r--r-- 1 1001 10011397 7月17 20:51 LICENSE
drwxr-xr-x 2 1001 10014096 8月29 17:32 man
-rw-r--r-- 1 1001 100149 7月17 20:51 README
drwxr-xr-x 8 1001 10014096 8月29 17:32 src
(3).新建nginx用戶
[root@web nginx]# groupadd -g 108-r nginx
[root@web nginx]# useradd -u 108 -r -g 108nginx
[root@web nginx]# id nginx
uid=108(nginx) gid=108(nginx)組=108(nginx)
(4).修改權(quán)限
[root@web nginx]# chown -R root:nginx/usr/local/nginx/*
[root@web nginx]# ll
總用量588
drwxr-xr-x 6 root nginx4096 8月29 17:32 auto
-rw-r--r-- 1 root nginx 222366 7月17 20:51 CHANGES
-rw-r--r-- 1 root nginx 338680 7月17 20:51 CHANGES.ru
drwxr-xr-x 2 root nginx4096 8月29 17:32 conf
-rwxr-xr-x 1 root nginx2369 7月17 20:51 configure
drwxr-xr-x 3 root nginx4096 8月29 17:32 contrib
drwxr-xr-x 2 root nginx4096 8月29 17:32 html
-rw-r--r-- 1 root nginx1397 7月17 20:51 LICENSE
drwxr-xr-x 2 root nginx4096 8月29 17:32 man
-rw-r--r-- 1 root nginx49 7月17 20:51 README
drwxr-xr-x 8 root nginx4096 8月29 17:32 src
(5).編譯nginx
[root@web nginx]# yum -y install pcre-devel
#
./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 --pid-path=/var/run/nginx/nginx.pid--lock-path=/var/lock/nginx.lock --user=nginx --group=nginx--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 --with-pcre
注榨汤,編譯過(guò)程中會(huì)出錯(cuò),下面是錯(cuò)誤信息积锅。
錯(cuò)誤1:
./configure: error: SSL modules require theOpenSSL library.
You can either do not enable the modules,or install the OpenSSL library
into the system, or build the OpenSSLlibrary statically from the source
with nginx by using--with-openssl= option.
解決1:
# yum install -y openssl-devel
再來(lái)編譯一下许溅,
# ./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--pid-path=/var/run/nginx/nginx.pid--lock-path=/var/lock/nginx.lock--user=nginx--group=nginx--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
出現(xiàn)在下面的選項(xiàng)說(shuō)明編譯成功韭脊,
Configuration summary
+using system PCRE library
+using system OpenSSL library
+md5: using OpenSSL library
+sha1: using OpenSSL library
+using system zlib library
nginx path prefix: "/usr"
nginx binary file: "/usr/sbin/nginx"
nginx configuration prefix: "/etc/nginx"
nginx configuration file: "/etc/nginx/nginx.conf"
nginx pid file: "/var/run/nginx/nginx.pid"
nginx error log file: "/var/log/nginx/error.log"
nginx http access log file: "/var/log/nginx/access.log"
nginx http client request body temporary files: "/var/tmp/nginx/client/"
nginx http proxy temporary files: "/var/tmp/nginx/proxy/"
nginx http fastcgi temporary files:"/var/tmp/nginx/fcgi/"
nginx http uwsgi temporary files: "/var/tmp/nginx/uwsgi"
nginx http scgi temporary files: "/var/tmp/nginx/scgi"
# make && make install
3.為nginx提供SysV init腳本
# vim/etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops thenginx daemon
#
# chkconfig:- 85 15
# description:Nginx is an HTTP(S) server, HTTP(S) reverse\
#proxy and IMAP/POP3 proxyserver
# 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
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: "
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
4.為此腳本賦予執(zhí)行權(quán)限
[root@web ~]# chmod +x /etc/init.d/nginx
10.添加至服務(wù)管理列表,并讓其開(kāi)機(jī)自動(dòng)啟動(dòng)
[root@web ~]# chmod +x/etc/init.d/nginx
[root@web ~]# chkconfig --add nginx
[root@web ~]# chkconfig nginx on
[root@web ~]# chkconfig --list nginx
nginx0:關(guān)閉1:關(guān)閉2:啟用3:啟用4:啟用5:啟用6:關(guān)閉
5.啟動(dòng)nginx
[root@web ~]# service nginx start
正在啟動(dòng)nginx:[確定]
6.查看一下端口號(hào)
[root@web ~]# netstat -ntulp
Active Internet connections (onlyservers)
Proto Recv-Q Send-Q Local AddressForeign AddressStatePID/Program name
tcp00 0.0.0.0:800.0.0.0:*LISTEN4801/nginx
tcp00 0.0.0.0:220.0.0.0:*LISTEN1033/sshd
tcp00 127.0.0.1:250.0.0.0:*LISTEN1110/master
tcp00 127.0.0.1:60100.0.0.0:*LISTEN1144/sshd
tcp00 127.0.0.1:60110.0.0.0:*LISTEN1203/sshd
tcp00 :::22:::*LISTEN1033/sshd
tcp00 ::1:25:::*LISTEN1110/master
tcp00 ::1:6010:::*LISTEN1144/sshd
tcp00 ::1:6011:::*LISTEN1203/sshd
7.測(cè)試訪問(wèn)一下
7?YbXgW