nginx的安装
大约 5 分钟
Nginx (engine x) 是一个轻量级的、高性能的、基于 Http 的、反向代理服务器,静态 web 服务器。
Nginx 的官网: http://nginx.org
nginx 中文文档:https://www.nginx.cn/doc/index.html
nginx 源码安装(推荐)
########################
## Fancy美化索引目录-源码地址: https://github.com/aperezdc/ngx-fancyindex
## Fancy美化索引目录-主题地址: https://github.com/Naereen/Nginx-Fancyindex-Theme
########################
# 安装依赖库
yum install -y gcc gcc-c++ pcre-devel openssl-devel
# 创建临时目录,编译完毕可删除(可选)
mkdir -p ~/app && cd ~/app
wget http://nginx.org/download/nginx-1.23.1.tar.gz
tar -zxvf nginx-1.23.1.tar.gz
mv nginx-1.23.1 nginx
# Fancy美化索引目录
wget -O fancyindex.zip https://github.com/aperezdc/ngx-fancyindex/archive/v0.4.3.zip
unzip fancyindex.zip
mv ngx-fancyindex-* ngx-fancyindex
# 生成 makefile
cd ./nginx && ./configure \
--prefix=/usr/local/nginx \
--with-cc-opt=-O2 \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_auth_request_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--add-module=../ngx-fancyindex
# 没有报错,开始编译安装
make && make install
# 编译安装完成,查看目录
ll /usr/local/nginx/
# 添加到环境变量
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
yum 安装
- 官网地址:http://nginx.org/en/linux_packages.html
- 易白教程:https://www.yiibai.com/nginx/nginx-install-linux-packages.html
sudo yum install yum-utils
## 配置yum源
#rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
cat >> /etc/yum.repos.d/nginx.repo << EOF
[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
EOF
yum-config-manager --enable nginx-mainline
## yum安装nginx
yum install nginx -y
## 一般情况下,nginx.conf文件在 /usr/local/nginx/conf 或者 /etc/nginx 或者 /usr/local/etc/nginx 目录下
离线安装
## rmp下载地址
# http://nginx.org/packages/centos/7/x86_64/RPMS/
wget http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.22.0-1.el7.ngx.x86_64.rpm
## 检查是否安装了nginx
rpm -qa|grep nginx
## 卸载nginx
systemctl stop nginx && rpm -e nginx && rm -rf /etc/nginx/
## 安装nginx
rpm -ivh nginx-1.22.0-1.el7.ngx.x86_64.rpm
## 报错:libpcre2-8.so.0(openssl)(64bit) 被 nginx-1:1.22.0-1.el7.ngx.x86_64 需要
## 缺少openssl, 直接rpm安装 http://mirrors.163.com/centos/7/os/x86_64/Packages/
wget http://mirrors.163.com/centos/7/os/x86_64/Packages/openssl-libs-1.0.2k-19.el7.x86_64.rpm
rpm -ivh openssl-libs-1.0.2k-19.el7.x86_64.rpm --force
离线安装
mkdir ~/nginx && cd ~/nginx
yum -y install wget
wget https://nginx.org/download/nginx-1.21.6.tar.gz
tar -xvzf nginx-1.21.6.tar.gz
# yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
yum install --downloadonly --downloaddir=~/nginx/ gcc-c++
yum install --downloadonly --downloaddir=~/nginx/ pcre pcre-devel4
yum install --downloadonly --downloaddir=~/nginx/ zlib zlib-devel
yum install --downloadonly --downloaddir=~/nginx/ openssl openssl-devel
rpm -ivh --nodeps *.rpm
cd nginx-1.21.6
./configure --prefix=/usr/local/nginx
make && make install
# 编译安装完成,查看目录
ll /usr/local/nginx/
# 添加到环境变量
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
nginx 命令
nginx 命令
# 查看 Nginx 命令的选项
nginx –h
# Nginx 版本信息
nginx –v
# 测试配置文件是否正确,默认只测试默认的配置文件 conf/nginx.conf。
nginx –t
# 测试配置文件是否正确,并显示配置文件内容。
nginx –T
# 在配置文件测试过程中,禁止显示非错误信息,即只显示错误信息。
nginx –tq
# 可以结合-c 选项指定要测试的配置文件。注意,其不会启动 nginx。
nginx -c /usr/local/nginx/conf/nginx.conf -t
# 强制停止 Nginx,无论当前工作进程是否正在处理工作。
nginx –s stop
# 优雅停止 Nginx,使当前的工作进程完成当前工作后停止
nginx –s quit
# 在不重启 Nginx 的前提下重新加载 Nginx 配置文件,称为平滑重启。
nginx -s reload
# 重新打开日志文件。
nginx –s reopen
# 指定 Nginx 配置文件的存放路径。
nginx –p /usr/local/nginx/
# nginx –c(小写字母)可启动 Nginx,启动成功后无任何提示。默认加载的是 Nginx 安装目录下的 conf/nginx.cnf。
nginx -c /usr/local/nginx/conf/nginx.conf
# 查看进程运行时间
ps -eo pid,user,lstart,etime,cmd | grep nginx
# 查看已经建立连接的数量
netstat -an | grep ESTABLISHED | wc -l
# 查看80端口的连接数
netstat -an | grep ":80" | wc -l
# 检查配置文件的是否正确 只显示错误信息
nginx -c /usr/local/nginx/conf/nginx.conf –tq
# 重新加载配置文件
nginx -s reload
# 优雅退出
nginx –s quit
nginx 开机自启
cat >> /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /usr/local/nginx/logs/nginx.pid
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload && systemctl enable nginx && systemctl start nginx && systemctl status nginx
脚本部署
脚本内容
#!/bin/bash
ck_ok()
{
if [ $? -ne 0 ]
then
echo "$1 error."
exit 1
fi
}
download_ng()
{
cd /usr/local/src
if [ -f nginx-1.23.0.tar.gz ]
then
echo "当前目录已经存在nginx-1.23.0.tar.gz"
echo "检测md5"
ng_md5=`md5sum nginx-1.23.0.tar.gz|awk '{print $1}'`
if [ ${ng_md5} == 'e8768e388f26fb3d56a3c88055345219' ]
then
return 0
else
sudo /bin/mv nginx-1.23.0.tar.gz nginx-1.23.0.tar.gz.old
fi
fi
sudo curl -O http://nginx.org/download/nginx-1.23.0.tar.gz
ck_ok "下载Nginx"
}
install_ng()
{
cd /usr/local/src
echo "解压Nginx"
sudo tar zxf nginx-1.23.0.tar.gz
ck_ok "解压Nginx"
cd nginx-1.23.0
echo "安装依赖"
if which yum >/dev/null 2>&1
then
## RHEL/Rocky
for pkg in gcc make pcre-devel zlib-devel openssl-devel
do
if ! rpm -q $pkg >/dev/null 2>&1
then
sudo yum install -y $pkg
ck_ok "yum 安装$pkg"
else
echo "$pkg已经安装"
fi
done
fi
if which apt >/dev/null 2>&1
then
##ubuntu
for pkg in make libpcre++-dev libssl-dev zlib1g-dev
do
if ! dpkg -l $pkg >/dev/null 2>&1
then
sudo apt install -y $pkg
ck_ok "apt 安装$pkg"
else
echo "$pkg已经安装"
fi
done
fi
echo "configure Nginx"
sudo ./configure --prefix=/usr/local/nginx --with-http_ssl_module
ck_ok "Configure Nginx"
echo "编译和安装"
sudo make && sudo make install
ck_ok "编译和安装"
echo "编辑systemd服务管理脚本"
cat > /tmp/nginx.service <<EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP \$(/bin/cat /usr/local/nginx/logs/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM \$(/bin/cat /usr/local/nginx/logs/nginx.pid)"
[Install]
WantedBy=multi-user.target
EOF
sudo /bin/mv /tmp/nginx.service /lib/systemd/system/nginx.service
ck_ok "编辑nginx.service"
echo "加载服务"
sudo systemctl unmask nginx.service
sudo systemctl daemon-reload
sudo systemctl enable nginx
echo "启动Nginx"
sudo systemctl start nginx
ck_ok "启动Nginx"
}
download_ng
install_ng
执行脚本
chmod +x nginx_install.sh && ./nginx_install.sh