Centos服务器编译安装Nginx-1.24.0

Centos服务器编译安装Nginx-1.24.0

1、下载源码包
#官方下载地址页面:
http://nginx.org/en/download.html

http://nginx.org/download/nginx-1.24.0.tar.gz
2、安装依赖

这些依赖根据需求安装,也可以./config的时候根据报错提示一个一个安装

yum install -y --setopt=protected_multilib=false gcc gcc-c++ make cmake automake autoconf gd file bison patch mlocate flex diffutils zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel libcurl libcurl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel  kernel-devel libtool-libs readline-devel gettext-devel libcap-devel php-mcrypt libmcrypt libmcrypt-devel recode-devel 

问题:yum install libmcrypt libmcrypt-devel mcrypt mhash -y

报错:No package libmcrypt available.

解决方法:yum install epel-release //扩展包更新包

3、创建用户组
#创建nginx用户
groupadd nginx 
useradd -g nginx -s /sbin/nologin -M nginx
4、解压文件 并生成配置文件
tar zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0

./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-stream --with-stream_ssl_preread_module --with-stream_ssl_module --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-openssl=/usr/local/openssl
–with-stream 4层转发
–with-http_ssl_module ssl协议支持
–with-stream_ssl_preread_module
–with-stream_ssl_module
--with-http_stub_status_module
--with-http_gzip_static_module 
--with-http_realip_module
5、编译安装
make && make install
6、配置启动脚本(启动文件个人编辑)
cp /opt/nginx-1.24.0/nginx  /etc/rc.d/init.d/
chmod 744 /etc/rc.d/init.d/nginx
chkconfig nginx on
service nginx start

nginx启动脚本内容:

#!/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/logs/$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
7、配置nignx文件及项目安装目录
# 用于存放nginx日志
mkdir -p /var/log/nginx
# 用于存放nginx配置文件
mkdir /usr/local/nginx/conf/conf.d
# 日志切割,配置文件等,具体模板可以
cp /opt/nginxlog /etc/logrotate.d/
cp /opt/nginx.conf /usr/local/nginx/conf/
cp /opt/web.conf /usr/local/nginx/conf/conf.d/
cp /opt/conf.common /usr/local/nginx/conf/conf.d/

模板文件

nginxlog文件

/var/log/nginx/*log {
    daily
    rotate 10
    missingok
    notifempty
    compress
    sharedscripts
    postrotate
        /bin/kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid 2>/dev/null) 2>/dev/null || :
    endscript
}

nginx.conf

user  nginx nginx;
worker_processes  1;

error_log   /var/log/nginx/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  16000;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    		'$http_x_forwarded_for ' 
                    '$upstream_addr $upstream_response_time $request_time '
                     '$http_host $request '
                     '"$status" $body_bytes_sent "$http_referer" '
                     '"$http_accept_language" "$http_user_agent" ';

   access_log     /var/log/nginx/local-access.log  main;
    #connlimitzone config 
    map $http_x_forwarded_for $clientRealIp {
    ""	$remote_addr;
    	~^(?P<firstAddr>[0-9\.]+),?.*$	$firstAddr;
    }
    limit_req_zone $binary_remote_addr zone=connlimit:100m rate=30r/s; 
    #limit_req_zone $clientRealIp zone=forwarded:100m rate=30r/s;
    limit_req_status 599;
    fastcgi_intercept_errors on;
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_comp_level 2;
    gzip_types text/plain application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary off;
    gzip_disable "MSIE [1-6]\.";
    upstream phpfpm {
        server 127.0.0.1:9000;
    }	
    include "conf.d/*.conf";

}

web.conf

server {
        listen       80; 
        server_name  test.cn;  #域名
        access_log /var/log/nginx/web-access.log main;
        error_log /var/log/nginx/web-error.log warn;
        root /data/www/;   #代码根目录
	    include  conf.d/conf.common;
}

conf.common

index index.php index.html index.htm;
location ~ /\. {
        deny all;
}

location ~ /(protected|yii){
        deny all;
}

location ~ /themes/\w+/views{
        deny all;
}

location ~(favicon.ico){
        log_not_found off;
        expires 99d;
        break;
}   

location ~ \.(jpg|gif|png|jpeg|css|js|mp4|mp3|ogg|wmv|swf){
        if (!-e $request_filename){
                break;
        }   
}   

if (!-e $request_filename){
        rewrite /(.*) /index.php/$1 last;
}

location ~ \.php {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass phpfpm;
	client_max_body_size 100m; 
        client_body_buffer_size 2048k;
        fastcgi_buffer_size 1024k; 
        fastcgi_buffers 6 256k; 
        fastcgi_busy_buffers_size 1024k;
}

你可能感兴趣的:(nginx,服务器,centos)