Heim  >  Artikel  >  Backend-Entwicklung  >  Ausführliche Erklärung zur Installation und Konfiguration von Nginx unter Linux

Ausführliche Erklärung zur Installation und Konfiguration von Nginx unter Linux

高洛峰
高洛峰Original
2017-02-07 16:52:263705Durchsuche

1. Nginx unter Linux installieren und konfigurieren

Bei der Erstinstallation von Nginx werden alle dabei auftretenden Probleme Schritt für Schritt gelöst.

Das verwendete Tool ist secureCRT, um eine Verbindung zum Server herzustellen und sich dort anzumelden.

1.1 rz-Befehl, ein Dialogfeld wird angezeigt. Wählen Sie das Nginx-komprimierte Paket zum Hochladen aus.

#rz

1.2 Entpacken

[root@vw010001135067 ~]# cd /usr/local/
[root@vw010001135067 local]# tar -zvxf nginx-1.10.2.tar.gz

1.3 Geben Sie den Nginx-Ordner ein und führen Sie den Befehl ./configure aus

[root@vw010001135067 local]# cd nginx-1.10.2
[root@vw010001135067 nginx-1.10.2]# ./configure

Der Fehler wird wie folgt gemeldet:

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

Dieser Fehler tritt auf. Dann ist das gcc-Paket nicht installiert.

1.3.1 gcc installieren

gcc anzeigen

[root@vw010001135067 nginx-1.10.2]# whereis gcc
gcc:

gcc installieren

[root@vw010001135067 nginx-1.10.2]# yum -y install gcc

Nach erfolgreicher Installation erneut prüfen

[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 ist installiert.

1.3.2 ./configure weiter ausführen

[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.

Der obige Fehler tritt auf. Installieren Sie pcre-devel

[root@vw010001135067 nginx-1.10.2]# yum install pcre-devel

1.3.3 und führen Sie es erneut aus./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.

Wenn dieser Fehler auftritt, dann ausführen

yum install zlib-devel

1.3.4 Nach der Ausführung von ./configure wird kein Fehler gemeldet

[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 Wenn Sie die OpenSSL-Funktion verwenden möchten , sha1-Funktion. Dann installieren Sie 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-Modulausführung aktivieren./configure –with-http_ssl_module

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

1.4 .2 Aktivieren Sie die Seite „Server+Status“ und führen Sie ./configure –with-http_stub_status_module aus

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

Die beiden oben genannten Befehle können gleichzeitig gestartet werden

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

1.5 Die obige Konfiguration wurde bestanden

Führen Sie den Befehl „make“ und den Befehl „make install“ aus

[root@vw010001135067 nginx-1.10.2]# make
[root@vw010001135067 nginx-1.10.2]# make install

An diesem Punkt wurde Nginx erfolgreich ausgeführt

1.6 Konfigurationsumgebungsvariable

Konfiguration zu /etc/profile hinzufügen

Konfigurationsdatei öffnen

[root@vw010001135067 nginx-1.10.2]# vi /etc/profile

zur Konfigurationsdatei hinzufügen
#nginx configure
export NGINX_HOME=/usr/local/nginx-1.10.2
export PATH=$PATH:$NGINX_HOME/sbin

Ich habe begonnen, es wie oben auszufüllen, aber es konnte bei Verwendung von nginx -v nicht gefunden werden. Mir ist aufgefallen, dass die Adresse meiner obigen nginx_home-Konfiguration falsch ist. Suchen Sie zuerst die Installationsadresse von Nginx

[root@vw010001135067 nginx-1.10.2]# whereis nginx
nginx: /usr/local/nginx

Die Adresse ist wirklich falsch, ändern Sie das Obige in

#nginx configure
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin

Kompilieren, speichern, beenden und ausführen

[root@vw010001135067 nginx-1.10.2]# source /etc/profile


, um die Konfiguration wirksam zu machen.

1.7 Nginx-Version anzeigen

[root@vw010001135067 nginx]# nginx -v
nginx version: nginx/1.10.2

Der gesamte Prozess war erfolgreich!

2. Ändern Sie nginx.conf

2.1 Starten Sie nginx

Mein Nginx-Dienst befindet sich unter http://10.1.135.67/. Starten Sie nginx jetzt

[root@vw010001135067 nginx]# cd /usr/local/nginx
[root@vw010001135067 nginx]# nginx -c conf/nginx.conf

Erfolgreich starten, http://10.1.135.67/ im Browser öffnen, die Standard-Portnummer ist 80.

Ausführliche Erklärung zur Installation und Konfiguration von Nginx unter Linux

Wie in der Abbildung oben funktioniert Nginx bereits normal.

2.2 Tomcat-Dienst konfigurieren

Jetzt ist mein Tomcat-Dienst am 10.1.29.15 und muss über Nginx weitergeleitet werden. Öffnen Sie dann nginx.conf und ändern Sie die Konfigurationsdatei. Fügen Sie wie folgt hinzu:

#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;
} 
 }
}

Speichern Sie nach der Konfiguration die Konfigurationsdatei und starten Sie nginx neu

[root@vw010001135067 nginx]# nginx -s reload

Ob der Aufruf des Upload-Projekts im Browser erfolgreich ist

Ausführliche Erklärung zur Installation und Konfiguration von Nginx unter Linux

Wie in der Abbildung gezeigt, kann auf das Projekt korrekt zugegriffen werden und die Konfiguration ist erfolgreich!

Das Obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, dass er zum Lernen aller beiträgt. Ich hoffe auch, dass jeder die PHP-Chinesisch-Website unterstützt.

Ausführlichere Artikel zur Installation und Konfiguration von Nginx unter Linux finden Sie auf der chinesischen PHP-Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn