Ubuntu 安装Nginx服务


前言

  Nginx(发音为"engine-x")是一个高性能的开源Web服务器和反向代理服务器,也可以用作负载均衡器、HTTP缓存以及作为前端服务器用于处理静态文件、媒体流、以及其他网络协议(如SMTP、POP3和IMAP)。Nginx最初由Igor Sysoev开发,并于2004年首次发布,目前由Nginx公司进行维护。

Nginx以其出色的性能和高可靠性而闻名,常用于大流量的网站和应用程序,如互联网公司、电子商务平台、内容分发网络(CDN)等。它的设计目标是解决C10K问题,即同时处理成千上万个并发连接的能力。Nginx采用了事件驱动的架构和异步I/O模型,能够高效地处理大量并发请求,同时占用较少的系统资源。

Nginx的主要特点包括:

  • 高性能:Nginx采用了高效的事件驱动和异步I/O模型,能够在高负载下保持出色的性能,处理大量并发请求。

  • 灵活的配置:Nginx使用简洁、易于理解的配置语法,支持灵活的配置选项和模块化的架构,可以根据需要进行定制化配置。

  • 反向代理和负载均衡:Nginx可以作为反向代理服务器,将请求转发给后端服务器,同时支持负载均衡功能,有效分发流量到多个后端服务器上。

  • HTTP缓存:Nginx可以作为HTTP缓存服务器,缓存静态文件和动态内容,减轻后端服务器的负载,提高网站性能。

  • 安全性:Nginx具有强大的安全特性,支持SSL/TLS协议,能够进行HTTPS加密通信,以及提供访问控制、防止DDoS攻击等安全功能。

  • 可扩展性:Nginx提供了丰富的第三方模块和插件,支持动态加载模块,可以扩展其功能,满足不同应用场景的需求。

  总之,Nginx是一个高性能、灵活、可靠和安全的Web服务器和反向代理服务器,适用于处理大量并发请求和高流量的网站和应用程序。它的出色性能和丰富的功能使其成为当前互联网架构中不可或缺的重要组件之一。


一、Nginx安装

1. Nginx默认安装

# 更新索引
sudo apt update
# 安装nginx
sudo apt install nginx

这种方法最方便,直接输入命令,等待完成就完事了。

2. Nginx指定版本安装

# 更新索引
sudo apt update
# 查看当前可用的版本
sudo apt-cache show nginx

查询结果如下:

Package: nginx
Architecture: amd64
Version: 1.18.0-6ubuntu14.4
Priority: optional
Section: web
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian Nginx Maintainers <pkg-nginx-maintainers@alioth-lists.debian.net>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 49
Depends: nginx-core (<< 1.18.0-6ubuntu14.4.1~) | nginx-full (<< 1.18.0-6ubuntu14.4.1~) | nginx-light (<< 1.18.0-6ubuntu14.4.1~) | nginx-extras (<< 1.18.0-6ubuntu14.4.1~), nginx-core (>= 1.18.0-6ubuntu14.4) | nginx-full (>= 1.18.0-6ubuntu14.4) | nginx-light (>= 1.18.0-6ubuntu14.4) | nginx-extras (>= 1.18.0-6ubuntu14.4)
Breaks: libnginx-mod-http-lua (<< 1.18.0-6ubuntu5)
Filename: pool/main/n/nginx/nginx_1.18.0-6ubuntu14.4_amd64.deb
Size: 3872
MD5sum: c50a1bfb0a399eeda5a918bad2b7826c
SHA1: db65e7261f613c2145ffb9e6477ee9ef15e9f994
SHA256: 38660dc920b2203436ee75788d9c12d064b9a49984eb7edd61dd572d95a65243
SHA512: 099e1a145adcb5a8543f41caa40a335c1d59bf1150368208829da6ab5b3484415d895223638d628eed3c14532b6302f824a2ff0b6a79163c07b2102453056e1a
Homepage: https://nginx.net
Description-en: small, powerful, scalable web/proxy server
 Nginx ("engine X") is a high-performance web and reverse proxy server
 created by Igor Sysoev. It can be used both as a standalone web server
 and as a proxy to reduce the load on back-end HTTP or mail servers.
 .
 This is a dependency package to install either nginx-core (by default),
 nginx-full, nginx-light or nginx-extras.
Description-md5: 902443ddbee17249123a068e7ca7c6d8

Package: nginx
Architecture: amd64
Version: 1.18.0-6ubuntu14.3
Priority: optional
Section: web
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian Nginx Maintainers <pkg-nginx-maintainers@alioth-lists.debian.net>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 49
Depends: nginx-core (<< 1.18.0-6ubuntu14.3.1~) | nginx-full (<< 1.18.0-6ubuntu14.3.1~) | nginx-light (<< 1.18.0-6ubuntu14.3.1~) | nginx-extras (<< 1.18.0-6ubuntu14.3.1~), nginx-core (>= 1.18.0-6ubuntu14.3) | nginx-full (>= 1.18.0-6ubuntu14.3) | nginx-light (>= 1.18.0-6ubuntu14.3) | nginx-extras (>= 1.18.0-6ubuntu14.3)
Breaks: libnginx-mod-http-lua (<< 1.18.0-6ubuntu5)
Filename: pool/main/n/nginx/nginx_1.18.0-6ubuntu14.3_amd64.deb
Size: 3882
MD5sum: e7457ada9637842d2fcedd602668af4f
SHA1: 5be8b5ec8e832ef7b13adbcd8a39bd9cf328d2d4
SHA256: 940f9d64167ae5bd58c088aa85303687c5b0dfa8b4f6590b9be2a3bc3ed58ac5
SHA512: d18450a685ce0b86a41c596af05a751012b61245e4d5a1b4d276a66ca70c731ac09dacb34a7e4dd203292f1c113ea3ff1bff70484546b05122f39f973362071e
Homepage: https://nginx.net
Description-en: small, powerful, scalable web/proxy server
 Nginx ("engine X") is a high-performance web and reverse proxy server
 created by Igor Sysoev. It can be used both as a standalone web server
 and as a proxy to reduce the load on back-end HTTP or mail servers.
 .
 This is a dependency package to install either nginx-core (by default),
 nginx-full, nginx-light or nginx-extras.
