Nginx实战基础篇

权声明:
本文遵循“署名非商业性使用相同方式共享 2.5 中国大陆”协议

您可以自由复制、发行、展览、表演、放映、广播或通过信息网络传播本作品
您可以根据本作品演义自己的作品
您必须按照作者或者许可人指定的方式对作品进行署名。
您不得将本作品用于商业目的。
如果您改变、转换本作品或者以本作品为基础进行创作,您只能采用与本协议相同的许可协议发布基于本作品的演绎作品。
对任何再使用或者发行,您都必须向他人清楚地展示本作品使用的许可协议条款。
如果得到著作权人的许可,您可以不受任何这些条件的限制。
Designed by 小诺(www.rsyslog.org dreamfire.blog.51cto.com)

实验步骤:

一、安装nginx必须的依赖包

[root@rhel6u3-7 ~]# yum -y install gcc openssl-devel pcre-devel zlib-devel //yum创建过程略,安装略

二、安装编译nginx,目前系统测试环境为rhel6.3 软件版本为nginx-1.27

[root@rhel6u3-7 ~]# useradd nginx -s /sbin/nologin //给nginx服务器创建后台进程管理用户
[root@rhel6u3-7 ~]# tar zxvf nginx-1.2.7.tar.gz //解压缩
[root@rhel6u3-7 ~]# cd nginx-1.2.7
[root@rhel6u3-7 nginx-1.2.7]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module
// --user=nginx –group=nginx 设置允许nginx允许的用户和组为nginx
// --prefix=/usr/local/nginx/ 设置nginx安装路径
// --with-http_stub_status_module 安装允许状态模块
// --with-http_ssl_module 安装ssl模块
//更多参数请参看 ./configure --help
…… //安装显示略,如果配置正确,最后会显示以下信息。
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/local/nginx/"
 nginx binary file: "/usr/local/nginx//sbin/nginx"
 nginx configuration prefix: "/usr/local/nginx//conf"
 nginx configuration file: "/usr/local/nginx//conf/nginx.conf"
 nginx pid file: "/usr/local/nginx//logs/nginx.pid"
 nginx error log file: "/usr/local/nginx//logs/error.log"
 nginx http access log file: "/usr/local/nginx//logs/access.log"
 nginx http client request body temporary files: "client_body_temp"
 nginx http proxy temporary files: "proxy_temp"
 nginx http fastcgi temporary files: "fastcgi_temp"
 nginx http uwsgi temporary files: "uwsgi_temp"
 nginx http scgi temporary files: "scgi_temp"
[root@rhel6u3-7 ~]# /usr/local/nginx/sbin/nginx –V //安装完成后查看Nginx的相关环境配置信息是否正确
nginx version: nginx/1.2.7
built by gcc 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC)
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module
[root@rhel6u3-7 ~]#
[root@rhel6u3-7 nginx-1.2.7]# make & make install //编译安装过程略

三、通过nginx自身脚本机器nginx服务器,并通过各种命令查看是否启动成功

[root@rhel6u3-7 ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf // -c指向nginx主配置文件
[root@rhel6u3-7 ~]# lsof -i :80 //查看nginx进程号及运行情况
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 4080 root 6u IPv4 21467 0t0 TCP *:http (LISTEN)
nginx 4081 nginx 6u IPv4 21467 0t0 TCP *:http (LISTEN)
[root@rhel6u3-7 ~]# ps -ef | grep nginx //查看nginx进程号及运行情况
root 4080 1 0 22:24 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx 4081 4080 0 22:24 ? 00:00:00 nginx: worker process
root 4088 1433 0 22:27 pts/0 00:00:00 grep nginx
[root@rhel6u3-7 ~]# netstat -nltp | grep 80 //查看nginx进程监听端口
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4080/nginx

四,测试nginx配置的默认web服务器是否能正常运行

通过linux自带命令links 测试

[root@rhel6u3-7 ~]# links 127.0.0.1 //出现 welcome to nginx!说明nginx服务启动成功

通过windows服务器IE浏览器测试

Nginx实战基础篇

扩展知识:

1、设置nginx开机自动启动,将nginx启动命令添加到/etc/rc.local

[root@rhel6u3-7 ~]# echo "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" >> /etc/rc.local
[root@rhel6u3-7 ~]# cat /etc/rc.local | grep nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

2、通过设置System V 脚本,使用server命令启动nginx服务

[root@rhel6u3-7 init.d]# vim nginx //在/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/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 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=$?
 echo
[ $retval -eq 0 ] && rm -f $lockfile
 return $retval
 killall -9 nginx
}

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

[root@rhel6u3-7 init.d]# chmod 755 nginx //修改脚本文件nginx的权限
[root@rhel6u3-7 init.d]# chkconfig --add nginx //将脚本文件加入chkconfig中
[root@rhel6u3-7 init.d]# chkconfig --level 35 nginx on //设置nginx开机在3和5级别自动启动
[root@rhel6u3-7 init.d]# chkconfig --list | grep nginx
nginx 0:off 1:off 2:off 3:on 4:off 5:on 6:off

测试nginx脚本文件是否能够正常使用

[root@rhel6u3-7 init.d]# /etc/init.d/nginx restart //重新启动
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
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
[root@rhel6u3-7 init.d]# /etc/init.d/nginx reload //不间断服务平滑重启
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
Reloading nginx: [ OK ]
[root@rhel6u3-7 ~]# cat /usr/local/nginx/logs/nginx.pid //存储了nginx运行的进程号
15799
[root@rhel6u3-7 ~]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid` //不间断服务重新启动nginx
 [root@rhel6u3-7 init.d]# /etc/init.d/nginx stop
Stopping nginx: [ OK ]
[root@rhel6u3-7 init.d]# killall -9 nginx //结束nginx的全部经常

 

[root@rhel6u3-7 html]# pwd //安装nginx完成后,默认网站的路径
/usr/local/nginx/html
[root@rhel6u3-7 html]# ll //可以查看到默认网站为index.html,内容为以上测试内容。
total 8
-rw-r--r-- 1 root root 537 Feb 25 22:21 50x.html
-rw-r--r-- 1 root root 612 Feb 25 22:21 index.html

相关推荐