Linux环境安装Nginx自动化脚本

小柒博客 评论65,3352字数 444阅读1分28秒阅读模式

此脚本是Nginx安装脚本,有需要朋友可以参考,脚本内容如下:

系统环境:CentOS 7.9

软件版本:1.16.1(最新稳定版)

[root@localhost ~]# vim auto_install_nginx.sh

#!/bin/bash
#2020-3-13 10:22:30
#By Author YangXingZhen
#Auto Install Nginx Soft

#Define Nginx path variables
NGINX_URL=http://nginx.org/download
NGINX_FILE=nginx-1.16.1.tar.gz
NGINX_FILE_DIR=nginx-1.16.1
NGINX_PREFIX=/usr/local/nginx
 
#Install Nginx Soft
if [ ! -d ${NGINX_PREFIX} ];then
	#Install Package
	yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++ wget
	wget -c ${NGINX_URL}/${NGINX_FILE}
	tar zxf ${NGINX_FILE}
	cd ${NGINX_FILE_DIR}
	sed -i 's/1.16.1/ /;s/nginx\//nginx/' src/core/nginx.h
	useradd -s /sbin/nologin www
	./configure --prefix=${NGINX_PREFIX} \
	--user=www \
	--group=www \
	--with-http_ssl_module \
	--with-http_stub_status_module
	if [ $? -eq 0 ];then
		make && make install
		echo -e "\033[32mThe Nginx Install Success...\033[0m"
	else
		echo -e "\033[31mThe Nginx Install Failed...\033[0m"
		exit 1
	fi
else
	echo -e "\033[31mThe Nginx already Install...\033[0m"
	exit 1
fi

#Config Nginx
ln -sf ${NGINX_PREFIX}/sbin/nginx /usr/sbin
cat >${NGINX_PREFIX}/conf/nginx.conf <<EOF
	user www www;
	worker_processes auto;
	pid /usr/local/nginx/logs/nginx.pid;
events {
	use epoll;
	worker_connections 10240;
	multi_accept on;
	}
http	{
	include       mime.types;
	default_type  application/octet-stream;
	log_format  main  '\$remote_addr - \$remote_user [\$time_local] "\$request" '
                      '\$status \$body_bytes_sent "\$http_referer" '
                      '"\$http_user_agent" "\$http_x_forwarded_for"';
	access_log logs/access.log main;
	error_log logs/error.log warn;
	sendfile        on;
	tcp_nopush          on;
	keepalive_timeout  120;
	tcp_nodelay         on;
	server_tokens off;
	gzip    on;
	gzip_min_length 1k;
	gzip_buffers    4 64k;
	gzip_http_version 1.1;
	gzip_comp_level 4;
	gzip_types      text/plain application/x-javascript text/css application/xml;
	gzip_vary       on;
	client_max_body_size 10m;
	client_body_buffer_size 128k;
	proxy_connect_timeout 90;
	proxy_send_timeout 90;
	proxy_buffer_size 4k;
	proxy_buffers 4 32k;
	proxy_busy_buffers_size 64k;
	large_client_header_buffers 4 4k;
	client_header_buffer_size 4k;
	open_file_cache_valid 30s;
	open_file_cache_min_uses 1;
server {
	listen	80;
	server_name	localhost;
	location / {
	root	html;
	index index.html index.htm;
	}
  }
}
EOF

#Start Nginx
${NGINX_PREFIX}/sbin/nginx -t >/dev/null 2>&1
if [ $? -eq 0 ];then
	${NGINX_PREFIX}/sbin/nginx
fi

#Add power on self start
grep -qw "${NGINX_PREFIX}" /etc/rc.d/rc.local
if [ $? -ne 0 ];then
	echo "${NGINX_PREFIX}/sbin/nginx" >>/etc/rc.d/rc.local
	chmod +x /etc/rc.d/rc.local
fi

脚本执行方式:

[root@localhost ~]# sh auto_install_nginx.sh

若文章图片、下载链接等信息出错,请在评论区留言反馈,博主将第一时间更新!如本文“对您有用”,欢迎随意打赏,谢谢!

继续阅读
Wechat
微信扫一扫,加我!
weinxin
微信号已复制
微信公众号
微信扫一扫,关注我!
weinxin
公众号已复制
Shell最后更新:2024-1-24
小柒博客
  • 本文由 小柒博客 发表于 2020年4月7日 16:26:33
  • 声明:本站所有文章,如无特殊说明或标注,本站文章均为原创。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。转载请务必保留本文链接:https://www.yangxingzhen.com/6682.html
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

拖动滑块以完成验证