Description-md5: 902443ddbee17249123a068e7ca7c6d8

Package: nginx
Architecture: amd64
Version: 1.18.0-6ubuntu14
Priority: optional
Section: web
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian Nginx Maintainers <pkg-nginx-maintainers@alioth-lists.debian.net>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 48
Depends: nginx-core (<< 1.18.0-6ubuntu14.1~) | nginx-full (<< 1.18.0-6ubuntu14.1~) | nginx-light (<< 1.18.0-6ubuntu14.1~) | nginx-extras (<< 1.18.0-6ubuntu14.1~), nginx-core (>= 1.18.0-6ubuntu14) | nginx-full (>= 1.18.0-6ubuntu14) | nginx-light (>= 1.18.0-6ubuntu14) | nginx-extras (>= 1.18.0-6ubuntu14)
Breaks: libnginx-mod-http-lua (<< 1.18.0-6ubuntu5)
Filename: pool/main/n/nginx/nginx_1.18.0-6ubuntu14_amd64.deb
Size: 3884
MD5sum: a87d97c1546b3cdf48fc705efd14a414
SHA1: 64d4cee238fe7ffefc34f211a7a02f85627c9b3e
SHA256: a7408f7ce51117543ccf42dfd21c0065f214250986d162fc053ee790bd6a386e
SHA512: 59425c26fded38ff92440b65f131bfd0f051d1a87082faedabead517516ac492eed810abc9e8ef7fbf5ca10a79f3bbd769346d572deb9411a45bd0bbe4706c8c
Homepage: https://nginx.net
Description-en: small, powerful, scalable web/proxy server
 Nginx ("engine X") is a high-performance web and reverse proxy server
 created by Igor Sysoev. It can be used both as a standalone web server
 and as a proxy to reduce the load on back-end HTTP or mail servers.
 .
 This is a dependency package to install either nginx-core (by default),
 nginx-full, nginx-light or nginx-extras.
Description-md5: 902443ddbee17249123a068e7ca7c6d8

查看查询结果找到版本号:

Version: 1.18.0-6ubuntu14.4
Version: 1.18.0-6ubuntu14.3
Version: 1.18.0-6ubuntu14

安装指定版本:

# 等于号后面给出来指定版本号即可
sudo apt install nginx=1.18.0-6ubuntu14.4

命令执行结果:
在这里插入图片描述

3. Nginx验证

使用curl命令访问服务:

# 使用curl命令访问服务
curl http://127.0.0.1

如果找不到命令,如下图所示:
在这里插入图片描述

先安装一下curl命令:

sudo apt install curl

然后再验证服务:

# 使用curl命令访问服务
curl http://127.0.0.1

出现以下内容说明服务安装成功:

ubuntu@ubuntu:~$ curl http://127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

4. Nginx服务控制

4.1 查看服务状态

systemctl status nginx

如下图所示,表示服务正在运行:

在这里插入图片描述

4.2 停止服务

sudo systemctl stop nginx

可看服务状态,下图表示服务停止成功:

在这里插入图片描述

4.3 启动服务

sudo systemctl start nginx

可看服务状态,下图表示服务启动成功:

在这里插入图片描述

4.4 重启服务

sudo systemctl restart nginx

可看服务状态,下图表示服务重启成功:

在这里插入图片描述

5. Nginx文件存放目录

路径描述
/var/log/nginxnginx运行日志的目录
/var/www/htmlweb项目目录
/usr/sbin/nginx服务文件
/etc/nginx配置文件目录

二、自己编译Nginx

1. 下载源码

下载页面:https://nginx.org/en/download.html
下载地址:https://nginx.org/download/nginx-1.24.0.tar.gz

curl -O https://nginx.org/download/nginx-1.24.0.tar.gz

-O 为大写字母

2. 依赖配置

sudo apt install gcc make libpcre3-dev zlib1g-dev openssl libssl-dev

3. 编译

解压源代码:

tar -zxvf nginx-1.24.0.tar.gz

生成编译脚本:

cd nginx-1.24.0/ 
./configure --prefix=/usr/local/nginx --with-http_ssl_module

编译:

make

编译完成:
在这里插入图片描述

安装:

make install

查看验证:

ls /usr/local/nginx
ls /usr/local/nginx/sbin/
sudo /usr/local/nginx/sbin/nginx -t

在这里插入图片描述

三、Nginx卸载

执行卸载命令:

sudo apt purge nginx

执行结果:
在这里插入图片描述
卸载完以后/var/log/nginx、/var/www/html、/etc/nginx这3个目录并不会删除,如果需要删除可以执行删除命令:

sudo rm -rf /var/log/nginx /var/www/html /etc/nginx

注意:删除文件要再三确认!

可以再删除依赖项:

sudo apt autoremove

总结

✍结尾 ❤️ 感谢您的支持和鼓励关注不迷路✍

  • 63
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五轮车

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值