梦里乡 發表於 2025-6-16 00:00:00

Ubuntu上安装、卸载、配置nginx的具体操作指南

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>一、安装 Nginx</li><li>二、基础配置(把配置文件独立出来)</li><li>三、启动HTTPS</li><li>四、卸载&nbsp;Nginx</li><li>五、故障排查</li><li>六、关键命令总结&nbsp;</li><li>拓展:Ubuntu安装rpm文件<ul class="second_class_ul"><li>1.&nbsp;安装&nbsp;alien&nbsp;工具</li><li>2.&nbsp;使用&nbsp;alien&nbsp;转换&nbsp;.rpm&nbsp;文件为&nbsp;.deb&nbsp;文件</li><li>3.&nbsp;安装转换后的&nbsp;.deb&nbsp;文件</li><li>4.&nbsp;处理依赖关系(如果有的话)</li></ul></li></ul></div><p class="maodian"></p><h2>一、安装 Nginx</h2>
<p>1. 更新软件包列表</p>
<div class="dxycode"><pre class="brush:bash;">sudo apt update</pre></div>
<p>2. 安装 Nginx</p>
<div class="dxycode"><pre class="brush:bash;">sudo apt install nginx -y</pre></div>
<p>3. 验证安装</p>
<div class="dxycode"><pre class="brush:bash;">nginx -v
# 输出示例:nginx version: 1.18.0 (Ubuntu)</pre></div>
<p>4. 启动并设置开机自启</p>
<div class="dxycode"><pre class="brush:bash;">sudo systemctl start nginx
sudo systemctl enable nginx</pre></div>
<p>5. 检查服务状态</p>
<div class="dxycode"><pre class="brush:bash;">sudo systemctl status nginx
# 正常应显示 "active (running)"</pre></div>
<p>6. 可以浏览器中测试看看:ip地址:80</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="363" src="https://zhuji.jb51.net/uploads/allimg/20250616/2-250616115103110.png" width="1185" /></p>
<p class="maodian"></p><h2>二、基础配置(把配置文件独立出来)</h2>
<p>1. 配置文件结构</p>
<div class="dxycode"><pre class="brush:bash;">/etc/nginx/
├── nginx.conf          # 主配置文件
├── sites-available/    # 可用站点配置
├── sites-enabled/      # 已启用站点(符号链接)
├── conf.d/             # 附加配置
└── snippets/         # 可复用配置片段</pre></div>
<p>2. 修改配置文件,将配置文件的目录独立出来</p>
<div class="dxycode"><pre class="brush:bash;">sudo vim /etc/nginx/nginx.conf</pre></div>
<p>2.1 <strong>i</strong>进入编辑模式,把第一行user修改为root。找到&quot;Virtual Host Configs&quot;下添加一行include /root/xx/nginx_cfg/*.conf;然后:wq保存退出</p>
<p>/xx/nginx_cfg:是自己在root目录创建的文件夹;自己可自定义</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="222" src="https://zhuji.jb51.net/uploads/allimg/20250616/2-250616115103545.png" width="494" /></p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="693" src="https://zhuji.jb51.net/uploads/allimg/20250616/2-250616115104548.png" width="777" /></p>
<p>3. 在创建的&ldquo;/xx/nginx_cfg&rdquo;目录里新建一个后缀名为conf的配置文件进行配置。</p>
<div class="dxycode"><pre class="brush:plain;">    server {
      listen       80;#监听端口号,如果没有域名不要用80
      server_namelocalhost; #域名(多个用空格分隔)
                charset utf-8;
      #静态文件处理
                location / {
            root   /root/html/shiyan-manager/dist; #网站根目录(前端代码打包在服务器位置)
                        try_files $uri $uri/ /index.html;
            indexindex.html index.htm;
            client_max_body_size 2048m;
      }
      #反向代理配置(指向本地应用)
                location /dev-api/ {
                        proxy_set_header Host $http_host;
                        proxy_set_header X-Real-IP $remote_addr;
                        proxy_set_header REMOTE-HOST $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size 2048m;
                        proxy_pass http://localhost:8080/; #转发到本地的8080端口(后端的端口号)
                }
      error_page   500 502 503 504/50x.html;
      location = /50x.html {
            root   html;
      }
    }</pre></div>
<p>4.配置文件写完后,刷新配置,浏览器访问即可</p>
<div class="dxycode"><pre class="brush:bash;">sudo nginx -t# 测试配置语法
sudo systemctl reload nginx</pre></div>
<p class="maodian"></p><h2>三、启动HTTPS</h2>
<p><strong>1.登录云服务器数字证书管理服务控制台,生成并下载SSL证书,选择Nginx服务器类型下载</strong></p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="782" src="https://zhuji.jb51.net/uploads/allimg/20250616/2-250616115104X1.png" width="2276" /></p>
<p><strong>2.把下载的证书上传到Linux服务器,位置自定义,可放在nginx目录</strong></p>
<p>2.1执行以下命令,在Nginx目录下创建一个用于存放证书的目录。</p>
<div class="dxycode"><pre class="brush:bash;">cd /etc/nginx#进入Nginx文件目录。或者自定义目录都行
mkdir cert#创建证书目录,命名为cert。</pre></div>
<p>2.2将证书文件和私钥文件上传到Nginx服务器的证书目录(/etc/nginx/cert)。</p>
<p><strong>3.修改配置</strong></p>
<div class="dxycode"><pre>server {
    listen 443 ssl;
    server_name example.com; #域名
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; #上传的证书
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.key; #上传的证书
    # 安全协议和加密套件
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers &#39;ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384&#39;;
    ssl_prefer_server_ciphers on;
    # HSTS(强制HTTPS)
    add_header Strict-Transport-Security &quot;max-age=31536000; includeSubDomains&quot; always;
    charset utf-8;
      #静态文件处理
                location / {
            root   /root/html/shiyan-manager/dist; #网站根目录(前端代码打包在服务器位置)
                        try_files $uri $uri/ /index.html;
            indexindex.html index.htm;
            client_max_body_size 2048m;
      }
      #反向代理配置(指向本地应用)
                location /dev-api/ {
                        proxy_set_header Host $http_host;
                        proxy_set_header X-Real-IP $remote_addr;
                        proxy_set_header REMOTE-HOST $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size 2048m;
                        proxy_pass http://localhost:8080/; #转发到本地的8080端口(后端的端口号)
                }
      error_page   500 502 503 504/50x.html;
      location = /50x.html {
            root   html;
      }
}
server {
    listen 80;
    #填写证书绑定的域名
    server_name ;
    #将所有HTTP请求通过rewrite指令重定向到HTTPS。
    rewrite ^(.*)$ https://$host$1;
    location / {
      index index.html index.htm;
    }
}</pre></div>
<p>4.配置文件写完后,刷新配置,浏览器访问即可&nbsp;</p>
<div class="dxycode"><pre class="brush:bash;">sudo nginx -t# 测试配置语法
sudo systemctl reload nginx</pre></div>
<p class="maodian"></p><h2>四、卸载&nbsp;Nginx</h2>
<p>1. 停止服务</p>
<div class="dxycode"><pre class="brush:bash;">sudo systemctl stop nginx
sudo systemctl disable nginx</pre></div>
<p>2. 卸载软件包</p>
<div class="dxycode"><pre class="brush:bash;">sudo apt purge nginx nginx-common</pre></div>
<p>3. 删除残留文件</p>
<div class="dxycode"><pre class="brush:bash;">sudo rm -rf /etc/nginx /var/log/nginx /var/www/html/*
sudo apt autoremove</pre></div>
<p class="maodian"></p><h2>五、故障排查</h2>
<table><thead><tr><th>问题现象</th><th>解决方法</th></tr></thead><tbody><tr><td><strong>端口占用</strong></td><td>`sudo ss -tulnp</td><td>grep :80` &rarr; 结束冲突进程</td></tr><tr><td><strong>配置语法错误</strong></td><td><code>sudo nginx -t</code>&nbsp;检查错误行</td></tr><tr><td><strong>403 Forbidden</strong></td><td>检查目录权限:<code>sudo chmod -R 755 /var/www</code></td></tr><tr><td><strong>502 Bad Gateway</strong></td><td>确认后端服务是否运行,<code>proxy_pass</code>&nbsp;地址是否正确</td></tr></tbody></table>
<table><tbody><tr></tr></tbody><tbody><tr></tr></tbody></table>
<p class="maodian"></p><h2>六、关键命令总结&nbsp;</h2>
<table><thead><tr><th>功能</th><th>命令</th></tr></thead><tbody><tr><td>测试配置</td><td><code>sudo nginx -t</code></td></tr><tr><td>重新加载配置</td><td><code>sudo systemctl reload nginx</code></td></tr><tr><td>查看访问日志</td><td><code>sudo tail -f /var/log/nginx/access.log</code></td></tr><tr><td>检查运行中的配置</td><td><code>sudo nginx -T</code></td></tr></tbody></table>
<blockquote><p>1. 所有的 Nginx 配置文件都在/etc/nginx/目录下</p>
<p>2. 主要的 Nginx 配置文件是/etc/nginx/nginx.conf</p>
<p>3. Nginx的启动文件:/sbin/nginx</p>
<p>4. Nginx 日志文件(access.log 和 error.log)定位在/var/log/nginx/目录下&nbsp;</p></blockquote>
<p class="maodian"></p><h2>拓展:Ubuntu安装rpm文件</h2>
<p class="maodian"></p><h3>1.&nbsp;安装&nbsp;alien&nbsp;工具</h3>
<p>首先,您需要安装&nbsp;<code>alien</code>&nbsp;工具,这个工具可以将&nbsp;<code>.rpm</code>&nbsp;文件转换为&nbsp;<code>.deb</code>&nbsp;文件,适用于&nbsp;Ubuntu 系统。</p>
<div class="dxycode"><pre class="brush:bash;">sudo apt update
sudo apt install alien</pre></div>
<p class="maodian"></p><h3>2.&nbsp;使用&nbsp;alien&nbsp;转换&nbsp;.rpm&nbsp;文件为&nbsp;.deb&nbsp;文件</h3>
<p>在安装了&nbsp;<code>alien</code>&nbsp;后,您可以将&nbsp;<code>.rpm</code>&nbsp;文件转换为&nbsp;<code>.deb</code>&nbsp;文件。</p>
<div class="dxycode"><pre class="brush:bash;">sudo alien -d xx.rpm</pre></div>
<ul><li><code>-d</code>&nbsp;参数表示转换为&nbsp;<code>.deb</code>&nbsp;格式。</li><li><code>xx.rpm</code>&nbsp;是您要安装的 RPM 文件的路径。</li></ul>
<p>该命令将创建一个与原始 RPM 文件相对应的&nbsp;<code>.deb</code>&nbsp;文件。</p>
<p class="maodian"></p><h3>3.&nbsp;安装转换后的&nbsp;.deb&nbsp;文件</h3>
<p>完成转换后,您可以使用&nbsp;<code>dpkg</code>&nbsp;或&nbsp;<code>apt</code>&nbsp;安装生成的&nbsp;<code>.deb</code>&nbsp;文件:</p>
<div class="dxycode"><pre class="brush:bash;">sudo dpkg -i xx.deb</pre></div>
<p>或者使用&nbsp;<code>apt</code>&nbsp;来安装(<code>apt</code>&nbsp;会自动处理依赖关系):</p>
<div class="dxycode"><pre class="brush:bash;">sudo apt install ./xx.deb</pre></div>
<p class="maodian"></p><h3>4.&nbsp;处理依赖关系(如果有的话)</h3>
<p>如果安装过程中出现依赖错误,您可以运行以下命令来修复缺失的依赖关系:</p>
<div class="dxycode"><pre class="brush:bash;">sudo apt --fix-broken install</pre></div>
<p>以上就是ubuntu上安装、卸载、配置nginx的详细内容,更多相关资料请阅读琼殿技术社区其它文章!</p>
頁: [1]
查看完整版本: Ubuntu上安装、卸载、配置nginx的具体操作指南