Linux 部署 Nginx

本文详细介绍了如何下载Nginx并在Linux上进行部署,包括修改配置文件以改变监听端口号,以及如何使用Nginx转发请求到后端服务。此外,还列举了几个常用的Nginx管理命令。


一、Nginx 下载

从官网中下载 nginx 压缩包到本地(http://nginx.org/en/download.html)
在这里插入图片描述

二、部署步骤

  1. 在 /usr/local 目录下新建 nginx 文件夹
[root@iZwz9cwntagbp2m20emj0qZ local]# mkdir nginx                                                                    
[root@iZwz9cwntagbp2m20emj0qZ local]# ls                                                                             
aegis  app  bin  etc  games  include  lib  lib64  libexec  nginx  sbin  share  src
  1. 将 nginx 压缩包使用 SFTP 上传到 Linux 系统的 /usr/local/nginx 目录下
[root@iZwz9cwntagbp2m20emj0qZ nginx]# ls                                                                             
nginx-1.22.1.tar.gz  
  1. 解压 nginx-1.22.1.tar.gz
[root@iZwz9cwntagbp2m20emj0qZ nginx]# tar -zxvf nginx-1.22.1.tar.gz                                                  
nginx-1.22.1/                                                                                                        
nginx-1.22.1/auto/ 
......
[root@iZwz9cwntagbp2m20emj0qZ nginx]# ls                                                                             
nginx-1.22.1  nginx-1.22.1.tar.gz
  1. 进入 nginx,找到 configure
[root@iZwz9cwntagbp2m20emj0qZ /]# cd usr/local/nginx/nginx-1.22.1/                                                                        
[root@iZwz9cwntagbp2m20emj0qZ nginx-1.22.1]# ls                                                                                           
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src 
  1. 运行 configure,命令 ./configure
[root@iZwz9cwntagbp2m20emj0qZ nginx-1.22.1]# ./configure

①如果报错执行yum -y install pcre-devel // 安装pore
②如果再次报错执行 yum -y install openssl openssl-devel //安装openssl

  1. 编译 nginx 文件
[root@iZwz9cwntagbp2m20emj0qZ nginx-1.22.1]# make
[root@iZwz9cwntagbp2m20emj0qZ nginx-1.22.1]# make install
  1. 检查是否安装成功
[root@iZwz9cwntagbp2m20emj0qZ nginx-1.22.1]# whereis nginx                                                                                
nginx: /usr/local/nginx
  1. 找到 ngingx 的 sbin 目录
[root@iZwz9cwntagbp2m20emj0qZ /]# cd usr/local/nginx/                                                                                     
[root@iZwz9cwntagbp2m20emj0qZ nginx]# ll                                                                                                  
total 1092                                                                                                                                
drwx------ 2 nobody root    4096 Mar 14 11:39 client_body_temp                                                                            
drwxr-xr-x 2 root   root    4096 Mar 14 14:31 conf                                                                                        
drwx------ 2 nobody root    4096 Mar 14 11:39 fastcgi_temp                                                                                
drwxr-xr-x 2 root   root    4096 Mar 14 11:37 html                                                                                        
drwxr-xr-x 2 root   root    4096 Mar 14 14:37 logs                                                                                        
drwxr-xr-x 9   1001 1001    4096 Mar 14 11:33 nginx-1.22.1                                                                                
-rw-r--r-- 1 root   root 1073948 Mar 14 11:19 nginx-1.22.1.tar.gz                                                                         
drwx------ 2 nobody root    4096 Mar 14 11:39 proxy_temp                                                                                  
drwxr-xr-x 2 root   root    4096 Mar 14 11:37 sbin                                                                                        
drwx------ 2 nobody root    4096 Mar 14 11:39 scgi_temp                                                                                   
drwx------ 2 nobody root    4096 Mar 14 11:39 uwsgi_temp
[root@iZwz9cwntagbp2m20emj0qZ nginx]# cd sbin/
  1. 启动 nginx
[root@iZwz9cwntagbp2m20emj0qZ sbin]# ./nginx
  1. 访问测试
    在这里插入图片描述

三、演示修改 Nginx 配置,修改端口号

  1. 找到 /conf 目录下的 nginx.conf 配置文件
[root@iZwz9cwntagbp2m20emj0qZ /]# cd usr/local/nginx/nginx-1.22.1/conf/                                                                   
[root@iZwz9cwntagbp2m20emj0qZ conf]# ll                                                                                                   
total 40                                                                                                                                  
-rw-r--r-- 1 1001 1001 1077 Oct 19 16:02 fastcgi.conf                                                                                     
-rw-r--r-- 1 1001 1001 1007 Oct 19 16:02 fastcgi_params                                                                                   
-rw-r--r-- 1 1001 1001 2837 Oct 19 16:02 koi-utf                                                                                          
-rw-r--r-- 1 1001 1001 2223 Oct 19 16:02 koi-win                                                                                          
-rw-r--r-- 1 1001 1001 5349 Oct 19 16:02 mime.types                                                                                       
-rw-r--r-- 1 1001 1001 2656 Oct 19 16:02 nginx.conf                                                                                       
-rw-r--r-- 1 1001 1001  636 Oct 19 16:02 scgi_params                                                                                      
-rw-r--r-- 1 1001 1001  664 Oct 19 16:02 uwsgi_params                                                                                     
-rw-r--r-- 1 1001 1001 3610 Oct 19 16:02 win-utf 
  1. 使用 vim 编辑 nginx.conf 文件
[root@iZwz9cwntagbp2m20emj0qZ conf]# vim nginx.conf

修改监听端口为666

server {                                                                                                                              
        listen       666;                                                                                                                  
        server_name  localhost;
}        

保存并退出:esc -> shift + : -> wq
3. 重新加载 nginx.conf 配置文件

[root@iZwz9cwntagbp2m20emj0qZ /]# cd usr/local/nginx/sbin/                                                                                
[root@iZwz9cwntagbp2m20emj0qZ sbin]# ll                                                                                                   
total 3804                                                                                                                                
-rwxr-xr-x 1 root root 3892016 Mar 14 11:37 nginx                                                                                         
[root@iZwz9cwntagbp2m20emj0qZ sbin]# ./nginx -s reload
  1. 测试端口修改是否成功
    在这里插入图片描述

四、使用 Nginx 转发访问后端服务

  1. 启动 spring boot 项目
[root@iZwz9cwntagbp2m20emj0qZ /]# cd usr/local/app/                                                                                                                                                                                        
[root@iZwz9cwntagbp2m20emj0qZ app]# ll                                                                                                                                                                                                     
total 16160                                                                                                                                                                                                                                
-rw-r--r-- 1 root root 16546230 Mar 14 14:23 springboot-0.0.1-SNAPSHOT.jar                                                                                                                                                                 
[root@iZwz9cwntagbp2m20emj0qZ app]# java -jar springboot-0.0.1-SNAPSHOT.jar                                                                                                                                                                
                                                                                                                                                                                                                                           
  .   ____          _            __ _ _                                                                                                                                                                                                    
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \                                                                                                                                                                                                   
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \                                                                                                                                                                                                  
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )                                                                                                                                                                                                 
  '  |____| .__|_| |_|_| |_\__, | / / / /                                                                                                                                                                                                  
 =========|_|==============|___/=/_/_/_/                                                                                                                                                                                                   
 :: Spring Boot ::        (v2.3.6.RELEASE)                                                                                                                                                                                                 
                                                                                                                                                                                                                                           
