nginx 安装方法

张立军的架构之路 2020-09-28

一、yum安装(推荐)
centos7系统库中默认是没有nginx的rpm包的,所以我们自己需要先更新下rpm依赖库

(1)使用yum安装nginx需要包括Nginx的库,安装Nginx的库

#rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

(2)使用下面命令安装nginx

#yum install nginx

(3)启动Nginx

#service nginx start



#systemctl start nginx.service

配置文件在 /etc/nginx/conf.d/

二、手动安装
一、安装准备
首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库,这些依赖库主要有g++、gcc、openssl-devel、pcre-devel和zlib-devel 所以执行如下命令安装

$ yum install gcc-c++
$ yum install pcre pcre-devel
$ yum install zlib zlib-devel
$ yum install openssl openssl--devel

二、安装Nginx
安装之前,最好检查一下是否已经安装有nginx

$ find -name nginx

如果系统已经安装了nginx,那么就先卸载

$ yum remove nginx

首先进入/usr/local目录

$ cd /usr/local

从官网下载最新版的nginx

$ wget http://nginx.org/download/nginx-1.7.4.tar.gz

解压nginx压缩包

$ tar -zxvf nginx-1.7.4.tar.gz

会产生一个nginx-1.7.4 目录,这时进入nginx-1.7.4目录

$ cd nginx-1.7.4

接下来安装,使用--prefix参数指定nginx安装的目录,make、make install安装

./configure --prefix=/usr/local/nginx --with-debug --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module

这里进行解压,如果这里报错提示
./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.

很显然,错误提示非常明显,缺失 GD 库,即使你不安装该模块也可以继续进行安装,但是后续使用
可能会产生错误,这里应该进行解决,安装该模块即可。
你用  yum search GD 命令可以查到缺失的包,然后安装
[centos@localhost nginx-1.16.1]$ yum search GD
已加载插件:fastestmirror, langpacks
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Repository contrib is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
 * webtatic: us-east.repo.webtatic.com
