/ 中存储网

CentOS编译方式安装Nginx服务器

2015-11-07 16:08:39 来源:kejihao

1. 安装nginx相关的依赖包

yum -y install gcc openssl-devel pcre-devel zlib-devel pcre-devel libtool gcc-c++

2. 下载安装nginx

wget http://nginx.org/download/nginx-1.2.4.tar.gz

tar zxvf nginx-1.2.4.tar.gz

cd nginx-1.2.4

./configure --help |more    #查看编译的参数选项

useradd www

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

make && make install

3. 设定nginx配置文件

vi /usr/local/nginx/conf/nginx.conf

user  www www;

worker_processes 8;

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

pid        /var/run/nginx.pid;

error_log  /usr/local/nginx/logs/error.log  crit;

worker_rlimit_nofile 65535;

events

{

use epoll;

worker_connections 65535;

}

http

{

include      mime.types;

default_type  application/octet-stream;

server_names_hash_bucket_size 128;

keepalive_timeout 0;

client_header_buffer_size    16k;

client_max_body_size        32m;

large_client_header_buffers  8 1024k;

client_header_timeout  3m;

client_body_timeout    3m;

send_timeout          3m;

connection_pool_size        256;

request_pool_size        4k;

output_buffers  16 128k;

postpone_output  1460;

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

fastcgi_buffer_size 128k;

fastcgi_buffers 16 256k;

fastcgi_busy_buffers_size 512k;

fastcgi_temp_file_write_size 512k;

fastcgi_temp_path /dev/shm;

open_file_cache max=65536 inactive=20s;

open_file_cache_min_uses 1;

open_file_cache_valid 30s;

tcp_nodelay on;

gzip on;

gzip_min_length  1k;

gzip_buffers    16 1024k;

gzip_http_version 1.1;

gzip_types      text/plain application/x-javascript text/css application/xml;

log_format  access  '$remote_addr - $remote_user [$time_local] "GET http://$host$request_uri $server_protocol" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" $http_x_forwarded_for';

server

{

listen 80;

server_name _;

location ~ / {

deny all;

}

}

include vhosts/*.conf;

}

vi /usr/local/nginx/conf/vhosts/linuxidc.conf

server

{

listen 80 default;

server_name www.linuxidc.com;

root /data/httpd/www.linuxidc.com;

index index.html index.php index.htm;

error_page 404 /index.html;

error_page 502 /index.html;

location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$

{

expires      30d;

}

location ~ .*.(js|css)?$

{

expires      1h;

}

access_log /usr/local/nginx/logs/access.log;

}

4. 设定nginx启动文件

vi /etc/init.d/nginx

nginxd=/usr/local/nginx/sbin/nginx

nginx_config=/usr/local/nginx/conf/nginx.conf

nginx_pid=/var/run/nginx.pid

RETVAL=0

prog="nginx"

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

# Check that networking is up.

[ ${NETWORKING} = "no" ] && exit 0

[ -x $nginxd ] || exit 0

# Start nginx daemons functions.

start() {

if [ -e $nginx_pid ];then

echo "nginx already running...."

exit 1

fi

echo -n $"Starting $prog: "

daemon $nginxd -c ${nginx_config}

RETVAL=$?

echo

[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx

return $RETVAL

}

# Stop nginx daemons functions.

stop() {

echo -n $"Stopping $prog: "

killproc $nginxd

RETVAL=$?

echo

[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid

}

# reload nginx service functions.

reload() {

echo -n $"Reloading $prog: "

#kill -HUP `cat ${nginx_pid}`

killproc $nginxd -HUP

RETVAL=$?

echo

}

# See how we were called.

case "$1" in

start)

start

;;

stop)

stop

;;

reload)

reload

;;

restart)

stop

start

;;

status)

status $prog

RETVAL=$?

;;

*)

echo $"Usage: $prog {start|stop|restart|reload|status|help}"

exit 1

esac

exit $RETVAL

5.  修改iptables后启动Nginx

/usr/local/nginx/sbin/nginx -t  #测试配置文件格式

/etc/init.d/nginx start