Nginx安装及配置

Nginx官网下载地址:
http://nginx.org/en/download.html

官网下载分三种版本:
Mainline version 主线版本
Stable version 稳定版本
Legacy versions 历史版本

选择当前稳定版本下载即可。

Windows版本安装及配置

点击nginx/Windows-1.21.6直接下载压缩包,解压后,双击nginx.exe即可运行启动。

Nginx目录结构:
conf 配置文件目录
html 默认静态资源目录
logs 日志目录
temp 临时文件目录

浏览器输入地址:http://localhost,如果出现以下页面,则说明Nginx服务已正常启动。

Nginx-01.pngNginx-01.png

下一步可将Nginx服务注册为Windows服务。

Linux版本安装及配置

需要的安装包:
1、pcre-8.35.tar.gz
2、nginx-1.21.6.tar.gz

安装pcre包

[@zw_27_105 /]# cd /opt/servers/
[@zw_27_105 /opt/servers]# tar -zxvf pcre-8.35.tar.gz 
pcre-8.35/
pcre-8.35/pcre_scanner.h
pcre-8.35/LICENCE
pcre-8.35/makevp_c.txt
...
[@zw_27_105 /opt/servers]# cd pcre-8.35
[@zw_27_105 /opt/servers/pcre-8.35]# ./configure
[@zw_27_105 /opt/servers/pcre-8.35]# make
[@zw_27_105 /opt/servers/pcre-8.35]# make install

报错:configure: error: in `/opt/servers/pcre-8.35':
configure: error: no acceptable C compiler found in $PATH

解决办法:首先安装GCC:
 [@zw_27_105 /opt/servers]#yum install gcc

如果报错:configure: error: You need a C++ compiler for C++ support.
或:
./libtool: line 990: g++: command not found

解决办法:
[@zw_27_105 /opt/servers]#yum install -y gcc gcc-c++

报错:

--> Finished Dependency Resolution
Error: Package: libstdc++-devel-4.4.7-17.el6.x86_64 (Server)
           Requires: libstdc++(x86-64) = 4.4.7-17.el6
           Installed: libstdc++-4.4.7-18.el6.x86_64 (@6ASU8-updates)
               libstdc++(x86-64) = 4.4.7-18.el6
           Available: libstdc++-4.4.6-4.el6.x86_64 (6ASU8-updates)
               libstdc++(x86-64) = 4.4.6-4.el6
           Available: libstdc++-4.4.7-17.el6.x86_64 (Server)
               libstdc++(x86-64) = 4.4.7-17.el6
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles –nodigest

解决办法:
[@zw_27_105 /opt/servers]# yum downgrade libstdc++

  128  2017-05-02 11:23:33 yum downgrade libgomp
  129  2017-05-02 13:55:38 yum downgrade libstdc++
  130  2017-05-02 13:56:37 yum downgrade libgcc
  131  2017-05-02 13:57:15 yum downgrade cpp

报错:/usr/bin/ld: .libs/pcrecpp.o: relocation R_X86_64_32S against `.bss' can not be used when making a shared object; recompile with -fPIC
.libs/pcrecpp.o: could not read symbols: Bad value

解决办法:
 ./configure --disable-shared --with-pic

安装Nginx

[@zw_27_105 /opt/servers/pcre-8.35]# cd ..
[@zw_27_105 /opt/servers]# tar -zxvf nginx-1.21.6.tar.gz 
[@zw_27_105 /opt/servers]# cd nginx-1.21.6
[@zw_27_105 /opt/servers]# ./configure

如果为https请求,需启用SSL支持并且能够处理HTTPS请求

[@zw_27_105 /opt/servers]# ./configure --with-http_ssl_module

[@zw_27_105 /opt/servers]# make
[@zw_27_105 /opt/servers]# make install

报错:./configure: error: the HTTP gzip module requires the zlib library.

yum install zlib-devel -y

报错:./configure: error: the HTTP rewrite module requires the PCRE library.
则指定下pcre的路径:

./configure --with-pcre=/opt/servers/pcre-8.35

测试nginx是否安装成功:

启动Nginx: /usr/local/nginx/sbin/nginx
停止Nginx:/usr/local/nginx/sbin/nginx -s stop

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl= option.

解决办法:yum -y install openssl openssl-devel

注:在64位系统上启动时有可能报如下错误:
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
解决办法如下:

ln -s /usr/local/lib/libpcre.so.1 /lib64/libpcre.so.1

重新启动即可;

启动Nginx后,浏览器访问返回Nginx首页,则说明已安装成功。

Nginx常用命令

/usr/local/nginx/sbin/nginx -h 查看帮助说明
/usr/local/nginx/sbin/nginx 启动Nginx
/usr/local/nginx/sbin/nginx -v 显示Nginx版本号 
/usr/local/nginx/sbin/nginx -V 显示Nginx版本号及配置参数
/usr/local/nginx/sbin/nginx -t 检查配置文件语法是否正确
/usr/local/nginx/sbin/nginx -s 发送信号给主进程,常用有(stop, quit, reopen, reload)
/usr/local/nginx/sbin/nginx -c 指定配置文件

注意:启动时如果不指定-c配置文件,则nginx默认使用/usr/local/nginx/conf/nginx.conf配置

[root@iZ2zegbfyk4k ~]# /usr/local/nginx/sbin/nginx -h
nginx version: nginx/1.21.6
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

[root@iZ2zegbfyk4k ~]# 
[root@iZ2zegbfyk4k ~]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.21.6
[root@iZ2zegbfyk4k ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.21.6
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --with-http_ssl_module --with-pcre=/opt/servers/pcre-8.35
[root@iZ2zegbfyk4k ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@iZ2zegbfyk4k ~]# 

Linux服务器Nginx服务注册服务及开机自启动配置详见:
Linux服务器上配置Nginx服务开机自启动

Nginx配置说明

参考地址:
Nginx配置文件详解
https://blog.51cto.com/abcd/3304123

最后修改于:2022年08月31日 10:01

添加新评论