首頁 運維 Nginx Linux下安裝設定nginx的方法

Linux下安裝設定nginx的方法

May 23, 2023 pm 11:22 PM
linux nginx

一、linux下安裝設定nginx

第一次安裝nginx,中間出現的問題一步步驟解決。

使用到的工具securecrt,連線並登入伺服器。

1.1 rz指令,會彈出會話框,選擇要上傳的nginx壓縮包。

#rz
登入後複製

1.2 解壓縮

[root@vw010001135067 ~]# cd /usr/local/
[root@vw010001135067 local]# tar -zvxf nginx-1.10.2.tar.gz
登入後複製

1.3 進入nginx資料夾,執行./configure指令

[root@vw010001135067 local]# cd nginx-1.10.2
[root@vw010001135067 nginx-1.10.2]# ./configure
登入後複製

錯誤如下:

checking for os
 + linux 2.6.32-431.el6.x86_64 x86_64
checking for c compiler ... not found

./configure: error: c compiler cc is not found
登入後複製

出現這個錯誤。那就是gcc 包沒有安裝。

1.3.1 安裝gcc

查看gcc

[root@vw010001135067 nginx-1.10.2]# whereis gcc
gcc:
登入後複製

安裝gcc

[root@vw010001135067 nginx-1.10.2]# yum -y install gcc
登入後複製

安裝成功後再查看

[root@vw010001135067 nginx-1.10.2]# whereis gcc
gcc: /usr/bin/gcc /usr/lib/gcc /usr/libexec/gcc /usr/share/man/man1/gcc.1.gz
登入後複製

gcc安裝好了。

1.3.2 繼續執行./configure

[root@vw010001135067 nginx-1.10.2]# ./configure
checking for os
 + linux 2.6.32-431.el6.x86_64 x86_64
checking for c compiler ... found
......
checking for pcre library ... not found
checking for pcre library in /usr/local/ ... not found
checking for pcre library in /usr/include/pcre/ ... not found
checking for pcre library in /usr/pkg/ ... not found
checking for pcre library in /opt/local/ ... not found

./configure: error: the http rewrite module requires the pcre library.
you can either disable the module by using --without-http_rewrite_module
option, or install the pcre library into the system, or build the pcre library
statically from the source with nginx by using --with-pcre=<path> option.
登入後複製

出現如上錯誤。安裝pcre-devel

[root@vw010001135067 nginx-1.10.2]# yum install pcre-devel
登入後複製

1.3.3 再執行./configure

error: the http gzip module requires the zlib library.
you can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
登入後複製

如果有這個錯誤那麼執行

yum install zlib-devel
登入後複製

1.3.4 執行./configure後沒有報錯

[root@vw010001135067 nginx-1.10.2]# ./configure
checking for os
 + linux 2.6.32-431.el6.x86_64 x86_64
checking for c compiler ... found
 + using gnu c compiler
 + gcc version: 4.4.7 20120313 (red hat 4.4.7-17) (gcc) 
.......
configuration summary
 + using system pcre library
 + openssl library is not used
 + md5: using system crypto library
 + sha1: using system crypto library
 + using system zlib library

 nginx path prefix: "/usr/local/nginx"
 nginx binary file: "/usr/local/nginx/sbin/nginx"
 nginx modules path: "/usr/local/nginx/modules"
 nginx configuration prefix: "/usr/local/nginx/conf"
 nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
 nginx pid file: "/usr/local/nginx/logs/nginx.pid"
 nginx error log file: "/usr/local/nginx/logs/error.log"
 nginx http access log file: "/usr/local/nginx/logs/access.log"
 nginx http client request body temporary files: "client_body_temp"
 nginx http proxy temporary files: "proxy_temp"
 nginx http fastcgi temporary files: "fastcgi_temp"
 nginx http uwsgi temporary files: "uwsgi_temp"
 nginx http scgi temporary files: "scgi_temp"
登入後複製

1.4 如果你想使用openssl 功能,sha1 功能。那麼安裝openssl ,sha1 吧

[root@vw010001135067 nginx-1.10.2]# yum install openssl openssl-devel 
[root@vw010001135067 nginx-1.10.2]# install perl-digest-sha1.x86_64
登入後複製

1.4.1 開啟ssl 模組執行./configure –with-http_ssl_module

[root@vw010001135067 nginx-1.10.2]# ./configure --with-http_ssl_module
登入後複製

1.4.2 啟用「server status」頁,執行./configure –with -http_stub_status_module

[root@vw010001135067 nginx-1.10.2]# ./configure --with-http_stub_status_module
登入後複製

上面兩個指令同時啟動可以

複製程式碼 程式碼如下:

##[root@vw010001135067 nginx-1.10.2]# ./configure --with-http_stub_status_module --with-http_ssl_module

#1.5 上面configure就通過了

執行make 指令,執行make install 指令

[root@vw010001135067 nginx-1.10.2]# make
[root@vw010001135067 nginx-1.10.2]# make install
登入後複製

至此,nginx 執行成功了

1.6 設定環境變數

在/etc/profile 中加入設定


開啟設定檔

[root@vw010001135067 nginx-1.10.2]# vi /etc/profile
登入後複製

在設定檔中加入

#nginx configure
export nginx_home=/usr/local/nginx-1.10.2
export path=$path:$nginx_home/sbin
登入後複製

我開始像上面填寫,結果nginx -v的時候查找不到。注意到上面我的nginx_home配置的位址不對。先找到nginx的安裝位址

[root@vw010001135067 nginx-1.10.2]# whereis nginx
nginx: /usr/local/nginx
登入後複製

還真是位址寫錯了,把上面的改成

#nginx configure
export nginx_home=/usr/local/nginx
export path=$path:$nginx_home/sbin
登入後複製

編譯完儲存退出並執行

[root@vw010001135067 nginx-1.10.2]# source /etc/profile
登入後複製

使設定生效。

1.7 查看nginx版本

[root@vw010001135067 nginx]# nginx -v
nginx version: nginx/1.10.2
登入後複製

整個過程成功了!

二、修改nginx.conf

2.1 啟動nginx

我的nginx服務在http://10.1.135.67/,設定成功後,現在啟動nginx

[root@vw010001135067 nginx]# cd /usr/local/nginx
[root@vw010001135067 nginx]# nginx -c conf/nginx.conf
登入後複製

啟動成功,在瀏覽器開啟http://10.1.135.67/,預設連接埠號碼80.

Linux下安裝設定nginx的方法

如上圖, nginx已經正常運作了。

2.2 設定tomcat服務

現在我的tomcat服務在10.1.29.15,需要透過nginx轉送。那麼打開nginx.conf,修改設定檔。如下,新增:

#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid  logs/nginx.pid;


events {
 worker_connections 1024;#最大连接数,默认为512
 accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
 multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
 #use epoll;  #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport 
}


http {
 #文件扩展名与文件类型映射表
 include  mime.types;

 #默认文件类型,默认为text/plain 
 default_type application/octet-stream;

 #自定义格式
 log_format main &#39;$remote_addr - $remote_user [$time_local] "$request" &#39;
      &#39;$status $body_bytes_sent "$http_referer" &#39;
      &#39;"$http_user_agent" "$http_x_forwarded_for"&#39;; 

 #combined为日志格式的默认值
 access_log logs/access.log main;

 #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块
 sendfile  on;
 sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。

 #tcp_nopush  on;

 #连接超时时间,默认为75s,可以在http,server,location块。
 keepalive_timeout 65;

 #gzip on;

 upstream upload {
  server 10.1.29.15:8080;
 }

 error_page 404 https://www.baidu.com; #错误页

 server {
  keepalive_requests 120; #单连接请求上限次数。
  listen  80; #监听端口
  server_name localhost; #监听地址 

  #charset koi8-r;

  #access_log logs/host.access.log main;

  location ~ ^.*?/upload/[^/]*?$ {
   proxy_connect_timeout 15;
   proxy_send_timeout 15;
   proxy_read_timeout 15;
   proxy_set_header host $host;
   proxy_set_header x-real-ip $remote_addr;
   proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
   proxy_set_header connection "";
   proxy_pass http://upload; #请求转向upload 定义的服务器列表
   client_max_body_size 1024m;
} 
 }
}
登入後複製

配置好後,儲存設定文件,並且重啟nginx

[root@vw010001135067 nginx]# nginx -s reload
登入後複製
在瀏覽器呼叫upload專案是否成功


Linux下安裝設定nginx的方法

如圖能正確存取項目,配置成功!

以上是Linux下安裝設定nginx的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Android TV Box 獲得非官方 Ubuntu 24.04 升級 Android TV Box 獲得非官方 Ubuntu 24.04 升級 Sep 05, 2024 am 06:33 AM

Android TV Box 獲得非官方 Ubuntu 24.04 升級

deepseek網頁版入口 deepseek官網入口 deepseek網頁版入口 deepseek官網入口 Feb 19, 2025 pm 04:54 PM

deepseek網頁版入口 deepseek官網入口

deepseek怎麼安裝 deepseek怎麼安裝 Feb 19, 2025 pm 05:48 PM

deepseek怎麼安裝

BitPie比特派錢包app下載位址 BitPie比特派錢包app下載位址 Sep 10, 2024 pm 12:10 PM

BitPie比特派錢包app下載位址

BITGet官方網站安裝(2025新手指南) BITGet官方網站安裝(2025新手指南) Feb 21, 2025 pm 08:42 PM

BITGet官方網站安裝(2025新手指南)

歐易okx安裝包直接進 歐易okx安裝包直接進 Feb 21, 2025 pm 08:00 PM

歐易okx安裝包直接進

gate.io安裝包免費拿 gate.io安裝包免費拿 Feb 21, 2025 pm 08:21 PM

gate.io安裝包免費拿

歐易交易所下載官方入口 歐易交易所下載官方入口 Feb 21, 2025 pm 07:51 PM

歐易交易所下載官方入口

See all articles