一 yum 安装Nginx
1.1 首先安装RHEL/CentOSrepository
sudo vim /etc/yum.repos.d/nginx.repo
下面的配置添加到这个文件
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1
注意:
# baseurl中,OS取决于你的操作系统,如果是Centos这里就是centos,如果是Redhat这里就是rhel
# baseurl中,OSRELEASE取决于的操作系统版本,如果是centos6.x,这里就是6,如果是7.x这里就是7
所以我的baseurl是:
http://nginx.org/packages/centos/7/$basearch/
1.2 开始安装nginx
sudo install -y nginx
1.3 测试安装成功与否
nginx –v
nginx version: nginx/1.12.2
二 源码包安装
2. 1 首先下载源码包
wget http://mirrors.sohu.com/nginx/nginx-1.10.3.tar.gz
2.2 检测nginx需要的依赖
yum install -y gcc gcc-c++ autoconf automake zlibzlib-devel openssl openssl-devel pcre pcre-devel
zlib:nginx提供gzip模块,需要zlib库支持
openssl: nginx提供ssl功能
pcre :支持地址重写rewrite功能
2.3 配置nginx
./configure --prefix=/opt/app/nginx \
--pid-path=/opt/app/nginx/nginx.pid \
--error-log-path=/opt/app/nginx/logs/error.log \
--http-log-path=/opt/app/nginx/logs/access.log \
--with-http_ssl_module
如果你希望使用自己安装的zlib,pcre等包,可以根据nginx的安装文档自行配置
结果:
nginx pathprefix: "/opt/app/nginx"
nginxbinary file: "/opt/app/nginx/sbin/nginx"
nginxmodules path: "/opt/app/nginx/modules"
nginxconfiguration prefix: "/opt/app/nginx/conf"
nginxconfiguration file: "/opt/app/nginx/conf/nginx.conf"
nginx pidfile: "/opt/app/nginx/nginx.pid"
nginx errorlog file: "/opt/app/nginx/logs/error.log"
nginx httpaccess log file: "/opt/app/nginx/logs/access.log"
nginx httpclient request body temporary files: "client_body_temp"
nginx httpproxy temporary files: "proxy_temp"
nginx httpfastcgi temporary files: "fastcgi_temp"
nginx httpuwsgi temporary files: "uwsgi_temp"
nginx httpscgi temporary files: "scgi_temp"
2.4 编译安装
make && make install
2.5 配置环境变量
# NGINX_HOME
export NGINX_HOME=/opt/app/nginx
export PATH=$PATH:$NGINX_HOME/sbin
2.6 查看nginx位置和版本
whereis nginx && nginx -v
nginx: /opt/app/nginx/sbin/nginx
nginx version: nginx/1.10.3
三 Nginx命令详解
nginx -h
-v/V: 显示nginx版本和配置选项信息等
-t: 测试配置文件
-T: 测试配置文件并且dump
-s: 发送信号给master进程:选项有stop,quit,reopen,reload
stop: 快速关闭
quit: 优雅关闭
reopen: 重新打开日志文件
reload: 重新加载配置文件
-c: 设置配置文件,默认是conf/nginx.conf
另外至于信号,我们也可以借助Linux的kill命令来发送:
TERM, INT: 快速关闭
QUIT: 优雅的关闭,即等待所有请求结束后在关闭
HUP:改变配置文件,开启一个新的工作者进程处理,然后再优雅关闭老的进程
USR1:重读日志
USR2: 平滑的升级
WINCH: 优雅关闭旧进程,配合USR2使用
/bin/kill -信号选项 nginx的主进程号
/bin/kill -HUP pid
/bin/kill -信号控制 cat /xxx/path/log/nginx.pid
/bin/klill -USR1 cat /xxx/path/log/nginx.pid
/bin/kill -s QUIT pid 优雅关闭
/bin/kill -s TERM pid 快速关闭
四 启动Nginx
[dubbo@dubbo-01 sbin]$ nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (13:Permission denied)
发现失败了,这是因为普通用户只能用1024以上的端口,1024以内的端口只能由root用户使用。但是为了避免每次启动都通过root用户,可以通过set UID的方式来解决此问题,而且这种方式只适合二进制文件比如exe,不适合shell脚本。
sudo chown root:root nginx
sudo chmod u+s nginx
五 检测是否Nginx已经启动
ps -A|grep nginx
14725 ? 00:00:00 nginx
14726 ? 00:00:00 nginx
六 Nginx作为开机启动
6.1 Centos6.X
vim /etc/inid.d/nginx
#!/bin/bash
# nginx Startup script for the Nginx HTTPServer
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web andproxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /opt/app/nginx/conf/nginx.conf
nginxd=/opt/app/nginx/sbin/nginx
nginx_config=/opt/app/nginx/conf/nginx.conf
nginx_pid=/opt/app/ngin/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo"nginx already running...."
exit1
fi
echo -n$"Starting $prog: "
daemon$nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL= 0 ] && touch /var/lock/subsys/nginx
return$RETVAL
}
# Stop nginx daemons functions.
stop() {
echo-n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[$RETVAL = 0 ] && rm -f /var/lock/subsys/nginx/opt/app/nginx/logs/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n$"Reloading $prog: "
#kill-HUP `cat ${nginx_pid}`
killproc$nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo$"Usage: $prog {start|stop|restart|reload|status|help}"
exit1
esac
exit $RETVAL
# 设置访问权限
sudo chmod a+x /etc/init.d/nginx
# 最后将nginx加入到rc.local文件中,这样开机的时候nginx就默认启动了
vim /etc/rc.local
添加
/etc/init.d/nginx start
6.2 Centos7.X
Centos 系统服务脚本目录:
用户登录后才能运行的程序,存在用户/usr/lib/systemd/
需要开机没有登陆情况下就能运行的程序,存在系统服务/lib/systemd/system/
sudo vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.targetnss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/opt/app/nginx/nginx.pid
ExecStartPre=/opt/app/nginx/sbin/nginx -t -c/opt/app/nginx/conf/nginx.conf
ExecStart=/opt/app/nginx/sbin/nginx -c/opt/app/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target [Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
# 设置开机启动
sudo systemctl enable nginx.service
Created symlink from/etc/systemd/system/multi-user.target.wants/nginx.service to/usr/lib/systemd/system/nginx.service.
七 查找并安装第三方模块
7.1 查找模块,下载并且解压缩
7.2 通过configure-add-module=<path>选项配置使用该模块。