2023-03-14 20:05:22.440  INFO 30885 --- [           main] c.json.springboot.SpringbootApplication  : Starting SpringbootApplication v0.0.1-SNAPSHOT on iZwz9cwntagbp2m20emj0qZ with PID 30885 (/usr/local/app/springboot-0.0.1-SNAPSHOT.jar
 started by root in /usr/local/app)                                                                                                                                                                                                        
2023-03-14 20:05:22.454  INFO 30885 --- [           main] c.json.springboot.SpringbootApplication  : No active profile set, falling back to default profiles: default                                                                      
2023-03-14 20:05:24.718  INFO 30885 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 666 (http)                                                                                           
2023-03-14 20:05:24.747  INFO 30885 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]                                                                                                             
2023-03-14 20:05:24.748  INFO 30885 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.39]                                                                                       
2023-03-14 20:05:24.891  INFO 30885 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/json]   : Initializing Spring embedded WebApplicationContext                                                                                    
2023-03-14 20:05:24.892  INFO 30885 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2320 ms                                                                       
2023-03-14 20:05:25.793  INFO 30885 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'                                                                                
2023-03-14 20:05:26.188  INFO 30885 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 666 (http) with context path '/json'                                                                       
2023-03-14 20:05:26.219  INFO 30885 --- [           main] c.json.springboot.SpringbootApplication  : Started SpringbootApplication in 4.683 seconds (JVM running for 5.522)                                                                
2023-03-14 20:06:06.170  INFO 30885 --- [-nio-666-exec-4] o.a.c.c.C.[Tomcat].[localhost].[/json]   : Initializing Spring DispatcherServlet 'dispatcherServlet'                                                                             
2023-03-14 20:06:06.170  INFO 30885 --- [-nio-666-exec-4] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'                                                                                              
2023-03-14 20:06:06.187  INFO 30885 --- [-nio-666-exec-4] o.s.web.servlet.DispatcherServlet        : Completed initialization in 17 ms
  1. 在浏览器中测试访问接口
    在这里插入图片描述

  2. 在 nginx.conf 文件中配置 location

[root@iZwz9cwntagbp2m20emj0qZ ~]# cd /                                                                                                                                                                                                     
[root@iZwz9cwntagbp2m20emj0qZ /]# cd usr/local/nginx/nginx-1.22.1/conf/                                                                                                                                                                    
[root@iZwz9cwntagbp2m20emj0qZ conf]# vim nginx.conf

server {                                                                                                                                                                                                                               
    listen       80;                                                                                                                                                                                                                   
    server_name  localhost;                                                                                                                                                                                                                                                                                                                                                                                                                                             
    
    location /json/ {                                                                                                                                                                                                                  
        proxy_pass http://112.74.190.252:666/json/;                                                                                                                                                                                    
    }
}
  1. 重新加载 nginx.conf 配置文件
[root@iZwz9cwntagbp2m20emj0qZ /]# cd usr/local/nginx/sbin/                                                                                                                                                                                 
[root@iZwz9cwntagbp2m20emj0qZ sbin]# ll                                                                                                                                                                                                    
total 3804                                                                                                                                                                                                                                 
-rwxr-xr-x 1 root root 3892016 Mar 14 11:37 nginx                                                                                                                                                                                          
[root@iZwz9cwntagbp2m20emj0qZ sbin]# ./nginx -s reload
  1. 浏览器中测试(使用 Nginx 监听的 80 端口访问)
    在这里插入图片描述

五、Nginx 常用命令

  1. 查看 nginx 位置
[root@iZwz9cwntagbp2m20emj0qZ /]# whereis nginx                                                                                           
nginx: /usr/local/nginx
  1. 启动 nginx
[root@iZwz9cwntagbp2m20emj0qZ /]# cd usr/local/nginx/sbin/                                                                                
[root@iZwz9cwntagbp2m20emj0qZ sbin]# ./nginx
  1. 停止 nginx
[root@iZwz9cwntagbp2m20emj0qZ /]# cd usr/local/nginx/sbin/                                                                                
[root@iZwz9cwntagbp2m20emj0qZ sbin]# ./nginx -s stop
  1. 安全退出 nginx
