一、yum安装
yum install epel-release -y
yum install nginx -y
或者
[root@web-1 ~]# cd /etc/yum.repos.d/
[root@web-1 yum.repos.d]# vim nginx.repo
[root@web-1 yum.repos.d]# cat nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@web-1 yum.repos.d]# yum install nginx -y
配置文件位置:/etc/nginx/
存放页面文件位置:/usr/share/nginx/html
二、编译安装
[root@web ~]# cat onekey_install_nginx_vts.sh
#!/bin/bash
#解决软件的依赖关系,需要安装的软件包
yum -y install unzip zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make psmisc net-tools lsof vim wget -y
#新建 nginx1 用户和组
id nginx1|| useradd nginx1 -s /sbin/nologin
#下载 nginx 软件
mkdir /nginx1 -p
cd /nginx1
wget https://nginx.org/download/nginx-1.24.0.tar.gz
#解压软件
tar xf nginx-1.24.0.tar.gz
#进入解压后的文件夹
cd nginx-1.24.0
#解压vts模块包nginx-module-vts-master.zip
cp /root/nginx-module-vts-master.zip .
unzip nginx-module-vts-master.zip
#编译前的配置
./configure --prefix=/usr/local/nginx1 --user=nginx1 --group=nginx1 --with-http_ssl_module --with-threads --with-http_v2_module --with-http_stub_status_module --with-stream --with-http_gunzip_module
#如果上面的编译前的配置失败,直接退出脚本
if (( $? != 0));then
exit
fi
#编译,启动 2 个进程去编译,这样速度快
make -j 2
#编译安装
make install
#修改 PATH 变量
echo "PATH=$PATH:/usr/local/nginx1/sbin" >>/etc/bashrc
#firewalld and selinux
#stop firewall 和设置下次开机不启动 firewalld
service firewalld stop
systemctl disable firewalld
#临时停止 selinux 和永久停止 selinux
setenforce 0
sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config
#开机启动
chmod +x /etc/rc.d/rc.local
echo "/usr/local/nginx1/sbin/nginx" >>/etc/rc.local
#启动 nginx
/usr/local/nginx1/sbin/nginx
配置文件路径 /usr/local/nginx1/conf/
此时可使用nginx 启动nginx
停止nginx : nginx -s stop
修改nginx.conf后使用nginx -t 检查nginx.conf是否存在错误
使用nginx -s reload 重新加载
编译安装,很多功能可自己定义:
编译安装的参数 | 功能 |
---|---|
–with-http_stub_status_module | 状态统计功能 |
–with-stream | 4层负载均衡功能 |
–with-http_ssl_module | https |
–with-http_realip_module | 让后端的web服务器知道前面的代理的ip地址,获得真正的客户端的ip地址 |
–with-http_geo_module | 根据客户端的ip地址进行限制的 |
如何卸载
删除安装指定的目录,在PATH变量里删除nginx的安装路径
升级问题:
(1)重新去编译安装一个新的
(2)热升级-滚动升级
- 备份老的准备nginx二进制文件
- 准备新的nginx二进制文件,新的nginx二进制各配置路径保持一致
- 向老的nginx进程发送“SIGUSR2 (12)”信号,启动新的进程(新老进程并存)
- 向老的nginx进程发送“SIGQUIT (3)”信号停掉老的nginx进程
也可以创建一个 nginx.service文件,让systemctl进行管理
cd /usr/lib/systemd/system
vim nginx.service
[Unit]
Description=nginx - high performance web server
[Service]
Type=forking
PIDFile=/usr/local/nginx1/logs/nginx.pid
ExecStart=/usr/local/nginx1/sbin/nginx -c /usr/local/nginx1/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat $PIDFile)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat $PIDFile)"
[Install]
WantedBy=multi-user.target
需要重新加载 systemd 配置:systemctl daemon-reload
然后可使用systemctl start nginx启动nginx,
systemctl stop nginx 停止nginx,
systemctl restart nginx 重启nginx,
systemctl enable nginx 设置nginx开机启动