首页
Javascript
Html
Css
Node.js
Electron
移动开发
工具类
服务端
浏览器相关
前端收藏
其他
关于

CentOS 7上安装Nginx

2018年10月11日 发布 阅读(514) 作者:懒炖

CentOS 可以直接通过yum安装Nginx,现在就yum如何安装Nginx讲下流程

1、添加Nginx安装源,假设我们是在云服务器的根目录下,以腾讯云服务器为例

使用rpm命令添加源,如果没有事先添加源,执行yum install nginx会报错

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

访问http://nginx.org/packages/ ,有关于种系统的源,选择自己需要的

执行命令:

  1. [root@VM_0_8_centos /]# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
  2. Retrieving http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
  3. warning: /var/tmp/rpm-tmp.ZBdJtm: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
  4. Preparing... ################################# [100%]
  5. Updating / installing...
  6. 1:nginx-release-centos-7-0.el7.ngx ################################# [100%]
  7. [root@VM_0_8_centos /]#

有一个warning,这个warning我们先忽略,不影响安装
执行完上面命令后,会在 /etc/yum.repos.d/目录下添加一个Nginx.repo文件,如下查看

  1. [root@VM_0_8_centos yum.repos.d]# cd /etc/yum.repos.d/
  2. [root@VM_0_8_centos yum.repos.d]# ls -la
  3. total 60
  4. drwxr-xr-x. 2 root root 4096 Oct 11 15:21 .
  5. drwxr-xr-x. 89 root root 12288 Sep 20 14:20 ..
  6. -rw-r--r--. 1 root root 614 Sep 13 10:48 CentOS-Base.repo
  7. -rw-r--r-- 1 root root 1309 Aug 13 22:34 CentOS-CR.repo
  8. -rw-r--r-- 1 root root 649 Aug 13 22:34 CentOS-Debuginfo.repo
  9. -rw-r--r-- 1 root root 230 Sep 13 10:48 CentOS-Epel.repo
  10. -rw-r--r-- 1 root root 314 Aug 13 22:34 CentOS-fasttrack.repo
  11. -rw-r--r-- 1 root root 630 Aug 13 22:34 CentOS-Media.repo
  12. -rw-r--r-- 1 root root 1331 Aug 13 22:34 CentOS-Sources.repo
  13. -rw-r--r-- 1 root root 4768 Aug 13 22:34 CentOS-Vault.repo
  14. -rw-r--r-- 1 root root 664 May 11 11:35 epel-7.repo
  15. -rw-r--r-- 1 root root 113 Jul 15 2014 nginx.repo
  16. [root@VM_0_8_centos yum.repos.d]#

