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

Ubuntu 12.10 server Nginx 产品安装

2012-11-02 14:13 495 查看
一直都是从Nginx站点下载源代码进行编译安装,从手动模式进步到自动脚本。但是如果要做多机器部署的话,这种玩法显然不好,部署就不要扯进代码编译这件事情。
因此今天试了一下Ubuntu自带的安装包。
执行apt-get install nginx后,自动安装了nginx,并且启动脚本也创建好了。
通过运行nginx -v 发现版本是1.2.1,还不错。
再运行nginx -V观察加载的模块,找到了http_ssl模块
--with-http_ssl_module一切不错。完整的配置参数如下:
nginx -V
nginx version: nginx/1.2.1
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-pcre-jit --with-debug --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6 --with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module --add-module=/build/buildd/nginx-1.2.1/debian/modules/nginx-auth-pam --add-module=/build/buildd/nginx-1.2.1/debian/modules/nginx-echo --add-module=/build/buildd/nginx-1.2.1/debian/modules/nginx-upstream-fair --add-module=/build/buildd/nginx-1.2.1/debian/modules/nginx-dav-ext-module
再看看/etc/init.d/nginx的脚本:
#!/bin/sh

### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx

# Include nginx defaults if available
if [ -f /etc/default/nginx ]; then
. /etc/default/nginx
fi

test -x $DAEMON || exit 0

set -e

. /lib/lsb/init-functions

test_nginx_config() {
if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; then
return 0
else
$DAEMON -t $DAEMON_OPTS
return $?
fi
}

case "$1" in
start)
echo -n "Starting $DESC: "
test_nginx_config
# Check if the ULIMIT is set in /etc/default/nginx
if [ -n "$ULIMIT" ]; then
# Set the ulimits
ulimit $ULIMIT
fi
start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON -- $DAEMON_OPTS || true
echo "$NAME."
;;

stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON || true
echo "$NAME."
;;

restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile \
/var/run/$NAME.pid --exec $DAEMON || true
sleep 1
test_nginx_config
# Check if the ULIMIT is set in /etc/default/nginx
if [ -n "$ULIMIT" ]; then
# Set the ulimits
ulimit $ULIMIT
fi
start-stop-daemon --start --quiet --pidfile \
/var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
echo "$NAME."
;;

reload)
echo -n "Reloading $DESC configuration: "
test_nginx_config
start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON || true
echo "$NAME."
;;

configtest|testconfig)
echo -n "Testing $DESC configuration: "
if test_nginx_config; then
echo "$NAME."
else
exit $?
fi
;;

status)
status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
;;
*)
echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
exit 1
;;
esac

exit 0

比之前自己写的完整很多,提供了一些很好的reload, force-reload, configtest功能。非常好。
还可看到nginx被安装在/usr/sbin/目录下。
进程id文件是/var/run/nginx.pid
如果定义了变量$ULIMIT ,会作为参数让ulimit命令使用,不过默认应该没有定义。
同样的,启动参数你也可以赋给变量DAEMON_OPTS,默认值为空。
很好的基础脚本。

注意,不能用pidof nginx来查找进程id,而只应该读取进程id文件内容:
root@sloop2:/etc/init.d# pidof nginx
6639 6638 6637 6636 6635
root@sloop2:/etc/init.d# cat /var/run/nginx.pid
6635
因为这时启动的nginx有5个之多,一个master,四个worker。
root@sloop2:/etc/init.d# ps -def | grep nginx
root 6635 1 0 14:22 ? 00:00:00 nginx: master process /usr/sbin/nginx
www-data 6636 6635 0 14:22 ? 00:00:00 nginx: worker process
www-data 6637 6635 0 14:22 ? 00:00:00 nginx: worker process
www-data 6638 6635 0 14:22 ? 00:00:00 nginx: worker process
www-data 6639 6635 0 14:22 ? 00:00:00 nginx: worker process
这叫Master进程模型,比之前用的Single进程模型要好。master负责初始化配置,创建worker,然后接受请求,转发给worker去处理。

worker数目是可以配置的,一般是系统中CPU核数-1比较好。

nginx的配置文件在哪?运行whereis nginx命令后可以看到,在/etc/nginx目录下。
在nginx.conf文件里,第二行就是worker_processes的设置,默认是4,我这里CPU才两核,所以设置为1.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: