Linux环境安装Nginx自动化脚本

此脚本是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

原创文章,作者:admin,如若转载,请注明出处:https://hostingchat.cn/1093.html

(0)
admin的头像admin
上一篇 2020年4月6日
下一篇 2020年4月7日

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注