CentOS自动安装nginx

本文介绍了如何在CentOS上自动安装nginx,包括从下载nginx.rar到根据readme.txt进行安装,以及如何通过shell命令启动、停止服务。安装完成后,nginx配置文件位于/etc/nginx。
摘要由CSDN通过智能技术生成

下载 nginx.rar 按照 readme.txt 操作即可

首次安装自动下载

开启:service nginx start

关闭:service nginx stop

安装位置:/etc/nginx


贴出shell文件:


#!/bin/sh

#关闭进程
killpids(){
  pids=($(ps -ef | grep $1 | awk '{print $2}'))
  for pid in ${pids[@]}; do
    echo "关闭进程: pid ${pid}"
    kill -9 ${pid}
  done
}
killpids nginx

#uninstall old rpms
rmrpms(){
  oldRpms=($(rpm -qa|grep $1))
  for oldRpm in ${oldRpms[@]}; do
    echo "旧版$1: ${oldRpm}"
    echo "删除..."
    yum -y remove $1 ${oldRpm}
  done
}
rmrpms nginx

#删除
rm -rf /etc/rc.d/init.d/nginx
rm -rf /etc/logrotate.d/nginx
rm -rf /etc/sysconfig/nginx
rm -rf /etc/nginx
rm -rf /usr/lib64/nginx
rm -rf /usr/sbin/nginx
rm -rf /usr/libexec/initscripts/legacy-actions/nginx
rm -rf /usr/share/nginx
rm -rf /var/log/nginx
rm -rf /var/cache/nginx


#install nginx
echo "安装依赖"
yum install -y pcre pcre-devel  
yum install -y zlib zlib-devel  
yum install -y openssl openssl-devel
yum install -y python-daemon
#查看CentOS版本号
centosVersion=$(rpm -q centos-release)
echo "CentOS版本:$centosVersion"
if [[ $centosVersion =~ "-7-" ]]
  then
    echo "安装包:nginx-nr-agent-2.0.0-11.el7.ngx.noarch.rpm"
	# http://nginx.org/packages/centos/7/noarch/RPMS/nginx-nr-agent-2.0.0-11.el7.ngx.noarch.rpm
	rpm -ivh nginx-nr-agent-2.0.0-11.el7.ngx.noarch.rpm
  else
    echo "安装包:nginx-nr-agent-2.0.0-11.el6.ngx.noarch.rpm"
	# http://nginx.org/packages/centos/6/noarch/RPMS/nginx-nr-agent-2.0.0-11.el6.ngx.noarch.rpm
	rpm -ivh nginx-nr-agent-2.0.0-11.el6.ngx.noarch.rpm
fi

#设置YUM源
repo='/etc/yum.repos.d/nginx.repo'
rm -rf $repo
touch $repo
echo '[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1' >> $repo

#安装
yum -y install nginx

#启动并查看状态
service nginx start
service nginx status

#开机启动
echo ""
echo "开机启动"
chkconfig nginx on

#修改端口号
echo "端口号: 80"

#显示版本
echo "version:"
rpm -qa|grep nginx
echo ""

#代理端口
echo '若要代理8080端口,执行如下命令:
\cp -f /usr/nginx/default.conf /etc/nginx/conf.d/'
echo ""




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值