下载
wget http://nginx.org/download/nginx-1.9.6.tar.gz
解压缩
tar zxvf nginx-1.9.6.tar.gz
进入nginx目录
cd nginx-1.9.6
设置,编译,安装(configure过程可能出现的情况,见文章最尾)
./configure --prefix=/home/server/nginx
make && make install && make clean
添加用户组www
groupadd www
向用户组www添加用户www
useradd -r -g www -s /bin/false www
修改nginx配置文件
vi /home/server/nginx/conf/nginx.conf
user www;
#CPU Core options
#Nginx每个进程耗费10M~12M内存,这里只开启一个Nginx进程,节省内存。
worker_processes 1;
# 日志类型 [ debug | info | notice | warn | error | crit ]
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
error_log logs/nginx_error.log error;
#nginx Process options
#pid logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events {
use epoll;
#maxclient = worker_processes * worker_connections / cpu_number
worker_connections 51200;
}
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"';
#access_log logs/access.log main;
# 设置默认文档格式
charset utf8;
# General options
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
# ignore_invalid_headers on;
# recursive_error_pages on;
server_name_in_redirect off;
sendfile on;
# timeouts
keepalive_timeout 60;
# TCP options
tcp_nopush on;
tcp_nodelay on;
#fastcgi options
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 128k;
# fastcgi_intercept_errors on;
# size limits
client_max_body_size 100m;
# client_body_buffer_size 256k;
# gzip compression
# 对网页文件、CSS、JS、XML等启动gzip压缩,减少数据传输量,提高访问速度。
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain text/css application/x-javascript application/xml;
gzip_vary on;
# limit_zone crawler $binary_remote_addr 10m;
# 包含所有虚拟主机的配置文件
include vhosts/*.conf;
}
创建针对localhost的配置文件
vi /home/server/nginx/conf/vhosts/localhost.conf
内容如下
server {
listen 80;
server_name localhost;
location / {
# 网站根目录
root /home/webroot/localhost;
# 默认文档
index index.html index.htm;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
创建nginx系统服务脚本
vi /etc/init.d/nginx
填入如下内容
#!/bin/sh
#
#nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /home/server/nginx/conf/nginx.conf
# pidfile: /data/webroot/logs/localhost/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/home/server/nginx/sbin/nginx" # nginx启动文件
prog=$(basename $nginx)
NGINX_CONF_FILE="/home/server/nginx/conf/nginx.conf" # 配置文件路径
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
保存
:wq
对脚本添加可执行权限
chmod +x /etc/init.d/nginx
启动nginx
/etc/init.d/nginx start
添加nginx到自启动服务
chkconfig --add nginx
设置开机启动
chkconfig nginx on
开启访问端口
vi /etc/sysconfig/iptables
添加如下
-A INPUT –m state –state NEW –m tcp –p tcp –dport 80 –j ACCEPT
重启防火墙以应用规则
/etc/init.d/iptables restart
此后,执行命令脚本即可,无需执行原路径的执行文件
/etc/init.d/nginx start # 启动nginx
/etc/init.d/nginx stop # 停止nginx
/etc/init.d/nginx restart # 重启nginx
/etc/init.d/nginx reload # 重新加载配置文件
原操作命令
/data/server/nginx/sbin/nginx # start server
/data/server/nginx/sbin/nginx -s stop # fast shutdown
/data/server/nginx/sbin/nginx -s quit # graceful shutdown
/data/server/nginx/sbin/nginx -s reload # reloading the configuration file
/data/server/nginx/sbin/nginx -s reopen # reopening the log files
至此,nginx的安装与最基本的配置已完成
configure过程可能出现的情况
缺少C编译器,需要安装gcc
运行安装命令,yum install -y gcc
未安装PCRE
运行安装命令,yum install -y pcre pcre-devel
运行安装命令,
yum install -y openssl openssl-devel