您的位置:首页 > 运维架构 > Nginx

Debian系统下nginx的安装与配置

2011-12-28 16:02 447 查看
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。

1. 安装Nginx的程序库,不需要太多包,只需要 pcre, ssl 和 zlib
# aptitude install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev

2. 编译安装nginx-0.8.46.tar.gz (可以提前下载)
# ./configure --sbin-path=/usr/local/sbin --with-http_mp4_module --with-http_stub_status_module
# make && make install clean
注:如果不能make( 编译 ),出现以下错误:
-bash: make: command not found
这是因为Debian默认不能make,需要安装开发工具,可以这样:
# apt-get -y install kdevelop
# apt-get install build-essential

3. 创建nginx启动脚本
vim /etc/init.d/nginx
#! /bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-De.ion: starts the nginx web server
# De.ion: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/nginx
NAME=nginx
DESC=nginx
test -x $DAEMON || exit 0
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/nginx.pid \
--exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/nginx.pid \
--exec $DAEMON
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile \
/usr/local/nginx/logs/nginx.pid --exec $DAEMON
sleep 1
start-stop-daemon --start --quiet --pidfile \
/usr/local/nginx/logs/nginx.pid --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/nginx.pid \
--exec $DAEMON
echo "$NAME."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0
4.修改脚本权限
# chmod 777 /etc/init.d/nginx
5.由于nginx是安装在/usr/local/,可以链接到我们常用的/etc/下
# ln -s /usr/local/nginx /etc/nginx
6.运行nginx
# /etc/init.d/nginx star
7.测试
客户端在浏览器中输入nginx服务器地址访问,若出现 Welcome to nginx! 的字样,说明Nginx安装成功!
8.在不停止Nginx服务的情况下平滑变更Nginx配置
输入以下命令查看Nginx主进程号:
# ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
屏幕显示的即为Nginx主进程号,例如:
  6302
  这时,执行以下命令即可使修改过的Nginx配置文件生效:
# kill -HUP 6302
或者无需这么麻烦,找到Nginx的Pid文件:
# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
9.编写每天定时切割Nginx日志的脚本
创建脚本/usr/local/nginx/sbin/cut_nginx_log.sh
# vi /usr/local/nginx/cut_nginx_log.sh
输入以下内容:
#!/bin/bash
# This script run at 00:00

# The Nginx logs path
logs_path="/usr/local/nginx/logs/"

mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`

# chmod 777 /usr/local/nginx/cut_nginx_log.sh

设置crontab,每天凌晨00:00切割nginx访问日志
# crontab -e
00 00 * * * /bin/bash /usr/local/nginx/cut_nginx_log.sh
10.默认安装的路径是/usr/local/nginx
更多的安装配置
./configure --prefix=/usr/local/nginx
--with-openssl=/usr/include (启用ssl)
--with-pcre=/usr/include/pcre/ (启用正规表达式)
--with-http_stub_status_module (安装可以查看nginx状态的程序)
--with-http_memcached_module (启用memcache缓存)
--with-http_rewrite_module (启用支持url重写)
nginx配置文件:
vi /usr/local/nginx/conf/nginx.conf
测试配置文件:
nginx -t
如果屏幕显示以下两行信息,说明配置文件正确:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
nginx的日志文件:
/usr/local/nginx/logs/

配置开机自动启动Nginx + PHP
vi /root/run.sh
在末尾增加以下内容:
ulimit -SHn 65535
/usr/local/php/sbin/php-fpm start
/usr/local/nginx/sbin/nginx

优化Linux内核参数
vi /etc/sysctl.conf

  在末尾增加以下内容:
# Add
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768

net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800

#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024 65535

  使配置立即生效:
/sbin/sysctl -p

nginx.conf 应用程序的基本配置文件
mime.types 一个文件扩展列表文件,它们与MIME类型关联
fastcgi.conf 与FastCGI相关的配置文件
proxy.conf 与Proxy相关的配置文件
sites.conf 配置Nginx提供的web站点,也包括众所周知的虚拟主机。

nginx配置文件
vi /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1; //参数是1,它标志着Nginx作为单个worker进程
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  职场 Debian 休闲 nginx