基于centos7.9的多种方式部署nginx

简述

在工作中可能因为各种需求会涉及到nginx的不同安装方式,本文将采用yum安装、rpm包安装、编译安装三种方式简述nginx的安装过程。

centos7.9系统初始化设置请参考:centos7.9安装之初始化配置

1.yum方式安装

#1.下载依赖
sudo yum install yum-utils

#2. 配置最新稳定版
cat > /etc/yum.repos.d/nginx.repo << 'EOF'
[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
EOF

#3.清空yum源,安装最新版nginx
yum clean all
yum install nginx

#4. 安装完nginx版本查看
[root@nginx-test yum.repos.d]# which nginx
/usr/sbin/nginx
[root@nginx-test yum.repos.d]# nginx -V
nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (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@nginx-test yum.repos.d]# 

#5 查看默认安装路径
[root@nds-1 app]# find / -name nginx
/etc/logrotate.d/nginx
/etc/nginx
/var/log/nginx
/var/cache/nginx
/usr/sbin/nginx
/usr/lib64/nginx
/usr/share/nginx
/usr/libexec/initscripts/legacy-actions/nginx
[root@nds-1 app]# 

#6.启动
nginx
[root@nds-1 app]# netstat -ntlp |grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1619/nginx: master

访问验证:浏览器地址栏输入IP地址进行验证
在这里插入图片描述

2.rpm安装

#1 rpm包的获取
wget http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.20.1-1.el7.ngx.x86_64.rpm

#2 安装
rpm -ivh nginx-1.20.1-1.el7.ngx.x86_64.rpm

#3 查看安装版本
[root@nginx-test app]# nginx -v
nginx version: nginx/1.20.1

#4 查看都指定了哪些参数
[root@nginx-test app]# nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (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'


#5 查看默认安装路径
[root@nginx-test app]# find / -name nginx
/etc/logrotate.d/nginx
/etc/nginx
/var/lib/yum/repos/x86_64/7/nginx
/var/log/nginx
/var/cache/yum/x86_64/7/nginx
/var/cache/nginx
/usr/sbin/nginx
/usr/lib64/nginx
/usr/share/nginx
/usr/libexec/initscripts/legacy-actions/nginx
[root@nginx-test app]#

#6 启动(已自动配置变量,直接输入ginx即可启动)
nginx
[root@nginx-test home]# netstat -ntlp |grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      12209/nginx: master

#7nginx卸载
rpm -e nginx
 
#注解:
-i <套件档>或--install<套件档>  安装指定的套件档
-v  显示指令执行过程
-h  或--hash  套件安装时列出标记
-q  使用询问模式,当遇到任何问题时,rpm指令会先询问用户
-a  查询所有套件
-U <套件档>或--upgrade<套件档> 升级指定的套件档
-e <套件档>或--erase<套件档>  删除指定的套件

访问验证:浏览器地址栏输入IP地址进行验证
在这里插入图片描述

3.编译安装

安装包下载地址:下载
在这里插入图片描述

#1.安装前依赖下载并安装
yum -y install pcre pcre-devel  openssl openssl-devel gcc  gcc-c++ make zlib zlib-devel libtool

#2.创建
mkdir /opt/my_tools
cd /opt/my_tools
#3.下载
wget -c http://nginx.org/download/nginx-1.18.0.tar.gz

#4.解压
tar -xf nginx-1.18.0.tar.gz

#5.进入解压目录
cd nginx-1.18.0

#6.指定参数开始预编译(主要作用检查环境是否符合安装条件),如指定用户,安装路径,所需模块等
./configure --user=nginx --group=nginx  --prefix=/opt/nginx118 --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-threads --with-file-aio 

#8 编译并安装
make && make install

#9 进入安装目录并启动nginx
cd /opt/nginx118/sbin 
./nginx

#10 查看启动端口(有80端口则安装启动成功)
netstat -ntlp

#11 将nginx启动加入到环境变量,使得不论在哪个目录都可已直接使用“nginx”命令启动直接修改配置文件/etc/profile,修改完后直接source /etc/profile使得配置文件生效
#在配置问价末尾添加如下启动路径 PATH=$PATH:/opt/nginx118/sbin
echo "PATH=$PATH:/opt/nginx118/sbin" >> /etc/profile
[root@nginx-test /]# tail -1 /etc/profile
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/opt/nginx118/sbin

# 使得配置生效
[root@nginx-test /]# source /etc/profile

#随便在哪个目录都可以使用启动文件名直接执行即可启动
[root@nds-1 /]# nginx118
[root@nds-1 /]#  netstat -ntlp |grep nginx
tcp        0      0 0.0.0.0:81              0.0.0.0:*               LISTEN      10779/nginx: master


访问验证:浏览器地址栏输入IP地址进行验证
在这里插入图片描述
至此nginx的三种安装方式完成,可以愉快的使用nginx服务了,有问题欢迎留言探讨!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值