在 Linux 上安装 nginx 通常有多种方式,最常见的是使用系统包管理器安装和源码编译安装。下面按不同发行版给你说明。
# 更新软件包列表
sudo apt update
# 安装 nginx
sudo apt install nginx -y
# 启动并设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx
验证:
systemctl status nginx
或浏览器访问 http://服务器IP
sudo yum install epel-release -y
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
sudo dnf install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
sudo zypper install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
以 Ubuntu 22.04 为例:
sudo apt install curl gnupg2 ca-certificates lsb-release -y
# 添加 nginx 官方源
echo "deb http://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" | \
sudo tee /etc/apt/sources.list.d/nginx.list
# 导入密钥
curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
sudo apt update
sudo apt install nginx -y
# 安装依赖
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev -y
# 下载源码
wget http://nginx.org/download/nginx-1.26.0.tar.gz
tar -zxvf nginx-1.26.0.tar.gz
cd nginx-1.26.0
# 编译
./configure --prefix=/usr/local/nginx
make
sudo make install
# 启动
/usr/local/nginx/sbin/nginx
sudo ufw allow 80
sudo ufw allow 443
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
nginx -v # 查看版本
sudo nginx -t # 测试配置
sudo systemctl reload nginx
sudo systemctl restart nginx
如果你告诉我:
我可以给你更精确的安装和配置方案。