Linux部署Nginx詳細(xì)圖文教程
一、Nginx 下載
從官網(wǎng)中下載 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 系統(tǒng)的 /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
- 進(jìn)入 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
- 運(yùn)行 configure,命令 ./configure
[root@iZwz9cwntagbp2m20emj0qZ nginx-1.22.1]# ./configure
①如果報錯執(zhí)行yum -y install pcre-devel // 安裝pore
②如果再次報錯執(zhí)行 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
修改監(jiān)聽端口為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 轉(zhuǎn)發(fā)訪問后端服務(wù)
- 啟動 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 監(jiān)聽的 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 目錄下執(zhí)行)
- 查看 nginx 進(jìn)程
[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
總結(jié)
到此這篇關(guān)于Linux部署Nginx的文章就介紹到這了,更多相關(guān)Linux部署Nginx內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx?502?Bad?Gateway錯誤解決詳細(xì)指南與實例
這篇文章主要介紹了Nginx?502?Bad?Gateway錯誤解決的相關(guān)資料,502BadGateway錯誤是Web開發(fā)和運(yùn)維中常見的錯誤,表示一個服務(wù)器在充當(dāng)網(wǎng)關(guān)或代理時,從上游服務(wù)器收到了一個無效的響應(yīng),需要的朋友可以參考下2024-11-11
Nginx?error_page自定義錯誤頁面設(shè)置過程
這篇文章主要介紹了Nginx?error_page自定義錯誤頁面設(shè)置過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
Nginx訪問本地靜態(tài)資源詳細(xì)步驟(推薦)
Nginx?(engine?x)?是一個高性能的HTTP和反向代理web服務(wù)器,同時也提供了IMAP/POP3/SMTP服務(wù),這篇文章主要介紹了nginx配置訪問本地靜態(tài)資源,需要的朋友可以參考下2022-12-12
Mac M1 Nginx 配置多站點(diǎn)的實現(xiàn)
這篇文章主要介紹了Mac M1 Nginx 配置多站點(diǎn)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
利用Nginx實現(xiàn)反向代理Node.js的方法詳解
這篇文章主要給大家介紹了關(guān)于利用Nginx實現(xiàn)反向代理Node.js的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08