============================================= N/S matched: GD =============================================
compat-libgdata13.i686 : Compat package with libgdata 0.13 libraries
compat-libgdata13.x86_64 : Compat package with libgdata 0.13 libraries
gd-devel.i686 : The development libraries and header files for gd
gd-devel.x86_64 : The development libraries and header files for gd
gd-progs.x86_64 : Utility programs that use libgd
gdb-doc.noarch : Documentation for GDB (the GNU source-level debugger)
gdb-gdbserver.x86_64 : A standalone server for GDB (the GNU source-level debugger)
gdbm-devel.i686 : Development libraries and header files for the gdbm library
gdbm-devel.x86_64 : Development libraries and header files for the gdbm library
gdk-pixbuf2-devel.i686 : Development files for gdk-pixbuf
gdk-pixbuf2-devel.x86_64 : Development files for gdk-pixbuf
gdk-pixbuf2-tests.x86_64 : Tests for the gdk-pixbuf2 package
gdm-devel.i686 : Development files for gdm
gdm-devel.x86_64 : Development files for gdm
gdm-pam-extensions-devel.i686 : Macros for developing GDM extensions to PAM
gdm-pam-extensions-devel.x86_64 : Macros for developing GDM extensions to PAM
graphviz-gd.i686 : Graphviz plugin for renderers based on gd
graphviz-gd.x86_64 : Graphviz plugin for renderers based on gd
gupnp-igd.i686 : Library to handle UPnP IGD port mapping
gupnp-igd.x86_64 : Library to handle UPnP IGD port mapping
gupnp-igd-devel.i686 : Development files for gupnp-igd
gupnp-igd-devel.x86_64 : Development files for gupnp-igd
gupnp-igd-python.x86_64 : Python bindings for gupnp-igd
libgdata.i686 : Library for the GData protocol
libgdata.x86_64 : Library for the GData protocol
libgdata-devel.i686 : Development files for libgdata
libgdata-devel.x86_64 : Development files for libgdata
libgdither-devel.i686 : Development files for libgdither
libgdither-devel.x86_64 : Development files for libgdither
libreoffice-gdb-debug-support.x86_64 : Additional support for debugging with gdb
perl-GD.x86_64 : Perl interface to the GD graphics library
perl-GD-Barcode.noarch : Create barcode image with GD
php-gd.x86_64 : A module for PHP applications for using the gd graphics library
php55w-gd.x86_64 : A module for PHP applications for using the gd graphics library
php56w-gd.x86_64 : A module for PHP applications for using the gd graphics library
php70w-gd.x86_64 : A module for PHP applications for using the gd graphics library
php71w-gd.x86_64 : A module for PHP applications for using the gd graphics library
php72w-gd.x86_64 : A module for PHP applications for using the gd graphics library
pulseaudio-gdm-hooks.x86_64 : PulseAudio GDM integration
relaxngDatatype-javadoc.noarch : API documentation for relaxngDatatype
rubygem-bigdecimal.x86_64 : BigDecimal provides arbitrary-precision floating point decimal arithmetic
cgdcbxd.x86_64 : DCB network priority management daemon
gd.i686 : A graphics library for quick creation of PNG or JPEG images
gd.x86_64 : A graphics library for quick creation of PNG or JPEG images
gdb.x86_64 : A GNU source-level debugger for C, C++, Fortran, Go and other languages
gdbm.i686 : A GNU set of database routines which use extensible hashing
gdbm.x86_64 : A GNU set of database routines which use extensible hashing
gdisk.x86_64 : An fdisk-like partitioning tool for GPT disks
gdk-pixbuf2.i686 : An image loading library
gdk-pixbuf2.x86_64 : An image loading library
gdm.i686 : The GNOME Display Manager
gdm.x86_64 : The GNOME Display Manager
hunspell-gd.noarch : Scots Gaelic hunspell dictionaries
libgdither.i686 : Library for applying dithering to PCM audio sources
libgdither.x86_64 : Library for applying dithering to PCM audio sources
relaxngDatatype.noarch : RELAX NG Datatype API
swig3-gdb.x86_64 : Commands for easier debugging of SWIG

  名称和简介匹配 only,使用“search all”试试。

[centos@localhost nginx-1.16.1]$ # 安装 gd-devel.x86_64 库
[centos@localhost nginx-1.16.1]$ sudo yum install -y gd-devel.x86_64

   #用下面的命令执行编译和安装

$ make
$ make install


如果没有报错,顺利完成后,最好看一下nginx的安装目录

$ whereis nginx


安装完毕后,进入安装后目录(/usr/local/nginx)便可以启动或停止它了。

启动 /usr/local/nginx/sbin/nginx

重启 /usr/local/nginx/sbin/nginx –s reload

停止 /usr/local/nginx/sbin/nginx –s stop

测试配置文件是否正常 /usr/local/nginx/sbin/nginx –t

强制关闭 pkill nginx

nginx配置文件  /usr/local/nginx/conf/nginx.conf

好了,现在开始编辑配置文件并执行重启

可能你又会遇到如下情况

[error] open() "/usr/local/Nginx/logs/Nginx.pid

解决方法:

[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

使用nginx -c的参数指定nginx.conf文件的位置

[root@localhost nginx]# cd logs/

[root@localhost logs]# ll

       -rw-r--r-- 1 root root 1246 12月 9 18:10 access.log

-rw-r--r-- 1 root root 516 12月 10 15:39 error.log

-rw-r--r-- 1 root root 5 12月 10 15:38 nginx.pid

看nginx.pid文件已经有了。

再启动应该没问题了。

此时要注意下,如果你的nginx是1.15以上版本,且你配置了ssl,可能你会遇到这样的警告

 [warn] the "ssl" directive is deprecated, use the "listen ... ssl" directive instead

配置文件中 listen443;需要修改为listen443 ssl; 且 把ssl on去掉

相关推荐

lwplvx / 0评论 2020-11-22
岁月如歌 / 0评论 2020-07-21