[root@iZwz9cwntagbp2m20emj0qZ /]# cd usr/local/nginx/sbin/                                                                                
[root@iZwz9cwntagbp2m20emj0qZ sbin]# ./nginx -s quit  
  1. 重新加载配置文件
[root@iZwz9cwntagbp2m20emj0qZ /]# cd usr/local/nginx/sbin/                                                                                
[root@iZwz9cwntagbp2m20emj0qZ sbin]# ./nginx -s reload 

(注意:Nginx 启动、停止、退出、重新加载命令都需要在 sbin 目录下执行)

  1. 查看 nginx 进程
[root@iZwz9cwntagbp2m20emj0qZ /]# cd usr/local/nginx/sbin/                                                                                
[root@iZwz9cwntagbp2m20emj0qZ sbin]# ./nginx                                                                                              
[root@iZwz9cwntagbp2m20emj0qZ sbin]# ps aux|grep nginx                                                                                    
root     30537  0.0  0.0  20576   624 ?        Ss   16:41   0:00 nginx: master process ./nginx                                            
nobody   30538  0.0  0.0  21020  1320 ?        S    16:41   0:00 nginx: worker process                                                    
root     30542  0.0  0.0 112812   980 pts/0    S+   16:41   0:00 grep --color=auto nginx
### 如何在Linux系统中安装和配置Nginx #### 安装 Nginx 要在 Linux 上安装 Nginx,可以使用包管理器完成此操作。对于基于 Debian 的发行版(如 Ubuntu),可以通过 `apt` 命令来实现;而对于基于 Red Hat 的发行版(如 CentOS 或 RHEL),则可使用 `yum` 或 `dnf`。 以下是具体命令: ```bash sudo apt update && sudo apt install nginx -y # 对于Debian/Ubuntu系统[^1] sudo yum install epel-release -y && sudo yum install nginx -y # 对于CentOS/RHEL系统[^2] ``` 启动并启用服务以便开机自启: ```bash sudo systemctl start nginx sudo systemctl enable nginx ``` 验证 Nginx 是否正常运行,可通过浏览器访问服务器 IP 地址或者执行以下命令查看状态: ```bash curl http://localhost/ sudo systemctl status nginx ``` #### 配置 Nginx 默认情况下,Nginx 的主要配置文件位于 `/etc/nginx/nginx.conf`,而站点特定的配置通常存储在 `/etc/nginx/sites-available/` 和 `/etc/nginx/sites-enabled/` 中(适用于某些版本)。修改这些文件即可调整行为。 一个简单的虚拟主机配置如下所示: ```nginx server { listen 80; server_name example.com; location / { root /var/www/html; # 设置网站根目录路径 index index.html index.htm; } error_page 404 /404.html; # 自定义错误页面处理方式 } ``` 保存更改后需测试语法有效性以及重新加载配置才能生效: ```bash sudo nginx -t # 测试配置文件是否有误 sudo systemctl reload nginx # 应用新的配置而不中断现有连接 ``` #### SSL 证书设置 (HTTPS 支持) 为了增强安全性,建议为 Web 服务启用 HTTPS 协议。这涉及获取并应用有效的 TLS/SSL 证书到您的域上。Let’s Encrypt 提供免费选项,通过 Certbot 工具简化流程。 安装 Certbot 及其插件用于自动化过程: ```bash sudo apt install certbot python3-certbot-nginx -y # Debian/Ubuntu平台 sudo yum install certbot python3-certbot-nginx -y # CentOS/RHEL平台 ``` 接着按照提示运行下面这条指令以自动更新现有的 HTTP 到 HTTPS 跳转规则加上必要的字段认证机制。 ```bash sudo certbot --nginx -d example.com -d www.example.com ``` 最后记得定期续订即将过期的凭证以免影响在线业务连续性! ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值