Nginx(发音为“engine-x”)是一个开源的Web服务器,它也同时支持作为反向代理、负载均衡器、邮件代理和HTTP缓存。Nginx以其高性能和高可扩展性而闻名,被广泛用于处理高流量的网站。以下是Nginx的一些主要特点和用途:
特点:
- 高性能:Nginx采用事件驱动架构,能够处理大量的并发连接。 高可扩展性:Nginx可以通过增加更多的worker进程来处理更多的请求。
- 模块化设计:Nginx由多个独立的可加载模块组成,这意味着你可以根据需要启用或禁用特定功能。
- 非阻塞I/O:Nginx使用异步非阻塞I/O,提高了处理性能。 热部署:Nginx支持热部署,可以在不停止服务的情况下重新加载配置文件。
- 低内存消耗:与Apache等传统的Web服务器相比,Nginx在处理相同数量请求时通常使用更少的内存。
用途:
- Web服务器:Nginx可以作为静态文件服务器,处理HTML、CSS、JavaScript等静态文件。
- 反向代理:Nginx可以将客户端请求转发到后端服务器,实现负载均衡和提高网站可用性。
- 负载均衡器:Nginx可以分发客户端请求到多个后端服务器,根据不同的策略(如轮询、IP哈希、URL哈希等)来决定请求应该转发到哪个服务器。
- 邮件代理:Nginx可以作为邮件服务器的前端,接收邮件并将其转发到后端邮件服务器。
- HTTP缓存:Nginx可以缓存静态文件,减少对后端服务器的请求,提高网站响应速度。
- 动态内容处理:虽然Nginx主要处理静态内容,但它也可以通过支持如FastCGI、SCGI、uwsgi等协议来处理动态内容。
- Nginx的灵活性和高性能使其成为现代Web架构中不可或缺的一部分。它的配置文件基于文本,易于理解和修改,同时也有丰富的第三方模块和插件可供使用,以扩展其功能。
安装nginx按照步骤进行操作。假设你正在使用一个基于Linux的系统,如CentOS。你需要有root权限或者能够使用sudo来执行安装。以下是一个基本的安装过程:
一、安装依赖项
在编译安装Nginx之前,你需要安装一些必要的依赖项
[root@hcss-ecs-c0a1 nginx]# yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
Last metadata expiration check: 0:23:24 ago on Sun 23 Jun 2024 03:05:27 PM CST.
Package pcre-8.42-6.el8.x86_64 is already installed.
Package zlib-1.2.11-17.el8.x86_64 is already installed.
Package openssl-1:1.1.1k-5.el8_5.x86_64 is already installed.
Dependencies resolved.
=============================================================================================================================================================================================
Package Architecture Version Repository Size
=============================================================================================================================================================================================
Installing:
gcc-c++ x86_64 8.5.0-4.el8_5 AppStream 12 M
openssl-devel x86_64 1:1.1.1k-5.el8_5 BaseOS 2.3 M
pcre-devel x86_64 8.42-6.el8 BaseOS 551 k
zlib-devel x86_64 1.2.11-17.el8 BaseOS 58 k
Installing dependencies:
keyutils-libs-devel x86_64 1.5.10-9.el8 BaseOS 48 k
krb5-devel x86_64 1.18.2-14.el8 BaseOS 560 k
libcom_err-devel x86_64 1.45.6-2.el8 BaseOS 38 k
libkadm5 x86_64 1.18.2-14.el8 BaseOS 187 k
libselinux-devel x86_64 2.9-5.el8 BaseOS 200 k
libsepol-devel x86_64 2.9-3.el8 BaseOS 87 k
libstdc++-devel x86_64 8.5.0-4.el8_5 AppStream 2.0 M
libverto-devel x86_64 0.3.0-5.el8 BaseOS 18 k
pcre-cpp x86_64 8.42-6.el8 BaseOS 47 k
pcre-utf16 x86_64 8.42-6.el8 BaseOS 195 k
pcre-utf32 x86_64 8.42-6.el8 BaseOS 186 k
pcre2-devel x86_64 10.32-2.el8 BaseOS 605 k
pcre2-utf16 x86_64 10.32-2.el8 BaseOS 229 k
pcre2-utf32 x86_64 10.32-2.el8 BaseOS 220 k
Transaction Summary
=============================================================================================================================================================================================
Install 18 Packages
Total download size: 20 M
Installed size: 53 M
Downloading Packages:
(1/18): libcom_err-devel-1.45.6-2.el8.x86_64.rpm 1.2 MB/s | 38 kB 00:00
(2/18): keyutils-libs-devel-1.5.10-9.el8.x86_64.rpm 1.4 MB/s | 48 kB 00:00
(3/18): libkadm5-1.18.2-14.el8.x86_64.rpm 5.2 MB/s | 187 kB 00:00
(4/18): krb5-devel-1.18.2-14.el8.x86_64.rpm 7.2 MB/s | 560 kB 00:00
(5/18): libsepol-devel-2.9-3.el8.x86_64.rpm 4.9 MB/s | 87 kB 00:00
(6/18): libverto-devel-0.3.0-5.el8.x86_64.rpm 846 kB/s | 18 kB 00:00
(7/18): pcre-cpp-8.42-6.el8.x86_64.rpm 2.2 MB/s | 47 kB 00:00
(8/18): libselinux-devel-2.9-5.el8.x86_64.rpm 1.8 MB/s | 200 kB 00:00
(9/18): openssl-devel-1.1.1k-5.el8_5.x86_64.rpm 31 MB/s | 2.3 MB 00:00
(10/18): pcre-devel-8.42-6.el8.x86_64.rpm 12 MB/s | 551 kB 00:00
(11/18): pcre-utf32-8.42-6.el8.x86_64.rpm 8.6 MB/s | 186 kB 00:00
(12/18): pcre-utf16-8.42-6.el8.x86_64.rpm 3.4 MB/s | 195 kB 00:00
(13/18): pcre2-utf16-10.32-2.el8.x86_64.rpm 7.8 MB/s | 229 kB 00:00
(14/18): pcre2-devel-10.32-2.el8.x86_64.rpm 9.8 MB/s | 605 kB 00:00
(15/18): pcre2-utf32-10.32-2.el8.x86_64.rpm 8.3 MB/s | 220 kB 00:00
(16/18): zlib-devel-1.2.11-17.el8.x86_64.rpm 2.6 MB/s | 58 kB 00:00
(17/18): gcc-c++-8.5.0-4.el8_5.x86_64.rpm 42 MB/s | 12 MB 00:00
(18/18): libstdc++-devel-8.5.0-4.el8_5.x86_64.rpm 3.8 MB/s | 2.0 MB 00:00
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 26 MB/s | 20 MB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : libstdc++-devel-8.5.0-4.el8_5.x86_64 1/18
Installing : zlib-devel-1.2.11-17.el8.x86_64 2/18
Installing : pcre2-utf32-10.32-2.el8.x86_64 3/18
Installing : pcre2-utf16-10.32-2.el8.x86_64 4/18
Installing : pcre2-devel-10.32-2.el8.x86_64 5/18
Installing : pcre-utf32-8.42-6.el8.x86_64 6/18
Installing : pcre-utf16-8.42-6.el8.x86_64 7/18
Installing : pcre-cpp-8.42-6.el8.x86_64 8/18
Installing : libverto-devel-0.3.0-5.el8.x86_64 9/18
Installing : libsepol-devel-2.9-3.el8.x86_64 10/18
Installing : libselinux-devel-2.9-5.el8.x86_64 11/18
Installing : libkadm5-1.18.2-14.el8.x86_64 12/18
Installing : libcom_err-devel-1.45.6-2.el8.x86_64 13/18
Installing : keyutils-libs-devel-1.5.10-9.el8.x86_64 14/18
Installing : krb5-devel-1.18.2-14.el8.x86_64 15/18
Installing : openssl-devel-1:1.1.1k-5.el8_5.x86_64 16/18
Installing : pcre-devel-8.42-6.el8.x86_64 17/18
Installing : gcc-c++-8.5.0-4.el8_5.x86_64 18/18
Running scriptlet: gcc-c++-8.5.0-4.el8_5.x86_64 18/18
Verifying : keyutils-libs-devel-1.5.10-9.el8.x86_64 1/18
Verifying : krb5-devel-1.18.2-14.el8.x86_64 2/18
Verifying : libcom_err-devel-1.45.6-2.el8.x86_64 3/18
Verifying : libkadm5-1.18.2-14.el8.x86_64 4/18
Verifying : libselinux-devel-2.9-5.el8.x86_64 5/18
Verifying : libsepol-devel-2.9-3.el8.x86_64 6/18
Verifying : libverto-devel-0.3.0-5.el8.x86_64 7/18
Verifying : openssl-devel-1:1.1.1k-5.el8_5.x86_64 8/18
Verifying : pcre-cpp-8.42-6.el8.x86_64 9/18
Verifying : pcre-devel-8.42-6.el8.x86_64 10/18
Verifying : pcre-utf16-8.42-6.el8.x86_64 11/18
Verifying : pcre-utf32-8.42-6.el8.x86_64 12/18
Verifying : pcre2-devel-10.32-2.el8.x86_64 13/18
Verifying : pcre2-utf16-10.32-2.el8.x86_64 14/18
Verifying : pcre2-utf32-10.32-2.el8.x86_64 15/18
Verifying : zlib-devel-1.2.11-17.el8.x86_64 16/18
Verifying : gcc-c++-8.5.0-4.el8_5.x86_64 17/18
Verifying : libstdc++-devel-8.5.0-4.el8_5.x86_64 18/18
Installed:
gcc-c++-8.5.0-4.el8_5.x86_64 keyutils-libs-devel-1.5.10-9.el8.x86_64 krb5-devel-1.18.2-14.el8.x86_64 libcom_err-devel-1.45.6-2.el8.x86_64 libkadm5-1.18.2-14.el8.x86_64
libselinux-devel-2.9-5.el8.x86_64 libsepol-devel-2.9-3.el8.x86_64 libstdc++-devel-8.5.0-4.el8_5.x86_64 libverto-devel-0.3.0-5.el8.x86_64 openssl-devel-1:1.1.1k-5.el8_5.x86_64
pcre-cpp-8.42-6.el8.x86_64 pcre-devel-8.42-6.el8.x86_64 pcre-utf16-8.42-6.el8.x86_64 pcre-utf32-8.42-6.el8.x86_64 pcre2-devel-10.32-2.el8.x86_64
pcre2-utf16-10.32-2.el8.x86_64 pcre2-utf32-10.32-2.el8.x86_64 zlib-devel-1.2.11-17.el8.x86_64
Complete!
[root@hcss-ecs-c0a1 nginx]#
这里可能会出现yum源不生效或者比较慢的情况,我之前写过一篇切换国产yum源的小博客解决
二、下载Nginx源代码
从Nginx官网下载
[root@hcss-ecs-c0a1 home]# wget http://nginx.org/download/nginx-1.26.0.tar.gz
三、解压源代码
[root@hcss-ecs-c0a1 home]# tar -zxvf nginx-1.26.0.tar.gz
[root@hcss-ecs-c0a1 home]# mv nginx-1.26.0 nginx
[root@hcss-ecs-c0a1 home]# ll
total 820448
drwxr-xr-x 8 502 games 4096 Apr 23 22:58 nginx
-rw-r--r-- 1 root root 1244118 Jun 22 19:46 nginx-1.26.0.tar.gz
[root@hcss-ecs-c0a1 home]# cd nginx
[root@hcss-ecs-c0a1 nginx]# ll
total 848
drwxr-xr-x 6 502 games 4096 Jun 23 14:12 auto
-rw-r--r-- 1 502 games 326838 Apr 23 22:58 CHANGES
-rw-r--r-- 1 502 games 499951 Apr 23 22:58 CHANGES.ru
drwxr-xr-x 2 502 games 4096 Jun 23 14:12 conf
-rwxr-xr-x 1 502 games 2611 Apr 23 22:04 configure
drwxr-xr-x 4 502 games 4096 Jun 23 14:12 contrib
drwxr-xr-x 2 502 games 4096 Jun 23 14:12 html
-rw-r--r-- 1 502 games 1397 Apr 23 22:04 LICENSE
drwxr-xr-x 2 502 games 4096 Jun 23 14:12 man
-rw-r--r-- 1 502 games 49 Apr 23 22:04 README
drwxr-xr-x 9 502 games 4096 Apr 23 22:58 src
四、配置、编译和安装
[root@hcss-ecs-c0a1 nginx]# ./configure
[root@hcss-ecs-c0a1 nginx]# make
[root@hcss-ecs-c0a1 nginx]# make install
五、启动Nginx服务
/usr/local/nginx/sbin/nginx
或者
编写脚本
vim /etc/init.d/nginx
脚本如下:
#!/bin/bash
# 此文件由 'systemd' 管理。不要编辑。
#
# 此文件应从 /lib/systemd/system/nginx.service.d/*.conf 和 /etc/systemd/system/nginx.service.d/*.conf 引用。
#
# 有关更多信息,请参见 http://www.freedesktop.org/wiki/Software/systemd/WritingAservice
[Unit]
# 描述:Nginx 是一个高性能的 Web 服务器,也可以作为反向代理、负载均衡器、邮件代理和 HTTP 缓存使用。
Description=Nginx - 高性能的 Web 服务器
# After=network.target 指定服务应在网络启动后启动。
After=network.target
[Service]
# Type=forking 指定服务应在后台以独立进程组运行。
Type=forking
# PIDFile=/usr/local/nginx/logs/nginx.pid 指定 PID 文件路径。
PIDFile=/usr/local/nginx/logs/nginx.pid
# ExecStartPre=/usr/local/nginx/sbin/nginx -t 在启动服务前运行测试配置脚本。
ExecStartPre=/usr/local/nginx/sbin/nginx -t
# ExecStart=/usr/local/nginx/sbin/nginx -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 指定重新加载配置的命令。
ExecReload=/usr/local/nginx/sbin/nginx -s reload
# ExecStop=/usr/local/nginx/sbin/nginx -s stop 指定停止服务的命令。
ExecStop=/usr/local/nginx/sbin/nginx -s stop
# PrivateTmp=true 指定服务应使用私有临时目录。
PrivateTmp=true
[Install]
# WantedBy=multi-user.target 指定在多用户模式下服务应默认启用。
WantedBy=multi-user.target
相关命令
[root@hcss-ecs-c0a1 redis]# systemctl start nginx
[root@hcss-ecs-c0a1 redis]# systemctl status nginx
[root@hcss-ecs-c0a1 redis]# systemctl restart nginx
执行如下:
[root@hcss-ecs-c0a1 redis]# systemctl start nginx
[root@hcss-ecs-c0a1 redis]# systemctl status nginx
● nginx.service - Nginx - high performance web server
Loaded: loaded (/etc/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2024-06-23 17:45:05 CST; 6s ago
Process: 19188 ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
Process: 19186 ExecStartPre=/usr/local/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS)
Main PID: 19189 (nginx)
Tasks: 2 (limit: 23712)
Memory: 1.4M
CGroup: /system.slice/nginx.service
├─19189 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
└─19191 nginx: worker process
Jun 23 17:45:05 hcss-ecs-c0a1 systemd[1]: Starting Nginx - high performance web server...
Jun 23 17:45:05 hcss-ecs-c0a1 nginx[19186]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
Jun 23 17:45:05 hcss-ecs-c0a1 nginx[19186]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Jun 23 17:45:05 hcss-ecs-c0a1 systemd[1]: nginx.service: Failed to parse PID from file /usr/local/nginx/logs/nginx.pid: Invalid argument
Jun 23 17:45:05 hcss-ecs-c0a1 systemd[1]: Started Nginx - high performance web server.
[root@hcss-ecs-c0a1 redis]# systemctl restart nginx
[root@hcss-ecs-c0a1 redis]# systemctl status nginx
● nginx.service - Nginx - high performance web server
Loaded: loaded (/etc/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2024-06-23 17:45:30 CST; 2s ago
Process: 19196 ExecStop=/usr/local/nginx/sbin/nginx -s stop (code=exited, status=0/SUCCESS)
Process: 19202 ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
Process: 19200 ExecStartPre=/usr/local/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS)
Main PID: 19204 (nginx)
Tasks: 2 (limit: 23712)
Memory: 1.4M
CGroup: /system.slice/nginx.service
├─19204 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
└─19205 nginx: worker process
Jun 23 17:45:30 hcss-ecs-c0a1 systemd[1]: nginx.service: Succeeded.
Jun 23 17:45:30 hcss-ecs-c0a1 systemd[1]: Stopped Nginx - high performance web server.
Jun 23 17:45:30 hcss-ecs-c0a1 systemd[1]: Starting Nginx - high performance web server...
Jun 23 17:45:30 hcss-ecs-c0a1 nginx[19200]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
Jun 23 17:45:30 hcss-ecs-c0a1 nginx[19200]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Jun 23 17:45:30 hcss-ecs-c0a1 systemd[1]: nginx.service: Failed to parse PID from file /usr/local/nginx/logs/nginx.pid: Invalid argument
Jun 23 17:45:30 hcss-ecs-c0a1 systemd[1]: Started Nginx - high performance web server.
[root@hcss-ecs-c0a1 redis]#
提示 |
---|
本人以抱着学习的态度去分享,以上内容如有雷同,不胜荣幸!如有不足,欢迎评论留言! |