一、nginx简介:

Nginx是一个自由、开源、高性能及轻量级的http服务器及反向代理服务器。Nginx以其高性能、稳定、功能丰富、配置简单及占用系统资源少而著称。Nginx 超越 Apache 的高性能和稳定性,使得国内使用 Nginx 作为 Web 服务器的网站也越来越多。

注:nginx和apache各有优缺点,总体而言,静态和反向代理,优先选用nginx。

二、安装nginx:

1.系统平台centos 6.5:

一、nginx介绍及安装_稳定性

2.安装依赖包,否则无法编译安装:

yum groupinstall -y "Development tools" "Server Platform Development" "Desktop Platform Development" 
yum install -y pcre-devel

1)创建用户nginx和组nginx,实现以用户nginx运行nginx进行:

[root@shell ~]# groupadd -r nginx    #nginx需要调用1000以下的端口,需要系统用户
[root@shell ~]# useradd -r -g nginx nginx
[root@shell ~]# id nginx
uid=496(nginx) gid=493(nginx) groups=493(nginx)

一、nginx介绍及安装_style_02

2)解压nginx安装包,安装文件可以在www.nginx.org 下载安装文件:

[root@shell ~]# ls
anaconda-ks.cfg  install.log         nginx-1.9.3.tar.gz  temp.text  while.py
harlequin.vim    install.log.syslog  README.md           tet
[root@shell ~]# tar xf nginx-1.9.3.tar.gz 
[root@shell ~]# ls
anaconda-ks.cfg  install.log         nginx-1.9.3         README.md  tet
harlequin.vim    install.log.syslog  nginx-1.9.3.tar.gz  temp.text  while.py
[root@shell ~]# cd nginx-1.9.3
[root@shell nginx-1.9.3]# mkdir -pv /var/tmp/nginx/client
mkdir: created directory `/var/tmp/nginx'
mkdir: created directory `/var/tmp/nginx/client'
[root@shell nginx-1.9.3]# ./configure \
>   --prefix=/usr/local/nginx \   
>   --sbin-path=/usr/local/nginx/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

选项

说明

--prefix=<path>

指定nginx安装路径,默认是在/usr/local/nginx

--sbin-path=<path>

指定nginx可执行文件安装路径,默认安装在<prefix>/sbin/nginx

--conf-path=<path>

指定默认的nginx.conf路径,默认是在<prefix>/conf/

--error-log-path=<path>

在nginx.conf下未指定error_log情况下,指定默认的错误日志路径

--http-log-path=<path>

在nginx.conf下未指定http_log情况下,指定默认的错误日志路径

--pid-path=<path>

在nginx.conf未指定pid指令的情况下,指定nginx.pid路径
--lock-path=<path> 指定nginx.lock路径
--user=<user> 指定nginx服务运行者,未指定则默认为nobady
--group=<group> 指定nginx服务运行者,未指定则默认为nobady
--with-http_ssl_module 启用ssl,nginx就可以支持https,但是系统必须有openssl库
--with-http_flv_module 启用flv模块
--with-http_stub_status_module 启用Server Status页
--with-http_gzip_module 禁用gzip模块,默认是启用的,但是需要zlib库
--http-client-body-temp-path=<path> 存放http方问客户端请求报文的临时文件路径,默认不存在,需要提前创建
--http-proxy-temp-path=<path> 启用proxy模块,指定http代理临时文件路径,默认不存在,需要提前创建
--http-fastcgi-temp-path=<path> 启用http的fastcgi模块,指定存放fastcgi模块临时文件的路径,默认不存在,需要提前创建
--http-uwsgi-temp-path=<path> 启用http的uwsgi模块,指定存放uwsgi模块临时文件的路径,默认不存在,需要提前创建
-with-pcre 指定pcre库,需提前安装pcre-devel
--http-scgi-temp-path=<path> 启用http的scgi模块,指定存放scgi模块临时文件的路径,默认不存在,需要提前创建
 
 
运行make && make install
 
3)创建服务脚本:
vim /etc/rc.d/init.d/nginx,写入如下脚本:(对脚本做一下说明,有些路径是根据上面configer的选项书写的)
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# 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
# pidfile: /var/run/nginx/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="/etc/nginx/nginx.conf"

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
}

restart() {
configtest || return $?
stop
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
 
给脚本添加权限:
chmod +x /etc/rc.d/init.d/nginx
添加开机启动:
chkconfig –add nginx
chkconfig nginx on

4)添加环境变量:
# echo "PATH=$PATH:/usr/local/nginx/sbin" >> /etc/profile (永久生效)

#export PATH=$PATH:/usr/local/nginx/sbin (当前生效)

5)测试并启动:

启动之前先,通过nginx -t测试

[root@shell nginx-1.9.3]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@shell nginx-1.9.3]# service nginx start
Starting nginx:                                            [  OK  ]

 

3.网站访问nginx,查看服务器ip地址,浏览器直接输入对应ip地址,nginx默认端口80.

一、nginx介绍及安装_网站_03

注:输入ip,没有显示,请查看是否iptables开启。

关闭iptables:service iptables stop