相信大家在網上一搜,就能搜出很多這樣的文章,但我這個不一樣哦,我在腳本里加了些自定義的東西(如關閉版本號,修改Nginx版本頭信息,nginx性能優化等等),可以不用修改直接就可以使用.
系統:centos 7.x(64位)
腳本內容:
cat /root/soft_shell/auto_install_nginx.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/opt/bin:/opt/sbin:~/bin
export PATH
# Check if user is root
if [ $(id -u) != "0" ]
then
echo -e "Error: You must be root to run this script, please use root to install……"
exit 1
fi
# Check the network status
NET_NUM=$(ping -c 4 www.baidu.com |awk '/packet loss/{print $6}' |sed -e 's/%//')
if [ -z "$NET_NUM" ] || [ $NET_NUM -ne 0 ]
then
echo -e "Error: Please check your internet……"
exit 1
fi
# Check if selinux is Enforcing
selinux_status=$(getenforce)
if [ $selinux_status == "Enforcing" ]
then
sed -i 's/^SELINUX=.*$/SELINUX=disabled/' /etc/selinux/config
setenforce 0
fi
check_rpm=`rpm -qa wget|awk -F "-" '{print $1}'`
if [ -z $check_rpm ];then
yum -y install wget
fi
file_path=`pwd`
nginx_version="1.14.2"
read -p "Please input nginx display version infomation :" nginx_version
if [ "$nginx_version" = "" ];then
nginx_version="1.14.2"
fi
read -p "Please input display "server-name" infomation :" nginx_name
user_nginx=$(cat /etc/passwd |grep nginx|awk '{print $1}')
if [ -z "$user_nginx" ];then
useradd -M -r -s /sbin/nologin nginx
else
echo -e "33[31m user nginx already exists! 33[0m "
fi
if [ -s nginx-$nginx_version.tar.gz ];then
echo -e "33[31m nginx-$nginx_version.tar.gz find! 33[0m"
else
wget http://nginx.org/download/nginx-$nginx_version.tar.gz
fi
if [ -s jemalloc-4.5.0.tar.bz2 ];then
echo -e "33[31m jemalloc [found]33[0m"
else
wget http://download.slogra.com/tcmalloc/jemalloc-4.5.0.tar.bz2
fi
yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed GeoIP-devel libxslt libxslt-devel
if [ ! -s /usr/local/bin/jemalloc.sh ];then
tar jxf jemalloc-4.5.0.tar.bz2
cd jemalloc-4.5.0
./configure
make -j4 && make install
echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
ldconfig
fi
cd $file_path
if [ ! -d ngx_brotli ];then
git clone https://github.com/google/ngx_brotli
cd ngx_brotli/
git submodule update --init
cd -
else
echo -e "33[31m brotli [found]33[0m"
cd ngx_brotli/
git submodule update --init
cd -
fi
cd $file_path
tar zxf nginx-$nginx_version.tar.gz
cd nginx-$nginx_version
sed -i "/#define NGINX_VERSION/c #define NGINX_VERSION "${nginx_version}"" ./src/core/nginx.h
sed -i "/#define NGINX_VER "nginx/" NGINX_VERSION/c #define NGINX_VER "${nginx_name}/" NGINX_VERSION" ./src/core/nginx.h
sed -i "/static u_char ngx_http_server_string[] = "Server: nginx" CRLF;/c static u_char ngx_http_server_string[] = "Server: ${nginx_name} " CRLF;" ./src/http/ngx_http_header_filter_module.c
error_footer_len=$(expr $(grep -n "static u_char ngx_http_error_tail[] =" ./src/http/ngx_http_special_response.c|awk -F: '{print $1}') + 1 )
sed -i "${error_footer_len}d" ./src/http/ngx_http_special_response.c
sed -i "/static u_char ngx_http_error_tail[] =/a "<hr><center>${nginx_name}</center>" CRLF" ./src/http/ngx_http_special_response.c
export CFLAGS="-Werror"
./configure --user=nginx --group=nginx --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --add-module=../ngx_brotli --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --pid-path=/var/run/nginx.pid --with-ld-opt=-ljemalloc --lock-path=/var/lock/subsys/nginx --with-http_secure_link_module --with-http_random_index_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_perl_module --with-http_geoip_module --with-mail --with-mail_ssl_module --with-cc-opt=-O3 --with-cpu-opt=pentium --with-ld-opt="-Wl,-E"
make && make install
mkdir -p /etc/nginx/conf.d
mkdir -p /var/lib/nginx/tmp
echo '[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target' > /usr/lib/systemd/system/nginx.service
cat > /etc/nginx/nginx.conf <<-EOF
user nginx nginx;
#worker_processes $cpu_number;
worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 65535;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
use epoll;
accept_mutex off;
worker_connections 65535;
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" "$request_time"';
access_log /var/log/nginx/access.log main;
server_names_hash_bucket_size 128;
client_header_buffer_size 16k;
large_client_header_buffers 4 32k;
client_body_in_file_only clean;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
server_tokens off;
fastcgi_connect_timeout 300s;
fastcgi_send_timeout 300s;
fastcgi_read_timeout 300s;
fastcgi_buffer_size 128k;
fastcgi_buffers 8 128k;#8 128
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
#hiden php version
fastcgi_hide_header X-Powered-By;
proxy_connect_timeout 300;
proxy_read_timeout 300;
proxy_send_timeout 300;
gzip on;
gzip_min_length 1k;
gzip_buffers 16 64k;
gzip_http_version 1.0;
#gzip_disable "MSIE [1-5].";
gzip_comp_level 4;
gzip_types text/plain application/x-JAVAscript text/css application/xml image/gif image/jpg image/jpeg image/png;
gzip_vary on;
#proxy_hide_header Vary;
brotli on;
brotli_types text/xml text/plain application/json text/css image/svg application/font-woff application/vnd.ms-fontobject application/vnd.apple.mpegurl application/JavaScript image/x-icon image/jpeg image/gif image/png;
brotli_static on;
brotli_comp_level 6;
brotli_buffers 16 10k;
brotli_window 512k;
brotli_min_length 20;
server {
listen 80 default;
server_name _;
return 500;
}
include /etc/nginx/conf.d/*.conf;
}
EOF
cat > /etc/logrotate.d/nginx << EOF
/var/log/nginx/*log {
create 0644 nginx nginx
daily
rotate 7
missingok
notifempty
nocompress
sharedscripts
postrotate
/bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
endscript
}
EOF
systemctl daemon-reload
systemctl enable nginx
systemctl start nginx
echo -e "33[31m Install nginx success! 33[0m "
安裝完后效果圖如下:
nginx安裝完后效果圖
好了,大家有興趣的可以拿去試試.