nginx安装和部署

#本文档采用官方源码安装(http://nginx.org/)

#用浏览器打开http://nginx.org/en/download.html 找到pre-built packages;点击stable and mainline;

 #选择你的操作系统

 #配置yum仓库

RHEL/CentOS

Install the prerequisites:

    sudo yum install yum-utils

To set up the yum repository, create the file named /etc/yum.repos.d/nginx.repo with the following contents:

    [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

By default, the repository for stable nginx packages is used. If you would like to use mainline nginx packages, run the following command:

    sudo yum-config-manager --enable nginx-mainline

To install nginx, run the following command:

    sudo yum install nginx

When prompted to accept the GPG key, verify that the fingerprint matches 573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62, and if so, accept it. 

[root@localhost yum.repos.d]# vim /etc/yum.repos.d/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

#查看当前yum仓库

[root@localhost yum.repos.d]# yum repolist
已加载插件:fastestmirror
nginx-stable                                                                                                                                        | 2.9 kB  00:00:00     
nginx-stable/7/x86_64/primary_db                                                                                                                    |  50 kB  00:00:01     
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.huaweicloud.com
源标识                                                                源名称                                                                                         状态
base/7/x86_64                                                         CentOS-7 - Base                                                                                10,097
epel/x86_64                                                           Extra Packages for Enterprise Linux 7 - x86_64                                                 13,473
extras/7/x86_64                                                       CentOS-7 - Extras                                                                                 305
nginx-stable/7/x86_64                                                 nginx stable repo                                                                                 170
updates/7/x86_64                                                      CentOS-7 - Updates                                                                                738
repolist: 24,783

#查看安装源(显示应有nginx-stable,如果为@epel则会走epel源)

[root@localhost yum.repos.d]# yum list nginx
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.huaweicloud.com
可安装的软件包
nginx.x86_64      1:1.16.1-1.el7.ngx    nginx-stable

#安装nginx服务

[root@localhost yum.repos.d]# yum -y install nginx

依赖关系解决

===========================================================================================================================================================================
 Package                                  架构                               版本                                           源                                        大小
===========================================================================================================================================================================
正在安装:
 nginx                                    x86_64                             1:1.16.1-1.el7.ngx                             nginx-stable                             766 k
。。。。。。
已安装: nginx.x86_64
1:1.16.1-1.el7.ngx 作为依赖被升级: openssl.x86_64 1:1.0.2k-19.el7 openssl-libs.x86_64 1:1.0.2k-19.el7 完毕!

#将服务添加到开机自启

[root@localhost ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@localhost ~]# systemctl list-unit-files | grep nginx
nginx-debug.service                         disabled
nginx.service                               enabled 

#可查看版本(小v)和编译信息(大V)

[root@localhost yum.repos.d]# nginx -v
nginx version: nginx/1.16.1
[root@localhost yum.repos.d]# nginx -V
nginx version: nginx/1.16.1
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: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

 #启动服务

[root@localhost conf.d]# systemctl start nginx
[root@localhost conf.d]# systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since 二 2019-11-26 18:41:25 CST; 1min 26s ago
     Docs: http://nginx.org/en/docs/
  Process: 2542 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 2543 (nginx)
   CGroup: /system.slice/nginx.service
           ├─2543 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           └─2544 nginx: worker process

11月 26 18:41:25 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
11月 26 18:41:25 localhost.localdomain systemd[1]: Started nginx - high performance web server.

#查看端口是否正常

[root@localhost ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2543/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1134/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1873/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1134/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1873/master    

#通过浏览器访问服务

 #如果无法正常显示,去检查SELinux和防火墙(centos7是firewalld.service)

 * 参考oldboy视频整理

原文地址:https://www.cnblogs.com/sparkss/p/11933648.html