您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# Linux下安装Nginx的步骤是什么
Nginx是一款高性能的HTTP和反向代理服务器,在Linux系统中被广泛使用。本文将详细介绍在主流Linux发行版(Ubuntu/Debian/CentOS)上安装Nginx的完整流程,包含源码编译和包管理器两种安装方式。
## 一、准备工作
### 1. 系统要求
- Linux操作系统(推荐Ubuntu 20.04+/CentOS 7+)
- 至少1GB可用磁盘空间
- root或具有sudo权限的用户
### 2. 更新系统包
安装前建议先更新系统软件包:
```bash
# Ubuntu/Debian
sudo apt update && sudo apt upgrade -y
# CentOS/RHEL
sudo yum update -y
sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
sudo apt update
sudo apt install nginx -y
sudo yum install epel-release -y
sudo yum install nginx -y
nginx -v
# 应显示类似:nginx version: 1.25.3
wget https://nginx.org/download/nginx-1.25.3.tar.gz
tar zxvf nginx-1.25.3.tar.gz
cd nginx-1.25.3
# Ubuntu/Debian
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev
# CentOS/RHEL
sudo yum install gcc make pcre-devel zlib-devel openssl-devel
./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_gzip_static_module
make
sudo make install
创建服务文件/lib/systemd/system/nginx.service
:
[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=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MNPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
启用服务:
sudo systemctl enable nginx
sudo systemctl start nginx
# 启动服务
sudo systemctl start nginx
# 停止服务
sudo systemctl stop nginx
# 重启服务
sudo systemctl restart nginx
# 重载配置
sudo systemctl reload nginx
# 查看状态
sudo systemctl status nginx
/etc/nginx/nginx.conf
(包管理器安装)/etc/nginx/conf.d/
或/etc/nginx/sites-enabled/
/usr/share/nginx/html
sudo nginx -t
# 成功输出示例:
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful
# Ubuntu使用ufw
sudo ufw allow 'Nginx Full'
# CentOS使用firewalld
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
在浏览器访问服务器IP:
http://your_server_ip
应看到Nginx欢迎页面。
# Ubuntu/Debian
sudo apt purge nginx nginx-common -y
# CentOS/RHEL
sudo yum remove nginx -y
sudo rm -rf /usr/local/nginx
sudo rm /lib/systemd/system/nginx.service
sudo systemctl daemon-reload
通过以上步骤,您应该已成功在Linux系统上安装并运行Nginx服务器。建议生产环境使用包管理器安装以获得自动更新支持,开发环境可选用源码编译以获得更灵活的定制选项。 “`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。