美文网首页
如何在ubuntu20.04中安装和配置Nginx

如何在ubuntu20.04中安装和配置Nginx

作者: Benedict清水 | 来源:发表于2021-05-18 21:52 被阅读0次

在ubuntu20.04中安装和配置Nginx

前言

Nginx 是全球使用最广泛的web servers之一。本教程,我们学习如何在Ubuntu20.04中安装和配置Nginx。

一、准备工作

  • 一台在virtualBox中安装好的Ubuntu20.04服务器
  • Termius:Termius是可以在桌面上使用的SSH客户端

二、安装和调整

第一步:安装Nginx

Nginx已经整合到ubuntu的软件源,因此我们可以方便的使用apt来安装。

sudo apt install nginx

第二步:调整防火墙

在测试 Nginx 之前,我们需要调整防火墙,让他允许 Nginx 服务通过。Nginx ufw 在安装时会把他自身注册成为服务。

sudo ufw app list

输出结果:

benedict@benedict-pc:~$ sudo ufw app list
[sudo] password for benedict:
Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH

你可以看到 Nginx 提供了三个配置文件:

  • Nginx Full
    开端口80 正常,未加密的网络流量
    端口443 TLS / SSL加密的流量

  • Nginx HTTP
    仅打开端口80 正常,未加密

  • Nginx HTTPS
    仅打开端口443 TLS / SSL加密

我们使用 Nginx HTTP 来做本教程演示

sudo ufw allow 'Nginx HTTP'

我们用以下命令来查看更改结果

sudo ufw status

我们可以在输出结果中看到

benedict@benedict-pc:~$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
Nginx HTTP                 ALLOW       Anywhere                  

Nginx HTTP (v6)            ALLOW       Anywhere (v6)  

第三步:检查我们的 Web 服务器

在安装结束后,Ubuntu 会启动 Nginx 。 Web 服务器应该已经在运行了。
我们可以通过 systemd 来检查 init 系统状态,确保它正在运行:

systemctl status nginx

输出如下:

benedict@benedict-pc:~$ systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2021-05-18 12:30:12 UTC; 56min ago
       Docs: man:nginx(8)
    Process: 686 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 761 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 776 (nginx)
      Tasks: 2 (limit: 2281)
     Memory: 3.4M
     CGroup: /system.slice/nginx.service
             ├─776 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             └─778 nginx: worker process

May 18 12:30:11 benedict-pc systemd[1]: Starting A high performance web server and a reverse proxy server...
May 18 12:30:12 benedict-pc systemd[1]: Started A high performance web server and a reverse proxy server.

首先我们执行以下命令,这可以让我们在浏览器中查看他们是否正常工作。

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

如果要寻找我们服务器在公网的ip我们可以使用如下命令。

curl -4 icanhazip.com

但我们是在虚拟机中安装的ubuntu server20.04,可以利用virtualbox的端口转发功能,暴露虚拟机的80端口。


virtualbox1.png

打开浏览器输入ip 就能看到 Nginx 的默认页面。这也说明服务器运行起来了。


nginx.png

三、管理 Nginx

要停止Web服务器,输入:

sudo systemctl stop nginx

要在停止时,启动Web服务器,键入:

sudo systemctl start nginx

要停止,然后再次启动该服务,键入:

sudo systemctl restart nginx

如果我们只是修改配置,Nginx 可以在不终端的情况下热加载。我们可以键入:

sudo systemctl reload nginx

默认情况下,Nginx 会在服务器启动时,跟随系统启动,如果我们不想这样,我们可以用这个命令来禁止:

sudo systemctl disable nginx

要重新让系统启动时引导 Nginx 启动,那么我们可以输入:

sudo systemctl enable nginx

相关文章

网友评论

      本文标题:如何在ubuntu20.04中安装和配置Nginx

      本文链接:https://www.haomeiwen.com/subject/etayjltx.html