centos7 shell脚本一键安装nginx

【centos7 shell脚本一键安装nginx】相信大家在网上一搜,就能搜出很多这样的文章,但我这个不一样哦,我在脚本里加了些自定义的东西(如关闭版本号,修改Nginx版本头信息,nginx性能优化等等),可以不用修改直接就可以使用.
系统:centos 7.x(64位)
脚本内容:
cat /root/soft_shell/auto_install_nginx.sh
#!/bin/bashPATH=/bin:/sbin:/usr/bin:/usr/sbin:/opt/bin:/opt/sbin:~/binexport PATH # Check if user is rootif [ $(id -u) != "0" ]thenecho -e "Error: You must be root to run this script, please use root to install……"exit 1fi # Check the network statusNET_NUM=$(ping -c 4 www.baidu.com |awk '/packet loss/{print $6}' |sed -e 's/%//')if [ -z "$NET_NUM" ] || [ $NET_NUM -ne 0 ]thenecho -e "Error: Please check your internet……"exit 1fi # Check if selinux is Enforcingselinux_status=$(getenforce)if [ $selinux_status == "Enforcing" ]thensed -i 's/^SELINUX=.*$/SELINUX=disabled/' /etc/selinux/configsetenforce 0fi check_rpm=`rpm -qa wget|awk -F "-" '{print $1}'`if [ -z $check_rpm ];thenyum -y install wgetfi file_path=`pwd`nginx_version="1.14.2"read -p "Please input nginx display version infomation :" nginx_versionif [ "$nginx_version" = "" ];thennginx_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" ];thenuseradd -M -r -s /sbin/nologin nginxelseecho -e "33[31m user nginx already exists! 33[0m "fi if [ -s nginx-$nginx_version.tar.gz ];thenecho -e "33[31m nginx-$nginx_version.tar.gz find! 33[0m"elsewget http://nginx.org/download/nginx-$nginx_version.tar.gzfi if [ -s jemalloc-4.5.0.tar.bz2 ];thenecho -e "33[31m jemalloc [found]33[0m"elsewget http://download.slogra.com/tcmalloc/jemalloc-4.5.0.tar.bz2fi 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 ];thentar jxf jemalloc-4.5.0.tar.bz2cd jemalloc-4.5.0./configuremake -j4 && make installecho '/usr/local/lib' > /etc/ld.so.conf.d/local.confldconfigfi cd $file_path if [ ! -d ngx_brotli ];thengit clone https://github.com/google/ngx_brotlicd ngx_brotli/git submodule update --initcd -elseecho -e "33[31m brotli [found]33[0m"cd ngx_brotli/git submodule update --initcd -fi cd $file_pathtar zxf nginx-$nginx_version.tar.gzcd nginx-$nginx_version sed -i "/#define NGINX_VERSION/c #define NGINX_VERSION"${nginx_version}"" ./src/core/nginx.hsed -i "/#define NGINX_VER"nginx/" NGINX_VERSION/c #define NGINX_VER"${nginx_name}/" NGINX_VERSION" ./src/core/nginx.hsed -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.cerror_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.csed -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.dmkdir -p /var/lib/nginx/tmp echo '[Unit]Description=nginx - high performance web serverDocumentation=http://nginx.org/en/docs/After=network-online.target remote-fs.target nss-lookup.targetWants=network-online.target[Service]Type=forkingPIDFile=/var/run/nginx.pidExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.confExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.confExecReload=/bin/kill -s HUP $MAINPIDExecStop=/bin/kill -s TERM $MAINPID [Install]WantedBy=multi-user.target' > /usr/lib/systemd/system/nginx.service cat > /etc/nginx/nginx.conf <<-EOFuser 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_connections65535;multi_accept on;} http {includemime.types;default_typeApplication/octet-stream;log_formatmain'$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.logmain;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;sendfileon;tcp_nopushon;keepalive_timeout60;tcp_nodelay on;server_tokensoff;fastcgi_connect_timeout 300s;fastcgi_send_timeout 300s;fastcgi_read_timeout 300s;fastcgi_buffer_size 128k;fastcgi_buffers 8 128k;#8 128fastcgi_busy_buffers_size 256k;fastcgi_temp_file_write_size 256k;fastcgi_intercept_errors on;#hiden php versionfastcgi_hide_header X-Powered-By;proxy_connect_timeout300;proxy_read_timeout300;proxy_send_timeout300;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 {listen80 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 nginxdailyrotate 7missingoknotifemptynocompresssharedscriptspostrotate/bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || trueendscript}EOF systemctl daemon-reloadsystemctl enable nginxsystemctl start nginxecho -e "33[31m Install nginx success! 33[0m "


推荐阅读