一、Nginx 简介
Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web 和反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。
二、Nginx 功能特点
1、Nginx 做反向代理服务器
2、Nginx 做正向代理服务器
3、Nginx 做 Web 服务器,同 Apache 功能相同
4、Nginx 做负载均衡服务器
三、Nginx 安装部署
1、在安装 Nginx 前,我们先安装一下下面的支持软件。
[root@nginx ~]# yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
2、在官网下载 Nginx 最新的安装包
[root@nginx ~]# wget -c http://nginx.org/download/nginx-1.14.1.tar.gz
3、解压并编译
[root@nginx ~]# tar -zxvf nginx-1.14.1.tar.gz && cd nginx-1.14.1/
[root@nginx nginx-1.14.1]# ./configure --prefix=/usr/local/nginx --user=www --group=www --error-log-path=/var/log/wwwlogs/nginx_error.log --http-log-path=/var/log/wwwlogs --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/var/nginx.pid --user=www --group=www --with-http_degradation_module --with-http_ssl_module --with-http_v2_module --with-http_sub_module --with-http_stub_status_module --with-http_gzip_static_module --with-google_perftools_module --with-pcre
[root@nginx nginx-1.14.1]# make && make install
4、将 Nginx 命令做软连接到 /usr/bin 路径
[root@nginx nginx-1.14.1]# ln -sf /usr/local/nginx/sbin/nginx /usr/bin/nginx
5、修改 Nginx 配置文件
如上图:
1、为 Nginx 服务工作用户和用户组。
2、为 Nginx 工作进程数,这个需要根据服务器的 CPU 核心数来设置,一般设置为和 CPU 的核心数相等即可。
3、参数 worker_rlimit_nofile 为最大文件打开数(连接),可设置为系统优化后的ulimit -HSn的结果。
4、为 Nginx 单个后台进程的最大并发链接数(最大连接数=连接数*进程数)。
5、为 Nginx 设置 keep-alive 客户端连接在服务器端保持开启的超时值(默认75s);值为0会禁用 keep-alive 客户端连接。
这里需要修改的主要为这几个参数,其他的参数可在实际环境中按需调整,这里不做统一设置。
如上图:
1、为 Nginx 监听的端口,server_name 这里填写网站域名,index 这里写 Nginx 支持的网站后缀,root 为网站路径
2、为开启 Nginx 可监控模块,通常用于接第三方监控系统
3、为配置 Nginx 可监控页面路径以及将监控信息发送给本机的 9000 来处理
4、同 3 一样,一个意思
5、为配置网站的日志
四、Nginx 启动与验证
1、编写 Nginx 启动脚本,这里直接使用官方的启动脚本(下载地址:官网脚本地址,此地址供 redhat 系统使用,如果是其他系统,请到 官网脚本地址,下载),脚本内容如下:
#!/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/bin/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
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -n "$user" ]; then
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
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
fi
}
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
1、将该脚本保存到 /etc/init.d 路径下,并授权可执行
[root@nginx ~]# vim /etc/init.d/nginx
[root@nginx ~]# chmod 755 /etc/init.d/nginx
2、启动 Nginx 服务
[root@nginx ~]# service nginx start
3、查看服务进程和端口号
[root@nginx ~]# ps -ef | grep nginx
如图:
[root@nginx ~]# netstat -tpnl | grep 80
如图:
4、进行网页验证
a、在网站根目录下创建 phpinfo.php 文件
[root@nginx www]# vim phpinfo.php
如下图:
b、在浏览器中输入 http://IP/phpinfo.php 地址进行访问