# Ubuntu/Debian
sudo apt update
sudo apt install nginx -y
# CentOS/RHEL
sudo yum install epel-release -y
sudo yum install nginx -y
# 启动Nginx
sudo systemctl start nginx
sudo systemctl enable nginx
# 安装依赖
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev -y
# 下载源码
wget http://nginx.org/download/nginx-1.25.3.tar.gz
tar -zxvf nginx-1.25.3.tar.gz
cd nginx-1.25.3
# 配置编译选项
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-threads
# 编译安装
make
sudo make install
# 创建systemd服务文件
sudo tee /etc/systemd/system/nginx.service <<EOF
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT \$MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
# 启动服务
sudo systemctl daemon-reload
sudo systemctl start nginx
sudo systemctl enable nginx
apt update
或yum update
确保获取最新的软件包信息apt install nginx
或yum install nginx
从官方仓库安装systemctl
命令管理Nginx服务状态--prefix
:指定安装目录--with-*
:启用各种模块(SSL、HTTP2等)make
编译,make install
安装# 检查版本
nginx -v
# 检查运行状态
systemctl status nginx
# 测试配置文件
nginx -t
# 检查端口监听
ss -tulnp | grep nginx
/etc/nginx/nginx.conf
中的监听端口nginx -t
测试配置如需更详细的配置或特定场景的安装指导,请提供更多具体需求。