1.安装依赖
[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).创建软链接
[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).修改权限
[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
注,编译过程中会出错,下面是错误信息。
错误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
再来编译一下,
# ./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
出现在下面的选项说明编译成功,
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.为此脚本赋予执行权限
[root@web ~]# chmod +x /etc/init.d/nginx
10.添加至服务管理列表,并让其开机自动启动
[root@web ~]# chmod +x/etc/init.d/nginx
[root@web ~]# chkconfig --add nginx
[root@web ~]# chkconfig nginx on
[root@web ~]# chkconfig --list nginx
nginx0:关闭1:关闭2:启用3:启用4:启用5:启用6:关闭
5.启动nginx
[root@web ~]# service nginx start
正在启动nginx:[确定]
6.查看一下端口号
[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.测试访问一下
7�YbXgW