一、nginx 下载
从官网中下载 nginx 压缩包到本地(http://nginx.org/en/download.html)
二、部署步骤
- 在 /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
- 将 nginx 压缩包使用 sftp 上传到 linux 系统的 /usr/local/nginx 目录下
[root@izwz9cwntagbp2m20emj0qz nginx]# ls nginx-1.22.1.tar.gz
- 解压 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
- 进入 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
- 运行 configure,命令 ./configure
[root@izwz9cwntagbp2m20emj0qz nginx-1.22.1]# ./configure
①如果报错执行yum -y install pcre-devel // 安装pore
②如果再次报错执行 yum -y install openssl openssl-devel //安装openssl
- 编译 nginx 文件
[root@izwz9cwntagbp2m20emj0qz nginx-1.22.1]# make [root@izwz9cwntagbp2m20emj0qz nginx-1.22.1]# make install
- 检查是否安装成功
[root@izwz9cwntagbp2m20emj0qz nginx-1.22.1]# whereis nginx nginx: /usr/local/nginx
- 找到 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/
- 启动 nginx
[root@izwz9cwntagbp2m20emj0qz sbin]# ./nginx
- 访问测试
三、演示修改 nginx 配置,修改端口号
- 找到 /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
- 使用 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
- 测试端口修改是否成功
四、使用 nginx 转发访问后端服务
- 启动 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
在浏览器中测试访问接口
在 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/; } }
- 重新加载 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
- 浏览器中测试(使用 nginx 监听的 80 端口访问)
五、nginx 常用命令
- 查看 nginx 位置
[root@izwz9cwntagbp2m20emj0qz /]# whereis nginx nginx: /usr/local/nginx
- 启动 nginx
[root@izwz9cwntagbp2m20emj0qz /]# cd usr/local/nginx/sbin/ [root@izwz9cwntagbp2m20emj0qz sbin]# ./nginx
- 停止 nginx
[root@izwz9cwntagbp2m20emj0qz /]# cd usr/local/nginx/sbin/ [root@izwz9cwntagbp2m20emj0qz sbin]# ./nginx -s stop
- 安全退出 nginx
[root@izwz9cwntagbp2m20emj0qz /]# cd usr/local/nginx/sbin/ [root@izwz9cwntagbp2m20emj0qz sbin]# ./nginx -s quit
- 重新加载配置文件
[root@izwz9cwntagbp2m20emj0qz /]# cd usr/local/nginx/sbin/ [root@izwz9cwntagbp2m20emj0qz sbin]# ./nginx -s reload
(注意:nginx 启动、停止、退出、重新加载命令都需要在 sbin 目录下执行)
- 查看 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的文章就介绍到这了,更多相关linux部署nginx内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论