Nginx官网:http://nginx.org/

1. 使用yum方式安装

  1. 设置nginx 的 yum仓库地址。 在 /etc/yum.repos.d/ 目录下创建 nginx.repo 文件
    1. sudo vi /etc/yum.repos.d/nginx.repo
    内容如下: ```bash [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

  1. 然后执行安装
  2. ```bash
  3. sudo yum -y install nginx

看到如下信息表示安装完成

  1. ----------------------------------------------------------------------
  2. 验证中 : 1:nginx-1.18.0-1.el7.ngx.x86_64 1/1
  3. 已安装:
  4. nginx.x86_64 1:1.18.0-1.el7.ngx
  5. 完毕!

测试一下

  1. [root@localhost ~]# nginx -v
  2. nginx version: nginx/1.18.0

2. 下载源码自行安装

访问官网 自行选择对应版本下载 http://nginx.org/en/download.html
这里我下载的是 1.19.3 版本

源码安装需要进行编译,安装c 语言编译器 gcc

  1. yum -y install gcc

同时nginx默认安装的http_rewrite_module 需要PCRE库支持,安装PCRE库。 http_ssl_module 需要openssl支持

  1. yum -y install pcre pcre-devel openssl openssl-devel

上诉软件都可以到对应官网进行下载手动安装,为了方便操作,这里不多描述。

安装包上传到服务器,解压缩后进入文件夹

  1. tar -zxvf nginx-1.19.3.tar.gz
  2. cd nginx-1.19.3

运行 configure 脚本进行安装前的配置, 如下配置

  • —prefix 指定了安装目录
  • —pid-path 指定了nginx pid的生成路径
  • —with-http_ssl_module 添加了http的ssl模块,也就是nginx对于https的支持

其他配置项和模块如需加载可以参阅官网 http://nginx.org/en/docs/configure.html

  1. ./configure --prefix=/usr/local/nginx --pid-path=/run/nginx.pid --with-http_ssl_module

配置完成后如下显示

  1. Configuration summary
  2. + using system PCRE library
  3. + using system OpenSSL library
  4. + using system zlib library
  5. nginx path prefix: "/usr/local/nginx"
  6. nginx binary file: "/usr/local/nginx/sbin/nginx"
  7. nginx modules path: "/usr/local/nginx/modules"
  8. nginx configuration prefix: "/usr/local/nginx/conf"
  9. nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  10. nginx pid file: "/run/nginx.pid"
  11. nginx error log file: "/usr/local/nginx/logs/error.log"
  12. nginx http access log file: "/usr/local/nginx/logs/access.log"
  13. nginx http client request body temporary files: "client_body_temp"
  14. nginx http proxy temporary files: "proxy_temp"
  15. nginx http fastcgi temporary files: "fastcgi_temp"
  16. nginx http uwsgi temporary files: "uwsgi_temp"
  17. nginx http scgi temporary files: "scgi_temp"

进行编译并安装

  1. make && make install

安装完成后输入 /usr/local/nginx/sbin/nginx -v 命令进行测试

  1. [root@localhost nginx]# /usr/local/nginx/sbin/nginx -v
  2. nginx version: nginx/1.19.3

至此,安装完毕。

nginx 加入systemd管理

但是为了方便我们的管理,手动安装和yum安装还是有不少区别,我们需要进行进一步的配置。

yum安装的服务会自行加入systemd管理,也就是使用 我们CentOS 7 之后替代service 命令的 systemctl 命令来开启,重启和加入自启,而自行安装的服务不会自动加入systemd管理,为了方便后续的管理,我们要手动添加nginx的 .service 文件,也就是systemd下服务的配置文件,该文件用来配置服务的启动关闭和重启命令等 。简单查看其中一个service文件即可明白。如下是 httpd 的service文件。

  1. [Unit] # systemd把服务视为一个个单元,该处配置了单元的描述和启动顺序以及帮助文档
  2. Description=The Apache HTTP Server
  3. After=network.target remote-fs.target nss-lookup.target
  4. Documentation=man:httpd(8)
  5. Documentation=man:apachectl(8)
  6. [Service]
  7. Type=notify # 配置了服务启动行为
  8. EnvironmentFile=/etc/sysconfig/httpd
  9. ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND # 配置了启动命令
  10. ExecReload=/usr/sbin/httpd $OPTIONS -k graceful # 配置了重启命令
  11. ExecStop=/bin/kill -WINCH ${MAINPID} # 配置了关闭命令
  12. # We want systemd to give httpd some time to finish gracefully, but still want
  13. # it to kill httpd after TimeoutStopSec if something went wrong during the
  14. # graceful stop. Normally, Systemd sends SIGTERM signal right after the
  15. # ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
  16. # httpd time to finish.
  17. KillSignal=SIGCONT
  18. PrivateTmp=true
  19. [Install]
  20. WantedBy=multi-user.target

关于 .service 文件的描述可以自行查阅相关文献和网络资源。

如果我们使用 find / -name “*.service” 命令查询.service 文件 ,会发现有两个目录下存放了这些文件。

  • /usr/lib/systemd/system/ 这是配置文件的常规存放位置
  • /etc/systemd/system/multi-user.target.wants 该目录中存放都是 /usr/lib/systemd/system/ 中.service 文件的软链接。当我们将服务使用 systemctl enable 命令加入到自启项时,centos 会把相关服务的service文件在改目录中建立一个软连接。因此该目录是自启服务的存放目录。要注意不同target软连接生成的target.wants 目录也不相同

因此我们需要进入 /usr/lib/systemd/system/ 目录下 创建 nginx.service 文件

  1. vi /usr/lib/systemd/system/nginx.service

输入如下内容

  1. [Unit]
  2. Description=nginx
  3. Documentation=http://nginx.org/en/docs/
  4. After=network-online.target remote-fs.target nss-lookup.target
  5. Wants=network-online.target
  6. [Service]
  7. Type=forking
  8. ExecStart=/usr/local/nginx/sbin/nginx
  9. ExecReload=/usr/local/nginx/sbin/nginx -s reload
  10. ExecStop=/usr/local/nginx/sbin/nginx -s quit
  11. [Install]
  12. WantedBy=multi-user.target

完成上述配置后,手动安装的nginx也可以使用systemctl 命令进行服务的管理了。

生成执行文件的软连接

方便nginx的命令调用

  1. ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx

3. Nginx的启动和开机自启

如果是手动安装的,在配置完nginx.service 文件后建议先重新加载一下systemd单元配置

  1. systemctl daemon-reload

启动nginx

  1. systemctl start nginx

如果有使用防火墙,防火墙开放80端口

  1. firewall-cmd --zone=public --add-port=80/ctp --permanent
  2. firewall-cmd --reload

输入 ip 访问
image.png
配置开机自启

  1. [root@localhost sbin]# systemctl enable nginx
  2. Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

可以看到系统提示在/etc/systemd/system/multi-user.target.wants/ 下创建了 nginx.service 文件的软连接。

到这里nginx的安装和配置结束