Nginx安装部署不同方式

本文详细介绍了如何在 CentOS7.6 系统中通过 YUM 和编译两种方式安装 Nginx 1.20.2 版本。首先,讲解了使用 YUM 安装的步骤,包括添加官方源和 epel 源,然后列出不同版本并选择安装。接着,展示了编译安装的流程,包括下载源码、安装依赖、配置编译选项及启动服务。最后,提到了一些常见错误及其解决方案。
摘要由CSDN通过智能技术生成

Linux环境中安装nginx(1.20.2),系统版本centos7.6
Nginx 1.20.2 安装
安装方式如下:

YUM安装

yum安装:官方源安装,epol安装 二选一

添加YUM源

官方源安装
链接: http://nginx.org/en/linux_packages.html#RHEL-CentOS 可以对照自己的系统进行添加

[root@master-node ~]# 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

epol安装 (不建议,只能默认版本)

[root@master-node ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

加载yum包

[root@master-node ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base extras nginx-stable updates
Cleaning up list of fastest mirrors
Other repos take up 609 k of disk space (use --verbose for details)
[root@master-node ~]# yum makecache

安装指定版本

[root@master-node ~]# yum list nginx --showduplicates
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.nju.edu.cn
 * extras: mirrors.nju.edu.cn
 * updates: mirrors.nju.edu.cn
Available Packages
nginx.x86_64           1:1.8.0-1.el7.ngx              nginx-stable
·········
nginx.x86_64           1:1.20.1-1.el7.ngx             nginx-stable
nginx.x86_64           1:1.20.2-1.el7.ngx             nginx-stable
nginx.x86_64           1:1.22.0-1.el7.ngx             nginx-stable

[root@master-node ~]# yum -y install nginx-1.20.2-1.el7.ngx

查看版本

[root@master-node ~]# nginx -v
nginx version: nginx/1.20.2
[root@master-node ~]# rpm -qi nginx
Name        : nginx
Epoch       : 1
Version     : 1.20.2
·········
nginx [engine x] is an HTTP and reverse proxy server, as well as
a mail proxy server.

启动nginx

启动nginx 和 开机自启

[root@master-node ~]# systemctl start nginx
[root@master-node ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

默认配置

[root@master-node ~]# rpm -qc nginx
/etc/logrotate.d/nginx
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params

基本操作

# 检测nginx配置是否正确
[root@master-node ~]# nginx -t
nginx: the configuration file /etc/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/conf/nginx.conf test is successful

# 查看nginx进程 并实现不中断服务加载配置
[root@master-node ~]# ps -ef|grep nginx
root     12483     1  0 15:52 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx    12484 12483  0 15:52 ?        00:00:00 nginx: worker process
nginx    12485 12483  0 15:52 ?        00:00:00 nginx: worker process
nginx    12486 12483  0 15:52 ?        00:00:00 nginx: worker process
nginx    12487 12483  0 15:52 ?        00:00:00 nginx: worker process
root     12553 12296  0 15:56 pts/0    00:00:00 grep --color=auto nginx
[root@master-node ~]# kill -HUP 12483   

[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.18.0-1.el7.ngx.x86_64.rpm // rpm方式升级并安装某个版本的Nginx

编译安装

编译安装优势

  • 能够实现定制功能,需要什么功能就可以使用参数加上
  • 可以指定安装的路径

获取安装包

下载安装包并解压

[root@slave-node opt]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
[root@slave-node opt]# tar -zxvf nginx-1.20.2.tar.gz

安装依赖包

[root@slave-node opt]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make

创建用户、组

[root@slave-node opt]# useradd -M -s /sbin/nologin nginx

编译安装

安装路径/etc/nginx

[root@slave-node nginx-1.20.2]# cd /opt/nginx-1.20.2
[root@slave-node nginx-1.20.2]# ./configure --help
--with-http_ssl_module # 配置HTTPS时使用
--with-http_v2_module # 配置GOLANG语言时使用
--with-stream # 启用TCP/UDP代理服务

[root@slave-node nginx-1.20.2]# ./configure --prefix=/etc/nginx --user=nginx --group=nginx --with-http_stub_status_module

./configure \
--prefix=/etc/nginx \				#指定nginx的安装路径
--user=nginx \										#指定用户名
--group=nginx \										#指定组名
--with-http_stub_status_module						#启用 http_stub_status_module 模块以支持状态统计

[root@slave-node nginx-1.20.2]# make && make install

#让系统识别nginx的操作命令
[root@slave-node nginx-1.20.2]# ln -s /etc/nginx/sbin/nginx /usr/local/sbin/

添加系统服务

[root@slave-node nginx]# vi /lib/systemd/system/nginx.service

[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/etc/nginx/logs/nginx.pid
ExecStart=/etc/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

#赋权,除了root以外的用户都不能修改
[root@slave-node nginx]# chmod 754 /lib/systemd/system/nginx.service

启动nginx

启动nginx 和 开机自启

[root@master-node ~]# systemctl start nginx
[root@master-node ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

常见错误

  • ./configure: error: the HTTP rewrite module requires the PCRE library.
yum install pcre pcre-devel -y	
  • ./configure: error: SSL modules require the OpenSSL library.
yum install openssl openssl-devel -y 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值