相信大家在網上一搜,就能搜出很多這樣的文章,但我這個不一樣哦,我在腳本里加了些自定義的東西(如關閉版本號,修改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" ]then echo -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 ]then echo -e "Error: Please check your internet……" exit 1fi # Check if selinux is Enforcingselinux_status=$(getenforce)if [ $selinux_status == "Enforcing" ]then sed -i 's/^SELINUX=.*$/SELINUX=disabled/' /etc/selinux/config setenforce 0fi check_rpm=`rpm -qa wget|awk -F "-" '{print $1}'`if [ -z $check_rpm ];then yum -y install wgetfi file_path=`pwd`nginx_version="1.14.2"read -p "Please input nginx display version infomation :" nginx_versionif [ "$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 nginxelse echo -e "\033[31m user nginx already exists! \033[0m "fi if [ -s nginx-$nginx_version.tar.gz ];then echo -e "\033[31m nginx-$nginx_version.tar.gz find! \033[0m"else wget http://nginx.org/download/nginx-$nginx_version.tar.gzfi if [ -s jemalloc-4.5.0.tar.bz2 ];then echo -e "\033[31m jemalloc [found]\033[0m"else wget 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 ];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 ldconfigfi 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 "\033[31m brotli [found]\033[0m" cd ngx_brotli/ git submodule update --init cd -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_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-reloadsystemctl enable nginxsystemctl start nginxecho -e "\033[31m Install nginx success! \033[0m "
安裝完後效果圖如下:
nginx安裝完後效果圖
好了,大家有興趣的可以拿去試試.
最新評論