Appearance
Linux下安装Nginx
在Linux系统上安装Nginx有多种方式,包括包管理器安装、源码编译安装等。
使用包管理器安装
Ubuntu/Debian
bash
# 更新包索引
sudo apt update
# 安装Nginx
sudo apt install nginx
# 启动Nginx
sudo systemctl start nginx
# 设置开机自启
sudo systemctl enable nginxCentOS/RHEL
bash
# 安装EPEL仓库
sudo yum install epel-release
# 安装Nginx
sudo yum install nginx
# 启动Nginx
sudo systemctl start nginx
# 设置开机自启
sudo systemctl enable nginxFedora
bash
# 安装Nginx
sudo dnf install nginx
# 启动Nginx
sudo systemctl start nginx
# 设置开机自启
sudo systemctl enable nginx源码编译安装
源码编译安装可以获取最新版本,并自定义编译选项。
安装依赖
bash
# Ubuntu/Debian
sudo apt install build-essential libpcre3-dev zlib1g-dev libssl-dev
# CentOS/RHEL
sudo yum groupinstall "Development Tools"
sudo yum install pcre-devel zlib-devel openssl-devel下载源码
bash
# 下载最新版本
wget http://nginx.org/download/nginx-1.24.0.tar.gz
# 解压
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0编译安装
bash
# 配置编译选项
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_gzip_static_module \
--with-http_stub_status_module
# 编译
make
# 安装
sudo make install创建系统服务
创建/etc/systemd/system/nginx.service文件:
ini
[Unit]
Description=nginx web server
After=network.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target启用服务:
bash
sudo systemctl daemon-reload
sudo systemctl start nginx
sudo systemctl enable nginx官方仓库安装
使用Nginx官方仓库可以获取最新稳定版本。
Ubuntu/Debian
bash
# 安装依赖
sudo apt install curl gnupg2 ca-certificates lsb-release
# 添加Nginx官方GPG密钥
curl https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
# 添加仓库
echo "deb http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
# 更新包索引
sudo apt update
# 安装Nginx
sudo apt install nginxCentOS/RHEL
bash
# 安装yum工具
sudo yum install yum-utils
# 添加Nginx仓库
sudo vim /etc/yum.repos.d/nginx.repo添加以下内容:
ini
[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
[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安装Nginx:
bash
sudo yum install nginx验证安装
检查版本
bash
nginx -v检查服务状态
bash
sudo systemctl status nginx测试访问
在浏览器中访问 http://localhost 或 http://服务器IP,应该能看到Nginx欢迎页面。
检查端口
bash
sudo netstat -tlnp | grep nginx防火墙配置
如果服务器启用了防火墙,需要开放HTTP和HTTPS端口。
Ubuntu (UFW)
bash
sudo ufw allow 'Nginx Full'
sudo ufw allow 'Nginx HTTP'
sudo ufw allow 'Nginx HTTPS'CentOS (firewalld)
bash
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload常见问题
端口被占用
如果80端口被占用,可以修改Nginx监听端口或停止占用端口的程序。
权限问题
确保Nginx运行用户有权限访问相关目录和文件。
配置文件错误
修改配置后,使用以下命令检查配置:
bash
sudo nginx -t总结
Linux下安装Nginx的主要方式:
- 包管理器安装:简单快速,适合大多数场景
- 源码编译安装:灵活可定制,适合特殊需求
- 官方仓库安装:获取最新版本,适合追求最新功能
根据实际需求选择合适的安装方式,安装完成后记得配置防火墙和验证服务状态。