CentOS编译安装Nginx
#!/bin/sh # Description: 安装nginx-1.12.1 SOFTWARE_DIR='/root/' # ERROR Output error_echo(){ printf " 33[31m $* 33[0m " } # 安装nginx依赖包 nginx_package_install(){ cd ${SOFTWARE_DIR} && tar zxf zlib-1.2.11.tar.gz -C /usr/src/ cd ${SOFTWARE_DIR} && tar zxf pcre-8.40.tar.gz -C /usr/src/ cd ${SOFTWARE_DIR} && tar zxf openssl-1.0.2.tar.gz -C /usr/src/ } # 创建www用户 www_user_create(){ groupadd www useradd -s /sbin/nologin -M -g www www mkdir -p /data/web/ } # 安装nginx nginx_install(){ cd ${SOFTWARE_DIR} && tar zxf nginx-1.12.1.tar.gz -C /usr/src/ && cd /usr/src/nginx-1.12.1/ ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_v2_module --with-http_ssl_module --with-http_sub_module --with-http_realip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --with-zlib=/usr/src/zlib-1.2.11/ --with-pcre=/usr/src/pcre-8.40/ --with-openssl=/usr/src/openssl-1.0.2/ --pid-path=/usr/local/nginx/nginx.pid make && make install } # 配置nginx nginx_config(){ cd ${SOFTWARE_DIR} mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak cp -r nginx.conf /usr/local/nginx/conf/ && dos2unix /usr/local/nginx/conf/nginx.conf && ln -sf /usr/local/nginx/conf/nginx.conf /etc/nginx.conf rm -rf /usr/local/nginx/logs/ && mkdir -p /data/wwwlogs/ && ln -sf /data/wwwlogs /usr/local/nginx/logs mkdir -p /usr/local/nginx/conf/vhost/ ln -sf /usr/local/nginx/sbin/nginx /usr/bin/ } # 配置nginx启动脚本 nginx_init(){ cp -r nginx.init /etc/init.d/nginx && dos2unix /etc/init.d/nginx chmod a+x /etc/init.d/nginx chkconfig --add nginx && chkconfig --level 2345 nginx on } main(){ nginx_package_install; www_user_create; nginx_install; nginx_config; nginx_init; /usr/bin/nginx -t if [ $? -eq 0 ]; then /etc/init.d/nginx start echo "nginx install successfully !!!" else error_echo "nginx install failed !!!" fi } main
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
user www www; pid /usr/local/nginx/nginx.pid; worker_processes 4; worker_cpu_affinity 0001 0010 0100 1000; worker_processes auto; worker_rlimit_nofile 655350; worker_rlimit_core 800000000; events { use epoll; worker_connections 51200; multi_accept on; } http { include mime.types; default_type application/octet-stream; charset UTF-8; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 50m; client_body_buffer_size 128k; sendfile on; tcp_nopush on; keepalive_timeout 45; server_tokens off; tcp_nodelay on; # Proxy proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # Fastcgi fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 256k; # Gzip Compression gzip on; gzip_buffers 16 8k; gzip_comp_level 2; gzip_http_version 1.1; gzip_min_length 256; gzip_proxied any; gzip_vary on; gzip_types # MIME Type text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml text/javascript application/javascript application/x-javascript text/x-json application/json application/x-web-app-manifest+json text/css text/plain text/x-component font/opentype application/x-font-ttf application/vnd.ms-fontobject image/x-icon; gzip_disable "msie6"; # Cache open_file_cache max=51200 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 1; open_file_cache_errors on; # Log Format log_format access '$remote_addr - $remote_user [$time_local] $host ' '"$request" $status $body_bytes_sent "$request_body" $request_time ' '"$http_referer" "$http_user_agent" $http_x_forwarded_for ' '$upstream_addr $upstream_response_time'; ################################################## default ################################################## server { listen 8888; server_name 127.0.0.1; location /nginx_status { stub_status on; access_log off; } } server { listen 80 default; server_name 127.0.0.1; access_log /usr/local/nginx/logs/access.log access; error_log /usr/local/nginx/logs/error.log error; root /data/web/; index index.html index.htm index.php; location ~ [^/].php(/|$) { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } location = /favicon.ico { log_not_found off; access_log off; } location ~ .*.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ { expires 30d; access_log off; } location ~ .*.(js|css)?$ { expires 7d; access_log off; } if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; break; } } ################################################## vhost ################################################## include vhost/*.conf; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/bin/sh # chkconfig: 2345 80 20 # Description: Start and Stop Nginx # Provides: nginx # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin NAME=nginx NGINX_BIN=/usr/local/nginx/sbin/$NAME CONFIGFILE=/usr/local/nginx/conf/$NAME.conf PIDFILE=/usr/local/nginx/$NAME.pid SCRIPTNAME=/etc/init.d/$NAME case "$1" in start) echo -n "Starting $NAME... " if netstat -tnpl | grep -q nginx;then echo "$NAME (pid `pidof $NAME`) already running." exit 1 fi $NGINX_BIN -c $CONFIGFILE if [ "$?" != 0 ] ; then echo " failed" exit 1 else echo " done" fi ;; stop) echo -n "Stoping $NAME... " if ! netstat -tnpl | grep -q nginx; then echo "$NAME is not running." exit 1 fi $NGINX_BIN -s stop if [ "$?" != 0 ] ; then echo " failed. Use force-quit" exit 1 else echo " done" fi ;; status) if netstat -tnpl | grep -q nginx; then PID=`pidof nginx` echo "$NAME (pid $PID) is running..." else echo "$NAME is stopped" exit 0 fi ;; force-quit) echo -n "Terminating $NAME... " if ! netstat -tnpl | grep -q nginx; then echo "$NAME is not running." exit 1 fi kill `pidof $NAME` if [ "$?" != 0 ] ; then echo " failed" exit 1 else echo " done" fi ;; restart) $SCRIPTNAME stop sleep 1 $SCRIPTNAME start ;; reload) echo -n "Reload service $NAME... " if netstat -tnpl | grep -q nginx; then $NGINX_BIN -s reload echo " done" else echo "$NAME is not running, can't reload." exit 1 fi ;; configtest) echo -n "Test $NAME configure files... " $NGINX_BIN -t ;; *) echo "Usage: $SCRIPTNAME {start|stop|force-quit|restart|reload|status|configtest}" exit 1 ;; esac