centos7安装nginx

一.通过yum安装nginx(简单)

0.介绍

这种方式是非常简单方便,nginx官网上也有介绍

http://nginx.org/en/linux_packages.html#RHEL-CentOS

官方说明就是在/etc/yum.repo.d目录下面创建一个nginx的yum源,然后就可以直接用yum install nginx安装了,超级简单,这种方式就直接帮你把服务、都安装好了

1.查看安装源

[root@iZhp3bt567jwnvj9o9ho7oZ tengine-2.3.2]# yum repolist
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
源标识                     源名称                                                    状态
base/7/x86_64              CentOS-7                                                  10,072
epel/x86_64                Extra Packages for Enterprise Linux 7 - x86_64            13,464
extras/7/x86_64            CentOS-7                                                     448
updates/7/x86_64           CentOS-7                                                     773
repolist: 24,757

2.创建安装源

[root@iZhp3bt567jwnvj9o9ho7oZ tengine-2.3.2]# cd /etc/yum.repos.d/
[root@iZhp3bt567jwnvj9o9ho7oZ yum.repos.d]# vim nginx.repo

安装源内容

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

3.查看是否加载了nginx安装源

[root@iZhp3bt567jwnvj9o9ho7oZ yum.repos.d]# yum repolist
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
nginx-stable                                                        | 2.9 kB  00:00:00     
nginx-stable/7/x86_64/primary_db                                    |  59 kB  00:00:02     
源标识                        源名称                                                 状态
base/7/x86_64                 CentOS-7                                               10,072
epel/x86_64                   Extra Packages for Enterprise Linux 7 - x86_64         13,464
extras/7/x86_64               CentOS-7                                                  448
nginx-stable/7/x86_64         nginx stable repo                                         206
updates/7/x86_64              CentOS-7                                                  773
repolist: 24,963

4.通过yum安装nginx

[root@iZhp3bt567jwnvj9o9ho7oZ yum.repos.d]# yum install nginx
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 nginx.x86_64.1.1.18.0-2.el7.ngx 将被 安装
--> 解决依赖关系完成

依赖关系解决

===========================================================================================
 Package        架构            版本                           源                     大小
===========================================================================================
正在安装:
 nginx          x86_64          1:1.18.0-2.el7.ngx             nginx-stable          769 k

事务概要
===========================================================================================
安装  1 软件包

总下载量:769 k
安装大小:2.7 M
Is this ok [y/d/N]: y
Downloading packages:
警告:/var/cache/yum/x86_64/7/nginx-stable/packages/nginx-1.18.0-2.el7.ngx.x86_64.rpm: 头V4 RSA/SHA1 Signature, 密钥 ID 7bd9bf62: NOKEY
nginx-1.18.0-2.el7.ngx.x86_64.rpm 的公钥尚未安装
nginx-1.18.0-2.el7.ngx.x86_64.rpm                                   | 769 kB  00:00:59     
从 https://nginx.org/keys/nginx_signing.key 检索密钥
导入 GPG key 0x7BD9BF62:
 用户ID     : "nginx signing key <signing-key@nginx.com>"
 指纹       : 573b fd6b 3d8f bc64 1079 a6ab abf5 bd82 7bd9 bf62
 来自       : https://nginx.org/keys/nginx_signing.key
是否继续?[y/N]:y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : 1:nginx-1.18.0-2.el7.ngx.x86_64                                        1/1 
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* http://nginx.org/en/docs/

Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* http://nginx.org/en/support.html

Commercial subscriptions for nginx are available on:
* http://nginx.com/products/

----------------------------------------------------------------------
  验证中      : 1:nginx-1.18.0-2.el7.ngx.x86_64                                        1/1 

已安装:
  nginx.x86_64 1:1.18.0-2.el7.ngx                                                          

完毕!

5.使用systemctl启动nginx服务

设置开机启动

[root@iZhp3bt567jwnvj9o9ho7oZ yum.repos.d]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

启动nginx服务

[root@iZhp3bt567jwnvj9o9ho7oZ yum.repos.d]# systemctl start nginx
[root@iZhp3bt567jwnvj9o9ho7oZ yum.repos.d]# ps -ef|grep nginx
root       521     1  0 15:36 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      522   521  0 15:36 ?        00:00:00 nginx: worker process
root       702 31879  0 15:36 pts/0    00:00:00 grep --color=auto nginx

测试

nginx相关命令

systemctl enable nginx  #设置nginx为开机启动

systemctl start nginx   #启动nginx服务

systemctl stop nginx     #停止nginx
systemctl restart nginx  #重启nginx

二.手动安装nginx

安装所需环境

Nginx 是 C语言 开发,建议在 Linux 上运行,当然,也可以安装 Windows 版本,本篇则使用 CentOS 7 作为安装环境。

1.gcc 安装

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

yum install gcc-c++

2. PCRE pcre-devel 安装

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

yum install -y pcre pcre-devel

3. zlib 安装

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

yum install -y zlib zlib-devel

4. OpenSSL 安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel

5.官网下载

①直接下载.tar.gz安装包,地址:https://nginx.org/en/download.html

②使用wget命令下载(推荐)。确保系统已经安装了wget,如果没有安装,执行 yum install wget 安装。

wget -c http://nginx.org/download/nginx-1.18.0.tar.gz

6.解压

依然是直接命令:

tar -zxvf nginx-1.12.0.tar.gz
cd nginx-1.12.0

7.配置

其实在 nginx-1.12.0 版本中你就不需要去配置相关东西,默认就可以了。当然,如果你要自己配置目录也是可以的。
①.使用默认配置。这个命令会在目录里生成Makefile文件,成功后会在目录生成Makefile跟objs目录

./configure

②.自定义配置(不推荐)

./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

注:将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

8.编译安装

make
make install

查找安装路径:

whereis nginx

9.启动、停止nginx

cd /usr/local/nginx/sbin/
./nginx 
./nginx -s stop
./nginx -s quit
./nginx -s reload

站在巨人肩膀上摘苹果

https://www.cnblogs.com/6J2B2/p/12815639.html

https://www.cnblogs.com/liujuncm5/p/6713784.html

posted @ 2020-11-26 16:12  未月廿三  阅读(467)  评论(0编辑  收藏  举报