2、执行yum install nginx

  1. [root@VM_0_8_centos yum.repos.d]# yum install nginx
  2. Loaded plugins: fastestmirror, langpacks
  3. Repository epel is listed more than once in the configuration
  4. Determining fastest mirrors
  5. epel | 3.2 kB 00:00:00
  6. extras | 3.4 kB 00:00:00
  7. os | 3.6 kB 00:00:00
  8. updates | 3.4 kB 00:00:00
  9. (1/5): epel/7/x86_64/group_gz | 88 kB 00:00:00
  10. (2/5): epel/7/x86_64/updateinfo | 948 kB 00:00:00
  11. (3/5): extras/7/x86_64/primary_db | 204 kB 00:00:00
  12. (4/5): epel/7/x86_64/primary | 3.6 MB 00:00:00
  13. (5/5): updates/7/x86_64/primary_db | 6.0 MB 00:00:00
  14. epel 12723/12723
  15. Resolving Dependencies
  16. --> Running transaction check
  17. ---> Package nginx.x86_64 1:1.12.2-2.el7 will be installed
  18. --> Processing Dependency: nginx-filesystem = 1:1.12.2-2.el7 for package: 1:nginx-1.12.2-2.el7.x86_64
  19. --> Processing Dependency: nginx-all-modules = 1:1.12.2-2.el7 for package: 1:nginx-1.12.2-2.el7.x86_64
  20. --> Processing Dependency: nginx-filesystem for package: 1:nginx-1.12.2-2.el7.x86_64
  21. --> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.12.2-2.el7.x86_64
  22. --> Running transaction check
  23. ---> Package gperftools-libs.x86_64 0:2.6.1-1.el7 will be installed
  24. ---> Package nginx-all-modules.noarch 1:1.12.2-2.el7 will be installed
  25. --> Processing Dependency: nginx-mod-stream = 1:1.12.2-2.el7 for package: 1:nginx-all-modules-1.12.2-2.el7.noarch
  26. --> Processing Dependency: nginx-mod-mail = 1:1.12.2-2.el7 for package: 1:nginx-all-modules-1.12.2-2.el7.noarch
  27. --> Processing Dependency: nginx-mod-http-xslt-filter = 1:1.12.2-2.el7 for package: 1:nginx-all-modules-1.12.2-2.el7.noarch
  28. --> Processing Dependency: nginx-mod-http-perl = 1:1.12.2-2.el7 for package: 1:nginx-all-modules-1.12.2-2.el7.noarch
  29. --> Processing Dependency: nginx-mod-http-image-filter = 1:1.12.2-2.el7 for package: 1:nginx-all-modules-1.12.2-2.el7.noarch
  30. --> Processing Dependency: nginx-mod-http-geoip = 1:1.12.2-2.el7 for package: 1:nginx-all-modules-1.12.2-2.el7.noarch
  31. ---> Package nginx-filesystem.noarch 1:1.12.2-2.el7 will be installed
  32. --> Running transaction check
  33. ---> Package nginx-mod-http-geoip.x86_64 1:1.12.2-2.el7 will be installed
  34. ---> Package nginx-mod-http-image-filter.x86_64 1:1.12.2-2.el7 will be installed
  35. --> Processing Dependency: gd for package: 1:nginx-mod-http-image-filter-1.12.2-2.el7.x86_64
  36. --> Processing Dependency: libgd.so.2()(64bit) for package: 1:nginx-mod-http-image-filter-1.12.2-2.el7.x86_64
  37. ---> Package nginx-mod-http-perl.x86_64 1:1.12.2-2.el7 will be installed
  38. ---> Package nginx-mod-http-xslt-filter.x86_64 1:1.12.2-2.el7 will be installed
  39. --> Processing Dependency: libxslt.so.1(LIBXML2_1.0.18)(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64
  40. --> Processing Dependency: libxslt.so.1(LIBXML2_1.0.11)(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64
  41. --> Processing Dependency: libxslt.so.1()(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64
  42. --> Processing Dependency: libexslt.so.0()(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64
  43. ---> Package nginx-mod-mail.x86_64 1:1.12.2-2.el7 will be installed
  44. ---> Package nginx-mod-stream.x86_64 1:1.12.2-2.el7 will be installed
  45. --> Running transaction check
  46. ---> Package gd.x86_64 0:2.0.35-26.el7 will be installed
  47. --> Processing Dependency: libXpm.so.4()(64bit) for package: gd-2.0.35-26.el7.x86_64
  48. ---> Package libxslt.x86_64 0:1.1.28-5.el7 will be installed
  49. --> Running transaction check
  50. ---> Package libXpm.x86_64 0:3.5.12-1.el7 will be installed
  51. --> Finished Dependency Resolution
  52. Dependencies Resolved
  53. ==================================================================================================================================================================================================================
  54. Package Arch Version Repository Size
  55. ==================================================================================================================================================================================================================
  56. Installing:
  57. nginx x86_64 1:1.12.2-2.el7 epel 530 k
  58. Installing for dependencies:
  59. gd x86_64 2.0.35-26.el7 os 146 k
  60. gperftools-libs x86_64 2.6.1-1.el7 os 272 k
  61. libXpm x86_64 3.5.12-1.el7 os 55 k
  62. libxslt x86_64 1.1.28-5.el7 os 242 k
  63. nginx-all-modules noarch 1:1.12.2-2.el7 epel 16 k
  64. nginx-filesystem noarch 1:1.12.2-2.el7 epel 17 k
  65. nginx-mod-http-geoip x86_64 1:1.12.2-2.el7 epel 23 k
  66. nginx-mod-http-image-filter x86_64 1:1.12.2-2.el7 epel 26 k
  67. nginx-mod-http-perl x86_64 1:1.12.2-2.el7 epel 36 k
  68. nginx-mod-http-xslt-filter x86_64 1:1.12.2-2.el7 epel 26 k
  69. nginx-mod-mail x86_64 1:1.12.2-2.el7 epel 54 k
  70. nginx-mod-stream x86_64 1:1.12.2-2.el7 epel 76 k
  71. Transaction Summary
  72. ==================================================================================================================================================================================================================
  73. Install 1 Package (+12 Dependent packages)
  74. Total download size: 1.5 M
  75. Installed size: 4.3 M
  76. Is this ok [y/d/N]:

上面命令,最后一行Is this ok [y/d/N]:后面, 我们输入y,如果要跳过这个步骤,安装时执行yum install nginx -y

输入y后,命令继续执行如下

  1. Is this ok [y/d/N]: y
  2. Downloading packages:
  3. (1/13): gd-2.0.35-26.el7.x86_64.rpm | 146 kB 00:00:00
  4. (2/13): libXpm-3.5.12-1.el7.x86_64.rpm | 55 kB 00:00:00
  5. (3/13): gperftools-libs-2.6.1-1.el7.x86_64.rpm | 272 kB 00:00:00
  6. (4/13): libxslt-1.1.28-5.el7.x86_64.rpm | 242 kB 00:00:00
  7. (5/13): nginx-all-modules-1.12.2-2.el7.noarch.rpm | 16 kB 00:00:00
  8. (6/13): nginx-filesystem-1.12.2-2.el7.noarch.rpm | 17 kB 00:00:00
  9. (7/13): nginx-mod-http-geoip-1.12.2-2.el7.x86_64.rpm | 23 kB 00:00:00
  10. (8/13): nginx-mod-http-image-filter-1.12.2-2.el7.x86_64.rpm | 26 kB 00:00:00
  11. (9/13): nginx-1.12.2-2.el7.x86_64.rpm | 530 kB 00:00:00
  12. (10/13): nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64.rpm | 26 kB 00:00:00
  13. (11/13): nginx-mod-http-perl-1.12.2-2.el7.x86_64.rpm | 36 kB 00:00:00
  14. (12/13): nginx-mod-mail-1.12.2-2.el7.x86_64.rpm | 54 kB 00:00:00
  15. (13/13): nginx-mod-stream-1.12.2-2.el7.x86_64.rpm | 76 kB 00:00:00
  16. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  17. Total 3.0 MB/s | 1.5 MB 00:00:00
  18. Running transaction check
  19. Running transaction test
  20. Transaction test succeeded
  21. Running transaction
  22. Warning: RPMDB altered outside of yum.
  23. Installing : gperftools-libs-2.6.1-1.el7.x86_64 1/13
  24. Installing : libxslt-1.1.28-5.el7.x86_64 2/13
  25. Installing : 1:nginx-filesystem-1.12.2-2.el7.noarch 3/13
  26. Installing : libXpm-3.5.12-1.el7.x86_64 4/13
  27. Installing : gd-2.0.35-26.el7.x86_64 5/13
  28. Installing : 1:nginx-mod-mail-1.12.2-2.el7.x86_64 6/13
  29. Installing : 1:nginx-mod-http-geoip-1.12.2-2.el7.x86_64 7/13
  30. Installing : 1:nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64 8/13
  31. Installing : 1:nginx-mod-http-perl-1.12.2-2.el7.x86_64 9/13
  32. Installing : 1:nginx-mod-stream-1.12.2-2.el7.x86_64 10/13
  33. Installing : 1:nginx-1.12.2-2.el7.x86_64 11/13
  34. Installing : 1:nginx-mod-http-image-filter-1.12.2-2.el7.x86_64 12/13
  35. Installing : 1:nginx-all-modules-1.12.2-2.el7.noarch 13/13
  36. Verifying : libXpm-3.5.12-1.el7.x86_64 1/13
  37. Verifying : 1:nginx-filesystem-1.12.2-2.el7.noarch 2/13
  38. Verifying : gd-2.0.35-26.el7.x86_64 3/13
  39. Verifying : libxslt-1.1.28-5.el7.x86_64 4/13
  40. Verifying : gperftools-libs-2.6.1-1.el7.x86_64 5/13
  41. Verifying : 1:nginx-1.12.2-2.el7.x86_64 6/13
  42. Verifying : 1:nginx-mod-mail-1.12.2-2.el7.x86_64 7/13
  43. Verifying : 1:nginx-all-modules-1.12.2-2.el7.noarch 8/13
  44. Verifying : 1:nginx-mod-http-geoip-1.12.2-2.el7.x86_64 9/13
  45. Verifying : 1:nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64 10/13
  46. Verifying : 1:nginx-mod-http-image-filter-1.12.2-2.el7.x86_64 11/13
  47. Verifying : 1:nginx-mod-http-perl-1.12.2-2.el7.x86_64 12/13
  48. Verifying : 1:nginx-mod-stream-1.12.2-2.el7.x86_64 13/13
  49. Installed:
  50. nginx.x86_64 1:1.12.2-2.el7
  51. Dependency Installed:
  52. gd.x86_64 0:2.0.35-26.el7 gperftools-libs.x86_64 0:2.6.1-1.el7 libXpm.x86_64 0:3.5.12-1.el7 libxslt.x86_64 0:1.1.28-5.el7
  53. nginx-all-modules.noarch 1:1.12.2-2.el7 nginx-filesystem.noarch 1:1.12.2-2.el7 nginx-mod-http-geoip.x86_64 1:1.12.2-2.el7 nginx-mod-http-image-filter.x86_64 1:1.12.2-2.el7
  54. nginx-mod-http-perl.x86_64 1:1.12.2-2.el7 nginx-mod-http-xslt-filter.x86_64 1:1.12.2-2.el7 nginx-mod-mail.x86_64 1:1.12.2-2.el7 nginx-mod-stream.x86_64 1:1.12.2-2.el7
  55. Complete!
  56. [root@VM_0_8_centos yum.repos.d]#

安装完成。

3、安装完成后,我们查看下nginx安装在什么目录

  1. [root@VM_0_8_centos yum.repos.d]# whereis nginx
  2. nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man3/nginx.3pm.gz /usr/share/man/man8/nginx.8.gz

Nginx配置路径为 /etc/nginx/
查看Nginx配置文件内容,执行命令cat /etc/nginx/nginx.conf

  1. [root@VM_0_8_centos nginx]# cat /etc/nginx/nginx.conf
  2. # For more information on configuration, see:
  3. # * Official English Documentation: http://nginx.org/en/docs/
  4. # * Official Russian Documentation: http://nginx.org/ru/docs/
  5. user nginx;
  6. worker_processes auto;
  7. error_log /var/log/nginx/error.log;
  8. pid /run/nginx.pid;
  9. # Load dynamic modules. See /usr/share/nginx/README.dynamic.
  10. include /usr/share/nginx/modules/*.conf;
  11. events {
  12. worker_connections 1024;
  13. }
  14. http {
  15. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  16. '$status $body_bytes_sent "$http_referer" '
  17. '"$http_user_agent" "$http_x_forwarded_for"';
  18. access_log /var/log/nginx/access.log main;
  19. sendfile on;
  20. tcp_nopush on;
  21. tcp_nodelay on;
  22. keepalive_timeout 65;
  23. types_hash_max_size 2048;
  24. include /etc/nginx/mime.types;
  25. default_type application/octet-stream;
  26. # Load modular configuration files from the /etc/nginx/conf.d directory.
  27. # See http://nginx.org/en/docs/ngx_core_module.html#include
  28. # for more information.
  29. include /etc/nginx/conf.d/*.conf;
  30. server {
  31. listen 80 default_server;
  32. listen [::]:80 default_server;
  33. server_name _;
  34. root /usr/share/nginx/html;
  35. # Load configuration files for the default server block.
  36. include /etc/nginx/default.d/*.conf;
  37. location / {
  38. }
  39. error_page 404 /404.html;
  40. location = /40x.html {
  41. }
  42. error_page 500 502 503 504 /50x.html;
  43. location = /50x.html {
  44. }
  45. }
  46. # Settings for a TLS enabled server.
  47. #
  48. # server {
  49. # listen 443 ssl http2 default_server;
  50. # listen [::]:443 ssl http2 default_server;
  51. # server_name _;
  52. # root /usr/share/nginx/html;
  53. #
  54. # ssl_certificate "/etc/pki/nginx/server.crt";
  55. # ssl_certificate_key "/etc/pki/nginx/private/server.key";
  56. # ssl_session_cache shared:SSL:1m;
  57. # ssl_session_timeout 10m;
  58. # ssl_ciphers HIGH:!aNULL:!MD5;
  59. # ssl_prefer_server_ciphers on;
  60. #
  61. # # Load configuration files for the default server block.
  62. # include /etc/nginx/default.d/*.conf;
  63. #
  64. # location / {
  65. # }
  66. #
  67. # error_page 404 /404.html;
  68. # location = /40x.html {
  69. # }
  70. #
  71. # error_page 500 502 503 504 /50x.html;
  72. # location = /50x.html {
  73. # }
  74. # }
  75. }
  76. [root@VM_0_8_centos nginx]#

通过nginx.conf文件,我们可以看到日志文件的路径
错误日志路径: /var/log/nginx/error.log
正常日志路径: /var/log/nginx/access.log

4、测试Nginx是否安装成功

执行nginx,启动nginx
执行nginx -t命令测试,如下,测试成功
执行ps aux | grep nginx查看nginx是否启动成功

  1. [root@VM_0_8_centos nginx]# nginx
  2. [root@VM_0_8_centos nginx]# nginx -t
  3. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  4. nginx: configuration file /etc/nginx/nginx.conf test is successful
  5. [root@VM_0_8_centos nginx]# ps aux | grep nginx
  6. root 445 0.0 0.2 120792 2100 ? Ss 15:57 0:00 nginx: master process nginx
  7. nginx 446 0.0 0.3 123264 3552 ? S 15:57 0:00 nginx: worker process
  8. root 546 0.0 0.0 112704 976 pts/0 R+ 15:58 0:00 grep --color=auto nginx
  9. [root@VM_0_8_centos nginx]#

nginx启动成功后,通过云服务器的IP访问,我们在浏览器看到这个页面

版权声明:本站文章除特别声明外,均采用署名-非商业性使用-禁止演绎 4.0 国际 许可协议,如需转载,请注明出处

评论

  •  
  • Linux rpm命令详解

    RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序

    发布:2019-08-02 阅读(77)

  • Linux中apt与apt-get命令的区别与解释

    Ubuntu 16.04 发布时,一个引人注目的新特性便是 apt 命令的引入。其实早在 2014 年,apt 命令就已经发布了第一个稳定版,只是直到 2016 年的 Ubuntu 16.04 系统发布时才开始引人关注。随着 apt install package 命令的使用频率和普遍性逐步超过 apt-get install package,越来越多的其它 Linux 发行版也开始遵循 Ubuntu 的脚步,开始鼓励用户使用 apt 而不是 apt-get。

    发布:2019-03-04 阅读(370)

  • centos7设置、查看、删除环境变量的方法

    centos查看环境变量与设置环境变量在使用过程中很常见,本文整理了一些常用的与环境变量相关的命令,感兴趣的朋友可以参考下希望对你有所帮助

    发布:2018-12-27 阅读(813)

  • linux CentOS 安装oh my Zsh

    linux cenos下安装oh my zsh,oh my zsh 用于管理zsh配置,使shell界面更美观。 Zsh 是一款功能强大终端(shell)软件,既可以作为一个交互式终端,也可以作为一个脚本解释器。它在兼容 Bash 的同时 (默认不兼容,除非设置成 emulate sh) 还有提供了很多改进。

    发布:2018-12-20 阅读(916)

  • linux云服务通过Nginx绑定多个域名

    通过Nginx反向代理,在一个云服务器上绑定多个域名,不同的域名访问不同的目录,端口

    发布:2018-10-11 阅读(426)

  • CentOS 7上安装Nginx

    如何在Linux(CentOS 7)上安装Nginx,作为反向代理服务器。

    发布:2018-10-11 阅读(514)

  • tail命令详情

    命令从指定点开始将 File 参数指定的文件写到标准输出。如果没有指定文件,那么会使用标准输入。Number 变量指定将多少单元写到标准输出。Number 变量的值可以是正的或负的整数。如果值的前面有 +(加号),从文件开头指定的单元数开始将文件写到标准输出。如果值的前面有 -(减号),那么从文件末尾指定的单元数开始将文件写到标准输出。如果值前面没有 +(加号)或 -(减号),那么从文件末尾指定的单元号开始读取文件

    发布:2017-05-23 阅读(472)