一、Nginx 源码包安装与配置
1、环境准备
操作系统、内核版本: | CentOS 6.8 2.6.32-642.el6.x86_64 |
Nginx 软件版本: | nginx-1.10.2 |
2、创建Nginx用户
[root@web01 ~]# groupadd -g 108 -r nginx
[root@web01 ~]# useradd -u 108 -r -g 108 nginx
[root@web01 ~]# id nginx
uid=108(nginx) gid=108(nginx) groups=108(nginx)
3、安装Nginx所需依赖包
# yum -y install pcre-devel openssl-devel gcc-c++ zlib-devel
4、编译安装Nginx-1.10.2
# cd /root/soft
# tar -xf nginx-1.10.2.tar.gz
# cd nginx-1.10.2
# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_dav_module
# make
# make install
5、为nginx提供SysV init启动脚本(/etc/init.d/nginx)
#!/bin/sh
# # nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse
# proxy and IMAP/POP3 proxy server
# 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/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/etc/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: "
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
6、Nginx启动脚本赋予执行权限
# chmod +x /etc/init.d/nginx
7、添加到服务管理列表,并设置开机自动启动服务
# chmod +x /etc/init.d/nginx
# chkconfig --add nginx
# chkconfig nginx on
# chkconfig --list ngin
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
8、启动Nginx服务
Nginx启动前检查语法:(修改配置,重启服务前一定要先检查配置文件语法。)
[root@web01 nginx-1.10.2]# /usr/local/nginx/sbin/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启动Nginx服务:[root@web01 nginx-1.10.2]# /etc/init.d/nginx start Starting nginx: [ OK ]
9、查看Nginx服务对应端口是否启动成功
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 4547 root 6u IPv4 17508 0t0 TCP *:http (LISTEN)
nginx 4548 nginx 6u IPv4 17508 0t0 TCP *:http (LISTEN)
[root@web01 nginx-1.10.2]# netstat -lnt| grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
tcp 0 0 :::32980 :::* LISTEN
10、检查Nginx启动的实际效果,通过浏览器访问
好了到这里Nginx的安装就完成了,接下来会介绍Ngnix相关的配置!