下载需要版本的Nginx
http://nginx.org/en/download.html
1)安装支持软件
Nginx的配置及运行需要pcre、zlib等软件包的支持,因此应预先安装这些软件的开发包(devel),以便提供相应的库和头文件,确保Nginx的安装顺利完成。
[root@rslinux3 ~]#yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel
2)创建运行用户、组
[root@rslinux3 ~]#useradd -M -s /sbin/nologin nginx
[root@rslinux3 ~]#id nginx
uid=500(nginx)gid=500(nginx) 组=500(nginx)
3)编译安装Nginx
[root@rslinux3 ~]#ls
anaconda-ks.cfg install.log.syslog 模板 图片 下载 桌面
install.log nginx-1.6.2.tar.gz 公共的 视频 文档 音乐
[root@rslinux3 ~]#tar -xf nginx-1.6.2.tar.gz -C /usr/src/
[root@rslinux3 ~]#cd /usr/src/nginx-1.6.2/
[[email protected]]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx--with-http_stub_status_module --with-http_ssl_module --with-http_flv_module--with-http_gzip_static_module && make && make install
注:配置前可以参考:./configure --help给出说明
--prefix 设定Nginx的安装目录
--user和--group 指定Nginx运行用户和组
--with-http_stub_status_module 启用http_stub_status_module模块以支持状态统计
--with-http_ssl_module 启用SSL模块
--with-http_flv_module 启用FLV模块,提供寻求内存使用基于时间的偏移量文件
4)由于Nginx服务编译安装是没有脚本的,所以需要自己来
编写Nginx服务脚本
[root@rslinux3 ~]#vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: NginxServer
prog="/usr/local/nginx/sbin/nginx"
pidf="/usr/local/nginx/logs/nginx.pid"
starting () {
netstat -anpt | grepnginx &> /dev/null
if [ $? -eq 0 ] ;then
echo "nginx is running ......"
elif
$prog &> /dev/null && [$? -eq 0 ]
then
echo "启动nginx... [ 成功]"
else
echo "启动nginx... [ 失败]"
fi
}
stoping () {
netstat -anpt | grepnginx &> /dev/null
if [ $? -eq 0 ] ;then
kill -s QUIT $(cat $pidf) &>/dev/null
if [ $? -eq 0 ] ; then
echo "nginx服务关闭 [ 成功]"
else
echo "nginx服务关闭 [ 失败]"
fi
else
echo "nginx服务没有在运行,不需要关闭!!!"
fi
}
case "$1"in
start)
starting
;;
stop)
stoping
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $pidf)
;;
*)
echo "usage: $0 { start | stop |restart | reload }"
exit 1
esac
exit 0
[root@rslinux3 ~]#chmod +x /etc/init.d/nginx
[root@rslinux3 ~]#chkconfig nginx --add 添加为系统服务
[root@rslinux3 ~]#chkconfig nginx on 开机自启
[root@rslinux3 ~]#chkconfig nginx --list 查看状态
nginx 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
[root@rslinux3 ~]#service nginx start
启动nginx... [ 成功 ]
[root@rslinux3 ~]#netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5737/nginx
5)测试服务
[root@rslinux3 ~]#elinks 192.168.4.43
6)Nginx运行控制
与Apache的主程序httpd类似,Nginx的主程序也提供了"-t"选项用来对配置文件进行检查,以便找出不当或错误的配置。
配置文件nginx.conf默认位于安装目录/usr/local/nginx/conf/目录中。若要检查位于其他位置的配置文件,可使用"-c"选项来指定路径
[root@rslinux3 ~]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@rslinux2 ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ 将Nginx下的可执行程序添加到系统环境变量PATH中
[root@rslinux3 ~]# 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
7)启动:
需要注意的是,若服务器中已安装有httpd等其他WEB服务软件,应采取措施(修改端口,停用或卸载)避免部突。
[root@rslinux3 ~]# netstat -anpt | grep nginx
[root@rslinux3 ~]# nginx 启动Nginx
[root@rslinux3 ~]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3395/nginx
8)停止Nginx
Nginx支持标准的进程信号,通过kill或者killall命令传送
HUP 重载配置 等同于-1
QUIT 退出进程 等同于-3
KILL 杀死进程
USR1 重新写入日志
USR2 平滑重启
[root@rslinux3 ~]# killall -s HUP nginx
[root@rslinux3 ~]# killall -s QUIT nginx
[root@rslinux3 ~]# netstat -anpt | grep :80
当Nginx进程运行时,PID号默认存放在logs/目录下的nginx.pid文件中,因此若改用kill命令,也可以根据nginx.pid文件中的PID号来进行控制
9)配置文件nginx.conf
在Nginx服务器的主配置文件nginx.conf中,包括 全局配置、I/O事件配置、HTTP配置这三大块内容,配置语句的格式为"关键字 值;"(末尾以分号表示结束),以"#"开始的部分表示注释
1.全局配置
由各种配置语句组成,不使用特定的界定标记。全局配置部分包括运行用户、工作进程数、错误日志、PID存放位置等基本设置。
常用配置项:
user nginx;
//运行用户,Nginx的运行用户实际是编译时指定的nginx,若编译时未指定则默认为nobody
worker_processes 2;
//指定nginx启动的工作进程数量,建议按照cpu数目来指定,一般为它的倍数
worker_cpu_affinity 00000001 00000010;
//为每个进程分配cpu,上例中将2个进程分配到两个cpu,当然可以写多个,或者将一个进程分配到多个cpu
worker_rlimit_nofile 102400;
//这个指令是指当一个nginx进程打开的最多文件数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。(通过"ulimit �Cn 数值"可以修改打开的最多文件数目)
error_log logs/error.log; //全局错误日志文件的位置
pid logs/nginx.pid; //PID文件的位置
2.I/O事件配置:
使用"events {}"界定标记,用来指定Nginx进程的I/O响应模型,每个进程的连接数等设置
events {
use epool;
//使用epool模型,对于2.6以上的内核,建议使用epool模型以提高性能
worker_connections 4096;
//每个进程允许的最多连接数(默认为1024),每个进程的连接数应根据实际需要来定,一般在10000以下,理论上每台nginx服务器的最大连接数为worker_processes*worker_connections,具体还要看服务器的硬件、带宽等。
}
3)HTTP配置
使用"http{}"界定标记,包括访问日志、HTTP端口、网页目录、默认字符集、连接保持、以及虚拟主机、PHP解析等一系列设置。其中大部分配置语句包含在子界定标记"server {}"内。
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
//访问日志位置
sendfile on;
//支持文件发送(下载)
keepalive_timeout 65;
//连接保持超时
server { //web服务的监听配置
listen 80; //监听地址及端口
server_name www.rslinux.com; //网站名称(FQDN)
charset utf-8; //网页的默认字符集
location / { //根目录配置
root html; //网站根目录的位置 安装位置的html中
index index.html index.htm;//默认首页(索引页)
}
error_page 500 502 503 504 /50x.html; //内部错误的反馈页面
location = /50x.html { //错误页面配置
root html;
}
}
}
listen 支持采用 IP地址:端口 形式
4.状态统计及虚拟主机应用:
Nginx内置了HTTP_STUB_STATUS状态统计模块,用来反馈当前的WEB访问情况。配置编译参数时可添加―with-http_stub_stastus_module来启用此模块。要使用Nginx的状态统计功能,除了启用内建模块以外,还需要修改nginx.conf文件,指定访问位置并打开stub_status配置。在http{}配置的server{}子配置内添加如下配置项
[root@nginx3~]# vim /usr/local/nginx/conf/nginx.conf
location ~ /status {
stub_status on; //打开状态统计功能
access_log off; //关闭此位置的日志记录
}
[root@nginx conf]# service nginx restart
浏览器访问 http://192.168.4.43/status
--------------------------------------------------
Active connections: 1
server accepts handled requests
1 1 3
Reading: 0 Writing: 1 Waiting: 0
--------------------------------------------------
Active connections 表示当前活跃的连接数,第三行的三个数字表示Nginx当前总共
处理了1个连接,
成功创建1次握手,
总共处理了3个请求。
最后一行的Reading表示Nginx
读取到客户端Header信息数,
Writing表示Nginx返回给客户端的Header信息数
“Waiting”表示Nginx已经处理完,正在等候下一次请求指令时的驻留连接数。
5.虚拟主机:
使用Nginx搭建虚拟主机服务器时,每个虚拟WEB站点拥有独立的"server {}"配置段,各自监听的IP地址、端口号可以单独指定,当然网站名称也是不同的。
例如:要创建两个站点nginx.benet.com和nginx.accp.com
为两个虚拟WEB主机分别建立根目录,并准备测试首页
[root@nginx ~]# mkdir /usr/local/nginx/html/benet
[root@nginx ~]# mkdir /usr/local/nginx/html/accp
[root@nginx ~]# echo "<h1>nginx.benet.com</h1>" > /usr/local/nginx/html/benet/index.html
[root@nginx ~]# echo "<h1>nginx.accp.com</h1>" > /usr/local/nginx/html/accp/index.html
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name nginx.benet.com;
charset utf-8;
access_log logs/benet.access.log main;
location / {
root html/benet;
index index.html index.htm;
}
}
server {
listen 80;
server_name nginx.accp.com;
charset utf-8;
access_log logs/accp.access.log main;
location / {
root html/accp;
index index.html index.htm;
}
}
}
[root@nginx ~]# service nginx reload
[root@nginx ~]# vim /etc/hosts
192.168.4.43 nginx.benet.com
192.168.4.43 nginx.accp.com
[root@nginx ~]# elinks --dump http://nginx.benet.com
nginx.benet.com
[root@nginx ~]# elinks --dump http://nginx.accp.com
nginx.accp.com