首先致謝:本文大量參考了金山逍遙網系統架構師-張宴的Nginx 0.7.x + PHP 5.2.10(FastCGI)搭建勝過Apache十倍的Web伺服器(第5版)[原創] 一文,謝謝張宴為nginx推廣普及所作的貢獻。
之所以要在ubuntu server 9.04(以下簡稱us904)下編譯安裝nginx,是因為us904官方源中提供的版本有些舊,為nginx0.6.35,而當前nginx官方最新穩定版為0.7.61,最新開發版本為0.8.9(詳見:http://www.nginx.org/),下面以0.8.9版為例進行nginx下的編譯安裝:
首先安裝編譯nginx所需的軟體包:
1.Perl 5 Compatible Regular Expression Library - development files
sudo apt-get install libpcre3-dev
2.SSL development libraries, header files and documentation
sudo apt-get install libssl-dev
3.make 工具
sudo apt-get install make
建立啟用nginx所使用的使用者組和使用者
sudo groupadd www
sudo useradd -g www www
sudo mkdir -v -p /www/htdocs/site1
sudo chown -R www:www /www/htdocs/site1
然後下載nginx源碼包
wget http://sysoev.ru/nginx/nginx-0.8.9.tar.gz
開始編譯安裝nginx
tar zxvf nginx-0.8.9.tar.gz
cd nginx-0.8.9
./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module
sudo make
sudo make install
然後建立nginx服務控制指令碼程式
sudo nano /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-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/local/webserver/nginx/sbin/nginxNAME=nginxDESC=nginxtest -x $DAEMON || exit 0# Include nginx defaults if availableif [ -f /etc/default/nginx ] ; then . /etc/default/nginxfiset -e. /lib/lsb/init-functionscase "$1" instart) echo -n "Starting $DESC: " 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 start-stop-daemon --start --quiet --pidfile / /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true echo "$NAME." ;;reload)echo -n "Reloading $DESC configuration: "start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid /--exec $DAEMON || trueecho "$NAME.";;status)status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?;;*) N=/etc/init.d/$NAME echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2 exit 1 ;;esacexit 0#! /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-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/local/webserver/nginx/sbin/nginxNAME=nginxDESC=nginxtest -x $DAEMON || exit 0# Include nginx defaults if availableif [ -f /etc/default/nginx ] ; then. /etc/default/nginxfiset -e. /lib/lsb/init-functionscase "$1" in start)echo -n "Starting $DESC: "start-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 1start-stop-daemon --start --quiet --pidfile //var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || trueecho "$NAME.";; reload) echo -n "Reloading $DESC configuration: " start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid / --exec $DAEMON || true echo "$NAME." ;; status) status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $? ;; *)N=/etc/init.d/$NAMEecho "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2exit 1;;esacexit 0然後就可以通過sudo /etc/init.d/nginx start(stop|restart|status)等命令及參數來控制nginx和查看狀態資訊了至於nginx的具體配置,留待下一次繼續討論學習。在測試本次編譯安裝的同時,我在另一台虛擬機器us904上通過sudo apt-get install nginx安裝nginx來查看兩種方式的不同,發現nginx_0.6.35-0ubuntu1_i386.deb檔案主要有以下檔案拷貝過程sudo dpkg -L nginx/./usr/usr/sbin/usr/sbin/nginx/usr/share/usr/share/doc/usr/share/doc/nginx/usr/share/doc/nginx/changelog.gz/usr/share/doc/nginx/README/usr/share/doc/nginx/copyright/usr/share/doc/nginx/NEWS.Debian.gz/usr/share/doc/nginx/changelog.Debian.gz/usr/share/man/usr/share/man/man1/usr/share/man/man1/nginx.1.gz/etc/etc/nginx/etc/nginx/sites-available/etc/nginx/sites-available/default/etc/nginx/sites-enabled/etc/nginx/conf.d/etc/nginx/fastcgi_params/etc/nginx/koi-utf/etc/nginx/koi-win/etc/nginx/mime.types/etc/nginx/nginx.conf/etc/nginx/win-utf/etc/init.d/etc/init.d/nginx/etc/logrotate.d/etc/logrotate.d/nginx/var/var/log/var/log/nginx/var/lib/var/lib/nginx/var/www/var/www/nginx-default/var/www/nginx-default/50x.html/var/www/nginx-default/index.html由此猜想在生產環境中是否可以避免安裝過多軟體包,而直接替換/usr/sbin/nginx檔案來實現,具體運行穩定性有待驗證。ps:1.如果不想輸入命令時帶有sudo,可以通過下面方式切換至rootA.sudo -s -H 輸入目前使用者密碼B.sudo passwd root 設定root密碼 su root 輸入root密碼2.查看某個包具體所在的包名,可以通過sudo apt-cache search *** 來尋找