一直都是從Nginx網站下載原始碼進行編譯安裝,從手動模式進步到自動指令碼。但是如果要做多機器部署的話,這種玩法顯然不好,部署就不要扯進代碼編譯這件事情。
因此今天試了一下Ubuntu內建的安裝包。
執行apt-get install nginx後,自動安裝了nginx,並且啟動指令碼也建立好了。
通過運行nginx -v 發現版本是1.2.1,還不錯。
再運行nginx -V觀察載入的模組,找到了http_ssl模組
--with-http_ssl_module
一切不錯。完整的配置參數如下:
nginx -Vnginx version: nginx/1.2.1TLS SNI support enabledconfigure 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 INFOPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binDAEMON=/usr/sbin/nginxNAME=nginxDESC=nginx# Include nginx defaults if availableif [ -f /etc/default/nginx ]; then. /etc/default/nginxfitest -x $DAEMON || exit 0set -e. /lib/lsb/init-functionstest_nginx_config() {if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; thenreturn 0else$DAEMON -t $DAEMON_OPTSreturn $?fi}case "$1" instart)echo -n "Starting $DESC: "test_nginx_config# Check if the ULIMIT is set in /etc/default/nginxif [ -n "$ULIMIT" ]; then# Set the ulimitsulimit $ULIMITfistart-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \ --exec $DAEMON -- $DAEMON_OPTS || trueecho "$NAME.";;stop)echo -n "Stopping $DESC: "start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \ --exec $DAEMON || trueecho "$NAME.";;restart|force-reload)echo -n "Restarting $DESC: "start-stop-daemon --stop --quiet --pidfile \ /var/run/$NAME.pid --exec $DAEMON || truesleep 1test_nginx_config# Check if the ULIMIT is set in /etc/default/nginxif [ -n "$ULIMIT" ]; then# Set the ulimitsulimit $ULIMITfistart-stop-daemon --start --quiet --pidfile \ /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || trueecho "$NAME.";;reload)echo -n "Reloading $DESC configuration: "test_nginx_configstart-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \ --exec $DAEMON || trueecho "$NAME.";;configtest|testconfig)echo -n "Testing $DESC configuration: "if test_nginx_config; thenecho "$NAME."elseexit $?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}" >&2exit 1;;esacexit 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 nginx6639 6638 6637 6636 6635root@sloop2:/etc/init.d# cat /var/run/nginx.pid 6635
因為這時啟動的nginx有5個之多,一個master,四個worker。
root@sloop2:/etc/init.d# ps -def | grep nginxroot 6635 1 0 14:22 ? 00:00:00 nginx: master process /usr/sbin/nginxwww-data 6636 6635 0 14:22 ? 00:00:00 nginx: worker processwww-data 6637 6635 0 14:22 ? 00:00:00 nginx: worker processwww-data 6638 6635 0 14:22 ? 00:00:00 nginx: worker processwww-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.