|
1、安装依赖包(CentOS 6.7)
[root@mail soft]# yum -y groupinstall "Development tools,Server platform development,Desktop platform development"
[root@mail soft]# yum -y install pcre-devel openssl-devel
2、编译安装Nginx
[root@mail soft]# tar xf nginx-1.8.0.tar.gz
[root@mail soft]# cd nginx-1.8.0
[root@mail nginx-1.8.0]# groupadd -r nginx
[root@mail nginx-1.8.0]# useradd -g nginx -s /sbin/nologin -M nginx
[root@mail nginx-1.8.0]# ./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/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
[root@mail nginx-1.8.0]# make && make install
##注意:编译后有些文件夹不会自动创建
[root@mail nginx-1.8.0]# mkdir -pv /var/tmp/nginx/{client,proxy,fcgi,uwsgi,scgi}
mkdir: created directory `/var/tmp/nginx'
mkdir: created directory `/var/tmp/nginx/client'
mkdir: created directory `/var/tmp/nginx/proxy'
mkdir: created directory `/var/tmp/nginx/fcgi'
mkdir: created directory `/var/tmp/nginx/uwsgi'
mkdir: created directory `/var/tmp/nginx/scgi'
这里的二进制文件是直接指定在PATH环境变量里面的,所有可以直接使用,不用导出:
配置vim,使其编辑nginx配置文件时语法着色,默认没有
[root@mail nginx-1.8.0]# cd
[root@mail ~]# mkdir .vim
[root@mail ~]# cp -ra /u01/soft/nginx-1.8.0/contrib/vim/* .vim
[root@mail ~]# ls .vim
ftdetect indent syntax
编写启动脚本:
[root@mail ~]# vim /etc/rc.d/init.d/nginx
#!/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.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/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
[root@mail ~]# chmod +x /etc/rc.d/init.d/nginx #添加可执行权限
[root@mail ~]# chkconfig --add nginx #添加到服务列表
[root@mail ~]# chkconfig nginx on #设置开机自启动
[root@mail ~]# service nginx start #启动nginx
Starting nginx: [ OK ]
[root@mail ~]# ss -tnl #查看是否监听80端口
[root@mail ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 *:22 *:*
##查看页面
[root@mail ~]# curl http://localhost
Welcome to nginx!
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
#######################编译安装已经完成。
配置文件介绍
主要有两部分:分别是
main:主体部分
http{}:虚拟主机配置部分
配置指令主要以分号结尾;配置语法:directive value1 [value2 ....]
支持使用的变量
模块内置的变量
自定义变量:set var_name value
主配置段的指令类别:
用于调试和定位问题:
(1)daemon [on|off]: 是否以守护进程的方式启动nginx;
(2)master_press [on|off]: 是否以master/worker模型来运行nginx;
(3)error_log /path/to/error_loglevel: 指明错误日志文件级别,处于调试目的,可以使用debug级别,但次级别只有在编译nginx时使用了--with-debug选项才有效 ;
正常运行必备的配置:
(1)user USERNAME [GROUPNAME]:指定运行worker的用户和用户组;例如 user nginx nginx
(2)pid /path/to/nginx.pid : 指定pid文件
(3)worker_rlimit_nofile # : 指定一个worker进程能够打开的最大文件句柄数
(4)worker_rlimit_sigpending # : 指定每个用户能够发往worker信号的数量
优化性能相关的配置:
(1)worker_processes # :worker进程的个数,通常是cpu核心数减1
(2)worker_cpu_affinity cpuumask :绑定worker进程至指定的CPU上
(3)timer-resolution t :时间解析度,在x86服务器上可以不用配置
(4)worker_priority NICE :调整nice值(-20,19);nice值越大,越优先分配cpu
事件相关的配置;
(1)accept_mutex [on|off] :内部调动用户请求至各worker时的负载均衡锁;启用时表示能够让多个worker轮流的、序列化的响应请求
(2)lock_file /path/to/lock_file :指定锁文件
(3)accept_mutex_delay #ms: 取得负载均衡锁的时间
(4)use [epoll|poll|select|rgsig]:定义使用的事件模型,建议让nginx自动选择
(5)worker_connections #:每个worker进程所能够响应的最大并发请求数
|
|
|