Nginx CentOS7下的安装配置

Nginx CentOS7下的安装配置

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

1. 使用yum方式安装

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

[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

然后执行安装

sudo yum -y install nginx

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


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

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

完毕!

测试一下

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

2. 下载源码自行安装

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

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

yum -y install gcc

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

yum -y install pcre pcre-devel openssl openssl-devel

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

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

tar -zxvf nginx-1.19.3.tar.gz
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

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

配置完成后如下显示


Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/run/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

进行编译并安装

make && make install

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

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

至此,安装完毕。

nginx加入systemd管理

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

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

[Unit]  # systemd把服务视为一个个单元,该处配置了单元的描述和启动顺序以及帮助文档
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=notify   # 配置了服务启动行为
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND   # 配置了启动命令
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful   # 配置了重启命令
ExecStop=/bin/kill -WINCH ${MAINPID}              # 配置了关闭命令
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true

[Install]
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 文件

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

输入如下内容

[Unit]
Description=nginx
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit

[Install]
WantedBy=multi-user.target

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

生成执行文件的软连接

方便nginx的命令调用

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

3. Nginx的启动和开机自启

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

systemctl daemon-reload

启动nginx

systemctl start nginx

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

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

输入 ip 访问
在这里插入图片描述

配置开机自启

[root@localhost sbin]# systemctl enable nginx
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的安装和配置结束

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值